Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/IntegerExpressions.cs @ 14727

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

#2665 PushGP HL Integration, Views, Parameters

File size: 5.2 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
2  using System;
3
4  using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
5  using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
6
7  using Interpreter;
8
9  /// <summary>
10  ///     Pushes the sum of the top two items.
11  /// </summary>
12  [PushExpression(StackType.Integer, "INTEGER.+")]
13  public class IntegerAddExpression : PushResultExpression<long> {
14    public override void Eval(IPushGpInterpreter interpreter) {
15      this.Eval(interpreter.IntegerStack, 2, values => values[0] + values[1]);
16    }
17  }
18
19  /// <summary>
20  ///     Pushes the difference of the top two items; that is, the second item minus the top item.
21  /// </summary>
22  [PushExpression(StackType.Integer, "INTEGER.-")]
23  public class IntegerSubtractExpression : PushResultExpression<long> {
24    public override void Eval(IPushGpInterpreter interpreter) {
25      this.Eval(interpreter.IntegerStack, 2, values => values[0] - values[1]);
26    }
27  }
28
29  /// <summary>
30  ///     Pushes the product of the top two items.
31  /// </summary>
32  [PushExpression(StackType.Integer, "INTEGER.*")]
33  public class IntegerMultiplyExpression : PushResultExpression<long> {
34    public override void Eval(IPushGpInterpreter interpreter) {
35      this.Eval(interpreter.IntegerStack, 2, values => values[0] * values[1]);
36    }
37  }
38
39  /// <summary>
40  ///     Pushes the quotient of the top two items; that is, the second item divided by the top item.
41  ///     If the top item is zero this acts as a NOOP.
42  /// </summary>
43  [PushExpression(StackType.Integer, "INTEGER./")]
44  public class IntegerDivideExpression : PushResultExpression<long> {
45    public override void Eval(IPushGpInterpreter interpreter) {
46      this.Eval(interpreter.IntegerStack, 2, values => values[0] / values[1], 0);
47    }
48  }
49
50  /// <summary>
51  ///     Pushes the second stack item modulo the top stack item. If the top item is zero this acts as a NOOP. The modulus is
52  ///     computed as the
53  ///     remainder of the quotient, where the quotient has first been truncated toward negative infinity. (This is taken
54  ///     from the definition
55  ///     for the generic MOD function in Common Lisp, which is described for example at
56  ///     http://www.lispworks.com/reference/HyperSpec/Body/f_mod_r.htm.)
57  /// </summary>
58  [PushExpression(StackType.Integer, "INTEGER.%")]
59  public class IntegerModuloExpression : PushResultExpression<long> {
60    public override void Eval(IPushGpInterpreter interpreter) {
61      this.Eval(interpreter.IntegerStack, 2, values => values[0] % values[1], 0);
62    }
63  }
64
65  /// <summary>
66  ///     Pushes the minimum of the top two items.
67  /// </summary>
68  [PushExpression(StackType.Integer, "INTEGER.MIN")]
69  public class IntegerMinExpression : PushResultExpression<long> {
70    public override void Eval(IPushGpInterpreter interpreter) {
71      this.Eval(interpreter.IntegerStack, 2, values => Math.Min(values[0], values[1]));
72    }
73  }
74
75  /// <summary>
76  ///     Pushes the maximum of the top two items.
77  /// </summary>
78  [PushExpression(StackType.Integer, "INTEGER.MAX")]
79  public class IntegerMaxExpression : PushResultExpression<long> {
80    public override void Eval(IPushGpInterpreter interpreter) {
81      this.Eval(interpreter.IntegerStack, 2, values => Math.Max(values[0], values[1]));
82    }
83  }
84
85  /// <summary>
86  ///     Pushes TRUE onto the BOOLEAN stack if the second item is less than the top item, or FALSE otherwise.
87  /// </summary>
88  [PushExpression(StackType.Integer, "INTEGER.<")]
89  public class IntegerSmallerThanExpression : PushResultExpression<long> {
90    public override void Eval(IPushGpInterpreter interpreter) {
91      this.Eval(interpreter.IntegerStack, interpreter.BooleanStack, 2, values => values[0] < values[1]);
92    }
93  }
94
95  /// <summary>
96  ///     Pushes TRUE onto the BOOLEAN stack if the second item is greater than the top item, or FALSE otherwise.
97  /// </summary>
98  [PushExpression(StackType.Integer, "INTEGER.>")]
99  public class IntegerGreaterThanExpression : PushResultExpression<long> {
100    public override void Eval(IPushGpInterpreter interpreter) {
101      this.Eval(interpreter.IntegerStack, interpreter.BooleanStack, 2, values => values[0] > values[1]);
102    }
103  }
104
105  /// <summary>
106  ///     Pushes 1 if the top BOOLEAN is TRUE, or 0 if the top BOOLEAN is FALSE.
107  /// </summary>
108  [PushExpression(StackType.Integer, "INTEGER.FROMBOOLEAN")]
109  public class IntegerFromBooleanExpression : StatelessExpression {
110    public override void Eval(IPushGpInterpreter interpreter) {
111      if (interpreter.BooleanStack.Count == 0) return;
112
113      var condition = interpreter.BooleanStack.Pop();
114      var value = condition ? 1 : 0;
115
116      interpreter.IntegerStack.Push(value);
117    }
118  }
119
120  /// <summary>
121  ///     Pushes the result of truncating the top FLOAT.
122  /// </summary>
123  [PushExpression(StackType.Integer, "INTEGER.FROMFLOAT")]
124  public class IntegerFromFloatExpression : StatelessExpression {
125    public override void Eval(IPushGpInterpreter interpreter) {
126      if (interpreter.FloatStack.Count == 0) return;
127
128      var value = (int)interpreter.FloatStack.Pop();
129
130      interpreter.IntegerStack.Push(value);
131    }
132  }
133}
Note: See TracBrowser for help on using the repository browser.