Free cookie consent management tool by TermsFeed Policy Generator

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