Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 12.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
4
5namespace HeuristicLab.Problems.ProgramSynthesis {
6
7  /// <summary>
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.
9  /// </summary>
10  /// <typeparam name="T">Stack item type</typeparam>
11  [StorableClass]
12  public abstract class DefineExpression<T> : StatelessExpression {
13    protected DefineExpression() { }
14    [StorableConstructor]
15    protected DefineExpression(bool deserializing) : base(deserializing) { }
16
17    protected void Eval(
18      IPushStack<T> stack,
19      IPushStack<string> nameStack,
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
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)]
35  [StorableClass]
36  public class CodeDefineExpression : DefineExpression<Expression> {
37    public CodeDefineExpression() { }
38    [StorableConstructor]
39    protected CodeDefineExpression(bool deserializing) : base(deserializing) { }
40
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) {
48      var pool = interpreter.PoolContainer.GetStatefulExpressionPool<ExecPushExpression>();
49
50      Eval(
51        interpreter.CodeStack,
52        interpreter.NameStack,
53        interpreter.CustomExpressions,
54        v => ExecPushExpression.Create(pool, v));
55
56      interpreter.CodeStack.Pop();
57    }
58  }
59
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,
65    requiredBlockCount: 1)]
66  [StorableClass]
67  public class ExecDefineExpression : DefineExpression<Expression> {
68    public ExecDefineExpression() { }
69    [StorableConstructor]
70    protected ExecDefineExpression(bool deserializing) : base(deserializing) { }
71
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) {
78      var pool = interpreter.PoolContainer.GetStatefulExpressionPool<ExecPushExpression>();
79
80      Eval(
81        interpreter.ExecStack,
82        interpreter.NameStack,
83        interpreter.CustomExpressions,
84        v => ExecPushExpression.Create(pool, v));
85
86      interpreter.ExecStack.Pop();
87    }
88  }
89
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)]
95  [StorableClass]
96  public class FloatDefineExpression : DefineExpression<double> {
97    public FloatDefineExpression() { }
98    [StorableConstructor]
99    protected FloatDefineExpression(bool deserializing) : base(deserializing) { }
100
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) {
107      var pool = interpreter.PoolContainer.GetStatefulExpressionPool<FloatPushExpression>();
108
109      Eval(
110        interpreter.FloatStack,
111        interpreter.NameStack,
112        interpreter.CustomExpressions,
113        v => FloatPushExpression.Create(pool, v));
114    }
115  }
116
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)]
122  [StorableClass]
123  public class IntegerDefineExpression : DefineExpression<long> {
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) {
134      var pool = interpreter.PoolContainer.GetStatefulExpressionPool<IntegerPushExpression>();
135
136      Eval(
137        interpreter.IntegerStack,
138        interpreter.NameStack,
139        interpreter.CustomExpressions,
140        v => IntegerPushExpression.Create(pool, v));
141    }
142  }
143
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)]
149  [StorableClass]
150  public class BooleanDefineExpression : DefineExpression<bool> {
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) {
161      var pool = interpreter.PoolContainer.GetStatefulExpressionPool<BooleanPushExpression>();
162
163      Eval(
164        interpreter.BooleanStack,
165        interpreter.NameStack,
166        interpreter.CustomExpressions,
167        v => BooleanPushExpression.Create(pool, v));
168    }
169  }
170
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)]
176  [StorableClass]
177  public class CharDefineExpression : DefineExpression<char> {
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) {
188      var pool = interpreter.PoolContainer.GetStatefulExpressionPool<CharPushExpression>();
189
190      Eval(
191        interpreter.CharStack,
192        interpreter.NameStack,
193        interpreter.CustomExpressions,
194        v => CharPushExpression.Create(pool, v));
195    }
196  }
197
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)]
203  [StorableClass]
204  public class StringDefineExpression : DefineExpression<string> {
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) {
215      var pool = interpreter.PoolContainer.GetStatefulExpressionPool<StringPushExpression>();
216
217      Eval(
218        interpreter.StringStack,
219        interpreter.NameStack,
220        interpreter.CustomExpressions,
221        v => StringPushExpression.Create(pool, v));
222    }
223  }
224
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)]
230  [StorableClass]
231  public class IntegerVectorDefineExpression : DefineExpression<IReadOnlyList<long>> {
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) {
242      var pool = interpreter.PoolContainer.GetStatefulExpressionPool<IntegerVectorPushExpression>();
243
244      Eval(
245        interpreter.IntegerVectorStack,
246        interpreter.NameStack,
247        interpreter.CustomExpressions,
248        v => IntegerVectorPushExpression.Create(pool, v));
249    }
250  }
251
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)]
257  [StorableClass]
258  public class FloatVectorDefineExpression : DefineExpression<IReadOnlyList<double>> {
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) {
269      var pool = interpreter.PoolContainer.GetStatefulExpressionPool<FloatVectorPushExpression>();
270
271      Eval(
272        interpreter.FloatVectorStack,
273        interpreter.NameStack,
274        interpreter.CustomExpressions,
275        v => FloatVectorPushExpression.Create(pool, v));
276    }
277  }
278
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)]
284  [StorableClass]
285  public class BooleanVectorDefineExpression : DefineExpression<IReadOnlyList<bool>> {
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) {
296      var pool = interpreter.PoolContainer.GetStatefulExpressionPool<BooleanVectorPushExpression>();
297
298      Eval(
299        interpreter.BooleanVectorStack,
300        interpreter.NameStack,
301        interpreter.CustomExpressions,
302        v => BooleanVectorPushExpression.Create(pool, v));
303    }
304  }
305
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)]
311  [StorableClass]
312  public class StringVectorDefineExpression : DefineExpression<IReadOnlyList<string>> {
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) {
323      var pool = interpreter.PoolContainer.GetStatefulExpressionPool<BooleanVectorPushExpression>();
324
325      Eval(
326        interpreter.StringVectorStack,
327        interpreter.NameStack,
328        interpreter.CustomExpressions,
329        v => StringVectorPushExpression.Create(pool, v));
330    }
331  }
332}
Note: See TracBrowser for help on using the repository browser.