1 | namespace 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 void Eval(IPushInterpreter interpreter) {
|
---|
13 | var name = NameGenerator.RandomName(interpreter.Random);
|
---|
14 |
|
---|
15 | interpreter.NameStack.Push(name);
|
---|
16 | }
|
---|
17 | }
|
---|
18 |
|
---|
19 | /// <summary>
|
---|
20 | /// Pushes a random integer.
|
---|
21 | /// </summary>
|
---|
22 | [PushExpression(StackType.Integer, "INTEGER.RAND")]
|
---|
23 | public class IntegerRandExpression : StatelessExpression {
|
---|
24 | public override void Eval(IPushInterpreter interpreter) {
|
---|
25 | var value = interpreter.Random.Next(
|
---|
26 | interpreter.Configuration.MinRandomInteger,
|
---|
27 | interpreter.Configuration.MaxRandomInteger);
|
---|
28 |
|
---|
29 | interpreter.IntegerStack.Push(value);
|
---|
30 | }
|
---|
31 | }
|
---|
32 |
|
---|
33 | /// <summary>
|
---|
34 | /// Pushes a random float.
|
---|
35 | /// </summary>
|
---|
36 | [PushExpression(StackType.Float, "FLOAT.RAND")]
|
---|
37 | public class FloatRandExpression : StatelessExpression {
|
---|
38 | public override void Eval(IPushInterpreter interpreter) {
|
---|
39 | var value = interpreter.Random.NextDouble() * (interpreter.Configuration.MaxRandomFloat - interpreter.Configuration.MinRandomFloat) + interpreter.Configuration.MinRandomFloat;
|
---|
40 |
|
---|
41 | interpreter.FloatStack.Push(value);
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | /// <summary>
|
---|
46 | /// Pushes a random boolean.
|
---|
47 | /// </summary>
|
---|
48 | [PushExpression(StackType.Boolean, "BOOLEAN.RAND")]
|
---|
49 | public class BooleanRandExpression : StatelessExpression {
|
---|
50 | public override void Eval(IPushInterpreter interpreter) {
|
---|
51 | var value = interpreter.Random.NextDouble() > 0.5;
|
---|
52 |
|
---|
53 | interpreter.BooleanStack.Push(value);
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | /// <summary>
|
---|
58 | /// Pushes random expressions onto the code stack.
|
---|
59 | /// </summary>
|
---|
60 | [PushExpression(StackType.Code, "CODE.RAND")]
|
---|
61 | public class CodeRandExpression : StatelessExpression {
|
---|
62 | public override void Eval(IPushInterpreter interpreter) {
|
---|
63 | if (interpreter.IntegerStack.Count == 0 ||
|
---|
64 | interpreter.IntegerStack.Top < 1) return;
|
---|
65 |
|
---|
66 | var size = (int)(interpreter.IntegerStack.Pop() % interpreter.Configuration.MaxPointsInRandomExpression);
|
---|
67 | var program = CodeGenerator.RandomExpandExpression(size, interpreter.Random, interpreter.Configuration, interpreter.CustomExpressions);
|
---|
68 |
|
---|
69 | interpreter.CodeStack.Push(program);
|
---|
70 | }
|
---|
71 | }
|
---|
72 | } |
---|