Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/RandExpressions.cs @ 14834

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

#2665 LexicaseSelector, Performance improvements, UI Fixes, Debugger only shows used stacks, fixed Debugger stepping, Added vector expressions, ERCOptions,

File size: 3.7 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
2  using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
3  using HeuristicLab.Problems.ProgramSynthesis.Push.Generators;
4  using HeuristicLab.Problems.ProgramSynthesis.Push.Generators.CodeGenerator;
5  using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
6  using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
7  using HeuristicLab.Random;
8
9  /// <summary>
10  ///     Pushes a random NAME.
11  /// </summary>
12  [PushExpression(StackTypes.Name, "NAME.RAND")]
13  public class NameRandExpression : StatelessExpression {
14    public override bool Eval(IInternalPushInterpreter interpreter) {
15      if (!interpreter.Configuration.ErcOptions.NameErcOptions.IsEnabled)
16        return false;
17
18      var name = interpreter.CustomExpressions.Count == 0 || interpreter.Random.NextDouble() < interpreter.Configuration.ErcOptions.NameErcOptions.NewNameProbability
19                   ? ErcUtils.GetName(interpreter.Configuration.ErcOptions.NameErcOptions, interpreter.Random)
20                   : interpreter.CustomExpressions.Keys.SampleRandom(interpreter.Random);
21
22      interpreter.NameStack.Push(name);
23      return true;
24    }
25  }
26
27  /// <summary>
28  ///     Pushes a random integer.
29  /// </summary>
30  [PushExpression(StackTypes.Integer, "INTEGER.RAND")]
31  public class IntegerRandExpression : StatelessExpression {
32    public override bool Eval(IInternalPushInterpreter interpreter) {
33      if (!interpreter.Configuration.ErcOptions.IntegerErcOptions.IsEnabled)
34        return false;
35
36      var value = ErcUtils.GetInteger(interpreter.Configuration.ErcOptions.IntegerErcOptions, interpreter.Random);
37      interpreter.IntegerStack.Push(value);
38      return true;
39    }
40  }
41
42  /// <summary>
43  ///     Pushes a random float.
44  /// </summary>
45  [PushExpression(StackTypes.Float, "FLOAT.RAND")]
46  public class FloatRandExpression : StatelessExpression {
47    public override bool Eval(IInternalPushInterpreter interpreter) {
48      if (!interpreter.Configuration.ErcOptions.FloatErcOptions.IsEnabled)
49        return false;
50
51      var value = ErcUtils.GetDouble(interpreter.Configuration.ErcOptions.FloatErcOptions, interpreter.Random);
52      interpreter.FloatStack.Push(value);
53      return true;
54    }
55  }
56
57  /// <summary>
58  ///     Pushes a random boolean.
59  /// </summary>
60  [PushExpression(StackTypes.Boolean, "BOOLEAN.RAND")]
61  public class BooleanRandExpression : StatelessExpression {
62    public override bool Eval(IInternalPushInterpreter interpreter) {
63      if (!interpreter.Configuration.ErcOptions.BooleanErcOptions.IsEnabled)
64        return false;
65
66      var value = ErcUtils.GetBoolean(interpreter.Configuration.ErcOptions.BooleanErcOptions, interpreter.Random);
67      interpreter.BooleanStack.Push(value);
68      return true;
69    }
70  }
71
72  /// <summary>
73  ///     Pushes random expressions onto the code stack.
74  /// </summary>
75  [PushExpression(StackTypes.Code, "CODE.RAND")]
76  public class CodeRandExpression : StatelessExpression {
77    public override bool Eval(IInternalPushInterpreter interpreter) {
78      if (interpreter.IntegerStack.Count == 0 ||
79          interpreter.IntegerStack.Top < 1) return false;
80
81      var size = (int)(interpreter.IntegerStack.Pop() % interpreter.Configuration.MaxPointsInRandomExpression);
82      var program = LinearCodeGenerator.RandomProgram(
83        interpreter.PoolContainer.PushProgramPool,
84        interpreter.PoolContainer.ExpressionListPool,
85        size,
86        interpreter.Random,
87        interpreter.Configuration,
88        interpreter.CustomExpressions);
89
90      interpreter.CodeStack.Push(program);
91      return true;
92    }
93  }
94}
Note: See TracBrowser for help on using the repository browser.