[14727] | 1 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
|
---|
| 2 | using System;
|
---|
| 3 |
|
---|
| 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(StackType.Float, "FLOAT.+")]
|
---|
| 12 | public class FloatAddExpression : PushResultExpression<double> {
|
---|
| 13 | public override void Eval(IPushGpInterpreter interpreter) {
|
---|
| 14 | Eval(interpreter.FloatStack, 2, values => values[0] + values[1]);
|
---|
| 15 | }
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | /// <summary>
|
---|
| 19 | /// Pushes the difference of the top two items; that is, the second item minus the top item.
|
---|
| 20 | /// </summary>
|
---|
| 21 | [PushExpression(StackType.Float, "FLOAT.-")]
|
---|
| 22 | public class FloatSubtractExpression : PushResultExpression<double> {
|
---|
| 23 | public override void Eval(IPushGpInterpreter interpreter) {
|
---|
| 24 | this.Eval(interpreter.FloatStack, 2, values => values[0] - values[1]);
|
---|
| 25 | }
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | /// <summary>
|
---|
| 29 | /// Pushes the product of the top two items.
|
---|
| 30 | /// </summary>
|
---|
| 31 | [PushExpression(StackType.Float, "FLOAT.*")]
|
---|
| 32 | public class FloatMultiplyExpression : PushResultExpression<double> {
|
---|
| 33 | public override void Eval(IPushGpInterpreter interpreter) {
|
---|
| 34 | this.Eval(interpreter.FloatStack, 2, values => values[0] * values[1]);
|
---|
| 35 | }
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | /// <summary>
|
---|
| 39 | /// Pushes the quotient of the top two items; that is, the second item divided by the top item.
|
---|
| 40 | /// If the top item is zero this acts as a NOOP.
|
---|
| 41 | /// </summary>
|
---|
| 42 | [PushExpression(StackType.Float, "FLOAT./")]
|
---|
| 43 | public class FloatDivideExpression : PushResultExpression<double> {
|
---|
| 44 | public override void Eval(IPushGpInterpreter interpreter) {
|
---|
| 45 | this.Eval(interpreter.FloatStack, 2, values => values[0] / values[1], 0);
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | /// <summary>
|
---|
| 50 | /// Pushes the second stack item modulo the top stack item. If the top item is zero this acts as a NOOP.
|
---|
| 51 | /// The modulus is computed as the remainder of the quotient, where the quotient has first been truncated toward
|
---|
| 52 | /// negative infinity.
|
---|
| 53 | /// (This is taken from the definition for the generic MOD function in Common Lisp, which is described
|
---|
| 54 | /// for example at http://www.lispworks.com/reference/HyperSpec/Body/f_mod_r.htm.)
|
---|
| 55 | /// </summary>
|
---|
| 56 | [PushExpression(StackType.Float, "FLOAT.%")]
|
---|
| 57 | public class FloatModuloExpression : PushResultExpression<double> {
|
---|
| 58 | public override void Eval(IPushGpInterpreter interpreter) {
|
---|
| 59 | this.Eval(interpreter.FloatStack, 2, values => values[0] % values[1], 0);
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | /// <summary>
|
---|
| 64 | /// Pushes the minimum of the top two items.
|
---|
| 65 | /// </summary>
|
---|
| 66 | [PushExpression(StackType.Float, "FLOAT.MIN")]
|
---|
| 67 | public class FloatMinExpression : PushResultExpression<double> {
|
---|
| 68 | public override void Eval(IPushGpInterpreter interpreter) {
|
---|
| 69 | this.Eval(interpreter.FloatStack, 2, values => Math.Min(values[0], values[1]));
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | /// <summary>
|
---|
| 74 | /// Pushes the maximum of the top two items.
|
---|
| 75 | /// </summary>
|
---|
| 76 | [PushExpression(StackType.Float, "FLOAT.MAX")]
|
---|
| 77 | public class FloatMaxExpression : PushResultExpression<double> {
|
---|
| 78 | public override void Eval(IPushGpInterpreter interpreter) {
|
---|
| 79 | this.Eval(interpreter.FloatStack, 2, values => Math.Max(values[0], values[1]));
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | /// <summary>
|
---|
| 84 | /// Pushes TRUE onto the BOOLEAN stack if the second item is less than the top item, or FALSE otherwise.
|
---|
| 85 | /// </summary>
|
---|
| 86 | [PushExpression(StackType.Float, "FLOAT.<")]
|
---|
| 87 | public class FloatSmallerThanExpression : PushResultExpression<double> {
|
---|
| 88 | public override void Eval(IPushGpInterpreter interpreter) {
|
---|
| 89 | this.Eval(interpreter.FloatStack, interpreter.BooleanStack, 2, values => values[0] < values[1]);
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | /// <summary>
|
---|
| 94 | /// Pushes TRUE onto the BOOLEAN stack if the second item is greater than the top item, or FALSE otherwise.
|
---|
| 95 | /// </summary>
|
---|
| 96 | [PushExpression(StackType.Float, "FLOAT.>")]
|
---|
| 97 | public class FloatGreaterThanExpression : PushResultExpression<double> {
|
---|
| 98 | public override void Eval(IPushGpInterpreter interpreter) {
|
---|
| 99 | this.Eval(interpreter.FloatStack, interpreter.BooleanStack, 2, values => values[0] > values[1]);
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | /// <summary>
|
---|
| 104 | /// Pushes the sine of the top item.
|
---|
| 105 | /// </summary>
|
---|
| 106 | [PushExpression(StackType.Float, "FLOAT.SIN")]
|
---|
| 107 | public class FloatSineExpression : PushResultExpression<double> {
|
---|
| 108 | public override void Eval(IPushGpInterpreter interpreter) {
|
---|
| 109 | this.Eval(interpreter.FloatStack, 1, values => Math.Sin(values[0]));
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | /// <summary>
|
---|
| 114 | /// Pushes the cosine of the top item.
|
---|
| 115 | /// </summary>
|
---|
| 116 | [PushExpression(StackType.Float, "FLOAT.COS")]
|
---|
| 117 | public class FloatCosineExpression : PushResultExpression<double> {
|
---|
| 118 | public override void Eval(IPushGpInterpreter interpreter) {
|
---|
| 119 | this.Eval(interpreter.FloatStack, 1, values => Math.Cos(values[0]));
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | /// <summary>
|
---|
| 124 | /// Pushes 1 if the top BOOLEAN is TRUE, or 0 if the top BOOLEAN is FALSE.
|
---|
| 125 | /// </summary>
|
---|
| 126 | [PushExpression(StackType.Float, "FLOAT.FROMBOOLEAN")]
|
---|
| 127 | public class FloatFromBooleanExpression : StatelessExpression {
|
---|
| 128 | public override void Eval(IPushGpInterpreter interpreter) {
|
---|
| 129 | if (interpreter.BooleanStack.Count == 0) return;
|
---|
| 130 |
|
---|
| 131 | var condition = interpreter.BooleanStack.Pop();
|
---|
| 132 | var value = condition ? 1 : 0;
|
---|
| 133 |
|
---|
| 134 | interpreter.FloatStack.Push(value);
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | /// <summary>
|
---|
| 139 | /// Pushes a floating point version of the top INTEGER.
|
---|
| 140 | /// </summary>
|
---|
| 141 | [PushExpression(StackType.Float, "FLOAT.FROMINTEGER")]
|
---|
| 142 | public class FloatFromIntegerExpression : StatelessExpression {
|
---|
| 143 | public override void Eval(IPushGpInterpreter interpreter) {
|
---|
| 144 | if (interpreter.IntegerStack.Count == 0) return;
|
---|
| 145 |
|
---|
| 146 | var value = (double)interpreter.IntegerStack.Pop();
|
---|
| 147 |
|
---|
| 148 | interpreter.FloatStack.Push(value);
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
| 151 | } |
---|