Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/IntegerExpressions.cs @ 17432

Last change on this file since 17432 was 15771, checked in by bburlacu, 7 years ago

#2895: Add solution skeleton for PushGP with genealogy analysis.

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