Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/01/17 09:28:34 (7 years ago)
Author:
pkimmesw
Message:

#2665 Fixed Benchmark Problem Definition, Converted LoopExpressions to stateless expressions, Added several unit test to ensure funcionality, Fixed UI bugs

File:
1 edited

Legend:

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

    r14952 r15017  
    11namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
     2  using System;
    23  using System.Collections.Generic;
    3 
    4   using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    5   using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
    6   using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
    7 
     4  using System.Linq;
     5  using Attributes;
     6  using Constants;
     7  using Data.Pool;
     8  using Extensions;
     9  using Interpreter;
     10  using Persistence.Default.CompositeSerializers.Storable;
     11  using Stack;
     12
     13  [Serializable]
    814  [StorableClass]
    915  public abstract class PushExpression<T> : StatefulExpression<T> {
     
    2026      stack.Push(State);
    2127    }
    22 
    23     public override string StringRepresentation { get { return State.ToString(); } }
    24   }
    25 
    26   [StorableClass]
     28  }
     29
     30  [StorableClass]
     31  [Serializable]
     32  public abstract class VectorPushExpression<T> : PushExpression<IReadOnlyList<T>> {
     33    [StorableConstructor]
     34    protected VectorPushExpression(bool deserializing) : base(deserializing) { }
     35
     36    protected VectorPushExpression(IReadOnlyList<T> state) : base(state, null) { }
     37
     38    protected override string GetStringRepresentation() {
     39      return PushEnvironment.VectorStartSymbolStr +
     40             string.Join(PushEnvironment.VectorSeparatorSymbolStr, State) +
     41             PushEnvironment.VectorEndSymbolStr;
     42    }
     43
     44    protected override int CalcHashCode() {
     45      return State.HashCode();
     46    }
     47  }
     48
     49  [StorableClass]
     50  [Serializable]
     51  [PushExpression(StackTypes.Integer, "INTEGER.PUSH", isHidden: true)]
    2752  public class IntegerPushExpression : PushExpression<long> {
    2853    [StorableConstructor]
    2954    protected IntegerPushExpression(bool deserializing) : base(deserializing) { }
    3055
     56    public static IntegerPushExpression Create(IManagedPool<Expression> statefulExpressionPool, long state) {
     57      var expression = (IntegerPushExpression)statefulExpressionPool.Get();
     58      expression.State = state;
     59
     60      return expression;
     61    }
     62
     63    /// <summary>
     64    /// Pushes default(long)
     65    /// </summary>
     66    public IntegerPushExpression() : base(default(long), null) {
     67    }
     68
     69    public IntegerPushExpression(long state)
     70      : base(state, null) {
     71    }
     72
     73    public override void Eval(IInternalPushInterpreter interpreter) {
     74      Eval(interpreter.IntegerStack);
     75    }
     76  }
     77
     78  [StorableClass]
     79  [Serializable]
     80  [PushExpression(StackTypes.Float, "FLOAT.PUSH", isHidden: true)]
     81  public class FloatPushExpression : PushExpression<double> {
     82    [StorableConstructor]
     83    protected FloatPushExpression(bool deserializing) : base(deserializing) { }
     84
     85    public static FloatPushExpression Create(IManagedPool<Expression> statefulExpressionPool, double state) {
     86      var expression = (FloatPushExpression)statefulExpressionPool.Get();
     87      expression.State = state;
     88
     89      return expression;
     90    }
     91
     92    /// <summary>
     93    /// Pushes default(double)
     94    /// </summary>
     95    public FloatPushExpression() : base(default(double), null) {
     96    }
     97
     98    public FloatPushExpression(double state)
     99      : base(state, null) {
     100    }
     101
     102    public override void Eval(IInternalPushInterpreter interpreter) {
     103      Eval(interpreter.FloatStack);
     104    }
     105  }
     106
     107  [StorableClass]
     108  [Serializable]
     109  [PushExpression(StackTypes.Boolean, "BOOLEAN.PUSH", isHidden: true)]
     110  public class BooleanPushExpression : PushExpression<bool> {
     111    [StorableConstructor]
     112    protected BooleanPushExpression(bool deserializing, object other) : base(deserializing) { }
     113
     114    public static BooleanPushExpression Create(IManagedPool<Expression> statefulExpressionPool, bool state) {
     115      var expression = (BooleanPushExpression)statefulExpressionPool.Get();
     116      expression.State = state;
     117
     118      return expression;
     119    }
     120
     121    /// <summary>
     122    /// Pushes default(bool)
     123    /// </summary>
     124    public BooleanPushExpression() : base(default(bool), null) {
     125    }
     126
     127    public BooleanPushExpression(bool state)
     128      : base(state, null) {
     129    }
     130
     131    public override void Eval(IInternalPushInterpreter interpreter) {
     132      Eval(interpreter.BooleanStack);
     133    }
     134  }
     135
     136  [StorableClass]
     137  [Serializable]
     138  [PushExpression(StackTypes.Name, "NAME.PUSH", isHidden: true)]
     139  public class NamePushExpression : PushExpression<string> {
     140    [StorableConstructor]
     141    protected NamePushExpression(bool deserializing) : base(deserializing) { }
     142    public static NamePushExpression Create(IManagedPool<Expression> statefulExpressionPool, string state) {
     143      var expression = (NamePushExpression)statefulExpressionPool.Get();
     144      expression.State = state;
     145
     146      return expression;
     147    }
     148
     149    public NamePushExpression() : base(string.Empty, null) {
     150    }
     151
     152    public NamePushExpression(string state)
     153      : base(state, null) {
     154    }
     155
     156    public override void Eval(IInternalPushInterpreter interpreter) {
     157      Eval(interpreter.NameStack);
     158    }
     159  }
     160
     161  [StorableClass]
     162  [Serializable]
     163  [PushExpression(StackTypes.Exec, "EXEC.PUSH", isHidden: true)]
     164  public class ExecPushExpression : PushExpression<Expression> {
     165    [StorableConstructor]
     166    protected ExecPushExpression(bool deserializing) : base(deserializing) { }
     167    public static ExecPushExpression Create(IManagedPool<Expression> statefulExpressionPool, Expression state) {
     168      var expression = (ExecPushExpression)statefulExpressionPool.Get();
     169      expression.State = state;
     170
     171      return expression;
     172    }
     173
     174    private static readonly Expression Noop = ExpressionTable.GetStatelessExpression<ExecNoopExpression>();
     175
     176    /// <summary>
     177    /// Pushes ExecNoop
     178    /// </summary>
     179    public ExecPushExpression() : base(Noop, null) {
     180    }
     181
     182    public ExecPushExpression(Expression state)
     183      : base(state, null) {
     184    }
     185
     186    public override void Eval(IInternalPushInterpreter interpreter) {
     187      Eval(interpreter.ExecStack);
     188    }
     189  }
     190
     191  [StorableClass]
     192  [Serializable]
     193  [PushExpression(StackTypes.Char, "CHAR.PUSH", isHidden: true)]
     194  public class CharPushExpression : PushExpression<char> {
     195    [StorableConstructor]
     196    protected CharPushExpression(bool deserializing) : base(deserializing) { }
     197
    31198    // not supported so far
    32     //public static IntegerPushExpression Create(IManagedPool<Expression> statefulExpressionPool, long state) {
    33     //  var expression = (IntegerPushExpression)statefulExpressionPool.Get();
    34     //  expression.State = state;
    35 
    36     //  return expression;
    37     //}
    38 
    39     public IntegerPushExpression(long state)
    40       : base(state, null) {
    41     }
    42 
    43     public override void Eval(IInternalPushInterpreter interpreter) {
    44       Eval(interpreter.IntegerStack);
    45     }
    46   }
    47 
    48   [StorableClass]
    49   public class FloatPushExpression : PushExpression<double> {
    50     [StorableConstructor]
    51     protected FloatPushExpression(bool deserializing) : base(deserializing) { }
    52 
    53     // not supported so far
    54     //public static FloatPushExpression Create(IManagedPool<Expression> statefulExpressionPool, double state) {
    55     //  var expression = (FloatPushExpression)statefulExpressionPool.Get();
    56     //  expression.State = state;
    57 
    58     //  return expression;
    59     //}
    60 
    61     public FloatPushExpression(double state)
    62       : base(state, null) {
    63     }
    64 
    65     public override void Eval(IInternalPushInterpreter interpreter) {
    66       Eval(interpreter.FloatStack);
    67     }
    68   }
    69 
    70   [StorableClass]
    71   public class BooleanPushExpression : PushExpression<bool> {
    72     [StorableConstructor]
    73     protected BooleanPushExpression(bool deserializing, object other) : base(deserializing) { }
    74 
    75     // not supported so far
    76     //public static BooleanPushExpression Create(IManagedPool<Expression> statefulExpressionPool, bool state) {
    77     //  var expression = (BooleanPushExpression)statefulExpressionPool.Get();
    78     //  expression.State = state;
    79 
    80     //  return expression;
    81     //}
    82 
    83     public BooleanPushExpression(bool state)
    84       : base(state, null) {
    85     }
    86 
    87     public override void Eval(IInternalPushInterpreter interpreter) {
    88       Eval(interpreter.BooleanStack);
    89     }
    90   }
    91 
    92   [StorableClass]
    93   public class NamePushExpression : PushExpression<string> {
    94     [StorableConstructor]
    95     protected NamePushExpression(bool deserializing) : base(deserializing) { }
    96 
    97     // not supported so far
    98     //public static NamePushExpression Create(IManagedPool<Expression> statefulExpressionPool, string state) {
    99     //  var expression = (NamePushExpression)statefulExpressionPool.Get();
    100     //  expression.State = state;
    101 
    102     //  return expression;
    103     //}
    104 
    105     public NamePushExpression(string state)
    106       : base(state, null) {
    107     }
    108 
    109     public override void Eval(IInternalPushInterpreter interpreter) {
    110       Eval(interpreter.NameStack);
    111     }
    112   }
    113 
    114   [StorableClass]
    115   public class ExecPushExpression : PushExpression<Expression> {
    116     [StorableConstructor]
    117     protected ExecPushExpression(bool deserializing) : base(deserializing) { }
    118 
    119     // not supported so far
    120     //public static ExecPushExpression Create(IManagedPool<Expression> statefulExpressionPool, Expression state) {
    121     //  var expression = (ExecPushExpression)statefulExpressionPool.Get();
    122     //  expression.State = state;
    123 
    124     //  return expression;
    125     //}
    126 
    127     public ExecPushExpression(Expression state)
    128       : base(state, null) {
    129     }
    130 
    131     public override void Eval(IInternalPushInterpreter interpreter) {
    132       Eval(interpreter.ExecStack);
    133     }
    134   }
    135 
    136   [StorableClass]
    137   public class CharPushExpression : PushExpression<char> {
    138     [StorableConstructor]
    139     protected CharPushExpression(bool deserializing) : base(deserializing) { }
    140 
    141     // not supported so far
    142     //public static CharPushExpression Create(IManagedPool<Expression> statefulExpressionPool, char state) {
    143     //  var expression = (CharPushExpression)statefulExpressionPool.Get();
    144     //  expression.State = state;
    145 
    146     //  return expression;
    147     //}
     199    public static CharPushExpression Create(IManagedPool<Expression> statefulExpressionPool, char state) {
     200      var expression = (CharPushExpression)statefulExpressionPool.Get();
     201      expression.State = state;
     202
     203      return expression;
     204    }
     205
     206    /// <summary>
     207    /// Pushes default(char)
     208    /// </summary>
     209    public CharPushExpression() : base(default(char), null) {
     210    }
    148211
    149212    public CharPushExpression(char state)
     
    151214    }
    152215
     216    protected override string GetStringRepresentation() {
     217      return PushEnvironment.CharSymbol + State.Printify() + PushEnvironment.CharSymbol;
     218    }
     219
    153220    public override void Eval(IInternalPushInterpreter interpreter) {
    154221      Eval(interpreter.CharStack);
     
    157224
    158225  [StorableClass]
     226  [Serializable]
     227  [PushExpression(StackTypes.String, "STRING.PUSH", isHidden: true)]
    159228  public class StringPushExpression : PushExpression<string> {
    160229    [StorableConstructor]
    161230    protected StringPushExpression(bool deserializing) : base(deserializing) { }
    162231
    163     // not supported so far
    164     //public static StringPushExpression Create(IManagedPool<Expression> statefulExpressionPool, string state) {
    165     //  var expression = (StringPushExpression)statefulExpressionPool.Get();
    166     //  expression.State = state;
    167 
    168     //  return expression;
    169     //}
     232    public static StringPushExpression Create(IManagedPool<Expression> statefulExpressionPool, string state) {
     233      var expression = (StringPushExpression)statefulExpressionPool.Get();
     234      expression.State = state;
     235
     236      return expression;
     237    }
     238
     239    /// <summary>
     240    /// Pushes String.Empty
     241    /// </summary>
     242    public StringPushExpression() : base(string.Empty, null) {
     243    }
     244
     245    protected override string GetStringRepresentation() {
     246      return PushEnvironment.StringSymbol + State + PushEnvironment.StringSymbol;
     247    }
    170248
    171249    public StringPushExpression(string state)
     
    179257
    180258  [StorableClass]
    181   public class IntegerVectorPushExpression : PushExpression<List<long>> {
     259  [Serializable]
     260  [PushExpression(StackTypes.IntegerVector, "INTEGER[].PUSH", isHidden: true)]
     261  public class IntegerVectorPushExpression : VectorPushExpression<long> {
    182262    [StorableConstructor]
    183263    protected IntegerVectorPushExpression(bool deserializing) : base(deserializing) { }
    184264
    185     // not supported so far
    186     //public static IntegerVectorPushExpression Create(IManagedPool<Expression> statefulExpressionPool, List<long> state) {
    187     //  var expression = (IntegerVectorPushExpression)statefulExpressionPool.Get();
    188     //  expression.State = state;
    189 
    190     //  return expression;
    191     //}
    192 
    193     public IntegerVectorPushExpression(List<long> state)
    194       : base(state, null) {
     265    public static IntegerVectorPushExpression Create(IManagedPool<Expression> statefulExpressionPool, IReadOnlyList<long> state) {
     266      var expression = (IntegerVectorPushExpression)statefulExpressionPool.Get();
     267      expression.State = state;
     268
     269      return expression;
     270    }
     271
     272    [NonSerialized]
     273    private static readonly long[] EmptyState = new long[0];
     274
     275    /// <summary>
     276    /// Pushes long[0]
     277    /// </summary>
     278    public IntegerVectorPushExpression() : base(EmptyState) {
     279    }
     280
     281    public IntegerVectorPushExpression(IReadOnlyList<long> state) : base(state) {
    195282    }
    196283
     
    201288
    202289  [StorableClass]
    203   public class FloatVectorPushExpression : PushExpression<List<double>> {
     290  [Serializable]
     291  [PushExpression(StackTypes.FloatVector, "FLOAT[].PUSH", isHidden: true)]
     292  public class FloatVectorPushExpression : VectorPushExpression<double> {
    204293    [StorableConstructor]
    205294    protected FloatVectorPushExpression(bool deserializing) : base(deserializing) { }
    206295
    207     // not supported so far
    208     //public static FloatVectorPushExpression Create(IManagedPool<Expression> statefulExpressionPool, List<double> state) {
    209     //  var expression = (FloatVectorPushExpression)statefulExpressionPool.Get();
    210     //  expression.State = state;
    211 
    212     //  return expression;
    213     //}
    214 
    215     public FloatVectorPushExpression(List<double> state)
    216       : base(state, null) {
    217     }
     296    public static FloatVectorPushExpression Create(IManagedPool<Expression> statefulExpressionPool, IReadOnlyList<double> state) {
     297      var expression = (FloatVectorPushExpression)statefulExpressionPool.Get();
     298      expression.State = state;
     299
     300      return expression;
     301    }
     302
     303    [NonSerialized]
     304    private static readonly double[] EmptyState = new double[0];
     305
     306    /// <summary>
     307    /// Pushes double[0]
     308    /// </summary>
     309    public FloatVectorPushExpression() : base(EmptyState) {
     310    }
     311
     312    public FloatVectorPushExpression(IReadOnlyList<double> state) : base(state) { }
    218313
    219314    public override void Eval(IInternalPushInterpreter interpreter) {
     
    223318
    224319  [StorableClass]
    225   public class BooleanVectorPushExpression : PushExpression<List<bool>> {
     320  [Serializable]
     321  [PushExpression(StackTypes.BooleanVector, "BOOLEAN[].PUSH", isHidden: true)]
     322  public class BooleanVectorPushExpression : VectorPushExpression<bool> {
    226323    [StorableConstructor]
    227324    protected BooleanVectorPushExpression(bool deserializing) : base(deserializing) { }
    228325
    229     // not supported so far
    230     //public static BooleanVectorPushExpression Create(IManagedPool<Expression> statefulExpressionPool, List<bool> state) {
    231     //  var expression = (BooleanVectorPushExpression)statefulExpressionPool.Get();
    232     //  expression.State = state;
    233 
    234     //  return expression;
    235     //}
    236 
    237     public BooleanVectorPushExpression(List<bool> state)
    238       : base(state, null) {
    239     }
     326    public static BooleanVectorPushExpression Create(IManagedPool<Expression> statefulExpressionPool, IReadOnlyList<bool> state) {
     327      var expression = (BooleanVectorPushExpression)statefulExpressionPool.Get();
     328      expression.State = state;
     329
     330      return expression;
     331    }
     332
     333    [NonSerialized]
     334    private static readonly bool[] EmptyState = new bool[0];
     335
     336    /// <summary>
     337    /// Pushes bool[0]
     338    /// </summary>
     339    public BooleanVectorPushExpression() : base(EmptyState) {
     340    }
     341
     342    public BooleanVectorPushExpression(IReadOnlyList<bool> state) : base(state) { }
    240343
    241344    public override void Eval(IInternalPushInterpreter interpreter) {
     
    245348
    246349  [StorableClass]
    247   public class StringVectorPushExpression : PushExpression<List<string>> {
     350  [Serializable]
     351  [PushExpression(StackTypes.StringVector, "STRING[].PUSH", isHidden: true)]
     352  public class StringVectorPushExpression : VectorPushExpression<string> {
    248353    [StorableConstructor]
    249354    protected StringVectorPushExpression(bool deserializing) : base(deserializing) { }
    250355
    251     // not supported so far
    252     //public static StringVectorPushExpression Create(IManagedPool<Expression> statefulExpressionPool, List<string> state) {
    253     //  var expression = (StringVectorPushExpression)statefulExpressionPool.Get();
    254     //  expression.State = state;
    255 
    256     //  return expression;
    257     //}
    258 
    259     public StringVectorPushExpression(List<string> state)
    260       : base(state, null) {
    261     }
     356    public static StringVectorPushExpression Create(IManagedPool<Expression> statefulExpressionPool, IReadOnlyList<string> state) {
     357      var expression = (StringVectorPushExpression)statefulExpressionPool.Get();
     358      expression.State = state;
     359
     360      return expression;
     361    }
     362
     363    [NonSerialized]
     364    private static readonly string[] EmptyState = new string[0];
     365
     366    /// <summary>
     367    /// Pushes string[0]
     368    /// </summary>
     369    public StringVectorPushExpression() : base(EmptyState) {
     370    }
     371
     372    protected override string GetStringRepresentation() {
     373      return PushEnvironment.VectorStartSymbolStr +
     374             string.Join(PushEnvironment.VectorSeparatorSymbolStr, State.Select(str => PushEnvironment.StringSymbol + str + PushEnvironment.StringSymbol)) +
     375             PushEnvironment.VectorEndSymbolStr;
     376    }
     377
     378    public StringVectorPushExpression(IReadOnlyList<string> state) : base(state) { }
    262379
    263380    public override void Eval(IInternalPushInterpreter interpreter) {
Note: See TracChangeset for help on using the changeset viewer.