Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/PushExpressions.cs @ 17021

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

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

File size: 12.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
5
6namespace HeuristicLab.Problems.ProgramSynthesis {
7  [Serializable]
8  [StorableClass]
9  public abstract class PushExpression<T> : StatefulExpression<T> {
10    [StorableConstructor]
11    protected PushExpression(bool deserializing) : base(deserializing) { }
12
13    protected PushExpression(T state, object other) : base(state) { }
14
15    public override bool IsNoop(IInternalPushInterpreter interpreter) {
16      return false;
17    }
18
19    protected void Eval(IPushStack<T> stack) {
20      stack.Push(State);
21    }
22  }
23
24  [StorableClass]
25  [Serializable]
26  public abstract class VectorPushExpression<T> : PushExpression<IReadOnlyList<T>> {
27    [StorableConstructor]
28    protected VectorPushExpression(bool deserializing) : base(deserializing) { }
29
30    protected VectorPushExpression(IReadOnlyList<T> state) : base(state, null) { }
31
32    protected override string GetStringRepresentation() {
33      return PushEnvironment.VectorStartSymbolStr +
34             string.Join(PushEnvironment.VectorSeparatorSymbolStr, State) +
35             PushEnvironment.VectorEndSymbolStr;
36    }
37
38    protected override int CalcHashCode() {
39      return State.HashCode();
40    }
41  }
42
43  [StorableClass]
44  [Serializable]
45  [PushExpression(StackTypes.Integer, "INTEGER.PUSH", "Pushes a specific integer onto the INTEGER stack", isHidden: true)]
46  public class IntegerPushExpression : PushExpression<long> {
47    [StorableConstructor]
48    protected IntegerPushExpression(bool deserializing) : base(deserializing) { }
49
50    public static IntegerPushExpression Create(IManagedPool<Expression> statefulExpressionPool, long state) {
51      var expression = (IntegerPushExpression)statefulExpressionPool.Get();
52      expression.State = state;
53
54      return expression;
55    }
56
57    /// <summary>
58    /// Pushes default(long)
59    /// </summary>
60    public IntegerPushExpression() : base(default(long), null) {
61    }
62
63    public IntegerPushExpression(long state)
64      : base(state, null) {
65    }
66
67    public override void Eval(IInternalPushInterpreter interpreter) {
68      Eval(interpreter.IntegerStack);
69    }
70  }
71
72  [StorableClass]
73  [Serializable]
74  [PushExpression(StackTypes.Float, "FLOAT.PUSH", "Pushes a specific float onto the FLOAT stack", isHidden: true)]
75  public class FloatPushExpression : PushExpression<double> {
76    [StorableConstructor]
77    protected FloatPushExpression(bool deserializing) : base(deserializing) { }
78
79    public static FloatPushExpression Create(IManagedPool<Expression> statefulExpressionPool, double state) {
80      var expression = (FloatPushExpression)statefulExpressionPool.Get();
81      expression.State = state;
82
83      return expression;
84    }
85
86    /// <summary>
87    /// Pushes default(double)
88    /// </summary>
89    public FloatPushExpression() : base(default(double), null) {
90    }
91
92    public FloatPushExpression(double state)
93      : base(state, null) {
94    }
95
96    public override void Eval(IInternalPushInterpreter interpreter) {
97      Eval(interpreter.FloatStack);
98    }
99  }
100
101  [StorableClass]
102  [Serializable]
103  [PushExpression(StackTypes.Boolean, "BOOLEAN.PUSH", "Pushes a specific boolean onto the BOOLEAN stack", isHidden: true)]
104  public class BooleanPushExpression : PushExpression<bool> {
105    [StorableConstructor]
106    protected BooleanPushExpression(bool deserializing, object other) : base(deserializing) { }
107
108    public static BooleanPushExpression Create(IManagedPool<Expression> statefulExpressionPool, bool state) {
109      var expression = (BooleanPushExpression)statefulExpressionPool.Get();
110      expression.State = state;
111
112      return expression;
113    }
114
115    /// <summary>
116    /// Pushes default(bool)
117    /// </summary>
118    public BooleanPushExpression() : base(default(bool), null) {
119    }
120
121    public BooleanPushExpression(bool state)
122      : base(state, null) {
123    }
124
125    public override void Eval(IInternalPushInterpreter interpreter) {
126      Eval(interpreter.BooleanStack);
127    }
128  }
129
130  [StorableClass]
131  [Serializable]
132  [PushExpression(StackTypes.Name, "NAME.PUSH", "Pushes a specific name onto the NAME stack", isHidden: true)]
133  public class NamePushExpression : PushExpression<string> {
134    [StorableConstructor]
135    protected NamePushExpression(bool deserializing) : base(deserializing) { }
136    public static NamePushExpression Create(IManagedPool<Expression> statefulExpressionPool, string state) {
137      var expression = (NamePushExpression)statefulExpressionPool.Get();
138      expression.State = state;
139
140      return expression;
141    }
142
143    public NamePushExpression() : base(string.Empty, null) {
144    }
145
146    public NamePushExpression(string state)
147      : base(state, null) {
148    }
149
150    public override void Eval(IInternalPushInterpreter interpreter) {
151      Eval(interpreter.NameStack);
152    }
153  }
154
155  [StorableClass]
156  [Serializable]
157  [PushExpression(StackTypes.Exec, "EXEC.PUSH", "Pushes a specific expression onto the EXEC stack", isHidden: true)]
158  public class ExecPushExpression : PushExpression<Expression> {
159    [StorableConstructor]
160    protected ExecPushExpression(bool deserializing) : base(deserializing) { }
161    public static ExecPushExpression Create(IManagedPool<Expression> statefulExpressionPool, Expression state) {
162      var expression = (ExecPushExpression)statefulExpressionPool.Get();
163      expression.State = state;
164
165      return expression;
166    }
167
168    private static readonly Expression Noop = ExpressionTable.GetStatelessExpression<ExecNoopExpression>();
169
170    /// <summary>
171    /// Pushes ExecNoop
172    /// </summary>
173    public ExecPushExpression() : base(Noop, null) {
174    }
175
176    public ExecPushExpression(Expression state)
177      : base(state, null) {
178    }
179
180    public override void Eval(IInternalPushInterpreter interpreter) {
181      Eval(interpreter.ExecStack);
182    }
183  }
184
185  [StorableClass]
186  [Serializable]
187  [PushExpression(StackTypes.Char, "CHAR.PUSH", "Pushes a specific char onto the CHAR stack", isHidden: true)]
188  public class CharPushExpression : PushExpression<char> {
189    [StorableConstructor]
190    protected CharPushExpression(bool deserializing) : base(deserializing) { }
191
192    // not supported so far
193    public static CharPushExpression Create(IManagedPool<Expression> statefulExpressionPool, char state) {
194      var expression = (CharPushExpression)statefulExpressionPool.Get();
195      expression.State = state;
196
197      return expression;
198    }
199
200    /// <summary>
201    /// Pushes default(char)
202    /// </summary>
203    public CharPushExpression() : base(default(char), null) {
204    }
205
206    public CharPushExpression(char state)
207      : base(state, null) {
208    }
209
210    protected override string GetStringRepresentation() {
211      return PushEnvironment.CharSymbol + State.Printify() + PushEnvironment.CharSymbol;
212    }
213
214    public override void Eval(IInternalPushInterpreter interpreter) {
215      Eval(interpreter.CharStack);
216    }
217  }
218
219  [StorableClass]
220  [Serializable]
221  [PushExpression(StackTypes.String, "STRING.PUSH", "Pushes a specific string onto the STRING stack", isHidden: true)]
222  public class StringPushExpression : PushExpression<string> {
223    [StorableConstructor]
224    protected StringPushExpression(bool deserializing) : base(deserializing) { }
225
226    public static StringPushExpression Create(IManagedPool<Expression> statefulExpressionPool, string state) {
227      var expression = (StringPushExpression)statefulExpressionPool.Get();
228      expression.State = state;
229
230      return expression;
231    }
232
233    /// <summary>
234    /// Pushes String.Empty
235    /// </summary>
236    public StringPushExpression() : base(string.Empty, null) {
237    }
238
239    protected override string GetStringRepresentation() {
240      return PushEnvironment.StringSymbol + State + PushEnvironment.StringSymbol;
241    }
242
243    public StringPushExpression(string state)
244      : base(state, null) {
245    }
246
247    public override void Eval(IInternalPushInterpreter interpreter) {
248      Eval(interpreter.StringStack);
249    }
250  }
251
252  [StorableClass]
253  [Serializable]
254  [PushExpression(StackTypes.IntegerVector, "INTEGER[].PUSH", "Pushes a specific integer vector onto the INTEGER[] stack", isHidden: true)]
255  public class IntegerVectorPushExpression : VectorPushExpression<long> {
256    [StorableConstructor]
257    protected IntegerVectorPushExpression(bool deserializing) : base(deserializing) { }
258
259    public static IntegerVectorPushExpression Create(IManagedPool<Expression> statefulExpressionPool, IReadOnlyList<long> state) {
260      var expression = (IntegerVectorPushExpression)statefulExpressionPool.Get();
261      expression.State = state;
262
263      return expression;
264    }
265
266    [NonSerialized]
267    private static readonly long[] EmptyState = new long[0];
268
269    /// <summary>
270    /// Pushes long[0]
271    /// </summary>
272    public IntegerVectorPushExpression() : base(EmptyState) {
273    }
274
275    public IntegerVectorPushExpression(IReadOnlyList<long> state) : base(state) {
276    }
277
278    public override void Eval(IInternalPushInterpreter interpreter) {
279      Eval(interpreter.IntegerVectorStack);
280    }
281  }
282
283  [StorableClass]
284  [Serializable]
285  [PushExpression(StackTypes.FloatVector, "FLOAT[].PUSH", "Pushes a specific float vector onto the FLOAT[] stack", isHidden: true)]
286  public class FloatVectorPushExpression : VectorPushExpression<double> {
287    [StorableConstructor]
288    protected FloatVectorPushExpression(bool deserializing) : base(deserializing) { }
289
290    public static FloatVectorPushExpression Create(IManagedPool<Expression> statefulExpressionPool, IReadOnlyList<double> state) {
291      var expression = (FloatVectorPushExpression)statefulExpressionPool.Get();
292      expression.State = state;
293
294      return expression;
295    }
296
297    [NonSerialized]
298    private static readonly double[] EmptyState = new double[0];
299
300    /// <summary>
301    /// Pushes double[0]
302    /// </summary>
303    public FloatVectorPushExpression() : base(EmptyState) {
304    }
305
306    public FloatVectorPushExpression(IReadOnlyList<double> state) : base(state) { }
307
308    public override void Eval(IInternalPushInterpreter interpreter) {
309      Eval(interpreter.FloatVectorStack);
310    }
311  }
312
313  [StorableClass]
314  [Serializable]
315  [PushExpression(StackTypes.BooleanVector, "BOOLEAN[].PUSH", "Pushes a specific boolean vector onto the BOOLEAN[] stack", isHidden: true)]
316  public class BooleanVectorPushExpression : VectorPushExpression<bool> {
317    [StorableConstructor]
318    protected BooleanVectorPushExpression(bool deserializing) : base(deserializing) { }
319
320    public static BooleanVectorPushExpression Create(IManagedPool<Expression> statefulExpressionPool, IReadOnlyList<bool> state) {
321      var expression = (BooleanVectorPushExpression)statefulExpressionPool.Get();
322      expression.State = state;
323
324      return expression;
325    }
326
327    [NonSerialized]
328    private static readonly bool[] EmptyState = new bool[0];
329
330    /// <summary>
331    /// Pushes bool[0]
332    /// </summary>
333    public BooleanVectorPushExpression() : base(EmptyState) {
334    }
335
336    public BooleanVectorPushExpression(IReadOnlyList<bool> state) : base(state) { }
337
338    public override void Eval(IInternalPushInterpreter interpreter) {
339      Eval(interpreter.BooleanVectorStack);
340    }
341  }
342
343  [StorableClass]
344  [Serializable]
345  [PushExpression(StackTypes.StringVector, "STRING[].PUSH", "Pushes a specific string vector onto the STRING[] stack", isHidden: true)]
346  public class StringVectorPushExpression : VectorPushExpression<string> {
347    [StorableConstructor]
348    protected StringVectorPushExpression(bool deserializing) : base(deserializing) { }
349
350    public static StringVectorPushExpression Create(IManagedPool<Expression> statefulExpressionPool, IReadOnlyList<string> state) {
351      var expression = (StringVectorPushExpression)statefulExpressionPool.Get();
352      expression.State = state;
353
354      return expression;
355    }
356
357    [NonSerialized]
358    private static readonly string[] EmptyState = new string[0];
359
360    /// <summary>
361    /// Pushes string[0]
362    /// </summary>
363    public StringVectorPushExpression() : base(EmptyState) {
364    }
365
366    protected override string GetStringRepresentation() {
367      return PushEnvironment.VectorStartSymbolStr +
368             string.Join(PushEnvironment.VectorSeparatorSymbolStr, State.Select(str => PushEnvironment.StringSymbol + str + PushEnvironment.StringSymbol)) +
369             PushEnvironment.VectorEndSymbolStr;
370    }
371
372    public StringVectorPushExpression(IReadOnlyList<string> state) : base(state) { }
373
374    public override void Eval(IInternalPushInterpreter interpreter) {
375      Eval(interpreter.StringVectorStack);
376    }
377  }
378}
Note: See TracBrowser for help on using the repository browser.