Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/RandExpressions.cs

Last change on this file was 15771, checked in by bburlacu, 7 years ago

#2895: Add solution skeleton for PushGP with genealogy analysis.

File size: 9.3 KB
Line 
1using System.Collections.Generic;
2using System.Linq;
3using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
4
5namespace HeuristicLab.Problems.ProgramSynthesis {
6
7  /// <summary>
8  /// Pushes a random already defined name or a new one onto the NAME stack.
9  /// </summary>
10  [PushExpression(
11    StackTypes.Name,
12    "NAME.RAND",
13    "Pushes a random already defined name or a new one based on the Name-ERC-Options onto the NAME stack.")]
14  [StorableClass]
15  public class NameRandExpression : StatelessExpression {
16    public NameRandExpression() { }
17    [StorableConstructor]
18    protected NameRandExpression(bool deserializing) : base(deserializing) { }
19
20    public override bool IsNoop(IInternalPushInterpreter interpreter) {
21      return !interpreter.Configuration.ErcOptions.NameErcOptions.IsEnabled;
22    }
23
24    public override void Eval(IInternalPushInterpreter interpreter) {
25      var name = interpreter.Configuration.ErcOptions.NameErcOptions.GetErcValue(interpreter.Random);
26      interpreter.NameStack.Push(name);
27    }
28  }
29
30  /// <summary>
31  /// Pushes a random integer.
32  /// </summary>
33  [PushExpression(
34    StackTypes.Integer,
35    "INTEGER.RAND",
36    "Pushes a random integer based on the Integer-ERC-Options  onto the INTEGER stack.")]
37  [StorableClass]
38  public class IntegerRandExpression : StatelessExpression {
39    public IntegerRandExpression() { }
40    [StorableConstructor]
41    protected IntegerRandExpression(bool deserializing) : base(deserializing) { }
42
43    public override bool IsNoop(IInternalPushInterpreter interpreter) {
44      return !interpreter.Configuration.ErcOptions.IntegerErcOptions.IsEnabled;
45    }
46
47    public override void Eval(IInternalPushInterpreter interpreter) {
48      var value = interpreter.Configuration.ErcOptions.IntegerErcOptions.GetErcValue(interpreter.Random);
49      interpreter.IntegerStack.Push(value);
50    }
51  }
52
53  /// <summary>
54  ///  Pushes a random float.
55  /// </summary>
56  [PushExpression(
57    StackTypes.Float,
58    "FLOAT.RAND",
59    "Pushes a random float based on the Float-ERC-Options onto the FLOAT stack.")]
60  [StorableClass]
61  public class FloatRandExpression : StatelessExpression {
62    public FloatRandExpression() { }
63    [StorableConstructor]
64    protected FloatRandExpression(bool deserializing) : base(deserializing) { }
65
66    public override bool IsNoop(IInternalPushInterpreter interpreter) {
67      return !interpreter.Configuration.ErcOptions.FloatErcOptions.IsEnabled;
68    }
69
70    public override void Eval(IInternalPushInterpreter interpreter) {
71      var value = interpreter.Configuration.ErcOptions.FloatErcOptions.GetErcValue(interpreter.Random);
72      interpreter.FloatStack.Push(value);
73    }
74  }
75
76  /// <summary>
77  ///     Pushes a random boolean.
78  /// </summary>
79  [PushExpression(
80    StackTypes.Boolean,
81    "BOOLEAN.RAND",
82    "Pushes a random boolean based on the Boolean-ERC-Options onto the BOOLEAN stack.")]
83  [StorableClass]
84  public class BooleanRandExpression : StatelessExpression {
85    public BooleanRandExpression() { }
86    [StorableConstructor]
87    protected BooleanRandExpression(bool deserializing) : base(deserializing) { }
88
89    public override bool IsNoop(IInternalPushInterpreter interpreter) {
90      return !interpreter.Configuration.ErcOptions.BooleanErcOptions.IsEnabled;
91    }
92
93    public override void Eval(IInternalPushInterpreter interpreter) {
94      var value = interpreter.Configuration.ErcOptions.BooleanErcOptions.GetErcValue(interpreter.Random);
95      interpreter.BooleanStack.Push(value);
96    }
97  }
98
99  /// <summary>
100  ///     Pushes random expressions onto the code stack.
101  /// </summary>
102  [PushExpression(
103    StackTypes.Code,
104    "CODE.RAND",
105    "Pushes a random code onto the CODE stack.")]
106  [StorableClass]
107  public class CodeRandExpression : StatelessExpression {
108    public CodeRandExpression() { }
109    [StorableConstructor]
110    protected CodeRandExpression(bool deserializing) : base(deserializing) { }
111
112    public override bool IsNoop(IInternalPushInterpreter interpreter) {
113      return interpreter.IntegerStack.Count == 0 || interpreter.IntegerStack.Top < 1;
114    }
115
116    public override void Eval(IInternalPushInterpreter interpreter) {
117      var size = interpreter.IntegerStack.Pop().AsInt(interpreter.Configuration.MaxPointsInRandomExpression);
118      var program = LinearCodeGenerator.RandomProgram(
119        interpreter.PoolContainer.PushProgramPool,
120        interpreter.PoolContainer.ExpressionListPool,
121        size,
122        interpreter.Random,
123        interpreter.Configuration,
124        interpreter.CustomExpressions);
125
126      interpreter.CodeStack.Push(program);
127    }
128  }
129
130  /// <summary>
131  ///     Pushes a random char.
132  /// </summary>
133  [PushExpression(
134    StackTypes.Char,
135    "CHAR.RAND",
136    "Pushes a random char based on the Char-ERC-Options onto the CHAR stack.")]
137  [StorableClass]
138  public class CharRandExpression : StatelessExpression {
139    public CharRandExpression() { }
140    [StorableConstructor]
141    protected CharRandExpression(bool deserializing) : base(deserializing) { }
142
143    public override bool IsNoop(IInternalPushInterpreter interpreter) {
144      return !interpreter.Configuration.ErcOptions.CharErcOptions.IsEnabled;
145    }
146
147    public override void Eval(IInternalPushInterpreter interpreter) {
148      var value = interpreter.Configuration.ErcOptions.CharErcOptions.GetErcValue(interpreter.Random);
149      interpreter.CharStack.Push(value);
150    }
151
152    /// <summary>
153    ///     Pushes a random string.
154    /// </summary>
155    [PushExpression(
156      StackTypes.String,
157      "STRING.RAND",
158      "Pushes a random string based on the String-ERC-Options onto the STRING stack.")]
159    [StorableClass]
160    public class StringRandExpression : StatelessExpression {
161      public StringRandExpression() { }
162      [StorableConstructor]
163      protected StringRandExpression(bool deserializing) : base(deserializing) { }
164
165      public override bool IsNoop(IInternalPushInterpreter interpreter) {
166        return !interpreter.Configuration.ErcOptions.StringErcOptions.IsEnabled;
167      }
168
169      public override void Eval(IInternalPushInterpreter interpreter) {
170        var value = interpreter.Configuration.ErcOptions.StringErcOptions.GetErcValue(interpreter.Random);
171        interpreter.StringStack.Push(value);
172      }
173
174      /// <summary>
175      ///     Pushes a random interger vector.
176      /// </summary>
177      [PushExpression(
178        StackTypes.IntegerVector,
179        "INTEGER[].RAND",
180        "Pushes a random integer vector based on the Integer-Vector-ERC-Options onto the INTEGER[] stack.")]
181      [StorableClass]
182      public class IntegerVectorRandExpression : StatelessExpression {
183        public IntegerVectorRandExpression() { }
184        [StorableConstructor]
185        protected IntegerVectorRandExpression(bool deserializing) : base(deserializing) { }
186
187        public override bool IsNoop(IInternalPushInterpreter interpreter) {
188          return !interpreter.Configuration.ErcOptions.IntegerVectorErcOptions.IsEnabled;
189        }
190
191        public override void Eval(IInternalPushInterpreter interpreter) {
192          var value = interpreter.Configuration.ErcOptions.IntegerVectorErcOptions.GetErcValue(interpreter.Random);
193          interpreter.IntegerVectorStack.Push(new List<long>(value.Select(i => (long)i)));
194        }
195      }
196
197      /// <summary>
198      ///     Pushes a random float vector.
199      /// </summary>
200      [PushExpression(
201        StackTypes.FloatVector,
202        "FLOAT[].RAND",
203        "Pushes a random float vector based on the Float-Vector-ERC-Options onto the FLOAT[] stack.")]
204      [StorableClass]
205      public class FloatVectorRandExpression : StatelessExpression {
206        public FloatVectorRandExpression() { }
207        [StorableConstructor]
208        protected FloatVectorRandExpression(bool deserializing) : base(deserializing) { }
209
210        public override bool IsNoop(IInternalPushInterpreter interpreter) {
211          return !interpreter.Configuration.ErcOptions.FloatVectorErcOptions.IsEnabled;
212        }
213
214        public override void Eval(IInternalPushInterpreter interpreter) {
215          var value = interpreter.Configuration.ErcOptions.FloatVectorErcOptions.GetErcValue(interpreter.Random);
216          interpreter.FloatVectorStack.Push(value);
217        }
218      }
219
220      /// <summary>
221      ///     Pushes a random boolean vector.
222      /// </summary>
223      [PushExpression(
224        StackTypes.StringVector,
225        "STRING[].RAND",
226        "Pushes a random string vector based on the String-Vector-ERC-Options onto the STRING[] stack.")]
227      [StorableClass]
228      public class StringVectorRandExpression : StatelessExpression {
229        public StringVectorRandExpression() { }
230        [StorableConstructor]
231        protected StringVectorRandExpression(bool deserializing) : base(deserializing) { }
232
233        public override bool IsNoop(IInternalPushInterpreter interpreter) {
234          return !interpreter.Configuration.ErcOptions.StringVectorErcOptions.IsEnabled;
235        }
236
237        public override void Eval(IInternalPushInterpreter interpreter) {
238          var value = interpreter.Configuration.ErcOptions.StringVectorErcOptions.GetErcValue(interpreter.Random);
239          interpreter.StringVectorStack.Push(value);
240        }
241      }
242    }
243  }
244}
Note: See TracBrowser for help on using the repository browser.