Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/EqualsExpressions.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: 9.5 KB
Line 
1using System.Collections.Generic;
2using System.Linq;
3using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
4
5namespace HeuristicLab.Problems.ProgramSynthesis {
6
7  /// <summary>
8  /// Pushes TRUE onto the T stack if the top two items are equal, or FALSE otherwise
9  /// </summary>
10  /// <typeparam name="T">Stacktype</typeparam>
11  [StorableClass]
12  public abstract class EqualsExpression<T> : StatelessExpression {
13    protected EqualsExpression() { }
14    [StorableConstructor]
15    protected EqualsExpression(bool deserializing) : base(deserializing) { }
16
17    protected void Eval(IPushStack<T> stack, IPushStack<bool> booleanStack) {
18      var first = stack[1];
19      var second = stack.Top;
20      stack.Remove(2);
21
22      var equal = first.Equals(second);
23      booleanStack.Push(equal);
24    }
25
26    protected void Eval(IPushStack<IReadOnlyList<T>> vectorStack, IPushStack<bool> booleanStack) {
27      var first = vectorStack[1];
28      var second = vectorStack[0];
29      vectorStack.Remove(2);
30
31      var equal = first.SequenceEqual(second);
32      booleanStack.Push(equal);
33    }
34  }
35
36  [StorableClass]
37  [PushExpression(
38    StackTypes.Integer,
39    "INTEGER.=",
40    "Pushes TRUE onto the BOOLEAN stack if the top two INTEGER items are equal, or FALSE otherwise.",
41    StackTypes.Boolean)]
42  public class IntegerEqualsExpression : EqualsExpression<long> {
43    public IntegerEqualsExpression() { }
44    [StorableConstructor]
45    protected IntegerEqualsExpression(bool deserializing) : base(deserializing) { }
46
47    public override bool IsNoop(IInternalPushInterpreter interpreter) {
48      return interpreter.IntegerStack.Count < 2;
49    }
50
51    public override void Eval(IInternalPushInterpreter interpreter) {
52      Eval(interpreter.IntegerStack, interpreter.BooleanStack);
53    }
54  }
55
56  [StorableClass]
57  [PushExpression(
58    StackTypes.Float,
59    "FLOAT.=",
60    "Pushes TRUE onto the BOOLEAN stack if the top two FLOAT items are equal, or FALSE otherwise.",
61    StackTypes.Boolean)]
62  public class FloatEqualsExpression : EqualsExpression<double> {
63    public FloatEqualsExpression() { }
64    [StorableConstructor]
65    protected FloatEqualsExpression(bool deserializing) : base(deserializing) { }
66
67    public override bool IsNoop(IInternalPushInterpreter interpreter) {
68      return interpreter.FloatStack.Count < 2;
69    }
70
71    public override void Eval(IInternalPushInterpreter interpreter) {
72      Eval(interpreter.FloatStack, interpreter.BooleanStack);
73    }
74  }
75
76  [StorableClass]
77  [PushExpression(
78    StackTypes.Boolean,
79    "BOOLEAN.=",
80    "Pushes TRUE onto the BOOLEAN stack if the top two BOOLEAN items are equal, or FALSE otherwise.")]
81  public class BooleanEqualsExpression : EqualsExpression<bool> {
82    public BooleanEqualsExpression() { }
83    [StorableConstructor]
84    protected BooleanEqualsExpression(bool deserializing) : base(deserializing) { }
85
86    public override bool IsNoop(IInternalPushInterpreter interpreter) {
87      return interpreter.BooleanStack.Count < 2;
88    }
89
90    public override void Eval(IInternalPushInterpreter interpreter) {
91      Eval(interpreter.BooleanStack, interpreter.BooleanStack);
92    }
93  }
94
95  [StorableClass]
96  [PushExpression(
97    StackTypes.Name,
98    "NAME.=",
99    "Pushes TRUE onto the BOOLEAN stack if the top two NAME items are equal, or FALSE otherwise.",
100    StackTypes.Boolean)]
101  public class NameEqualsExpression : EqualsExpression<string> {
102    public NameEqualsExpression() { }
103    [StorableConstructor]
104    protected NameEqualsExpression(bool deserializing) : base(deserializing) { }
105
106    public override bool IsNoop(IInternalPushInterpreter interpreter) {
107      return interpreter.NameStack.Count < 2;
108    }
109
110    public override void Eval(IInternalPushInterpreter interpreter) {
111      Eval(interpreter.NameStack, interpreter.BooleanStack);
112    }
113  }
114
115  [StorableClass]
116  [PushExpression(
117    StackTypes.Exec,
118    "EXEC.=",
119    "Pushes TRUE onto the BOOLEAN stack if the top two EXEC items are equal, or FALSE otherwise.",
120    StackTypes.Boolean,
121    requiredBlockCount: 0)]
122  public class ExecEqualsExpression : EqualsExpression<Expression> {
123    public ExecEqualsExpression() { }
124    [StorableConstructor]
125    protected ExecEqualsExpression(bool deserializing) : base(deserializing) { }
126
127    public override bool IsNoop(IInternalPushInterpreter interpreter) {
128      return interpreter.ExecStack.Count < 2;
129    }
130
131    public override void Eval(IInternalPushInterpreter interpreter) {
132      Eval(interpreter.ExecStack, interpreter.BooleanStack);
133    }
134  }
135
136  [StorableClass]
137  [PushExpression(
138    StackTypes.Code,
139    "CODE.=",
140    "Pushes TRUE onto the BOOLEAN stack if the top two CODE items are equal, or FALSE otherwise.",
141    StackTypes.Boolean)]
142  public class CodeEqualsExpression : EqualsExpression<Expression> {
143    public CodeEqualsExpression() { }
144    [StorableConstructor]
145    protected CodeEqualsExpression(bool deserializing) : base(deserializing) { }
146
147    public override bool IsNoop(IInternalPushInterpreter interpreter) {
148      return interpreter.CodeStack.Count < 2;
149    }
150
151    public override void Eval(IInternalPushInterpreter interpreter) {
152      Eval(interpreter.CodeStack, interpreter.BooleanStack);
153    }
154  }
155
156  [StorableClass]
157  [PushExpression(
158    StackTypes.Char,
159    "CHAR.=",
160    "Pushes TRUE onto the BOOLEAN stack if the top two CHAR items are equal, or FALSE otherwise.",
161    StackTypes.Boolean)]
162  public class CharEqualsExpression : EqualsExpression<char> {
163    public CharEqualsExpression() { }
164    [StorableConstructor]
165    protected CharEqualsExpression(bool deserializing) : base(deserializing) { }
166
167    public override bool IsNoop(IInternalPushInterpreter interpreter) {
168      return interpreter.CharStack.Count < 2;
169    }
170
171    public override void Eval(IInternalPushInterpreter interpreter) {
172      Eval(interpreter.CharStack, interpreter.BooleanStack);
173    }
174  }
175
176  [StorableClass]
177  [PushExpression(
178    StackTypes.String,
179    "STRING.=",
180    "Pushes TRUE onto the BOOLEAN stack if the top two STRING items are equal, or FALSE otherwise.",
181    StackTypes.Boolean)]
182  public class StringEqualsExpression : EqualsExpression<string> {
183    public StringEqualsExpression() { }
184    [StorableConstructor]
185    protected StringEqualsExpression(bool deserializing) : base(deserializing) { }
186
187    public override bool IsNoop(IInternalPushInterpreter interpreter) {
188      return interpreter.StringStack.Count < 2;
189    }
190
191    public override void Eval(IInternalPushInterpreter interpreter) {
192      Eval(interpreter.StringStack, interpreter.BooleanStack);
193    }
194  }
195
196  [StorableClass]
197  [PushExpression(
198    StackTypes.IntegerVector,
199    "INTEGER[].=",
200    "Pushes TRUE onto the BOOLEAN stack if the top two INTEGER[] items are equal, or FALSE otherwise.",
201    StackTypes.Boolean)]
202  public class IntegerVectorEqualsExpression : EqualsExpression<long> {
203    public IntegerVectorEqualsExpression() { }
204    [StorableConstructor]
205    protected IntegerVectorEqualsExpression(bool deserializing) : base(deserializing) { }
206
207    public override bool IsNoop(IInternalPushInterpreter interpreter) {
208      return interpreter.IntegerVectorStack.Count < 2;
209    }
210
211    public override void Eval(IInternalPushInterpreter interpreter) {
212      Eval(interpreter.IntegerVectorStack, interpreter.BooleanStack);
213    }
214  }
215
216  [StorableClass]
217  [PushExpression(
218    StackTypes.FloatVector,
219    "FLOAT[].=",
220    "Pushes TRUE onto the BOOLEAN stack if the top two FLOAT[] items are equal, or FALSE otherwise.",
221    StackTypes.Boolean)]
222  public class FloatVectorEqualsExpression : EqualsExpression<double> {
223    public FloatVectorEqualsExpression() { }
224    [StorableConstructor]
225    protected FloatVectorEqualsExpression(bool deserializing) : base(deserializing) { }
226
227    public override bool IsNoop(IInternalPushInterpreter interpreter) {
228      return interpreter.FloatVectorStack.Count < 2;
229    }
230
231    public override void Eval(IInternalPushInterpreter interpreter) {
232      Eval(interpreter.FloatVectorStack, interpreter.BooleanStack);
233    }
234  }
235
236  [StorableClass]
237  [PushExpression(
238    StackTypes.BooleanVector,
239    "BOOLEAN[].=",
240    "Pushes TRUE onto the BOOLEAN stack if the top two BOOLEAN[] items are equal, or FALSE otherwise.",
241    StackTypes.Boolean)]
242  public class BooleanVectorEqualsExpression : EqualsExpression<bool> {
243    public BooleanVectorEqualsExpression() { }
244    [StorableConstructor]
245    protected BooleanVectorEqualsExpression(bool deserializing) : base(deserializing) { }
246
247    public override bool IsNoop(IInternalPushInterpreter interpreter) {
248      return interpreter.BooleanVectorStack.Count < 2;
249    }
250
251    public override void Eval(IInternalPushInterpreter interpreter) {
252      Eval(interpreter.BooleanVectorStack, interpreter.BooleanStack);
253    }
254  }
255
256  [StorableClass]
257  [PushExpression(
258    StackTypes.StringVector,
259    "STRING[].=",
260    "Pushes TRUE onto the BOOLEAN stack if the top two STRING[] items are equal, or FALSE otherwise.",
261    StackTypes.Boolean)]
262  public class StringVectorEqualsExpression : EqualsExpression<string> {
263    public StringVectorEqualsExpression() { }
264    [StorableConstructor]
265    protected StringVectorEqualsExpression(bool deserializing) : base(deserializing) { }
266
267    public override bool IsNoop(IInternalPushInterpreter interpreter) {
268      return interpreter.StringVectorStack.Count < 2;
269    }
270
271    public override void Eval(IInternalPushInterpreter interpreter) {
272      Eval(interpreter.StringVectorStack, interpreter.BooleanStack);
273    }
274  }
275}
Note: See TracBrowser for help on using the repository browser.