1 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
|
---|
2 | using System;
|
---|
3 | using Common;
|
---|
4 | using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
|
---|
5 | using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
|
---|
6 | using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
|
---|
7 |
|
---|
8 | /// <summary>
|
---|
9 | /// Pushes the sum of the top two items.
|
---|
10 | /// </summary>
|
---|
11 | [PushExpression(StackTypes.Float, "FLOAT.+")]
|
---|
12 | public class FloatAddExpression : StatelessExpression {
|
---|
13 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
14 | if (interpreter.FloatStack.Count < 2)
|
---|
15 | return false;
|
---|
16 |
|
---|
17 | var first = interpreter.FloatStack.Pop();
|
---|
18 | var second = interpreter.FloatStack.Top;
|
---|
19 | var result = second + first;
|
---|
20 |
|
---|
21 | if (double.IsPositiveInfinity(result)) result = double.MaxValue;
|
---|
22 | if (double.IsNegativeInfinity(result)) result = double.MinValue;
|
---|
23 |
|
---|
24 | interpreter.FloatStack.SetTop(result);
|
---|
25 | return true;
|
---|
26 | }
|
---|
27 | }
|
---|
28 |
|
---|
29 | /// <summary>
|
---|
30 | /// Pushes the difference of the top two items; that is, the second item minus the top item.
|
---|
31 | /// </summary>
|
---|
32 | [PushExpression(StackTypes.Float, "FLOAT.-")]
|
---|
33 | public class FloatSubtractExpression : StatelessExpression {
|
---|
34 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
35 | if (interpreter.FloatStack.Count < 2)
|
---|
36 | return false;
|
---|
37 |
|
---|
38 | var first = interpreter.FloatStack.Pop();
|
---|
39 | var second = interpreter.FloatStack.Top;
|
---|
40 | var result = second - first;
|
---|
41 |
|
---|
42 | if (double.IsPositiveInfinity(result)) result = double.MaxValue;
|
---|
43 | if (double.IsNegativeInfinity(result)) result = double.MinValue;
|
---|
44 |
|
---|
45 | interpreter.FloatStack.SetTop(result);
|
---|
46 | return true;
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | /// <summary>
|
---|
51 | /// Pushes the product of the top two items.
|
---|
52 | /// </summary>
|
---|
53 | [PushExpression(StackTypes.Float, "FLOAT.*")]
|
---|
54 | public class FloatMultiplyExpression : StatelessExpression {
|
---|
55 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
56 | if (interpreter.FloatStack.Count < 2)
|
---|
57 | return false;
|
---|
58 |
|
---|
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.SetTop(result);
|
---|
67 | return true;
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | /// <summary>
|
---|
72 | /// Pushes the quotient of the top two items; that is, the second item divided by the top item.
|
---|
73 | /// If the top item is zero this acts as a NOOP.
|
---|
74 | /// </summary>
|
---|
75 | [PushExpression(StackTypes.Float, "FLOAT./")]
|
---|
76 | public class FloatDivideExpression : StatelessExpression {
|
---|
77 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
78 | if (interpreter.FloatStack.Count < 2 ||
|
---|
79 | interpreter.FloatStack.Top.IsAlmost(0))
|
---|
80 | return false;
|
---|
81 |
|
---|
82 | var first = interpreter.FloatStack.Pop();
|
---|
83 | var second = interpreter.FloatStack.Top;
|
---|
84 | var result = second / first;
|
---|
85 |
|
---|
86 | if (double.IsPositiveInfinity(result)) result = double.MaxValue;
|
---|
87 | if (double.IsNegativeInfinity(result)) result = double.MinValue;
|
---|
88 |
|
---|
89 | interpreter.FloatStack.SetTop(result);
|
---|
90 | return true;
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | /// <summary>
|
---|
95 | /// Pushes the second stack item modulo the top stack item. If the top item is zero this acts as a NOOP.
|
---|
96 | /// The modulus is computed as the remainder of the quotient, where the quotient has first been truncated toward
|
---|
97 | /// negative infinity.
|
---|
98 | /// (This is taken from the definition for the generic MOD function in Common Lisp, which is described
|
---|
99 | /// for example at http://www.lispworks.com/reference/HyperSpec/Body/f_mod_r.htm.)
|
---|
100 | /// </summary>
|
---|
101 | [PushExpression(StackTypes.Float, "FLOAT.%")]
|
---|
102 | public class FloatModuloExpression : StatelessExpression {
|
---|
103 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
104 | if (interpreter.FloatStack.Count < 2 ||
|
---|
105 | interpreter.FloatStack.Top.IsAlmost(0))
|
---|
106 | return false;
|
---|
107 |
|
---|
108 | var first = interpreter.FloatStack.Pop();
|
---|
109 | var second = interpreter.FloatStack.Top;
|
---|
110 | var result = second % first;
|
---|
111 |
|
---|
112 | interpreter.FloatStack.SetTop(result);
|
---|
113 | return true; ;
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | /// <summary>
|
---|
118 | /// Pushes the minimum of the top two items.
|
---|
119 | /// </summary>
|
---|
120 | [PushExpression(StackTypes.Float, "FLOAT.MIN")]
|
---|
121 | public class FloatMinExpression : StatelessExpression {
|
---|
122 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
123 | if (interpreter.FloatStack.Count < 2)
|
---|
124 | return false;
|
---|
125 |
|
---|
126 | var first = interpreter.FloatStack.Pop();
|
---|
127 | var second = interpreter.FloatStack.Top;
|
---|
128 | var result = Math.Min(second, first);
|
---|
129 |
|
---|
130 | interpreter.FloatStack.SetTop(result);
|
---|
131 | return true;
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | /// <summary>
|
---|
136 | /// Pushes the maximum of the top two items.
|
---|
137 | /// </summary>
|
---|
138 | [PushExpression(StackTypes.Float, "FLOAT.MAX")]
|
---|
139 | public class FloatMaxExpression : StatelessExpression {
|
---|
140 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
141 | if (interpreter.FloatStack.Count < 2)
|
---|
142 | return false;
|
---|
143 |
|
---|
144 | var first = interpreter.FloatStack.Pop();
|
---|
145 | var second = interpreter.FloatStack.Top;
|
---|
146 | var result = Math.Max(second, first);
|
---|
147 |
|
---|
148 | interpreter.FloatStack.SetTop(result);
|
---|
149 | return true;
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | /// <summary>
|
---|
154 | /// Pushes TRUE onto the BOOLEAN stack if the second item is less than the top item, or FALSE otherwise.
|
---|
155 | /// </summary>
|
---|
156 | [PushExpression(StackTypes.Float, "FLOAT.<", StackTypes.Boolean)]
|
---|
157 | public class FloatSmallerThanExpression : StatelessExpression {
|
---|
158 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
159 | if (interpreter.FloatStack.Count < 2)
|
---|
160 | return false;
|
---|
161 |
|
---|
162 | var first = interpreter.FloatStack.Top;
|
---|
163 | var second = interpreter.FloatStack[1];
|
---|
164 | interpreter.FloatStack.Remove(2);
|
---|
165 |
|
---|
166 | var result = second < first;
|
---|
167 | interpreter.BooleanStack.Push(result);
|
---|
168 | return true;
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | /// <summary>
|
---|
173 | /// Pushes TRUE onto the BOOLEAN stack if the second item is less than the top item, or FALSE otherwise.
|
---|
174 | /// </summary>
|
---|
175 | [PushExpression(StackTypes.Float, "FLOAT.<=", StackTypes.Boolean)]
|
---|
176 | public class FloatSmallerThanOrEqualExpression : StatelessExpression {
|
---|
177 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
178 | if (interpreter.FloatStack.Count < 2)
|
---|
179 | return false;
|
---|
180 |
|
---|
181 | var first = interpreter.FloatStack.Top;
|
---|
182 | var second = interpreter.FloatStack[1];
|
---|
183 | interpreter.FloatStack.Remove(2);
|
---|
184 |
|
---|
185 | var result = second < first || second.IsAlmost(first);
|
---|
186 | interpreter.BooleanStack.Push(result);
|
---|
187 | return true;
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | /// <summary>
|
---|
192 | /// Pushes TRUE onto the BOOLEAN stack if the second item is greater than the top item, or FALSE otherwise.
|
---|
193 | /// </summary>
|
---|
194 | [PushExpression(StackTypes.Float, "FLOAT.>", StackTypes.Boolean)]
|
---|
195 | public class FloatGreaterThanExpression : StatelessExpression {
|
---|
196 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
197 | if (interpreter.FloatStack.Count < 2)
|
---|
198 | return false;
|
---|
199 |
|
---|
200 | var first = interpreter.FloatStack.Top;
|
---|
201 | var second = interpreter.FloatStack[1];
|
---|
202 | interpreter.FloatStack.Remove(2);
|
---|
203 |
|
---|
204 | var result = second > first;
|
---|
205 | interpreter.BooleanStack.Push(result);
|
---|
206 | return true;
|
---|
207 | }
|
---|
208 | }
|
---|
209 |
|
---|
210 | /// <summary>
|
---|
211 | /// Pushes TRUE onto the BOOLEAN stack if the second item is greater than or equal to the top item, or FALSE otherwise.
|
---|
212 | /// </summary>
|
---|
213 | [PushExpression(StackTypes.Float, "FLOAT.>=", StackTypes.Boolean)]
|
---|
214 | public class FloatGreaterThanOrEqualExpression : StatelessExpression {
|
---|
215 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
216 | if (interpreter.FloatStack.Count < 2)
|
---|
217 | return false;
|
---|
218 |
|
---|
219 | var first = interpreter.FloatStack.Top;
|
---|
220 | var second = interpreter.FloatStack[1];
|
---|
221 | interpreter.FloatStack.Remove(2);
|
---|
222 |
|
---|
223 | var result = second > first || second.IsAlmost(first);
|
---|
224 | interpreter.BooleanStack.Push(result);
|
---|
225 | return true;
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | /// <summary>
|
---|
230 | /// Pushes the sine of the top item.
|
---|
231 | /// </summary>
|
---|
232 | [PushExpression(StackTypes.Float, "FLOAT.SIN")]
|
---|
233 | public class FloatSineExpression : StatelessExpression {
|
---|
234 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
235 | if (interpreter.FloatStack.IsEmpty)
|
---|
236 | return false;
|
---|
237 |
|
---|
238 | var result = Math.Sin(interpreter.FloatStack.Top);
|
---|
239 | interpreter.FloatStack.SetTop(result);
|
---|
240 | return true;
|
---|
241 | }
|
---|
242 | }
|
---|
243 |
|
---|
244 | /// <summary>
|
---|
245 | /// Pushes the cosine of the top item.
|
---|
246 | /// </summary>
|
---|
247 | [PushExpression(StackTypes.Float, "FLOAT.COS")]
|
---|
248 | public class FloatCosineExpression : StatelessExpression {
|
---|
249 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
250 | if (interpreter.FloatStack.IsEmpty)
|
---|
251 | return false;
|
---|
252 |
|
---|
253 | var result = Math.Cos(interpreter.FloatStack.Top);
|
---|
254 | interpreter.FloatStack.SetTop(result);
|
---|
255 | return true;
|
---|
256 | }
|
---|
257 | }
|
---|
258 |
|
---|
259 | /// <summary>
|
---|
260 | /// Pushes 1 if the top BOOLEAN is TRUE, or 0 if the top BOOLEAN is FALSE.
|
---|
261 | /// </summary>
|
---|
262 | [PushExpression(StackTypes.Float, "FLOAT.FROMBOOLEAN", StackTypes.Boolean)]
|
---|
263 | public class FloatFromBooleanExpression : StatelessExpression {
|
---|
264 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
265 | if (interpreter.BooleanStack.Count == 0) return false;
|
---|
266 |
|
---|
267 | var condition = interpreter.BooleanStack.Pop();
|
---|
268 | var value = condition ? 1 : 0;
|
---|
269 |
|
---|
270 | interpreter.FloatStack.Push(value);
|
---|
271 |
|
---|
272 | return true;
|
---|
273 | }
|
---|
274 | }
|
---|
275 |
|
---|
276 | /// <summary>
|
---|
277 | /// Pushes a floating point version of the top INTEGER.
|
---|
278 | /// </summary>
|
---|
279 | [PushExpression(StackTypes.Float, "FLOAT.FROMINTEGER", StackTypes.Integer)]
|
---|
280 | public class FloatFromIntegerExpression : StatelessExpression {
|
---|
281 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
282 | if (interpreter.IntegerStack.Count == 0) return false;
|
---|
283 |
|
---|
284 | var value = (double)interpreter.IntegerStack.Pop();
|
---|
285 |
|
---|
286 | interpreter.FloatStack.Push(value);
|
---|
287 |
|
---|
288 | return true;
|
---|
289 | }
|
---|
290 | }
|
---|
291 | } |
---|