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