Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/20/16 22:57:11 (7 years ago)
Author:
pkimmesw
Message:

#2665 Added Problem.ProgramSynthesis Project, Fixed Expression Issues, Fixed Code Generation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/FloatExpressions.cs

    r14398 r14513  
    1 using System;
    2 using HeuristicLab.Algorithms.PushGP.Interpreter;
    3 
    4 namespace HeuristicLab.Algorithms.PushGP.Expressions
     1namespace HeuristicLab.Algorithms.PushGP.Expressions
    52{
    6     /// <summary>
    7     /// Pushes the sum of the top two items.
    8     /// </summary>
    9     public class FloatAddExpression : PushResultExpression<double>
    10     {
    11         protected override string InitStringRepresentation() { return "FLOAT.+"; }
    12 
    13         public override void Eval(IInterpreter interpreter)
    14         {
    15             Eval(interpreter.FloatStack, 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     public class FloatSubtractExpression : PushResultExpression<double>
    23     {
    24         protected override string InitStringRepresentation() { return "FLOAT.-"; }
    25 
    26         public override void Eval(IInterpreter interpreter)
    27         {
    28             Eval(interpreter.FloatStack, 2, values => values[0] - values[1]);
    29         }
    30     }
    31 
    32     /// <summary>
    33     /// Pushes the product of the top two items.
    34     /// </summary>
    35     public class FloatMultiplyExpression : PushResultExpression<double>
    36     {
    37         protected override string InitStringRepresentation() { return "FLOAT.*"; }
    38 
    39         public override void Eval(IInterpreter interpreter)
    40         {
    41             Eval(interpreter.FloatStack, 2, values => values[0] * values[1]);
    42         }
    43     }
    44 
    45     /// <summary>
    46     /// Pushes the quotient of the top two items; that is, the second item divided by the top item.
    47     /// If the top item is zero this acts as a NOOP.
    48     /// </summary>
    49     public class FloatDivideExpression : PushResultExpression<double>
    50     {
    51         protected override string InitStringRepresentation() { return "FLOAT./"; }
    52 
    53         public override void Eval(IInterpreter interpreter)
    54         {
    55             Eval(interpreter.FloatStack, 2, values => values[0] / values[1], 0);
    56         }
    57     }
    58 
    59     /// <summary>
    60     /// Pushes the second stack item modulo the top stack item. If the top item is zero this acts as a NOOP.
    61     /// The modulus is computed as the remainder of the quotient, where the quotient has first been truncated toward negative infinity.
    62     /// (This is taken from the definition for the generic MOD function in Common Lisp, which is described
    63     /// for example at http://www.lispworks.com/reference/HyperSpec/Body/f_mod_r.htm.)
    64     /// </summary>
    65     public class FloatModuloExpression : PushResultExpression<double>
    66     {
    67         protected override string InitStringRepresentation() { return "FLOAT.%"; }
    68 
    69         public override void Eval(IInterpreter interpreter)
    70         {
    71             Eval(interpreter.FloatStack, 2, values => values[0] % values[1], 0);
    72         }
    73     }
    74 
    75     /// <summary>
    76     /// Pushes the minimum of the top two items.
    77     /// </summary>
    78     public class FloatMinExpression : PushResultExpression<double>
    79     {
    80         protected override string InitStringRepresentation() { return "FLOAT.MIN"; }
    81 
    82         public override void Eval(IInterpreter interpreter)
    83         {
    84             Eval(interpreter.FloatStack, 2, values => Math.Min(values[0], values[1]));
    85         }
    86     }
    87 
    88     /// <summary>
    89     /// Pushes the maximum of the top two items.
    90     /// </summary>
    91     public class FloatMaxExpression : PushResultExpression<double>
    92     {
    93         protected override string InitStringRepresentation() { return "FLOAT.MAX"; }
    94 
    95         public override void Eval(IInterpreter interpreter)
    96         {
    97             Eval(interpreter.FloatStack, 2, values => Math.Max(values[0], values[1]));
    98         }
    99     }
    100 
    101     /// <summary>
    102     /// Pushes TRUE onto the BOOLEAN stack if the second item is less than the top item, or FALSE otherwise.
    103     /// </summary>
    104     public class FloatSmallerThanExpression : PushResultExpression<double>
    105     {
    106         protected override string InitStringRepresentation() { return "FLOAT.<"; }
    107 
    108         public override void Eval(IInterpreter interpreter)
    109         {
    110             Eval(
    111                 interpreter.FloatStack,
    112                 interpreter.BooleanStack,
    113                 2,
    114                 values => values[0] < values[1]);
    115         }
    116     }
    117 
    118     /// <summary>
    119     /// Pushes TRUE onto the BOOLEAN stack if the second item is greater than the top item, or FALSE otherwise.
    120     /// </summary>
    121     public class FloatGreaterThanExpression : PushResultExpression<double>
    122     {
    123         protected override string InitStringRepresentation() { return "FLOAT.>"; }
    124 
    125         public override void Eval(IInterpreter interpreter)
    126         {
    127             Eval(
    128                 interpreter.FloatStack,
    129                 interpreter.BooleanStack,
    130                 2,
    131                 values => values[0] > values[1]);
    132         }
    133     }
    134 
    135     /// <summary>
    136     /// Pushes the sine of the top item.
    137     /// </summary>
    138     public class FloatSineExpression : PushResultExpression<double>
    139     {
    140         protected override string InitStringRepresentation() { return "FLOAT.SIN"; }
    141 
    142         public override void Eval(IInterpreter interpreter)
    143         {
    144             Eval(
    145                 interpreter.FloatStack,
    146                 1,
    147                 values => Math.Sin(values[0]));
    148         }
    149     }
    150 
    151     /// <summary>
    152     /// Pushes the cosine of the top item.
    153     /// </summary>
    154     public class FloatCosineExpression : PushResultExpression<double>
    155     {
    156         protected override string InitStringRepresentation() { return "FLOAT.COS"; }
    157 
    158         public override void Eval(IInterpreter interpreter)
    159         {
    160             Eval(
    161                 interpreter.FloatStack,
    162                 1,
    163                 values => Math.Cos(values[0]));
    164         }
    165     }
    166 
    167     /// <summary>
    168     /// Pushes 1 if the top BOOLEAN is TRUE, or 0 if the top BOOLEAN is FALSE.
    169     /// </summary>
    170     public class FloatFromBooleanExpression : StatelessExpression
    171     {
    172         protected override string InitStringRepresentation() { return "FLOAT.FROMBOOLEAN"; }
    173 
    174         public override void Eval(IInterpreter interpreter)
    175         {
    176             if (interpreter.BooleanStack.Count == 0)
    177                 return;
    178 
    179             var condition = interpreter.BooleanStack.Pop();
    180             var value = condition ? 1 : 0;
    181 
    182             interpreter.FloatStack.Push(value);
    183         }
    184     }
    185 
    186     /// <summary>
    187     /// Pushes a floating point version of the top INTEGER.
    188     /// </summary>
    189     public class FloatFromIntegerExpression : StatelessExpression
    190     {
    191         protected override string InitStringRepresentation() { return "FLOAT.FROMINTEGER"; }
    192 
    193         public override void Eval(IInterpreter interpreter)
    194         {
    195             if (interpreter.IntegerStack.Count == 0)
    196                 return;
    197 
    198             var value = (double)interpreter.IntegerStack.Pop();
    199 
    200             interpreter.FloatStack.Push(value);
    201         }
    202     }
     3  using System;
     4
     5  using HeuristicLab.Algorithms.PushGP.Interpreter;
     6
     7  /// <summary>
     8  ///     Pushes the sum of the top two items.
     9  /// </summary>
     10  public class FloatAddExpression : PushResultExpression<double>
     11  {
     12    protected override string InitStringRepresentation()
     13    {
     14      return "FLOAT.+";
     15    }
     16
     17    public override void Eval(IPushGpInterpreter interpreter)
     18    {
     19      this.Eval(interpreter.FloatStack, 2, values => values[0] + values[1]);
     20    }
     21  }
     22
     23  /// <summary>
     24  ///     Pushes the difference of the top two items; that is, the second item minus the top item.
     25  /// </summary>
     26  public class FloatSubtractExpression : PushResultExpression<double>
     27  {
     28    protected override string InitStringRepresentation()
     29    {
     30      return "FLOAT.-";
     31    }
     32
     33    public override void Eval(IPushGpInterpreter interpreter)
     34    {
     35      this.Eval(interpreter.FloatStack, 2, values => values[0] - values[1]);
     36    }
     37  }
     38
     39  /// <summary>
     40  ///     Pushes the product of the top two items.
     41  /// </summary>
     42  public class FloatMultiplyExpression : PushResultExpression<double>
     43  {
     44    protected override string InitStringRepresentation()
     45    {
     46      return "FLOAT.*";
     47    }
     48
     49    public override void Eval(IPushGpInterpreter interpreter)
     50    {
     51      this.Eval(interpreter.FloatStack, 2, values => values[0] * values[1]);
     52    }
     53  }
     54
     55  /// <summary>
     56  ///     Pushes the quotient of the top two items; that is, the second item divided by the top item.
     57  ///     If the top item is zero this acts as a NOOP.
     58  /// </summary>
     59  public class FloatDivideExpression : PushResultExpression<double>
     60  {
     61    protected override string InitStringRepresentation()
     62    {
     63      return "FLOAT./";
     64    }
     65
     66    public override void Eval(IPushGpInterpreter interpreter)
     67    {
     68      this.Eval(interpreter.FloatStack, 2, values => values[0] / values[1], 0);
     69    }
     70  }
     71
     72  /// <summary>
     73  ///     Pushes the second stack item modulo the top stack item. If the top item is zero this acts as a NOOP.
     74  ///     The modulus is computed as the remainder of the quotient, where the quotient has first been truncated toward
     75  ///     negative infinity.
     76  ///     (This is taken from the definition for the generic MOD function in Common Lisp, which is described
     77  ///     for example at http://www.lispworks.com/reference/HyperSpec/Body/f_mod_r.htm.)
     78  /// </summary>
     79  public class FloatModuloExpression : PushResultExpression<double>
     80  {
     81    protected override string InitStringRepresentation()
     82    {
     83      return "FLOAT.%";
     84    }
     85
     86    public override void Eval(IPushGpInterpreter interpreter)
     87    {
     88      this.Eval(interpreter.FloatStack, 2, values => values[0] % values[1], 0);
     89    }
     90  }
     91
     92  /// <summary>
     93  ///     Pushes the minimum of the top two items.
     94  /// </summary>
     95  public class FloatMinExpression : PushResultExpression<double>
     96  {
     97    protected override string InitStringRepresentation()
     98    {
     99      return "FLOAT.MIN";
     100    }
     101
     102    public override void Eval(IPushGpInterpreter interpreter)
     103    {
     104      this.Eval(interpreter.FloatStack, 2, values => Math.Min(values[0], values[1]));
     105    }
     106  }
     107
     108  /// <summary>
     109  ///     Pushes the maximum of the top two items.
     110  /// </summary>
     111  public class FloatMaxExpression : PushResultExpression<double>
     112  {
     113    protected override string InitStringRepresentation()
     114    {
     115      return "FLOAT.MAX";
     116    }
     117
     118    public override void Eval(IPushGpInterpreter interpreter)
     119    {
     120      this.Eval(interpreter.FloatStack, 2, values => Math.Max(values[0], values[1]));
     121    }
     122  }
     123
     124  /// <summary>
     125  ///     Pushes TRUE onto the BOOLEAN stack if the second item is less than the top item, or FALSE otherwise.
     126  /// </summary>
     127  public class FloatSmallerThanExpression : PushResultExpression<double>
     128  {
     129    protected override string InitStringRepresentation()
     130    {
     131      return "FLOAT.<";
     132    }
     133
     134    public override void Eval(IPushGpInterpreter interpreter)
     135    {
     136      this.Eval(interpreter.FloatStack, interpreter.BooleanStack, 2, values => values[0] < values[1]);
     137    }
     138  }
     139
     140  /// <summary>
     141  ///     Pushes TRUE onto the BOOLEAN stack if the second item is greater than the top item, or FALSE otherwise.
     142  /// </summary>
     143  public class FloatGreaterThanExpression : PushResultExpression<double>
     144  {
     145    protected override string InitStringRepresentation()
     146    {
     147      return "FLOAT.>";
     148    }
     149
     150    public override void Eval(IPushGpInterpreter interpreter)
     151    {
     152      this.Eval(interpreter.FloatStack, interpreter.BooleanStack, 2, values => values[0] > values[1]);
     153    }
     154  }
     155
     156  /// <summary>
     157  ///     Pushes the sine of the top item.
     158  /// </summary>
     159  public class FloatSineExpression : PushResultExpression<double>
     160  {
     161    protected override string InitStringRepresentation()
     162    {
     163      return "FLOAT.SIN";
     164    }
     165
     166    public override void Eval(IPushGpInterpreter interpreter)
     167    {
     168      this.Eval(interpreter.FloatStack, 1, values => Math.Sin(values[0]));
     169    }
     170  }
     171
     172  /// <summary>
     173  ///     Pushes the cosine of the top item.
     174  /// </summary>
     175  public class FloatCosineExpression : PushResultExpression<double>
     176  {
     177    protected override string InitStringRepresentation()
     178    {
     179      return "FLOAT.COS";
     180    }
     181
     182    public override void Eval(IPushGpInterpreter interpreter)
     183    {
     184      this.Eval(interpreter.FloatStack, 1, values => Math.Cos(values[0]));
     185    }
     186  }
     187
     188  /// <summary>
     189  ///     Pushes 1 if the top BOOLEAN is TRUE, or 0 if the top BOOLEAN is FALSE.
     190  /// </summary>
     191  public class FloatFromBooleanExpression : StatelessExpression
     192  {
     193    protected override string InitStringRepresentation()
     194    {
     195      return "FLOAT.FROMBOOLEAN";
     196    }
     197
     198    public override void Eval(IPushGpInterpreter interpreter)
     199    {
     200      if (interpreter.BooleanStack.Count == 0) return;
     201
     202      var condition = interpreter.BooleanStack.Pop();
     203      var value = condition ? 1 : 0;
     204
     205      interpreter.FloatStack.Push(value);
     206    }
     207  }
     208
     209  /// <summary>
     210  ///     Pushes a floating point version of the top INTEGER.
     211  /// </summary>
     212  public class FloatFromIntegerExpression : StatelessExpression
     213  {
     214    protected override string InitStringRepresentation()
     215    {
     216      return "FLOAT.FROMINTEGER";
     217    }
     218
     219    public override void Eval(IPushGpInterpreter interpreter)
     220    {
     221      if (interpreter.IntegerStack.Count == 0) return;
     222
     223      var value = (double)interpreter.IntegerStack.Pop();
     224
     225      interpreter.FloatStack.Push(value);
     226    }
     227  }
    203228}
Note: See TracChangeset for help on using the changeset viewer.