Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/FloatExpressions.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: 17.3 KB
Line 
1using System;
2using System.Globalization;
3
4using HeuristicLab.Common;
5using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
6
7namespace HeuristicLab.Problems.ProgramSynthesis {
8  /// <summary>
9  /// Pushes the sum of the top two items.
10  /// </summary>
11  [PushExpression(
12    StackTypes.Float,
13    "FLOAT.+",
14    "Pushes the sum of the top two items.")]
15  [StorableClass]
16  public class FloatAddExpression : StatelessExpression {
17    public FloatAddExpression() { }
18    [StorableConstructor]
19    protected FloatAddExpression(bool deserializing) : base(deserializing) { }
20
21    public override bool IsNoop(IInternalPushInterpreter interpreter) {
22      return interpreter.FloatStack.Count < 2;
23    }
24
25    public override void Eval(IInternalPushInterpreter interpreter) {
26      var first = interpreter.FloatStack.Pop();
27      var second = interpreter.FloatStack.Top;
28      var result = second + first;
29
30      if (double.IsPositiveInfinity(result)) result = double.MaxValue;
31      if (double.IsNegativeInfinity(result)) result = double.MinValue;
32
33      interpreter.FloatStack.Top = result;
34    }
35  }
36
37  /// <summary>
38  /// Pushes the difference of the top two items; that is, the second item minus the top item.
39  /// </summary>
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.")]
44  [StorableClass]
45  public class FloatSubtractExpression : StatelessExpression {
46    public FloatSubtractExpression() { }
47    [StorableConstructor]
48    protected FloatSubtractExpression(bool deserializing) : base(deserializing) { }
49
50    public override bool IsNoop(IInternalPushInterpreter interpreter) {
51      return interpreter.FloatStack.Count < 2;
52    }
53
54    public override void Eval(IInternalPushInterpreter interpreter) {
55      var first = interpreter.FloatStack.Pop();
56      var second = interpreter.FloatStack.Top;
57      var result = second - first;
58
59      if (double.IsPositiveInfinity(result)) result = double.MaxValue;
60      if (double.IsNegativeInfinity(result)) result = double.MinValue;
61
62      interpreter.FloatStack.Top = result;
63    }
64  }
65
66  /// <summary>
67  /// Pushes the product of the top two items.
68  /// </summary>
69  [PushExpression(
70    StackTypes.Float,
71    "FLOAT.*",
72    "Pushes the product of the top two items.")]
73  [StorableClass]
74  public class FloatMultiplyExpression : StatelessExpression {
75    public FloatMultiplyExpression() { }
76    [StorableConstructor]
77    protected FloatMultiplyExpression(bool deserializing) : base(deserializing) { }
78
79    public override bool IsNoop(IInternalPushInterpreter interpreter) {
80      return interpreter.FloatStack.Count < 2;
81    }
82
83    public override void Eval(IInternalPushInterpreter interpreter) {
84      var first = interpreter.FloatStack.Pop();
85      var second = interpreter.FloatStack.Top;
86      var result = second * first;
87
88      if (double.IsPositiveInfinity(result)) result = double.MaxValue;
89      if (double.IsNegativeInfinity(result)) result = double.MinValue;
90
91      interpreter.FloatStack.Top = result;
92    }
93  }
94
95  /// <summary>
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.
98  /// </summary>
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.")]
103  [StorableClass]
104  public class FloatDivideExpression : StatelessExpression {
105    public FloatDivideExpression() { }
106    [StorableConstructor]
107    protected FloatDivideExpression(bool deserializing) : base(deserializing) { }
108
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) {
114      var first = interpreter.FloatStack.Pop();
115      var second = interpreter.FloatStack.Top;
116      var result = second / first;
117
118      if (double.IsPositiveInfinity(result)) result = double.MaxValue;
119      if (double.IsNegativeInfinity(result)) result = double.MinValue;
120
121      interpreter.FloatStack.Top = result;
122    }
123  }
124
125  /// <summary>
126  /// Pushes the second stack item modulo the top stack item. If the top item is zero this acts as a NOOP.
127  /// </summary>
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.")]
132  [StorableClass]
133  public class FloatModuloExpression : StatelessExpression {
134    public FloatModuloExpression() { }
135    [StorableConstructor]
136    protected FloatModuloExpression(bool deserializing) : base(deserializing) { }
137
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) {
143      var first = interpreter.FloatStack.Pop();
144      var second = interpreter.FloatStack.Top;
145      var result = second % first;
146
147      interpreter.FloatStack.Top = result;
148    }
149  }
150
151  /// <summary>
152  /// Pushes the minimum of the top two items.
153  /// </summary>
154  [PushExpression(
155    StackTypes.Float,
156    "FLOAT.MIN",
157    "Pushes the minimum of the top two items.")]
158  [StorableClass]
159  public class FloatMinExpression : StatelessExpression {
160    public FloatMinExpression() { }
161    [StorableConstructor]
162    protected FloatMinExpression(bool deserializing) : base(deserializing) { }
163
164    public override bool IsNoop(IInternalPushInterpreter interpreter) {
165      return interpreter.FloatStack.Count < 2;
166    }
167
168    public override void Eval(IInternalPushInterpreter interpreter) {
169      var first = interpreter.FloatStack.Pop();
170      var second = interpreter.FloatStack.Top;
171      var result = Math.Min(second, first);
172
173      interpreter.FloatStack.Top = result;
174    }
175  }
176
177  /// <summary>
178  /// Pushes the maximum of the top two items.
179  /// </summary>
180  [PushExpression(
181    StackTypes.Float,
182    "FLOAT.MAX",
183    "Pushes the maximum of the top two items.")]
184  [StorableClass]
185  public class FloatMaxExpression : StatelessExpression {
186    public FloatMaxExpression() { }
187    [StorableConstructor]
188    protected FloatMaxExpression(bool deserializing) : base(deserializing) { }
189
190    public override bool IsNoop(IInternalPushInterpreter interpreter) {
191      return interpreter.FloatStack.Count < 2;
192    }
193
194    public override void Eval(IInternalPushInterpreter interpreter) {
195      var first = interpreter.FloatStack.Pop();
196      var second = interpreter.FloatStack.Top;
197      var result = Math.Max(second, first);
198
199      interpreter.FloatStack.Top = result;
200    }
201  }
202
203  /// <summary>
204  /// Pushes TRUE onto the BOOLEAN stack if the second item is less than the top item, or FALSE otherwise.
205  /// </summary>
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)]
211  [StorableClass]
212  public class FloatSmallerThanExpression : StatelessExpression {
213    public FloatSmallerThanExpression() { }
214    [StorableConstructor]
215    protected FloatSmallerThanExpression(bool deserializing) : base(deserializing) { }
216
217    public override bool IsNoop(IInternalPushInterpreter interpreter) {
218      return interpreter.FloatStack.Count < 2;
219    }
220
221    public override void Eval(IInternalPushInterpreter interpreter) {
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);
228    }
229  }
230
231  /// <summary>
232  /// Pushes TRUE onto the BOOLEAN stack if the second item is less than the top item, or FALSE otherwise.
233  /// </summary>
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)]
239  [StorableClass]
240  public class FloatSmallerThanOrEqualExpression : StatelessExpression {
241    public FloatSmallerThanOrEqualExpression() { }
242    [StorableConstructor]
243    protected FloatSmallerThanOrEqualExpression(bool deserializing) : base(deserializing) { }
244
245    public override bool IsNoop(IInternalPushInterpreter interpreter) {
246      return interpreter.FloatStack.Count < 2;
247    }
248
249    public override void Eval(IInternalPushInterpreter interpreter) {
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);
256    }
257  }
258
259  /// <summary>
260  /// Pushes TRUE onto the BOOLEAN stack if the second item is greater than the top item, or FALSE otherwise.
261  /// </summary>
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)]
267  [StorableClass]
268  public class FloatGreaterThanExpression : StatelessExpression {
269    public FloatGreaterThanExpression() { }
270    [StorableConstructor]
271    protected FloatGreaterThanExpression(bool deserializing) : base(deserializing) { }
272
273    public override bool IsNoop(IInternalPushInterpreter interpreter) {
274      return interpreter.FloatStack.Count < 2;
275    }
276
277    public override void Eval(IInternalPushInterpreter interpreter) {
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);
284    }
285  }
286
287  /// <summary>
288  /// Pushes TRUE onto the BOOLEAN stack if the second item is greater than or equal to the top item, or FALSE otherwise.
289  /// </summary>
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)]
295  [StorableClass]
296  public class FloatGreaterThanOrEqualExpression : StatelessExpression {
297    public FloatGreaterThanOrEqualExpression() { }
298    [StorableConstructor]
299    protected FloatGreaterThanOrEqualExpression(bool deserializing) : base(deserializing) { }
300
301    public override bool IsNoop(IInternalPushInterpreter interpreter) {
302      return interpreter.FloatStack.Count < 2;
303    }
304
305    public override void Eval(IInternalPushInterpreter interpreter) {
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);
312    }
313  }
314
315  /// <summary>
316  /// Pushes the sine of the top item.
317  /// </summary>
318  [PushExpression(StackTypes.Float, "FLOAT.SIN", "Pushes the sine of the top item.")]
319  [StorableClass]
320  public class FloatSineExpression : StatelessExpression {
321    public FloatSineExpression() { }
322    [StorableConstructor]
323    protected FloatSineExpression(bool deserializing) : base(deserializing) { }
324
325    public override bool IsNoop(IInternalPushInterpreter interpreter) {
326      return interpreter.FloatStack.IsEmpty;
327    }
328
329    public override void Eval(IInternalPushInterpreter interpreter) {
330      var result = Math.Sin(interpreter.FloatStack.Top);
331      interpreter.FloatStack.Top = result;
332    }
333  }
334
335  /// <summary>
336  /// Pushes the cosine of the top item.
337  /// </summary>
338  [PushExpression(StackTypes.Float, "FLOAT.COS", "Pushes the cosine of the top item.")]
339  [StorableClass]
340  public class FloatCosineExpression : StatelessExpression {
341    public FloatCosineExpression() { }
342    [StorableConstructor]
343    protected FloatCosineExpression(bool deserializing) : base(deserializing) { }
344
345    public override bool IsNoop(IInternalPushInterpreter interpreter) {
346      return interpreter.FloatStack.IsEmpty;
347    }
348
349    public override void Eval(IInternalPushInterpreter interpreter) {
350      var result = Math.Cos(interpreter.FloatStack.Top);
351      interpreter.FloatStack.Top = result;
352    }
353  }
354
355  /// <summary>
356  /// Pushes 1 if the top BOOLEAN is TRUE, or 0 if the top BOOLEAN is FALSE.
357  /// </summary>
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)]
363  [StorableClass]
364  public class FloatFromBooleanExpression : StatelessExpression {
365    public FloatFromBooleanExpression() { }
366    [StorableConstructor]
367    protected FloatFromBooleanExpression(bool deserializing) : base(deserializing) { }
368
369    public override bool IsNoop(IInternalPushInterpreter interpreter) {
370      return interpreter.BooleanStack.IsEmpty;
371    }
372
373    public override void Eval(IInternalPushInterpreter interpreter) {
374      var condition = interpreter.BooleanStack.Pop();
375      var value = condition ? 1 : 0;
376
377      interpreter.FloatStack.Push(value);
378    }
379  }
380
381  /// <summary>
382  /// Pushes a floating point version of the top INTEGER.
383  /// </summary>
384  [PushExpression(
385    StackTypes.Float,
386    "FLOAT.FROMINTEGER",
387    "Pushes a floating point version of the top INTEGER.",
388    StackTypes.Integer)]
389  [StorableClass]
390  public class FloatFromIntegerExpression : StatelessExpression {
391    public FloatFromIntegerExpression() { }
392    [StorableConstructor]
393    protected FloatFromIntegerExpression(bool deserializing) : base(deserializing) { }
394
395    public override bool IsNoop(IInternalPushInterpreter interpreter) {
396      return interpreter.IntegerStack.IsEmpty;
397    }
398
399    public override void Eval(IInternalPushInterpreter interpreter) {
400      var value = (double)interpreter.IntegerStack.Pop();
401      interpreter.FloatStack.Push(value);
402    }
403  }
404
405  /// <summary>
406  /// Pushes the result of truncating the top CHAR.
407  /// </summary>
408  [PushExpression(
409    StackTypes.Float,
410    "FLOAT.FROMCHAR",
411    "Pushes the result of truncating the top CHAR.",
412    StackTypes.Char)]
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>
430  /// Pushes the result of parsing the top STRING.
431  /// </summary>
432  [PushExpression(
433    StackTypes.Float,
434    "FLOAT.FROMSTRING",
435    "Pushes the result of parsing the top STRING.",
436    StackTypes.String)]
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 ||
447             !double.TryParse(
448               interpreter.StringStack.Top,
449               NumberStyles.AllowDecimalPoint | NumberStyles.Float,
450               CultureInfo.InvariantCulture,
451               out tmp);
452    }
453
454    public override void Eval(IInternalPushInterpreter interpreter) {
455      var str = interpreter.StringStack.Pop();
456      var value = double.Parse(str, NumberStyles.AllowDecimalPoint | NumberStyles.Float, CultureInfo.InvariantCulture);
457
458      interpreter.FloatStack.Push(value);
459    }
460  }
461
462  /// <summary>
463  /// Pushes the result of increasing the top INTEGER by 1.
464  /// </summary>
465  [PushExpression(
466    StackTypes.Float,
467    "FLOAT.INC",
468    "Pushes the result of increasing the top INTEGER by 1.")]
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>
485  /// Pushes the result of decreasing the top INTEGER by 1.
486  /// </summary>
487  [PushExpression(
488    StackTypes.Float,
489    "FLOAT.DEC",
490    "Pushes the result of decreasing the top INTEGER by 1.")]
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  }
505}
Note: See TracBrowser for help on using the repository browser.