Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/IntegerExpressions.cs @ 15344

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

#2665 Fixed Integer Overflow Exceptions, Adjusted Offspring Selection Experiments

File size: 15.8 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
2  using System;
3
4  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
5  using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
6  using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
7
8  using Interpreter;
9
10  /// <summary>
11  /// Pushes the sum of the top two items.
12  /// </summary>
13  [PushExpression(
14    StackTypes.Integer,
15    "INTEGER.+",
16    "Pushes the sum of the top two items.")]
17  [StorableClass]
18  public class IntegerAddExpression : StatelessExpression {
19    public IntegerAddExpression() { }
20    [StorableConstructor]
21    protected IntegerAddExpression(bool deserializing) : base(deserializing) { }
22
23    public override bool IsNoop(IInternalPushInterpreter interpreter) {
24      return interpreter.IntegerStack.Count < 2;
25    }
26
27    public override void Eval(IInternalPushInterpreter interpreter) {
28      var first = interpreter.IntegerStack.Pop();
29      var second = interpreter.IntegerStack.Top;
30      var result = unchecked(second + first);
31
32      interpreter.IntegerStack.Top = result;
33    }
34  }
35
36  /// <summary>
37  /// Pushes the difference of the top two items; that is, the second item minus the top item.
38  /// </summary>
39  [PushExpression(
40    StackTypes.Integer,
41    "INTEGER.-",
42    "Pushes the difference of the top two items; that is, the second item minus the top item.")]
43  [StorableClass]
44  public class IntegerSubtractExpression : StatelessExpression {
45    public IntegerSubtractExpression() { }
46    [StorableConstructor]
47    protected IntegerSubtractExpression(bool deserializing) : base(deserializing) { }
48
49    public override bool IsNoop(IInternalPushInterpreter interpreter) {
50      return interpreter.IntegerStack.Count < 2;
51    }
52
53    public override void Eval(IInternalPushInterpreter interpreter) {
54      var first = interpreter.IntegerStack.Pop();
55      var second = interpreter.IntegerStack.Top;
56      var result = unchecked(second - first);
57
58      interpreter.IntegerStack.Top = result;
59    }
60  }
61
62  /// <summary>
63  /// Pushes the product of the top two items.
64  /// </summary>
65  [PushExpression(
66    StackTypes.Integer,
67    "INTEGER.*",
68    "Pushes the product of the top two items.")]
69  [StorableClass]
70  public class IntegerMultiplyExpression : StatelessExpression {
71    public IntegerMultiplyExpression() { }
72    [StorableConstructor]
73    protected IntegerMultiplyExpression(bool deserializing) : base(deserializing) { }
74
75    public override bool IsNoop(IInternalPushInterpreter interpreter) {
76      return interpreter.IntegerStack.Count < 2;
77    }
78
79    public override void Eval(IInternalPushInterpreter interpreter) {
80      var first = interpreter.IntegerStack.Pop();
81      var second = interpreter.IntegerStack.Top;
82      var result = unchecked(second * first);
83
84      interpreter.IntegerStack.Top = result;
85    }
86  }
87
88  /// <summary>
89  /// Pushes the quotient of the top two items; that is, the second item divided by the top item.
90  /// If the top item is zero this acts as a NOOP.
91  /// </summary>
92  [PushExpression(
93    StackTypes.Integer,
94    "INTEGER./",
95    "Pushes the quotient of the top two items; that is, the second item divided by the top item. If the top item is zero this acts as a NOOP.")]
96  [StorableClass]
97  public class IntegerDivideExpression : StatelessExpression {
98    public IntegerDivideExpression() { }
99    [StorableConstructor]
100    protected IntegerDivideExpression(bool deserializing) : base(deserializing) { }
101
102    public override bool IsNoop(IInternalPushInterpreter interpreter) {
103      return interpreter.IntegerStack.Count < 2 ||
104             interpreter.IntegerStack.Top == 0 ||
105             (interpreter.IntegerStack.Top == -1 && interpreter.IntegerStack[1] == long.MinValue);
106    }
107
108    public override void Eval(IInternalPushInterpreter interpreter) {
109      var first = interpreter.IntegerStack.Pop();
110      var second = interpreter.IntegerStack.Top;
111      var result = second / first;
112
113      interpreter.IntegerStack.Top = result;
114    }
115  }
116
117  /// <summary>
118  /// Pushes the second stack item modulo the top stack item. If the top item is zero this acts as a NOOP.
119  /// </summary>
120  [PushExpression(
121    StackTypes.Integer,
122    "INTEGER.%",
123    "Pushes the second stack item modulo the top stack item. If the top item is zero this acts as a NOOP.")]
124  [StorableClass]
125  public class IntegerModuloExpression : StatelessExpression {
126    public IntegerModuloExpression() { }
127    [StorableConstructor]
128    protected IntegerModuloExpression(bool deserializing) : base(deserializing) { }
129
130    public override bool IsNoop(IInternalPushInterpreter interpreter) {
131      return interpreter.IntegerStack.Count < 2 ||
132             interpreter.IntegerStack.Top == 0 ||
133             (interpreter.IntegerStack.Top == -1 && interpreter.IntegerStack[1] == long.MinValue);
134    }
135
136    public override void Eval(IInternalPushInterpreter interpreter) {
137      var first = interpreter.IntegerStack.Pop();
138      var second = interpreter.IntegerStack.Top;
139      var result = second % first;
140
141      interpreter.IntegerStack.Top = result;
142    }
143  }
144
145  /// <summary>
146  /// Pushes the minimum of the top two items.
147  /// </summary>
148  [PushExpression(
149    StackTypes.Integer,
150    "INTEGER.MIN",
151    "Pushes the minimum of the top two items.")]
152  [StorableClass]
153  public class IntegerMinExpression : StatelessExpression {
154    public IntegerMinExpression() { }
155    [StorableConstructor]
156    protected IntegerMinExpression(bool deserializing) : base(deserializing) { }
157
158    public override bool IsNoop(IInternalPushInterpreter interpreter) {
159      return interpreter.IntegerStack.Count < 2;
160    }
161
162    public override void Eval(IInternalPushInterpreter interpreter) {
163      var first = interpreter.IntegerStack.Pop();
164      var second = interpreter.IntegerStack.Top;
165      var result = Math.Min(second, first);
166
167      interpreter.IntegerStack.Top = result;
168    }
169  }
170
171  /// <summary>
172  /// Pushes the maximum of the top two items.
173  /// </summary>
174  [PushExpression(
175    StackTypes.Integer,
176    "INTEGER.MAX",
177    "Pushes the maximum of the top two items.")]
178  [StorableClass]
179  public class IntegerMaxExpression : StatelessExpression {
180    public IntegerMaxExpression() { }
181    [StorableConstructor]
182    protected IntegerMaxExpression(bool deserializing) : base(deserializing) { }
183
184    public override bool IsNoop(IInternalPushInterpreter interpreter) {
185      return interpreter.IntegerStack.Count < 2;
186    }
187
188    public override void Eval(IInternalPushInterpreter interpreter) {
189      var first = interpreter.IntegerStack.Pop();
190      var second = interpreter.IntegerStack.Top;
191      var result = Math.Max(second, first);
192
193      interpreter.IntegerStack.Top = result;
194    }
195  }
196
197  /// <summary>
198  /// Pushes TRUE onto the BOOLEAN stack if the second item is less than the top item, or FALSE otherwise.
199  /// </summary>
200  [PushExpression(
201    StackTypes.Integer,
202    "INTEGER.<",
203    "Pushes TRUE onto the BOOLEAN stack if the second item is less than the top item, or FALSE otherwise.",
204    StackTypes.Boolean)]
205  [StorableClass]
206  public class IntegerSmallerThanExpression : StatelessExpression {
207    public IntegerSmallerThanExpression() { }
208    [StorableConstructor]
209    protected IntegerSmallerThanExpression(bool deserializing) : base(deserializing) { }
210
211    public override bool IsNoop(IInternalPushInterpreter interpreter) {
212      return interpreter.IntegerStack.Count < 2;
213    }
214
215    public override void Eval(IInternalPushInterpreter interpreter) {
216      var first = interpreter.IntegerStack.Top;
217      var second = interpreter.IntegerStack[1];
218      interpreter.IntegerStack.Remove(2);
219
220      var result = second < first;
221      interpreter.BooleanStack.Push(result);
222    }
223  }
224
225  /// <summary>
226  /// Pushes TRUE onto the BOOLEAN stack if the second item is less than or equal to the top item, or FALSE otherwise.
227  /// </summary>
228  [PushExpression(
229    StackTypes.Integer,
230    "INTEGER.<=",
231    "Pushes TRUE onto the BOOLEAN stack if the second item is less than or equal to the top item, or FALSE otherwise.",
232    StackTypes.Boolean)]
233  [StorableClass]
234  public class IntegerSmallerOrEqualToExpression : StatelessExpression {
235    public IntegerSmallerOrEqualToExpression() { }
236    [StorableConstructor]
237    protected IntegerSmallerOrEqualToExpression(bool deserializing) : base(deserializing) { }
238
239    public override bool IsNoop(IInternalPushInterpreter interpreter) {
240      return interpreter.IntegerStack.Count < 2;
241    }
242
243    public override void Eval(IInternalPushInterpreter interpreter) {
244      var first = interpreter.IntegerStack.Top;
245      var second = interpreter.IntegerStack[1];
246      interpreter.IntegerStack.Remove(2);
247
248      var result = second <= first;
249      interpreter.BooleanStack.Push(result);
250    }
251  }
252
253  /// <summary>
254  /// Pushes TRUE onto the BOOLEAN stack if the second item is greater than the top item, or FALSE otherwise.
255  /// </summary>
256  [PushExpression(
257    StackTypes.Integer,
258    "INTEGER.>",
259    "Pushes TRUE onto the BOOLEAN stack if the second item is greater than the top item, or FALSE otherwise.",
260    StackTypes.Boolean)]
261  [StorableClass]
262  public class IntegerGreaterThanExpression : StatelessExpression {
263    public IntegerGreaterThanExpression() { }
264    [StorableConstructor]
265    protected IntegerGreaterThanExpression(bool deserializing) : base(deserializing) { }
266
267    public override bool IsNoop(IInternalPushInterpreter interpreter) {
268      return interpreter.IntegerStack.Count < 2;
269    }
270
271    public override void Eval(IInternalPushInterpreter interpreter) {
272      var first = interpreter.IntegerStack.Top;
273      var second = interpreter.IntegerStack[1];
274      interpreter.IntegerStack.Remove(2);
275
276      var result = second > first;
277      interpreter.BooleanStack.Push(result);
278    }
279  }
280
281
282  /// <summary>
283  /// Pushes TRUE onto the BOOLEAN stack if the second item is greater than or equal to the top item, or FALSE otherwise.
284  /// </summary>
285  [PushExpression(
286    StackTypes.Integer,
287    "INTEGER.>=",
288    "Pushes TRUE onto the BOOLEAN stack if the second item is greater than or equal to the top item, or FALSE otherwise.",
289    StackTypes.Boolean)]
290  [StorableClass]
291  public class IntegerGreaterOrEqualToExpression : StatelessExpression {
292    public IntegerGreaterOrEqualToExpression() { }
293    [StorableConstructor]
294    protected IntegerGreaterOrEqualToExpression(bool deserializing) : base(deserializing) { }
295
296    public override bool IsNoop(IInternalPushInterpreter interpreter) {
297      return interpreter.IntegerStack.Count < 2;
298    }
299
300    public override void Eval(IInternalPushInterpreter interpreter) {
301      var first = interpreter.IntegerStack.Top;
302      var second = interpreter.IntegerStack[1];
303      interpreter.IntegerStack.Remove(2);
304
305      var result = second >= first;
306      interpreter.BooleanStack.Push(result);
307    }
308  }
309
310  /// <summary>
311  /// Pushes 1 if the top BOOLEAN is TRUE, or 0 if the top BOOLEAN is FALSE.
312  /// </summary>
313  [PushExpression(
314    StackTypes.Integer,
315    "INTEGER.FROMBOOLEAN",
316    "Pushes 1 if the top BOOLEAN is TRUE, or 0 if the top BOOLEAN is FALSE.",
317    StackTypes.Boolean)]
318  [StorableClass]
319  public class IntegerFromBooleanExpression : StatelessExpression {
320    public IntegerFromBooleanExpression() { }
321    [StorableConstructor]
322    protected IntegerFromBooleanExpression(bool deserializing) : base(deserializing) { }
323
324    public override bool IsNoop(IInternalPushInterpreter interpreter) {
325      return interpreter.BooleanStack.IsEmpty;
326    }
327
328    public override void Eval(IInternalPushInterpreter interpreter) {
329      var condition = interpreter.BooleanStack.Pop();
330      var value = condition ? 1 : 0;
331      interpreter.IntegerStack.Push(value);
332    }
333  }
334
335  /// <summary>
336  /// Pushes the result of truncating the top FLOAT.
337  /// </summary>
338  [PushExpression(
339    StackTypes.Integer,
340    "INTEGER.FROMFLOAT",
341    "Pushes the result of truncating the top FLOAT.",
342    StackTypes.Float)]
343  [StorableClass]
344  public class IntegerFromFloatExpression : StatelessExpression {
345    public IntegerFromFloatExpression() { }
346    [StorableConstructor]
347    protected IntegerFromFloatExpression(bool deserializing) : base(deserializing) { }
348
349    public override bool IsNoop(IInternalPushInterpreter interpreter) {
350      return interpreter.FloatStack.IsEmpty;
351    }
352
353    public override void Eval(IInternalPushInterpreter interpreter) {
354      var value = unchecked((long)interpreter.FloatStack.Pop());
355      interpreter.IntegerStack.Push(value);
356    }
357  }
358
359  /// <summary>
360  /// Pushes the result of truncating the top CHAR.
361  /// </summary>
362  [PushExpression(
363    StackTypes.Integer,
364    "INTEGER.FROMCHAR",
365    "Pushes the result of truncating the top CHAR.",
366    StackTypes.Char)]
367  [StorableClass]
368  public class IntegerFromCharExpression : StatelessExpression {
369    public IntegerFromCharExpression() { }
370    [StorableConstructor]
371    protected IntegerFromCharExpression(bool deserializing) : base(deserializing) { }
372
373    public override bool IsNoop(IInternalPushInterpreter interpreter) {
374      return interpreter.CharStack.IsEmpty;
375    }
376
377    public override void Eval(IInternalPushInterpreter interpreter) {
378      var value = interpreter.CharStack.Pop();
379      interpreter.IntegerStack.Push(value);
380    }
381  }
382
383  /// <summary>
384  /// Pushes the result of parsing the top STRING.
385  /// </summary>
386  [PushExpression(
387    StackTypes.Integer,
388    "INTEGER.FROMSTRING",
389    "Pushes the result of parsing the top STRING.",
390    StackTypes.String)]
391  [StorableClass]
392  public class IntegerFromStringExpression : StatelessExpression {
393    public IntegerFromStringExpression() { }
394    [StorableConstructor]
395    protected IntegerFromStringExpression(bool deserializing) : base(deserializing) { }
396
397    public override bool IsNoop(IInternalPushInterpreter interpreter) {
398      long tmp;
399
400      return interpreter.StringStack.IsEmpty ||
401             interpreter.StringStack.Top.Length == 0 ||
402             !long.TryParse(interpreter.StringStack.Top, out tmp);
403    }
404
405    public override void Eval(IInternalPushInterpreter interpreter) {
406      var str = interpreter.StringStack.Pop();
407      var value = long.Parse(str);
408      interpreter.IntegerStack.Push(value);
409    }
410  }
411
412  /// <summary>
413  /// Pushes the result of increasing the top INTEGER by 1.
414  /// </summary>
415  [PushExpression(
416    StackTypes.Integer,
417    "INTEGER.INC",
418    "Pushes the result of increasing the top INTEGER by 1.")]
419  [StorableClass]
420  public class IntegerIncExpression : StatelessExpression {
421    public IntegerIncExpression() { }
422    [StorableConstructor]
423    protected IntegerIncExpression(bool deserializing) : base(deserializing) { }
424
425    public override bool IsNoop(IInternalPushInterpreter interpreter) {
426      return interpreter.IntegerStack.IsEmpty;
427    }
428
429    public override void Eval(IInternalPushInterpreter interpreter) {
430      var result = unchecked(interpreter.IntegerStack.Top + 1);
431      interpreter.IntegerStack.Top = result;
432    }
433  }
434
435  /// <summary>
436  /// Pushes the result of decreasing the top INTEGER by 1.
437  /// </summary>
438  [PushExpression(
439    StackTypes.Integer,
440    "INTEGER.DEC",
441    "Pushes the result of decreasing the top INTEGER by 1.")]
442  [StorableClass]
443  public class IntegerDecExpression : StatelessExpression {
444    public IntegerDecExpression() { }
445    [StorableConstructor]
446    protected IntegerDecExpression(bool deserializing) : base(deserializing) { }
447
448    public override bool IsNoop(IInternalPushInterpreter interpreter) {
449      return interpreter.IntegerStack.IsEmpty;
450    }
451
452    public override void Eval(IInternalPushInterpreter interpreter) {
453      var result = unchecked(interpreter.IntegerStack.Top - 1);
454      interpreter.IntegerStack.Top = result;
455    }
456  }
457}
Note: See TracBrowser for help on using the repository browser.