Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Encoding/PlushVector.cs @ 15275

Last change on this file since 15275 was 15275, checked in by pkimmesw, 7 years ago

#2665 Added PlushEncoding, ZeroErrorDistributionAnalzer

File size: 3.0 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Encoding {
2  using System.Collections.Generic;
3
4  using HeuristicLab.Common;
5  using HeuristicLab.Core;
6  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
7  using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
8  using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
9
10  [StorableClass]
11  public class PlushVector : Item {
12
13    [Storable]
14    private readonly List<PlushEntry> entries;
15    public IReadOnlyList<PlushEntry> Entries { get { return entries; } }
16
17    public PlushVector() : this(0) { }
18
19    public PlushVector(int length) {
20      entries = new List<PlushEntry>(length);
21    }
22
23    [StorableConstructor]
24    public PlushVector(bool deserializing) : base(deserializing) {
25    }
26
27    public PlushVector(PlushVector origin, Cloner cloner) : base(origin, cloner) {
28    }
29
30    public override IDeepCloneable Clone(Cloner cloner) {
31      return new PlushVector(this, cloner);
32    }
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    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.