Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/FloatExpressions.cs @ 17133

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

#2665 Testet Problems, Improved Performance

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