Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/20/17 11:39:53 (7 years ago)
Author:
pkimmesw
Message:

#2665 Added PlushEncoding, ZeroErrorDistributionAnalzer

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Encoding/PlushVector.cs

    r15273 r15275  
    55  using HeuristicLab.Core;
    66  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     7  using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
     8  using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
    79
    810  [StorableClass]
    911  public class PlushVector : Item {
    1012
    11     private List<PlushEntry> entries;
     13    [Storable]
     14    private readonly List<PlushEntry> entries;
     15    public IReadOnlyList<PlushEntry> Entries { get { return entries; } }
    1216
    13     public PlushVector() : this(0) {
    14 
    15     }
     17    public PlushVector() : this(0) { }
    1618
    1719    public PlushVector(int length) {
     
    2931      return new PlushVector(this, cloner);
    3032    }
     33
     34    private PushProgram pushProgram;
     35
     36    public PushProgram PushProgram
     37    {
     38      get
     39      {
     40        if (pushProgram == null) CreatePushProgram();
     41        return pushProgram;
     42      }
     43    }
     44
     45    private void CreatePushProgram() {
     46      var close = 0;
     47      var currentIndex = 0;
     48
     49      pushProgram = FromPlush(ref currentIndex, ref close, 0);
     50    }
     51
     52    private PushProgram FromPlush(
     53     ref int currentIndex,
     54     ref int close,
     55     int depth) {
     56
     57      if (currentIndex >= entries.Count)
     58        return PushProgram.Empty;
     59
     60      var instructions = new List<Expression>();
     61
     62      for (var i = currentIndex; i < entries.Count; i++) {
     63        var entry = entries[i];
     64        var instructionType = entry.Instruction.GetType();
     65
     66        close += entry.Close;
     67
     68        PushExpressionAttribute attribute;
     69        if (ExpressionTable.TypeToAttributeTable.TryGetValue(instructionType, out attribute)) {
     70          for (var blockIdx = 0u; blockIdx < attribute.ExecIn && currentIndex < entries.Count; blockIdx++) {
     71            if (close != 0) {
     72              close--;
     73              instructions.Add(PushProgram.Empty);
     74            } else {
     75              currentIndex++;
     76              var subProgram = FromPlush(ref currentIndex, ref close, depth + 1);
     77              var subExpression = subProgram.Count == 1 ? subProgram.Expressions[0] : subProgram;
     78
     79              instructions.Add(subExpression);
     80            }
     81          }
     82        }
     83
     84        instructions.Add(entry.Instruction);
     85
     86        if (close > 0 && depth > 0) {
     87          close--;
     88          break;
     89        }
     90
     91        if (depth == 0) {
     92          close = 0;
     93        }
     94      }
     95
     96      return new PushProgram(instructions);
     97    }
     98
     99    public void UpdatePushProgram() {
     100      pushProgram = null;
     101    }
     102
     103    public void Add(PlushEntry entry) {
     104      entries.Add(entry);
     105    }
     106
     107    public PlushEntry this[int key]
     108    {
     109      get
     110      {
     111        return entries[key];
     112      }
     113    }
    31114  }
    32115}
Note: See TracChangeset for help on using the changeset viewer.