Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/FloatExpressions.cs @ 16724

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

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

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