Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/PushExpressions.cs @ 16801

Last change on this file since 16801 was 15032, checked in by pkimmesw, 7 years ago

#2665 Fixed bias 0 issue, PushExpressionFrequencyAnalyzer, Fixed probability for ERC settings, Fixed enable/disable instructions, Added expression descriptions

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