Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/DefineExpressions.cs @ 17432

Last change on this file since 17432 was 15771, checked in by bburlacu, 7 years ago

#2895: Add solution skeleton for PushGP with genealogy analysis.

File size: 12.2 KB
RevLine 
[15771]1using System;
2using System.Collections.Generic;
3using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
4
5namespace HeuristicLab.Problems.ProgramSynthesis {
[14727]6
7  /// <summary>
[15032]8  /// Defines the name on top of the NAME stack as an instruction that will push the top item of the T stack onto the EXEC stack.
[14727]9  /// </summary>
[15032]10  /// <typeparam name="T">Stack item type</typeparam>
[14952]11  [StorableClass]
[14727]12  public abstract class DefineExpression<T> : StatelessExpression {
[14952]13    protected DefineExpression() { }
14    [StorableConstructor]
15    protected DefineExpression(bool deserializing) : base(deserializing) { }
16
17    protected void Eval(
[14834]18      IPushStack<T> stack,
19      IPushStack<string> nameStack,
[14727]20      IDictionary<string, Expression> customExpressions,
21      Func<T, Expression> creator) {
22      var name = nameStack.Pop();
23      var expression = creator(stack.Top);
24
25      if (customExpressions.ContainsKey(name)) customExpressions[name] = expression;
26      else customExpressions.Add(name, expression);
27    }
28  }
29
[15032]30  [PushExpression(
31    StackTypes.Code,
32    "CODE.DEFINE",
33    "Defines the name on top of the NAME stack as an instruction that will push the top item of the CODE stack onto the EXEC stack.",
34    StackTypes.Name)]
[14952]35  [StorableClass]
[14727]36  public class CodeDefineExpression : DefineExpression<Expression> {
[14952]37    public CodeDefineExpression() { }
38    [StorableConstructor]
39    protected CodeDefineExpression(bool deserializing) : base(deserializing) { }
[14727]40
[14952]41    public override bool IsNoop(IInternalPushInterpreter interpreter) {
42      return (interpreter.Configuration.TopLevelPushCode && interpreter.CodeStack.Count < 2) ||
43             interpreter.CodeStack.IsEmpty ||
44             interpreter.NameStack.IsEmpty;
45    }
46
47    public override void Eval(IInternalPushInterpreter interpreter) {
[15017]48      var pool = interpreter.PoolContainer.GetStatefulExpressionPool<ExecPushExpression>();
49
[14908]50      Eval(
[14727]51        interpreter.CodeStack,
52        interpreter.NameStack,
53        interpreter.CustomExpressions,
[15017]54        v => ExecPushExpression.Create(pool, v));
[14727]55
56      interpreter.CodeStack.Pop();
57    }
58  }
59
[15032]60  [PushExpression(
61    StackTypes.Exec,
62    "EXEC.DEFINE",
63    "Defines the name on top of the NAME stack as an instruction that will push the top item of the EXEC stack onto the EXEC stack.",
64    StackTypes.Name,
[15334]65    requiredBlockCount: 1)]
[14952]66  [StorableClass]
[14727]67  public class ExecDefineExpression : DefineExpression<Expression> {
[14952]68    public ExecDefineExpression() { }
69    [StorableConstructor]
70    protected ExecDefineExpression(bool deserializing) : base(deserializing) { }
[14727]71
[14952]72    public override bool IsNoop(IInternalPushInterpreter interpreter) {
73      return interpreter.ExecStack.Count < 2 ||
74             interpreter.NameStack.IsEmpty;
75    }
76
77    public override void Eval(IInternalPushInterpreter interpreter) {
[15017]78      var pool = interpreter.PoolContainer.GetStatefulExpressionPool<ExecPushExpression>();
79
[14952]80      Eval(
[14727]81        interpreter.ExecStack,
82        interpreter.NameStack,
83        interpreter.CustomExpressions,
[15017]84        v => ExecPushExpression.Create(pool, v));
[14727]85
[14952]86      interpreter.ExecStack.Pop();
[14727]87    }
88  }
89
[15032]90  [PushExpression(
91    StackTypes.Float,
92    "FLOAT.DEFINE",
93    "Defines the name on top of the NAME stack as an instruction that will push the top item of the FLOAT stack onto the EXEC stack.",
94    StackTypes.Name)]
[14952]95  [StorableClass]
[14727]96  public class FloatDefineExpression : DefineExpression<double> {
[14952]97    public FloatDefineExpression() { }
98    [StorableConstructor]
99    protected FloatDefineExpression(bool deserializing) : base(deserializing) { }
[14727]100
[14952]101    public override bool IsNoop(IInternalPushInterpreter interpreter) {
102      return interpreter.FloatStack.IsEmpty ||
103             interpreter.NameStack.IsEmpty;
104    }
105
106    public override void Eval(IInternalPushInterpreter interpreter) {
[15017]107      var pool = interpreter.PoolContainer.GetStatefulExpressionPool<FloatPushExpression>();
108
[14952]109      Eval(
[14727]110        interpreter.FloatStack,
111        interpreter.NameStack,
112        interpreter.CustomExpressions,
[15017]113        v => FloatPushExpression.Create(pool, v));
[14727]114    }
115  }
116
[15032]117  [PushExpression(
118    StackTypes.Integer,
119    "INTEGER.DEFINE",
120    "Defines the name on top of the NAME stack as an instruction that will push the top item of the INTEGER stack onto the EXEC stack.",
121    StackTypes.Name)]
[14952]122  [StorableClass]
[14727]123  public class IntegerDefineExpression : DefineExpression<long> {
[14952]124    public IntegerDefineExpression() { }
125    [StorableConstructor]
126    protected IntegerDefineExpression(bool deserializing) : base(deserializing) { }
127
128    public override bool IsNoop(IInternalPushInterpreter interpreter) {
129      return interpreter.IntegerStack.IsEmpty ||
130             interpreter.NameStack.IsEmpty;
131    }
132
133    public override void Eval(IInternalPushInterpreter interpreter) {
[15017]134      var pool = interpreter.PoolContainer.GetStatefulExpressionPool<IntegerPushExpression>();
135
[14952]136      Eval(
[14727]137        interpreter.IntegerStack,
138        interpreter.NameStack,
139        interpreter.CustomExpressions,
[15017]140        v => IntegerPushExpression.Create(pool, v));
[14727]141    }
142  }
143
[15032]144  [PushExpression(
145    StackTypes.Boolean,
146    "BOOLEAN.DEFINE",
147    "Defines the name on top of the NAME stack as an instruction that will push the top item of the BOOLEAN stack onto the EXEC stack.",
148    StackTypes.Name)]
[14952]149  [StorableClass]
[14727]150  public class BooleanDefineExpression : DefineExpression<bool> {
[14952]151    public BooleanDefineExpression() { }
152    [StorableConstructor]
153    protected BooleanDefineExpression(bool deserializing) : base(deserializing) { }
154
155    public override bool IsNoop(IInternalPushInterpreter interpreter) {
156      return interpreter.BooleanStack.IsEmpty ||
157             interpreter.NameStack.IsEmpty;
158    }
159
160    public override void Eval(IInternalPushInterpreter interpreter) {
[15017]161      var pool = interpreter.PoolContainer.GetStatefulExpressionPool<BooleanPushExpression>();
162
[14952]163      Eval(
[14727]164        interpreter.BooleanStack,
165        interpreter.NameStack,
166        interpreter.CustomExpressions,
[15017]167        v => BooleanPushExpression.Create(pool, v));
[14727]168    }
169  }
[14777]170
[15032]171  [PushExpression(
172    StackTypes.Char,
173    "CHAR.DEFINE",
174    "Defines the name on top of the NAME stack as an instruction that will push the top item of the CHAR stack onto the EXEC stack.",
175    StackTypes.Name)]
[14952]176  [StorableClass]
[14777]177  public class CharDefineExpression : DefineExpression<char> {
[14952]178    public CharDefineExpression() { }
179    [StorableConstructor]
180    protected CharDefineExpression(bool deserializing) : base(deserializing) { }
181
182    public override bool IsNoop(IInternalPushInterpreter interpreter) {
183      return interpreter.CharStack.IsEmpty ||
184             interpreter.NameStack.IsEmpty;
185    }
186
187    public override void Eval(IInternalPushInterpreter interpreter) {
[15017]188      var pool = interpreter.PoolContainer.GetStatefulExpressionPool<CharPushExpression>();
189
[14952]190      Eval(
[14777]191        interpreter.CharStack,
192        interpreter.NameStack,
193        interpreter.CustomExpressions,
[15017]194        v => CharPushExpression.Create(pool, v));
[14777]195    }
196  }
197
[15032]198  [PushExpression(
199    StackTypes.String,
200    "STRING.DEFINE",
201    "Defines the name on top of the NAME stack as an instruction that will push the top item of the STRING stack onto the EXEC stack.",
202    StackTypes.Name)]
[14952]203  [StorableClass]
[14777]204  public class StringDefineExpression : DefineExpression<string> {
[14952]205    public StringDefineExpression() { }
206    [StorableConstructor]
207    protected StringDefineExpression(bool deserializing) : base(deserializing) { }
208
209    public override bool IsNoop(IInternalPushInterpreter interpreter) {
210      return interpreter.StringStack.IsEmpty ||
211             interpreter.NameStack.IsEmpty;
212    }
213
214    public override void Eval(IInternalPushInterpreter interpreter) {
[15017]215      var pool = interpreter.PoolContainer.GetStatefulExpressionPool<StringPushExpression>();
216
[14952]217      Eval(
[14777]218        interpreter.StringStack,
219        interpreter.NameStack,
220        interpreter.CustomExpressions,
[15017]221        v => StringPushExpression.Create(pool, v));
[14777]222    }
223  }
[14834]224
[15032]225  [PushExpression(
226    StackTypes.IntegerVector,
227    "INTEGER[].DEFINE",
228    "Defines the name on top of the NAME stack as an instruction that will push the top item of the INTEGER[] stack onto the EXEC stack.",
229    StackTypes.Name)]
[14952]230  [StorableClass]
[15017]231  public class IntegerVectorDefineExpression : DefineExpression<IReadOnlyList<long>> {
[14952]232    public IntegerVectorDefineExpression() { }
233    [StorableConstructor]
234    protected IntegerVectorDefineExpression(bool deserializing) : base(deserializing) { }
235
236    public override bool IsNoop(IInternalPushInterpreter interpreter) {
237      return interpreter.IntegerVectorStack.IsEmpty ||
238             interpreter.NameStack.IsEmpty;
239    }
240
241    public override void Eval(IInternalPushInterpreter interpreter) {
[15017]242      var pool = interpreter.PoolContainer.GetStatefulExpressionPool<IntegerVectorPushExpression>();
243
[14952]244      Eval(
[14834]245        interpreter.IntegerVectorStack,
246        interpreter.NameStack,
247        interpreter.CustomExpressions,
[15017]248        v => IntegerVectorPushExpression.Create(pool, v));
[14834]249    }
250  }
[14875]251
[15032]252  [PushExpression(
253    StackTypes.FloatVector,
254    "FLOAT[].DEFINE",
255    "Defines the name on top of the NAME stack as an instruction that will push the top item of the FLOAT[] stack onto the EXEC stack.",
256    StackTypes.Name)]
[14952]257  [StorableClass]
[15017]258  public class FloatVectorDefineExpression : DefineExpression<IReadOnlyList<double>> {
[14952]259    public FloatVectorDefineExpression() { }
260    [StorableConstructor]
261    protected FloatVectorDefineExpression(bool deserializing) : base(deserializing) { }
262
263    public override bool IsNoop(IInternalPushInterpreter interpreter) {
264      return interpreter.FloatVectorStack.IsEmpty ||
265             interpreter.NameStack.IsEmpty;
266    }
267
268    public override void Eval(IInternalPushInterpreter interpreter) {
[15017]269      var pool = interpreter.PoolContainer.GetStatefulExpressionPool<FloatVectorPushExpression>();
270
[14952]271      Eval(
[14875]272        interpreter.FloatVectorStack,
273        interpreter.NameStack,
274        interpreter.CustomExpressions,
[15017]275        v => FloatVectorPushExpression.Create(pool, v));
[14875]276    }
277  }
278
[15032]279  [PushExpression(
280    StackTypes.BooleanVector,
281    "BOOLEAN[].DEFINE",
282    "Defines the name on top of the NAME stack as an instruction that will push the top item of the BOOLEAN[] stack onto the EXEC stack.",
283    StackTypes.Name)]
[14952]284  [StorableClass]
[15017]285  public class BooleanVectorDefineExpression : DefineExpression<IReadOnlyList<bool>> {
[14952]286    public BooleanVectorDefineExpression() { }
287    [StorableConstructor]
288    protected BooleanVectorDefineExpression(bool deserializing) : base(deserializing) { }
289
290    public override bool IsNoop(IInternalPushInterpreter interpreter) {
291      return interpreter.BooleanVectorStack.IsEmpty ||
292             interpreter.NameStack.IsEmpty;
293    }
294
295    public override void Eval(IInternalPushInterpreter interpreter) {
[15017]296      var pool = interpreter.PoolContainer.GetStatefulExpressionPool<BooleanVectorPushExpression>();
297
[14952]298      Eval(
[14875]299        interpreter.BooleanVectorStack,
300        interpreter.NameStack,
301        interpreter.CustomExpressions,
[15017]302        v => BooleanVectorPushExpression.Create(pool, v));
[14875]303    }
304  }
305
[15032]306  [PushExpression(
307    StackTypes.StringVector,
308    "STRING[].DEFINE",
309    "Defines the name on top of the NAME stack as an instruction that will push the top item of the STRING[] stack onto the EXEC stack.",
310    StackTypes.Name)]
[14952]311  [StorableClass]
[15017]312  public class StringVectorDefineExpression : DefineExpression<IReadOnlyList<string>> {
[14952]313    public StringVectorDefineExpression() { }
314    [StorableConstructor]
315    protected StringVectorDefineExpression(bool deserializing) : base(deserializing) { }
316
317    public override bool IsNoop(IInternalPushInterpreter interpreter) {
318      return interpreter.StringVectorStack.IsEmpty ||
319             interpreter.NameStack.IsEmpty;
320    }
321
322    public override void Eval(IInternalPushInterpreter interpreter) {
[15017]323      var pool = interpreter.PoolContainer.GetStatefulExpressionPool<BooleanVectorPushExpression>();
324
[14952]325      Eval(
[14875]326        interpreter.StringVectorStack,
327        interpreter.NameStack,
328        interpreter.CustomExpressions,
[15017]329        v => StringVectorPushExpression.Create(pool, v));
[14875]330    }
331  }
[14727]332}
Note: See TracBrowser for help on using the repository browser.