Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/25/17 01:53:30 (7 years ago)
Author:
pkimmesw
Message:

#2665 PushGP HL Integration

File:
1 edited

Legend:

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

    r14513 r14602  
    11namespace HeuristicLab.Algorithms.PushGP.Expressions
    22{
    3   using System;
     3    using System;
     4    using HeuristicLab.Algorithms.PushGP.Interpreter;
    45
    5   using HeuristicLab.Algorithms.PushGP.Interpreter;
     6    /// <summary>
     7    ///     Pushes the sum of the top two items.
     8    /// </summary>
     9    public class IntegerAddExpression : PushResultExpression<long>
     10    {
     11        public IntegerAddExpression() : base("INTEGER.+")
     12        {
     13        }
    614
    7   /// <summary>
    8   ///     Pushes the sum of the top two items.
    9   /// </summary>
    10   public class IntegerAddExpression : PushResultExpression<long>
    11   {
    12     protected override string InitStringRepresentation()
    13     {
    14       return "INTEGER.+";
     15        public override void Eval(IPushGpInterpreter interpreter)
     16        {
     17            this.Eval(interpreter.IntegerStack, 2, values => values[0] + values[1]);
     18        }
    1519    }
    1620
    17     public override void Eval(IPushGpInterpreter interpreter)
     21    /// <summary>
     22    ///     Pushes the difference of the top two items; that is, the second item minus the top item.
     23    /// </summary>
     24    public class IntegerSubtractExpression : PushResultExpression<long>
    1825    {
    19       this.Eval(interpreter.IntegerStack, 2, values => values[0] + values[1]);
    20     }
    21   }
     26        public IntegerSubtractExpression() : base("INTEGER.-")
     27        {
     28        }
    2229
    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 IntegerSubtractExpression : PushResultExpression<long>
    27   {
    28     protected override string InitStringRepresentation()
    29     {
    30       return "INTEGER.-";
     30        public override void Eval(IPushGpInterpreter interpreter)
     31        {
     32            this.Eval(interpreter.IntegerStack, 2, values => values[0] - values[1]);
     33        }
    3134    }
    3235
    33     public override void Eval(IPushGpInterpreter interpreter)
     36    /// <summary>
     37    ///     Pushes the product of the top two items.
     38    /// </summary>
     39    public class IntegerMultiplyExpression : PushResultExpression<long>
    3440    {
    35       this.Eval(interpreter.IntegerStack, 2, values => values[0] - values[1]);
    36     }
    37   }
     41        public IntegerMultiplyExpression() : base("INTEGER.*")
     42        {
     43        }
    3844
    39   /// <summary>
    40   ///     Pushes the product of the top two items.
    41   /// </summary>
    42   public class IntegerMultiplyExpression : PushResultExpression<long>
    43   {
    44     protected override string InitStringRepresentation()
    45     {
    46       return "INTEGER.*";
     45        public override void Eval(IPushGpInterpreter interpreter)
     46        {
     47            this.Eval(interpreter.IntegerStack, 2, values => values[0] * values[1]);
     48        }
    4749    }
    4850
    49     public override void Eval(IPushGpInterpreter interpreter)
     51    /// <summary>
     52    ///     Pushes the quotient of the top two items; that is, the second item divided by the top item.
     53    ///     If the top item is zero this acts as a NOOP.
     54    /// </summary>
     55    public class IntegerDivideExpression : PushResultExpression<long>
    5056    {
    51       this.Eval(interpreter.IntegerStack, 2, values => values[0] * values[1]);
    52     }
    53   }
     57        public IntegerDivideExpression() : base("INTEGER./")
     58        {
     59        }
    5460
    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 IntegerDivideExpression : PushResultExpression<long>
    60   {
    61     protected override string InitStringRepresentation()
    62     {
    63       return "INTEGER./";
     61        public override void Eval(IPushGpInterpreter interpreter)
     62        {
     63            this.Eval(interpreter.IntegerStack, 2, values => values[0] / values[1], 0);
     64        }
    6465    }
    6566
    66     public override void Eval(IPushGpInterpreter interpreter)
     67    /// <summary>
     68    ///     Pushes the second stack item modulo the top stack item. If the top item is zero this acts as a NOOP. The modulus is
     69    ///     computed as the
     70    ///     remainder of the quotient, where the quotient has first been truncated toward negative infinity. (This is taken
     71    ///     from the definition
     72    ///     for the generic MOD function in Common Lisp, which is described for example at
     73    ///     http://www.lispworks.com/reference/HyperSpec/Body/f_mod_r.htm.)
     74    /// </summary>
     75    public class IntegerModuloExpression : PushResultExpression<long>
    6776    {
    68       this.Eval(interpreter.IntegerStack, 2, values => values[0] / values[1], 0);
    69     }
    70   }
     77        public IntegerModuloExpression() : base("INTEGER.%")
     78        {
     79        }
    7180
    72   /// <summary>
    73   ///     Pushes the second stack item modulo the top stack item. If the top item is zero this acts as a NOOP. The modulus is
    74   ///     computed as the
    75   ///     remainder of the quotient, where the quotient has first been truncated toward negative infinity. (This is taken
    76   ///     from the definition
    77   ///     for the generic MOD function in Common Lisp, which is described for example at
    78   ///     http://www.lispworks.com/reference/HyperSpec/Body/f_mod_r.htm.)
    79   /// </summary>
    80   public class IntegerModuloExpression : PushResultExpression<long>
    81   {
    82     protected override string InitStringRepresentation()
    83     {
    84       return "INTEGER.%";
     81        public override void Eval(IPushGpInterpreter interpreter)
     82        {
     83            this.Eval(interpreter.IntegerStack, 2, values => values[0] % values[1], 0);
     84        }
    8585    }
    8686
    87     public override void Eval(IPushGpInterpreter interpreter)
     87    /// <summary>
     88    ///     Pushes the minimum of the top two items.
     89    /// </summary>
     90    public class IntegerMinExpression : PushResultExpression<long>
    8891    {
    89       this.Eval(interpreter.IntegerStack, 2, values => values[0] % values[1], 0);
    90     }
    91   }
     92        public IntegerMinExpression() : base("INTEGER.MIN")
     93        {
     94        }
    9295
    93   /// <summary>
    94   ///     Pushes the minimum of the top two items.
    95   /// </summary>
    96   public class IntegerMinExpression : PushResultExpression<long>
    97   {
    98     protected override string InitStringRepresentation()
    99     {
    100       return "INTEGER.MIN";
     96        public override void Eval(IPushGpInterpreter interpreter)
     97        {
     98            this.Eval(interpreter.IntegerStack, 2, values => Math.Min(values[0], values[1]));
     99        }
    101100    }
    102101
    103     public override void Eval(IPushGpInterpreter interpreter)
     102    /// <summary>
     103    ///     Pushes the maximum of the top two items.
     104    /// </summary>
     105    public class IntegerMaxExpression : PushResultExpression<long>
    104106    {
    105       this.Eval(interpreter.IntegerStack, 2, values => Math.Min(values[0], values[1]));
    106     }
    107   }
     107        public IntegerMaxExpression() : base("INTEGER.MAX")
     108        {
     109        }
    108110
    109   /// <summary>
    110   ///     Pushes the maximum of the top two items.
    111   /// </summary>
    112   public class IntegerMaxExpression : PushResultExpression<long>
    113   {
    114     protected override string InitStringRepresentation()
    115     {
    116       return "INTEGER.MAX";
     111        public override void Eval(IPushGpInterpreter interpreter)
     112        {
     113            this.Eval(interpreter.IntegerStack, 2, values => Math.Max(values[0], values[1]));
     114        }
    117115    }
    118116
    119     public override void Eval(IPushGpInterpreter interpreter)
     117    /// <summary>
     118    ///     Pushes TRUE onto the BOOLEAN stack if the second item is less than the top item, or FALSE otherwise.
     119    /// </summary>
     120    public class IntegerSmallerThanExpression : PushResultExpression<long>
    120121    {
    121       this.Eval(interpreter.IntegerStack, 2, values => Math.Max(values[0], values[1]));
    122     }
    123   }
     122        public IntegerSmallerThanExpression() : base("INTEGER.<")
     123        {
     124        }
    124125
    125   /// <summary>
    126   ///     Pushes TRUE onto the BOOLEAN stack if the second item is less than the top item, or FALSE otherwise.
    127   /// </summary>
    128   public class IntegerSmallerThanExpression : PushResultExpression<long>
    129   {
    130     protected override string InitStringRepresentation()
    131     {
    132       return "INTEGER.<";
     126        public override void Eval(IPushGpInterpreter interpreter)
     127        {
     128            this.Eval(interpreter.IntegerStack, interpreter.BooleanStack, 2, values => values[0] < values[1]);
     129        }
    133130    }
    134131
    135     public override void Eval(IPushGpInterpreter interpreter)
     132    /// <summary>
     133    ///     Pushes TRUE onto the BOOLEAN stack if the second item is greater than the top item, or FALSE otherwise.
     134    /// </summary>
     135    public class IntegerGreaterThanExpression : PushResultExpression<long>
    136136    {
    137       this.Eval(interpreter.IntegerStack, interpreter.BooleanStack, 2, values => values[0] < values[1]);
    138     }
    139   }
     137        public IntegerGreaterThanExpression() : base("INTEGER.>")
     138        {
     139        }
    140140
    141   /// <summary>
    142   ///     Pushes TRUE onto the BOOLEAN stack if the second item is greater than the top item, or FALSE otherwise.
    143   /// </summary>
    144   public class IntegerGreaterThanExpression : PushResultExpression<long>
    145   {
    146     protected override string InitStringRepresentation()
    147     {
    148       return "INTEGER.>";
     141        public override void Eval(IPushGpInterpreter interpreter)
     142        {
     143            this.Eval(interpreter.IntegerStack, interpreter.BooleanStack, 2, values => values[0] > values[1]);
     144        }
    149145    }
    150146
    151     public override void Eval(IPushGpInterpreter interpreter)
     147    /// <summary>
     148    ///     Pushes 1 if the top BOOLEAN is TRUE, or 0 if the top BOOLEAN is FALSE.
     149    /// </summary>
     150    public class IntegerFromBooleanExpression : StatelessExpression
    152151    {
    153       this.Eval(interpreter.IntegerStack, interpreter.BooleanStack, 2, values => values[0] > values[1]);
    154     }
    155   }
     152        public IntegerFromBooleanExpression() : base("INTEGER.FROMBOOLEAN")
     153        {
     154        }
    156155
    157   /// <summary>
    158   ///     Pushes 1 if the top BOOLEAN is TRUE, or 0 if the top BOOLEAN is FALSE.
    159   /// </summary>
    160   public class IntegerFromBooleanExpression : StatelessExpression
    161   {
    162     protected override string InitStringRepresentation()
    163     {
    164       return "INTEGER.FROMBOOLEAN";
     156        public override void Eval(IPushGpInterpreter interpreter)
     157        {
     158            if (interpreter.BooleanStack.Count == 0) return;
     159
     160            var condition = interpreter.BooleanStack.Pop();
     161            var value = condition ? 1 : 0;
     162
     163            interpreter.IntegerStack.Push(value);
     164        }
    165165    }
    166166
    167     public override void Eval(IPushGpInterpreter interpreter)
     167    /// <summary>
     168    ///     Pushes the result of truncating the top FLOAT.
     169    /// </summary>
     170    public class IntegerFromFloatExpression : StatelessExpression
    168171    {
    169       if (interpreter.BooleanStack.Count == 0) return;
     172        public IntegerFromFloatExpression() : base("INTEGER.FROMFLOAT")
     173        {
     174        }
    170175
    171       var condition = interpreter.BooleanStack.Pop();
    172       var value = condition ? 1 : 0;
     176        public override void Eval(IPushGpInterpreter interpreter)
     177        {
     178            if (interpreter.FloatStack.Count == 0) return;
    173179
    174       interpreter.IntegerStack.Push(value);
     180            var value = (int) interpreter.FloatStack.Pop();
     181
     182            interpreter.IntegerStack.Push(value);
     183        }
    175184    }
    176   }
    177 
    178   /// <summary>
    179   ///     Pushes the result of truncating the top FLOAT.
    180   /// </summary>
    181   public class IntegerFromFloatExpression : StatelessExpression
    182   {
    183     protected override string InitStringRepresentation()
    184     {
    185       return "INTEGER.FROMFLOAT";
    186     }
    187 
    188     public override void Eval(IPushGpInterpreter interpreter)
    189     {
    190       if (interpreter.FloatStack.Count == 0) return;
    191 
    192       var value = (int)interpreter.FloatStack.Pop();
    193 
    194       interpreter.IntegerStack.Push(value);
    195     }
    196   }
    197185}
Note: See TracChangeset for help on using the changeset viewer.