Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/EqualsExpressions.cs @ 17603

Last change on this file since 17603 was 15334, checked in by pkimmesw, 7 years ago

#2665 Testet Problems, Testet error functions, Small fixes, Created HL files

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