Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2665 PooledPushProgram reduces memory usage and increases performance

File size: 2.8 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.Interpreter;
5  using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
6
7  /// <summary>
8  ///     Pushes a random NAME.
9  /// </summary>
10  [PushExpression(StackType.Name, "NAME.RAND")]
11  public class NameRandExpression : StatelessExpression {
12    public override bool Eval(IPushInterpreter interpreter) {
13      var name = NameGenerator.RandomName(interpreter.Random);
14
15      interpreter.NameStack.Push(name);
16      return true;
17    }
18  }
19
20  /// <summary>
21  ///     Pushes a random integer.
22  /// </summary>
23  [PushExpression(StackType.Integer, "INTEGER.RAND")]
24  public class IntegerRandExpression : StatelessExpression {
25    public override bool Eval(IPushInterpreter interpreter) {
26      var value = interpreter.Random.Next(
27          interpreter.Configuration.MinRandomInteger,
28          interpreter.Configuration.MaxRandomInteger);
29
30      interpreter.IntegerStack.Push(value);
31      return true;
32    }
33  }
34
35  /// <summary>
36  ///     Pushes a random float.
37  /// </summary>
38  [PushExpression(StackType.Float, "FLOAT.RAND")]
39  public class FloatRandExpression : StatelessExpression {
40    public override bool Eval(IPushInterpreter interpreter) {
41      var value = interpreter.Random.NextDouble() * (interpreter.Configuration.MaxRandomFloat - interpreter.Configuration.MinRandomFloat) + interpreter.Configuration.MinRandomFloat;
42
43      interpreter.FloatStack.Push(value);
44      return true;
45    }
46  }
47
48  /// <summary>
49  ///     Pushes a random boolean.
50  /// </summary>
51  [PushExpression(StackType.Boolean, "BOOLEAN.RAND")]
52  public class BooleanRandExpression : StatelessExpression {
53    public override bool Eval(IPushInterpreter interpreter) {
54      var value = interpreter.Random.NextDouble() > 0.5;
55
56      interpreter.BooleanStack.Push(value);
57      return true;
58    }
59  }
60
61  /// <summary>
62  ///     Pushes random expressions onto the code stack.
63  /// </summary>
64  [PushExpression(StackType.Code, "CODE.RAND")]
65  public class CodeRandExpression : StatelessExpression {
66    public override bool Eval(IPushInterpreter interpreter) {
67      if (interpreter.IntegerStack.Count == 0 ||
68          interpreter.IntegerStack.Top < 1) return false;
69
70      var size = (int)(interpreter.IntegerStack.Pop() % interpreter.Configuration.MaxPointsInRandomExpression);
71      var program = CodeGenerator.RandomProgram(
72        interpreter.PushProgramPool,
73        size,
74        interpreter.Random,
75        interpreter.Configuration,
76        interpreter.CustomExpressions);
77
78      interpreter.CodeStack.Push(program);
79      return true;
80    }
81  }
82}
Note: See TracBrowser for help on using the repository browser.