Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 15.7 KB
RevLine 
[15771]1using System;
2using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
3
4namespace HeuristicLab.Problems.ProgramSynthesis {
[14727]5  /// <summary>
[15032]6  /// Pushes the sum of the top two items.
[14727]7  /// </summary>
[15032]8  [PushExpression(
9    StackTypes.Integer,
10    "INTEGER.+",
11    "Pushes the sum of the top two items.")]
[14952]12  [StorableClass]
[14875]13  public class IntegerAddExpression : StatelessExpression {
[14952]14    public IntegerAddExpression() { }
15    [StorableConstructor]
16    protected IntegerAddExpression(bool deserializing) : base(deserializing) { }
[14875]17
[14952]18    public override bool IsNoop(IInternalPushInterpreter interpreter) {
19      return interpreter.IntegerStack.Count < 2;
20    }
21
22    public override void Eval(IInternalPushInterpreter interpreter) {
[14875]23      var first = interpreter.IntegerStack.Pop();
24      var second = interpreter.IntegerStack.Top;
[15344]25      var result = unchecked(second + first);
[14875]26
[15017]27      interpreter.IntegerStack.Top = result;
[14727]28    }
29  }
30
31  /// <summary>
[15032]32  /// Pushes the difference of the top two items; that is, the second item minus the top item.
[14727]33  /// </summary>
[15032]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.")]
[14952]38  [StorableClass]
[14875]39  public class IntegerSubtractExpression : StatelessExpression {
[14952]40    public IntegerSubtractExpression() { }
41    [StorableConstructor]
42    protected IntegerSubtractExpression(bool deserializing) : base(deserializing) { }
[14875]43
[14952]44    public override bool IsNoop(IInternalPushInterpreter interpreter) {
45      return interpreter.IntegerStack.Count < 2;
46    }
47
48    public override void Eval(IInternalPushInterpreter interpreter) {
[14875]49      var first = interpreter.IntegerStack.Pop();
50      var second = interpreter.IntegerStack.Top;
[15344]51      var result = unchecked(second - first);
[14875]52
[15017]53      interpreter.IntegerStack.Top = result;
[14727]54    }
55  }
56
57  /// <summary>
[15032]58  /// Pushes the product of the top two items.
[14727]59  /// </summary>
[15032]60  [PushExpression(
61    StackTypes.Integer,
62    "INTEGER.*",
63    "Pushes the product of the top two items.")]
[14952]64  [StorableClass]
[14875]65  public class IntegerMultiplyExpression : StatelessExpression {
[14952]66    public IntegerMultiplyExpression() { }
67    [StorableConstructor]
68    protected IntegerMultiplyExpression(bool deserializing) : base(deserializing) { }
[14875]69
[14952]70    public override bool IsNoop(IInternalPushInterpreter interpreter) {
71      return interpreter.IntegerStack.Count < 2;
72    }
73
74    public override void Eval(IInternalPushInterpreter interpreter) {
[14875]75      var first = interpreter.IntegerStack.Pop();
76      var second = interpreter.IntegerStack.Top;
[15344]77      var result = unchecked(second * first);
[14875]78
[15017]79      interpreter.IntegerStack.Top = result;
[14727]80    }
81  }
82
83  /// <summary>
[15032]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.
[14727]86  /// </summary>
[15032]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.")]
[14952]91  [StorableClass]
[14875]92  public class IntegerDivideExpression : StatelessExpression {
[14952]93    public IntegerDivideExpression() { }
94    [StorableConstructor]
95    protected IntegerDivideExpression(bool deserializing) : base(deserializing) { }
[14875]96
[14952]97    public override bool IsNoop(IInternalPushInterpreter interpreter) {
[15344]98      return interpreter.IntegerStack.Count < 2 ||
99             interpreter.IntegerStack.Top == 0 ||
100             (interpreter.IntegerStack.Top == -1 && interpreter.IntegerStack[1] == long.MinValue);
[14952]101    }
102
103    public override void Eval(IInternalPushInterpreter interpreter) {
[14875]104      var first = interpreter.IntegerStack.Pop();
105      var second = interpreter.IntegerStack.Top;
106      var result = second / first;
107
[15017]108      interpreter.IntegerStack.Top = result;
[14727]109    }
110  }
111
112  /// <summary>
[15032]113  /// Pushes the second stack item modulo the top stack item. If the top item is zero this acts as a NOOP.
[14727]114  /// </summary>
[15032]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.")]
[14952]119  [StorableClass]
[14875]120  public class IntegerModuloExpression : StatelessExpression {
[14952]121    public IntegerModuloExpression() { }
122    [StorableConstructor]
123    protected IntegerModuloExpression(bool deserializing) : base(deserializing) { }
[14875]124
[14952]125    public override bool IsNoop(IInternalPushInterpreter interpreter) {
[15344]126      return interpreter.IntegerStack.Count < 2 ||
127             interpreter.IntegerStack.Top == 0 ||
128             (interpreter.IntegerStack.Top == -1 && interpreter.IntegerStack[1] == long.MinValue);
[14952]129    }
130
131    public override void Eval(IInternalPushInterpreter interpreter) {
[14875]132      var first = interpreter.IntegerStack.Pop();
133      var second = interpreter.IntegerStack.Top;
134      var result = second % first;
135
[15017]136      interpreter.IntegerStack.Top = result;
[14727]137    }
138  }
139
140  /// <summary>
[15032]141  /// Pushes the minimum of the top two items.
[14727]142  /// </summary>
[15032]143  [PushExpression(
144    StackTypes.Integer,
145    "INTEGER.MIN",
146    "Pushes the minimum of the top two items.")]
[14952]147  [StorableClass]
[14875]148  public class IntegerMinExpression : StatelessExpression {
[14952]149    public IntegerMinExpression() { }
150    [StorableConstructor]
151    protected IntegerMinExpression(bool deserializing) : base(deserializing) { }
[14875]152
[14952]153    public override bool IsNoop(IInternalPushInterpreter interpreter) {
154      return interpreter.IntegerStack.Count < 2;
155    }
156
157    public override void Eval(IInternalPushInterpreter interpreter) {
[14875]158      var first = interpreter.IntegerStack.Pop();
159      var second = interpreter.IntegerStack.Top;
160      var result = Math.Min(second, first);
161
[15017]162      interpreter.IntegerStack.Top = result;
[14727]163    }
164  }
165
166  /// <summary>
[15032]167  /// Pushes the maximum of the top two items.
[14727]168  /// </summary>
[15032]169  [PushExpression(
170    StackTypes.Integer,
171    "INTEGER.MAX",
172    "Pushes the maximum of the top two items.")]
[14952]173  [StorableClass]
[14875]174  public class IntegerMaxExpression : StatelessExpression {
[14952]175    public IntegerMaxExpression() { }
176    [StorableConstructor]
177    protected IntegerMaxExpression(bool deserializing) : base(deserializing) { }
[14875]178
[14952]179    public override bool IsNoop(IInternalPushInterpreter interpreter) {
180      return interpreter.IntegerStack.Count < 2;
181    }
182
183    public override void Eval(IInternalPushInterpreter interpreter) {
[14875]184      var first = interpreter.IntegerStack.Pop();
185      var second = interpreter.IntegerStack.Top;
186      var result = Math.Max(second, first);
187
[15017]188      interpreter.IntegerStack.Top = result;
[14727]189    }
190  }
191
192  /// <summary>
[15032]193  /// Pushes TRUE onto the BOOLEAN stack if the second item is less than the top item, or FALSE otherwise.
[14727]194  /// </summary>
[15032]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)]
[14952]200  [StorableClass]
[14875]201  public class IntegerSmallerThanExpression : StatelessExpression {
[14952]202    public IntegerSmallerThanExpression() { }
203    [StorableConstructor]
204    protected IntegerSmallerThanExpression(bool deserializing) : base(deserializing) { }
[14875]205
[14952]206    public override bool IsNoop(IInternalPushInterpreter interpreter) {
207      return interpreter.IntegerStack.Count < 2;
208    }
209
210    public override void Eval(IInternalPushInterpreter interpreter) {
[14875]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);
[14727]217    }
218  }
219
220  /// <summary>
[15032]221  /// Pushes TRUE onto the BOOLEAN stack if the second item is less than or equal to the top item, or FALSE otherwise.
[15017]222  /// </summary>
[15032]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)]
[15017]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>
[15032]249  /// Pushes TRUE onto the BOOLEAN stack if the second item is greater than the top item, or FALSE otherwise.
[14727]250  /// </summary>
[15032]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)]
[14952]256  [StorableClass]
[14875]257  public class IntegerGreaterThanExpression : StatelessExpression {
[14952]258    public IntegerGreaterThanExpression() { }
259    [StorableConstructor]
260    protected IntegerGreaterThanExpression(bool deserializing) : base(deserializing) { }
[14875]261
[14952]262    public override bool IsNoop(IInternalPushInterpreter interpreter) {
263      return interpreter.IntegerStack.Count < 2;
264    }
265
266    public override void Eval(IInternalPushInterpreter interpreter) {
[14875]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);
[14727]273    }
274  }
275
[15017]276
[14727]277  /// <summary>
[15032]278  /// Pushes TRUE onto the BOOLEAN stack if the second item is greater than or equal to the top item, or FALSE otherwise.
[15017]279  /// </summary>
[15032]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)]
[15017]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>
[15032]306  /// Pushes 1 if the top BOOLEAN is TRUE, or 0 if the top BOOLEAN is FALSE.
[14727]307  /// </summary>
[15032]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)]
[14952]313  [StorableClass]
[14727]314  public class IntegerFromBooleanExpression : StatelessExpression {
[14952]315    public IntegerFromBooleanExpression() { }
316    [StorableConstructor]
317    protected IntegerFromBooleanExpression(bool deserializing) : base(deserializing) { }
[14727]318
[14952]319    public override bool IsNoop(IInternalPushInterpreter interpreter) {
320      return interpreter.BooleanStack.IsEmpty;
321    }
322
323    public override void Eval(IInternalPushInterpreter interpreter) {
[14727]324      var condition = interpreter.BooleanStack.Pop();
325      var value = condition ? 1 : 0;
326      interpreter.IntegerStack.Push(value);
327    }
328  }
329
330  /// <summary>
[15032]331  /// Pushes the result of truncating the top FLOAT.
[14727]332  /// </summary>
[15032]333  [PushExpression(
334    StackTypes.Integer,
335    "INTEGER.FROMFLOAT",
336    "Pushes the result of truncating the top FLOAT.",
337    StackTypes.Float)]
[14952]338  [StorableClass]
[14727]339  public class IntegerFromFloatExpression : StatelessExpression {
[14952]340    public IntegerFromFloatExpression() { }
341    [StorableConstructor]
342    protected IntegerFromFloatExpression(bool deserializing) : base(deserializing) { }
[14727]343
[14952]344    public override bool IsNoop(IInternalPushInterpreter interpreter) {
345      return interpreter.FloatStack.IsEmpty;
346    }
347
348    public override void Eval(IInternalPushInterpreter interpreter) {
[15344]349      var value = unchecked((long)interpreter.FloatStack.Pop());
[14727]350      interpreter.IntegerStack.Push(value);
351    }
352  }
[15017]353
354  /// <summary>
[15032]355  /// Pushes the result of truncating the top CHAR.
[15017]356  /// </summary>
[15032]357  [PushExpression(
358    StackTypes.Integer,
359    "INTEGER.FROMCHAR",
360    "Pushes the result of truncating the top CHAR.",
361    StackTypes.Char)]
[15017]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>
[15032]379  /// Pushes the result of parsing the top STRING.
[15017]380  /// </summary>
[15032]381  [PushExpression(
382    StackTypes.Integer,
383    "INTEGER.FROMSTRING",
384    "Pushes the result of parsing the top STRING.",
385    StackTypes.String)]
[15017]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>
[15032]408  /// Pushes the result of increasing the top INTEGER by 1.
[15017]409  /// </summary>
[15032]410  [PushExpression(
411    StackTypes.Integer,
412    "INTEGER.INC",
413    "Pushes the result of increasing the top INTEGER by 1.")]
[15017]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) {
[15344]425      var result = unchecked(interpreter.IntegerStack.Top + 1);
426      interpreter.IntegerStack.Top = result;
[15017]427    }
428  }
429
430  /// <summary>
[15032]431  /// Pushes the result of decreasing the top INTEGER by 1.
[15017]432  /// </summary>
[15032]433  [PushExpression(
434    StackTypes.Integer,
435    "INTEGER.DEC",
436    "Pushes the result of decreasing the top INTEGER by 1.")]
[15017]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) {
[15344]448      var result = unchecked(interpreter.IntegerStack.Top - 1);
449      interpreter.IntegerStack.Top = result;
[15017]450    }
451  }
[14727]452}
Note: See TracBrowser for help on using the repository browser.