1 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
|
---|
2 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
3 | using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
|
---|
4 | using HeuristicLab.Problems.ProgramSynthesis.Push.Generators.CodeGenerator;
|
---|
5 | using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
|
---|
6 | using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
|
---|
7 |
|
---|
8 | /// <summary>
|
---|
9 | /// Pushes a random NAME.
|
---|
10 | /// </summary>
|
---|
11 | [PushExpression(StackTypes.Name, "NAME.RAND")]
|
---|
12 | [StorableClass]
|
---|
13 | public class NameRandExpression : StatelessExpression {
|
---|
14 | public NameRandExpression() { }
|
---|
15 | [StorableConstructor]
|
---|
16 | protected NameRandExpression(bool deserializing) : base(deserializing) { }
|
---|
17 |
|
---|
18 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
19 | return !interpreter.Configuration.ErcOptions.NameErcOptions.IsEnabled;
|
---|
20 | }
|
---|
21 |
|
---|
22 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
23 | var name = interpreter.Configuration.ErcOptions.NameErcOptions.GetErcValue(interpreter.Random);
|
---|
24 | interpreter.NameStack.Push(name);
|
---|
25 | }
|
---|
26 | }
|
---|
27 |
|
---|
28 | /// <summary>
|
---|
29 | /// Pushes a random integer.
|
---|
30 | /// </summary>
|
---|
31 | [PushExpression(StackTypes.Integer, "INTEGER.RAND")]
|
---|
32 | [StorableClass]
|
---|
33 | public class IntegerRandExpression : StatelessExpression {
|
---|
34 | public IntegerRandExpression() { }
|
---|
35 | [StorableConstructor]
|
---|
36 | protected IntegerRandExpression(bool deserializing) : base(deserializing) { }
|
---|
37 |
|
---|
38 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
39 | return !interpreter.Configuration.ErcOptions.IntegerErcOptions.IsEnabled;
|
---|
40 | }
|
---|
41 |
|
---|
42 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
43 | var value = interpreter.Configuration.ErcOptions.IntegerErcOptions.GetErcValue(interpreter.Random);
|
---|
44 | interpreter.IntegerStack.Push(value);
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | /// <summary>
|
---|
49 | /// Pushes a random float.
|
---|
50 | /// </summary>
|
---|
51 | [PushExpression(StackTypes.Float, "FLOAT.RAND")]
|
---|
52 | [StorableClass]
|
---|
53 | public class FloatRandExpression : StatelessExpression {
|
---|
54 | public FloatRandExpression() { }
|
---|
55 | [StorableConstructor]
|
---|
56 | protected FloatRandExpression(bool deserializing) : base(deserializing) { }
|
---|
57 |
|
---|
58 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
59 | return !interpreter.Configuration.ErcOptions.FloatErcOptions.IsEnabled;
|
---|
60 | }
|
---|
61 |
|
---|
62 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
63 | var value = interpreter.Configuration.ErcOptions.FloatErcOptions.GetErcValue(interpreter.Random);
|
---|
64 | interpreter.FloatStack.Push(value);
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | /// <summary>
|
---|
69 | /// Pushes a random boolean.
|
---|
70 | /// </summary>
|
---|
71 | [PushExpression(StackTypes.Boolean, "BOOLEAN.RAND")]
|
---|
72 | [StorableClass]
|
---|
73 | public class BooleanRandExpression : StatelessExpression {
|
---|
74 | public BooleanRandExpression() { }
|
---|
75 | [StorableConstructor]
|
---|
76 | protected BooleanRandExpression(bool deserializing) : base(deserializing) { }
|
---|
77 |
|
---|
78 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
79 | return !interpreter.Configuration.ErcOptions.BooleanErcOptions.IsEnabled;
|
---|
80 | }
|
---|
81 |
|
---|
82 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
83 | var value = interpreter.Configuration.ErcOptions.BooleanErcOptions.GetErcValue(interpreter.Random);
|
---|
84 | interpreter.BooleanStack.Push(value);
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | /// <summary>
|
---|
89 | /// Pushes random expressions onto the code stack.
|
---|
90 | /// </summary>
|
---|
91 | [PushExpression(StackTypes.Code, "CODE.RAND")]
|
---|
92 | [StorableClass]
|
---|
93 | public class CodeRandExpression : StatelessExpression {
|
---|
94 | public CodeRandExpression() { }
|
---|
95 | [StorableConstructor]
|
---|
96 | protected CodeRandExpression(bool deserializing) : base(deserializing) { }
|
---|
97 |
|
---|
98 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
99 | return interpreter.IntegerStack.Count == 0 || interpreter.IntegerStack.Top < 1;
|
---|
100 | }
|
---|
101 |
|
---|
102 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
103 | var size = (int)(interpreter.IntegerStack.Pop() % interpreter.Configuration.MaxPointsInRandomExpression);
|
---|
104 | var program = LinearCodeGenerator.RandomProgram(
|
---|
105 | interpreter.PoolContainer.PushProgramPool,
|
---|
106 | interpreter.PoolContainer.ExpressionListPool,
|
---|
107 | size,
|
---|
108 | interpreter.Random,
|
---|
109 | interpreter.Configuration,
|
---|
110 | interpreter.CustomExpressions);
|
---|
111 |
|
---|
112 | interpreter.CodeStack.Push(program);
|
---|
113 | }
|
---|
114 | }
|
---|
115 | } |
---|