Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/18/17 01:15:25 (8 years ago)
Author:
pkimmesw
Message:

#2665 BenchmarkSuite, all examples, partially tested, VectorExpressions added

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/IntegerExpressions.cs

    r14834 r14875  
    1111  /// </summary>
    1212  [PushExpression(StackTypes.Integer, "INTEGER.+")]
    13   public class IntegerAddExpression : PushResultExpression<long> {
    14     public override bool Eval(IInternalPushInterpreter interpreter) {
    15       return Eval(interpreter.IntegerStack, 2, values => values[0] + values[1]);
     13  public class IntegerAddExpression : StatelessExpression {
     14    public override bool Eval(IInternalPushInterpreter interpreter) {
     15      if (interpreter.IntegerStack.Count < 2)
     16        return false;
     17
     18      var first = interpreter.IntegerStack.Pop();
     19      var second = interpreter.IntegerStack.Top;
     20      var result = second + first;
     21
     22      interpreter.IntegerStack.SetTop(result);
     23      return true;
    1624    }
    1725  }
     
    2129  /// </summary>
    2230  [PushExpression(StackTypes.Integer, "INTEGER.-")]
    23   public class IntegerSubtractExpression : PushResultExpression<long> {
    24     public override bool Eval(IInternalPushInterpreter interpreter) {
    25       return Eval(interpreter.IntegerStack, 2, values => values[0] - values[1]);
     31  public class IntegerSubtractExpression : StatelessExpression {
     32    public override bool Eval(IInternalPushInterpreter interpreter) {
     33      if (interpreter.IntegerStack.Count < 2)
     34        return false;
     35
     36      var first = interpreter.IntegerStack.Pop();
     37      var second = interpreter.IntegerStack.Top;
     38      var result = second - first;
     39
     40      interpreter.IntegerStack.SetTop(result);
     41      return true;
    2642    }
    2743  }
     
    3147  /// </summary>
    3248  [PushExpression(StackTypes.Integer, "INTEGER.*")]
    33   public class IntegerMultiplyExpression : PushResultExpression<long> {
    34     public override bool Eval(IInternalPushInterpreter interpreter) {
    35       return Eval(interpreter.IntegerStack, 2, values => values[0] * values[1]);
     49  public class IntegerMultiplyExpression : StatelessExpression {
     50    public override bool Eval(IInternalPushInterpreter interpreter) {
     51      if (interpreter.IntegerStack.Count < 2)
     52        return false;
     53
     54      var first = interpreter.IntegerStack.Pop();
     55      var second = interpreter.IntegerStack.Top;
     56      var result = second * first;
     57
     58      interpreter.IntegerStack.SetTop(result);
     59      return true;
    3660    }
    3761  }
     
    4266  /// </summary>
    4367  [PushExpression(StackTypes.Integer, "INTEGER./")]
    44   public class IntegerDivideExpression : PushResultExpression<long> {
    45     public override bool Eval(IInternalPushInterpreter interpreter) {
    46       return Eval(interpreter.IntegerStack, 2, values => values[0] / values[1], 0);
     68  public class IntegerDivideExpression : StatelessExpression {
     69    public override bool Eval(IInternalPushInterpreter interpreter) {
     70      if (interpreter.IntegerStack.Count < 2 ||
     71          interpreter.IntegerStack.Top == 0)
     72        return false;
     73
     74      var first = interpreter.IntegerStack.Pop();
     75      var second = interpreter.IntegerStack.Top;
     76      var result = second / first;
     77
     78      interpreter.IntegerStack.SetTop(result);
     79      return true;
    4780    }
    4881  }
     
    5083  /// <summary>
    5184  ///     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
     85  ///     computed as the remainder of the quotient, where the quotient has first been truncated toward negative infinity. (This is taken
     86  ///     from the definition for the generic MOD function in Common Lisp, which is described for example at
    5687  ///     http://www.lispworks.com/reference/HyperSpec/Body/f_mod_r.htm.)
    5788  /// </summary>
    5889  [PushExpression(StackTypes.Integer, "INTEGER.%")]
    59   public class IntegerModuloExpression : PushResultExpression<long> {
    60     public override bool Eval(IInternalPushInterpreter interpreter) {
    61       return Eval(interpreter.IntegerStack, 2, values => values[0] % values[1], 0);
     90  public class IntegerModuloExpression : StatelessExpression {
     91    public override bool Eval(IInternalPushInterpreter interpreter) {
     92      if (interpreter.IntegerStack.Count < 2 ||
     93          interpreter.IntegerStack.Top == 0)
     94        return false;
     95
     96      var first = interpreter.IntegerStack.Pop();
     97      var second = interpreter.IntegerStack.Top;
     98      var result = second % first;
     99
     100      interpreter.IntegerStack.SetTop(result);
     101      return true;
    62102    }
    63103  }
     
    67107  /// </summary>
    68108  [PushExpression(StackTypes.Integer, "INTEGER.MIN")]
    69   public class IntegerMinExpression : PushResultExpression<long> {
    70     public override bool Eval(IInternalPushInterpreter interpreter) {
    71       return Eval(interpreter.IntegerStack, 2, values => Math.Min(values[0], values[1]));
     109  public class IntegerMinExpression : StatelessExpression {
     110    public override bool Eval(IInternalPushInterpreter interpreter) {
     111      if (interpreter.IntegerStack.Count < 2)
     112        return false;
     113
     114      var first = interpreter.IntegerStack.Pop();
     115      var second = interpreter.IntegerStack.Top;
     116      var result = Math.Min(second, first);
     117
     118      interpreter.IntegerStack.SetTop(result);
     119      return true;
    72120    }
    73121  }
     
    77125  /// </summary>
    78126  [PushExpression(StackTypes.Integer, "INTEGER.MAX")]
    79   public class IntegerMaxExpression : PushResultExpression<long> {
    80     public override bool Eval(IInternalPushInterpreter interpreter) {
    81       return Eval(interpreter.IntegerStack, 2, values => Math.Max(values[0], values[1]));
     127  public class IntegerMaxExpression : StatelessExpression {
     128    public override bool Eval(IInternalPushInterpreter interpreter) {
     129      if (interpreter.IntegerStack.Count < 2)
     130        return false;
     131
     132      var first = interpreter.IntegerStack.Pop();
     133      var second = interpreter.IntegerStack.Top;
     134      var result = Math.Max(second, first);
     135
     136      interpreter.IntegerStack.SetTop(result);
     137      return true;
    82138    }
    83139  }
     
    87143  /// </summary>
    88144  [PushExpression(StackTypes.Integer, "INTEGER.<", StackTypes.Boolean)]
    89   public class IntegerSmallerThanExpression : PushResultExpression<long> {
    90     public override bool Eval(IInternalPushInterpreter interpreter) {
    91       return Eval(interpreter.IntegerStack, interpreter.BooleanStack, 2, values => values[0] < values[1]);
     145  public class IntegerSmallerThanExpression : StatelessExpression {
     146    public override bool Eval(IInternalPushInterpreter interpreter) {
     147      if (interpreter.IntegerStack.Count < 2)
     148        return false;
     149
     150      var first = interpreter.IntegerStack.Top;
     151      var second = interpreter.IntegerStack[1];
     152      interpreter.IntegerStack.Remove(2);
     153
     154      var result = second < first;
     155      interpreter.BooleanStack.Push(result);
     156      return true;
    92157    }
    93158  }
     
    97162  /// </summary>
    98163  [PushExpression(StackTypes.Integer, "INTEGER.>", StackTypes.Boolean)]
    99   public class IntegerGreaterThanExpression : PushResultExpression<long> {
    100     public override bool Eval(IInternalPushInterpreter interpreter) {
    101       return Eval(interpreter.IntegerStack, interpreter.BooleanStack, 2, values => values[0] > values[1]);
     164  public class IntegerGreaterThanExpression : StatelessExpression {
     165    public override bool Eval(IInternalPushInterpreter interpreter) {
     166      if (interpreter.IntegerStack.Count < 2)
     167        return false;
     168
     169      var first = interpreter.IntegerStack.Top;
     170      var second = interpreter.IntegerStack[1];
     171      interpreter.IntegerStack.Remove(2);
     172
     173      var result = second > first;
     174      interpreter.BooleanStack.Push(result);
     175      return true;
    102176    }
    103177  }
     
    109183  public class IntegerFromBooleanExpression : StatelessExpression {
    110184    public override bool Eval(IInternalPushInterpreter interpreter) {
    111       if (interpreter.BooleanStack.Count == 0) return false;
     185      if (interpreter.BooleanStack.Count == 0)
     186        return false;
    112187
    113188      var condition = interpreter.BooleanStack.Pop();
    114189      var value = condition ? 1 : 0;
    115 
    116190      interpreter.IntegerStack.Push(value);
    117 
    118191      return true;
    119192    }
     
    126199  public class IntegerFromFloatExpression : StatelessExpression {
    127200    public override bool Eval(IInternalPushInterpreter interpreter) {
    128       if (interpreter.FloatStack.Count == 0) return false;
     201      if (interpreter.FloatStack.Count == 0)
     202        return false;
    129203
    130204      var value = (int)interpreter.FloatStack.Pop();
    131 
    132205      interpreter.IntegerStack.Push(value);
    133 
    134206      return true;
    135207    }
Note: See TracChangeset for help on using the changeset viewer.