Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/RandExpressions.cs @ 18242

Last change on this file since 18242 was 15032, checked in by pkimmesw, 7 years ago

#2665 Fixed bias 0 issue, PushExpressionFrequencyAnalyzer, Fixed probability for ERC settings, Fixed enable/disable instructions, Added expression descriptions

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