Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/EqualsExpressions.cs @ 15032

Last change on this file since 15032 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: 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    StackTypes.Boolean)]
87  public class BooleanEqualsExpression : EqualsExpression<bool> {
88    public BooleanEqualsExpression() { }
89    [StorableConstructor]
90    protected BooleanEqualsExpression(bool deserializing) : base(deserializing) { }
91
92    public override bool IsNoop(IInternalPushInterpreter interpreter) {
93      return interpreter.BooleanStack.Count < 2;
94    }
95
96    public override void Eval(IInternalPushInterpreter interpreter) {
97      Eval(interpreter.BooleanStack, interpreter.BooleanStack);
98    }
99  }
100
101  [StorableClass]
102  [PushExpression(
103    StackTypes.Name,
104    "NAME.=",
105    "Pushes TRUE onto the BOOLEAN stack if the top two NAME items are equal, or FALSE otherwise.",
106    StackTypes.Boolean)]
107  public class NameEqualsExpression : EqualsExpression<string> {
108    public NameEqualsExpression() { }
109    [StorableConstructor]
110    protected NameEqualsExpression(bool deserializing) : base(deserializing) { }
111
112    public override bool IsNoop(IInternalPushInterpreter interpreter) {
113      return interpreter.NameStack.Count < 2;
114    }
115
116    public override void Eval(IInternalPushInterpreter interpreter) {
117      Eval(interpreter.NameStack, interpreter.BooleanStack);
118    }
119  }
120
121  [StorableClass]
122  [PushExpression(
123    StackTypes.Exec,
124    "EXEC.=",
125    "Pushes TRUE onto the BOOLEAN stack if the top two EXEC items are equal, or FALSE otherwise.",
126    StackTypes.Boolean,
127    execIn: 0)]
128  public class ExecEqualsExpression : EqualsExpression<Expression> {
129    public ExecEqualsExpression() { }
130    [StorableConstructor]
131    protected ExecEqualsExpression(bool deserializing) : base(deserializing) { }
132
133    public override bool IsNoop(IInternalPushInterpreter interpreter) {
134      return interpreter.ExecStack.Count < 2;
135    }
136
137    public override void Eval(IInternalPushInterpreter interpreter) {
138      Eval(interpreter.ExecStack, interpreter.BooleanStack);
139    }
140  }
141
142  [StorableClass]
143  [PushExpression(
144    StackTypes.Code,
145    "CODE.=",
146    "Pushes TRUE onto the BOOLEAN stack if the top two CODE items are equal, or FALSE otherwise.",
147    StackTypes.Boolean)]
148  public class CodeEqualsExpression : EqualsExpression<Expression> {
149    public CodeEqualsExpression() { }
150    [StorableConstructor]
151    protected CodeEqualsExpression(bool deserializing) : base(deserializing) { }
152
153    public override bool IsNoop(IInternalPushInterpreter interpreter) {
154      return interpreter.CodeStack.Count < 2;
155    }
156
157    public override void Eval(IInternalPushInterpreter interpreter) {
158      Eval(interpreter.CodeStack, interpreter.BooleanStack);
159    }
160  }
161
162  [StorableClass]
163  [PushExpression(
164    StackTypes.Char,
165    "CHAR.=",
166    "Pushes TRUE onto the BOOLEAN stack if the top two CHAR items are equal, or FALSE otherwise.",
167    StackTypes.Boolean)]
168  public class CharEqualsExpression : EqualsExpression<char> {
169    public CharEqualsExpression() { }
170    [StorableConstructor]
171    protected CharEqualsExpression(bool deserializing) : base(deserializing) { }
172
173    public override bool IsNoop(IInternalPushInterpreter interpreter) {
174      return interpreter.CharStack.Count < 2;
175    }
176
177    public override void Eval(IInternalPushInterpreter interpreter) {
178      Eval(interpreter.CharStack, interpreter.BooleanStack);
179    }
180  }
181
182  [StorableClass]
183  [PushExpression(
184    StackTypes.String,
185    "STRING.=",
186    "Pushes TRUE onto the BOOLEAN stack if the top two STRING items are equal, or FALSE otherwise.",
187    StackTypes.Boolean)]
188  public class StringEqualsExpression : EqualsExpression<string> {
189    public StringEqualsExpression() { }
190    [StorableConstructor]
191    protected StringEqualsExpression(bool deserializing) : base(deserializing) { }
192
193    public override bool IsNoop(IInternalPushInterpreter interpreter) {
194      return interpreter.StringStack.Count < 2;
195    }
196
197    public override void Eval(IInternalPushInterpreter interpreter) {
198      Eval(interpreter.StringStack, interpreter.BooleanStack);
199    }
200  }
201
202  [StorableClass]
203  [PushExpression(
204    StackTypes.IntegerVector,
205    "INTEGER[].=",
206    "Pushes TRUE onto the BOOLEAN stack if the top two INTEGER[] items are equal, or FALSE otherwise.",
207    StackTypes.Boolean)]
208  public class IntegerVectorEqualsExpression : EqualsExpression<long> {
209    public IntegerVectorEqualsExpression() { }
210    [StorableConstructor]
211    protected IntegerVectorEqualsExpression(bool deserializing) : base(deserializing) { }
212
213    public override bool IsNoop(IInternalPushInterpreter interpreter) {
214      return interpreter.IntegerVectorStack.Count < 2;
215    }
216
217    public override void Eval(IInternalPushInterpreter interpreter) {
218      Eval(interpreter.IntegerVectorStack, interpreter.BooleanStack);
219    }
220  }
221
222  [StorableClass]
223  [PushExpression(
224    StackTypes.FloatVector,
225    "FLOAT[].=",
226    "Pushes TRUE onto the BOOLEAN stack if the top two FLOAT[] items are equal, or FALSE otherwise.",
227    StackTypes.Boolean)]
228  public class FloatVectorEqualsExpression : EqualsExpression<double> {
229    public FloatVectorEqualsExpression() { }
230    [StorableConstructor]
231    protected FloatVectorEqualsExpression(bool deserializing) : base(deserializing) { }
232
233    public override bool IsNoop(IInternalPushInterpreter interpreter) {
234      return interpreter.FloatVectorStack.Count < 2;
235    }
236
237    public override void Eval(IInternalPushInterpreter interpreter) {
238      Eval(interpreter.FloatVectorStack, interpreter.BooleanStack);
239    }
240  }
241
242  [StorableClass]
243  [PushExpression(
244    StackTypes.BooleanVector,
245    "BOOLEAN[].=",
246    "Pushes TRUE onto the BOOLEAN stack if the top two BOOLEAN[] items are equal, or FALSE otherwise.",
247    StackTypes.Boolean)]
248  public class BooleanVectorEqualsExpression : EqualsExpression<bool> {
249    public BooleanVectorEqualsExpression() { }
250    [StorableConstructor]
251    protected BooleanVectorEqualsExpression(bool deserializing) : base(deserializing) { }
252
253    public override bool IsNoop(IInternalPushInterpreter interpreter) {
254      return interpreter.BooleanVectorStack.Count < 2;
255    }
256
257    public override void Eval(IInternalPushInterpreter interpreter) {
258      Eval(interpreter.BooleanVectorStack, interpreter.BooleanStack);
259    }
260  }
261
262  [StorableClass]
263  [PushExpression(
264    StackTypes.StringVector,
265    "STRING[].=",
266    "Pushes TRUE onto the BOOLEAN stack if the top two STRING[] items are equal, or FALSE otherwise.",
267    StackTypes.Boolean)]
268  public class StringVectorEqualsExpression : EqualsExpression<string> {
269    public StringVectorEqualsExpression() { }
270    [StorableConstructor]
271    protected StringVectorEqualsExpression(bool deserializing) : base(deserializing) { }
272
273    public override bool IsNoop(IInternalPushInterpreter interpreter) {
274      return interpreter.StringVectorStack.Count < 2;
275    }
276
277    public override void Eval(IInternalPushInterpreter interpreter) {
278      Eval(interpreter.StringVectorStack, interpreter.BooleanStack);
279    }
280  }
281}
Note: See TracBrowser for help on using the repository browser.