Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Encoding/PlushVector.cs @ 17104

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

#2665 Testet Problems, Testet error functions, Small fixes, Created HL files

File size: 3.2 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Encoding {
2  using System.Collections.Generic;
3  using HeuristicLab.Common;
4  using HeuristicLab.Core;
5  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
6  using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
7  using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
8
9  [StorableClass]
10  public sealed class PlushVector : Item {
11
12    [Storable]
13    private readonly List<PlushEntry> entries;
14    public IReadOnlyList<PlushEntry> Entries { get { return entries; } }
15
16    public PlushVector() : this(0) { }
17
18    public PlushVector(int length) {
19      entries = new List<PlushEntry>(length);
20    }
21
22    [StorableConstructor]
23    private PlushVector(bool deserializing) : base(deserializing) {
24    }
25
26    public PlushVector(PlushVector origin, Cloner cloner) : base(origin, cloner) {
27      entries = new List<PlushEntry>(origin.entries.Count);
28      for (var i = 0; i < origin.entries.Count; i++) {
29        entries.Add(cloner.Clone(origin.entries[i]));
30      }
31
32      pushProgram = origin.pushProgram;
33    }
34
35    public override IDeepCloneable Clone(Cloner cloner) {
36      return new PlushVector(this, cloner);
37    }
38
39    private PushProgram pushProgram;
40
41    public PushProgram PushProgram
42    {
43      get
44      {
45        if (pushProgram == null) CreatePushProgram();
46        return pushProgram;
47      }
48    }
49
50    private void CreatePushProgram() {
51      var close = 0;
52      var currentIndex = 0;
53
54      pushProgram = FromPlush(ref currentIndex, ref close, 0);
55    }
56
57    private PushProgram FromPlush(
58     ref int currentIndex,
59     ref int close,
60     int depth) {
61
62      if (currentIndex >= entries.Count)
63        return PushProgram.Empty;
64
65      var instructions = new List<Expression>();
66
67      for (; currentIndex < entries.Count; currentIndex++) {
68        var entry = entries[currentIndex];
69
70        if (entry.Silent)
71          continue;
72
73        close += entry.Close;
74        instructions.Add(entry.Instruction);
75
76        PushExpressionAttribute attribute;
77        var instructionType = entry.Instruction.GetType();
78
79        if (ExpressionTable.TypeToAttributeTable.TryGetValue(instructionType, out attribute)) {
80          for (var blockIdx = 0u; blockIdx < attribute.RequiredBlockCount; blockIdx++) {
81            if (close == 0 && currentIndex < entries.Count) {
82              currentIndex++;
83              var subProgram = FromPlush(ref currentIndex, ref close, depth + 1);
84              instructions.Add(subProgram);
85            } else {
86              close--;
87              instructions.Add(PushProgram.Empty);
88            }
89          }
90        }
91
92        if (close > 0 && depth > 0) {
93          close--;
94          break;
95        }
96
97        if (depth == 0) {
98          close = 0;
99        }
100      }
101
102      instructions.Reverse();
103      return new PushProgram(instructions);
104    }
105
106    public void UpdatePushProgram() {
107      pushProgram = null;
108    }
109
110    public void Add(PlushEntry entry) {
111      entries.Add(entry);
112    }
113
114    public PlushEntry this[int key]
115    {
116      get
117      {
118        return entries[key];
119      }
120    }
121  }
122}
Note: See TracBrowser for help on using the repository browser.