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
RevLine 
[14727]1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
[14834]2  using System.Collections.Generic;
[15017]3  using System.Linq;
4
[14897]5  using Attributes;
[14952]6
7  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
8
[14897]9  using Interpreter;
10  using Stack;
[14834]11
[14727]12  /// <summary>
[15032]13  /// Pushes TRUE onto the T stack if the top two items are equal, or FALSE otherwise
[14727]14  /// </summary>
15  /// <typeparam name="T">Stacktype</typeparam>
[14952]16  [StorableClass]
[14727]17  public abstract class EqualsExpression<T> : StatelessExpression {
[14952]18    protected EqualsExpression() { }
19    [StorableConstructor]
20    protected EqualsExpression(bool deserializing) : base(deserializing) { }
[14727]21
[14952]22    protected void Eval(IPushStack<T> stack, IPushStack<bool> booleanStack) {
[14875]23      var first = stack[1];
24      var second = stack.Top;
25      stack.Remove(2);
[14727]26
[15017]27      var equal = first.Equals(second);
28      booleanStack.Push(equal);
[14727]29    }
[15017]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    }
[14727]39  }
40
[14952]41  [StorableClass]
[15032]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)]
[14727]47  public class IntegerEqualsExpression : EqualsExpression<long> {
[14952]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;
[14727]54    }
[14952]55
56    public override void Eval(IInternalPushInterpreter interpreter) {
57      Eval(interpreter.IntegerStack, interpreter.BooleanStack);
58    }
[14727]59  }
60
[14952]61  [StorableClass]
[15032]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)]
[14727]67  public class FloatEqualsExpression : EqualsExpression<double> {
[14952]68    public FloatEqualsExpression() { }
69    [StorableConstructor]
70    protected FloatEqualsExpression(bool deserializing) : base(deserializing) { }
[14834]71
[14952]72    public override bool IsNoop(IInternalPushInterpreter interpreter) {
73      return interpreter.FloatStack.Count < 2;
74    }
[14875]75
[14952]76    public override void Eval(IInternalPushInterpreter interpreter) {
77      Eval(interpreter.FloatStack, interpreter.BooleanStack);
[14727]78    }
79  }
80
[14952]81  [StorableClass]
[15032]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)]
[14727]87  public class BooleanEqualsExpression : EqualsExpression<bool> {
[14952]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;
[14727]94    }
[14952]95
96    public override void Eval(IInternalPushInterpreter interpreter) {
97      Eval(interpreter.BooleanStack, interpreter.BooleanStack);
98    }
[14727]99  }
100
[14952]101  [StorableClass]
[15032]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)]
[14727]107  public class NameEqualsExpression : EqualsExpression<string> {
[14952]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;
[14727]114    }
[14952]115
116    public override void Eval(IInternalPushInterpreter interpreter) {
117      Eval(interpreter.NameStack, interpreter.BooleanStack);
118    }
[14727]119  }
120
[14952]121  [StorableClass]
[15032]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)]
[14727]128  public class ExecEqualsExpression : EqualsExpression<Expression> {
[14952]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;
[14727]135    }
[14952]136
137    public override void Eval(IInternalPushInterpreter interpreter) {
138      Eval(interpreter.ExecStack, interpreter.BooleanStack);
139    }
[14727]140  }
141
[14952]142  [StorableClass]
[15032]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)]
[14727]148  public class CodeEqualsExpression : EqualsExpression<Expression> {
[14952]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;
[14727]155    }
[14952]156
157    public override void Eval(IInternalPushInterpreter interpreter) {
158      Eval(interpreter.CodeStack, interpreter.BooleanStack);
159    }
[14727]160  }
[14777]161
[14952]162  [StorableClass]
[15032]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)]
[14777]168  public class CharEqualsExpression : EqualsExpression<char> {
[14952]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;
[14777]175    }
[14952]176
177    public override void Eval(IInternalPushInterpreter interpreter) {
178      Eval(interpreter.CharStack, interpreter.BooleanStack);
179    }
[14777]180  }
181
[14952]182  [StorableClass]
[15032]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)]
[14777]188  public class StringEqualsExpression : EqualsExpression<string> {
[14952]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;
[14777]195    }
[14952]196
197    public override void Eval(IInternalPushInterpreter interpreter) {
198      Eval(interpreter.StringStack, interpreter.BooleanStack);
199    }
[14777]200  }
[14834]201
[14952]202  [StorableClass]
[15032]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)]
[15017]208  public class IntegerVectorEqualsExpression : EqualsExpression<long> {
[14952]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;
[14834]215    }
[14952]216
217    public override void Eval(IInternalPushInterpreter interpreter) {
218      Eval(interpreter.IntegerVectorStack, interpreter.BooleanStack);
219    }
[14834]220  }
[14875]221
[14952]222  [StorableClass]
[15032]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)]
[15017]228  public class FloatVectorEqualsExpression : EqualsExpression<double> {
[14952]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;
[14875]235    }
[14952]236
237    public override void Eval(IInternalPushInterpreter interpreter) {
238      Eval(interpreter.FloatVectorStack, interpreter.BooleanStack);
239    }
[14875]240  }
241
[14952]242  [StorableClass]
[15032]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)]
[15017]248  public class BooleanVectorEqualsExpression : EqualsExpression<bool> {
[14952]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;
[14875]255    }
[14952]256
257    public override void Eval(IInternalPushInterpreter interpreter) {
258      Eval(interpreter.BooleanVectorStack, interpreter.BooleanStack);
259    }
[14875]260  }
261
[14952]262  [StorableClass]
[15032]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)]
[15017]268  public class StringVectorEqualsExpression : EqualsExpression<string> {
[14952]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;
[14875]275    }
[14952]276
277    public override void Eval(IInternalPushInterpreter interpreter) {
278      Eval(interpreter.StringVectorStack, interpreter.BooleanStack);
279    }
[14875]280  }
[14727]281}
Note: See TracBrowser for help on using the repository browser.