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/FloatExpressions.cs

    r14834 r14875  
    1010  /// </summary>
    1111  [PushExpression(StackTypes.Float, "FLOAT.+")]
    12   public class FloatAddExpression : PushResultExpression<double> {
    13     public override bool Eval(IInternalPushInterpreter interpreter) {
    14       return Eval(interpreter.FloatStack, 2,
    15         values => {
    16           var result = values[0] + values[1];
    17 
    18           if (double.IsPositiveInfinity(result)) return double.MaxValue;
    19           if (double.IsNegativeInfinity(result)) return double.MinValue;
    20 
    21           return result;
    22         });
     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;
    2326    }
    2427  }
     
    2831  /// </summary>
    2932  [PushExpression(StackTypes.Float, "FLOAT.-")]
    30   public class FloatSubtractExpression : PushResultExpression<double> {
    31     public override bool Eval(IInternalPushInterpreter interpreter) {
    32       return Eval(interpreter.FloatStack, 2,
    33         values => {
    34           var result = values[0] - values[1];
    35 
    36           if (double.IsPositiveInfinity(result)) return double.MaxValue;
    37           if (double.IsNegativeInfinity(result)) return double.MinValue;
    38 
    39           return result;
    40         });
     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;
    4147    }
    4248  }
     
    4652  /// </summary>
    4753  [PushExpression(StackTypes.Float, "FLOAT.*")]
    48   public class FloatMultiplyExpression : PushResultExpression<double> {
    49     public override bool Eval(IInternalPushInterpreter interpreter) {
    50       return Eval(interpreter.FloatStack, 2,
    51         values => {
    52           var result = values[0] * values[1];
    53 
    54           if (double.IsPositiveInfinity(result)) return double.MaxValue;
    55           if (double.IsNegativeInfinity(result)) return double.MinValue;
    56 
    57           return result;
    58         });
     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;
    5968    }
    6069  }
     
    6574  /// </summary>
    6675  [PushExpression(StackTypes.Float, "FLOAT./")]
    67   public class FloatDivideExpression : PushResultExpression<double> {
    68     public override bool Eval(IInternalPushInterpreter interpreter) {
    69       return Eval(interpreter.FloatStack, 2, values => {
    70         var result = values[0] / values[1];
    71 
    72         if (double.IsPositiveInfinity(result)) return double.MaxValue;
    73         if (double.IsNegativeInfinity(result)) return double.MinValue;
    74 
    75         return result;
    76       }, 0);
     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;
    7791    }
    7892  }
     
    86100  /// </summary>
    87101  [PushExpression(StackTypes.Float, "FLOAT.%")]
    88   public class FloatModuloExpression : PushResultExpression<double> {
    89     public override bool Eval(IInternalPushInterpreter interpreter) {
    90       return Eval(interpreter.FloatStack, 2, values => values[0] % values[1], 0);
     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; ;
    91114    }
    92115  }
     
    96119  /// </summary>
    97120  [PushExpression(StackTypes.Float, "FLOAT.MIN")]
    98   public class FloatMinExpression : PushResultExpression<double> {
    99     public override bool Eval(IInternalPushInterpreter interpreter) {
    100       return Eval(interpreter.FloatStack, 2, values => Math.Min(values[0], values[1]));
     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;
    101132    }
    102133  }
     
    106137  /// </summary>
    107138  [PushExpression(StackTypes.Float, "FLOAT.MAX")]
    108   public class FloatMaxExpression : PushResultExpression<double> {
    109     public override bool Eval(IInternalPushInterpreter interpreter) {
    110       return Eval(interpreter.FloatStack, 2, values => Math.Max(values[0], values[1]));
     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;
    111150    }
    112151  }
     
    116155  /// </summary>
    117156  [PushExpression(StackTypes.Float, "FLOAT.<", StackTypes.Boolean)]
    118   public class FloatSmallerThanExpression : PushResultExpression<double> {
    119     public override bool Eval(IInternalPushInterpreter interpreter) {
    120       return Eval(interpreter.FloatStack, interpreter.BooleanStack, 2, values => values[0] < values[1]);
     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;
    121169    }
    122170  }
     
    126174  /// </summary>
    127175  [PushExpression(StackTypes.Float, "FLOAT.<=", StackTypes.Boolean)]
    128   public class FloatSmallerThanOrEqualExpression : PushResultExpression<double> {
    129     public override bool Eval(IInternalPushInterpreter interpreter) {
    130       return Eval(interpreter.FloatStack, interpreter.BooleanStack, 2, values => values[0] < values[1] || values[0].IsAlmost(values[1]));
     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;
    131188    }
    132189  }
     
    136193  /// </summary>
    137194  [PushExpression(StackTypes.Float, "FLOAT.>", StackTypes.Boolean)]
    138   public class FloatGreaterThanExpression : PushResultExpression<double> {
    139     public override bool Eval(IInternalPushInterpreter interpreter) {
    140       return Eval(interpreter.FloatStack, interpreter.BooleanStack, 2, values => values[0] > values[1]);
     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;
    141207    }
    142208  }
     
    146212  /// </summary>
    147213  [PushExpression(StackTypes.Float, "FLOAT.>=", StackTypes.Boolean)]
    148   public class FloatGreaterThanOrEqualExpression : PushResultExpression<double> {
    149     public override bool Eval(IInternalPushInterpreter interpreter) {
    150       return Eval(interpreter.FloatStack, interpreter.BooleanStack, 2, values => values[0] > values[1] || values[0].IsAlmost(values[1]));
     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;
    151226    }
    152227  }
     
    156231  /// </summary>
    157232  [PushExpression(StackTypes.Float, "FLOAT.SIN")]
    158   public class FloatSineExpression : PushResultExpression<double> {
    159     public override bool Eval(IInternalPushInterpreter interpreter) {
    160       return Eval(interpreter.FloatStack, 1, values => Math.Sin(values[0]));
     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;
    161241    }
    162242  }
     
    166246  /// </summary>
    167247  [PushExpression(StackTypes.Float, "FLOAT.COS")]
    168   public class FloatCosineExpression : PushResultExpression<double> {
    169     public override bool Eval(IInternalPushInterpreter interpreter) {
    170       return Eval(interpreter.FloatStack, 1, values => Math.Cos(values[0]));
     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;
    171256    }
    172257  }
Note: See TracChangeset for help on using the changeset viewer.