Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/ExecExpressions.cs @ 16003

Last change on this file since 16003 was 15771, checked in by bburlacu, 7 years ago

#2895: Add solution skeleton for PushGP with genealogy analysis.

File size: 8.7 KB
Line 
1using System;
2using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
3
4namespace HeuristicLab.Problems.ProgramSynthesis {
5
6  /// <summary>
7  /// If the top item of the BOOLEAN stack is TRUE then this removes the second item on the EXEC stack, leaving the first
8  /// item to be executed. If it is false then it removes the first item, leaving the second to be executed. This is similar to
9  /// CODE.IF except that it operates on the EXEC stack. This acts as a NOOP unless there are at least two items on the EXEC stack and
10  /// one item on the BOOLEAN stack.
11  /// </summary>
12  [StorableClass]
13  [PushExpression(
14    StackTypes.Exec,
15    "EXEC.IF",
16    "If the top item of the BOOLEAN stack is TRUE then this removes the second item on the EXEC stack, leaving the first item to be executed. If it is false then it removes the first item, leaving the second to be executed.",
17    StackTypes.Boolean,
18    requiredBlockCount: 2)]
19  public class ExecIfExpression : StatelessExpression {
20    public ExecIfExpression() { }
21    [StorableConstructor]
22    protected ExecIfExpression(bool deserializing) : base(deserializing) { }
23
24    public override bool IsNoop(IInternalPushInterpreter interpreter) {
25      return (interpreter.BooleanStack.Count == 0) || (interpreter.ExecStack.Count < 2);
26    }
27
28    public override void Eval(IInternalPushInterpreter interpreter) {
29      var condition = interpreter.BooleanStack.Pop();
30
31      if (condition) interpreter.ExecStack.RemoveAt(interpreter.ExecStack.Count - 2);
32      else interpreter.ExecStack.RemoveTop();
33    }
34  }
35
36  /// <summary>
37  /// Inserts beneath the top item of the EXEC stack a new item of the form "( EXEC.Y <TopItem> )".
38  /// </summary>
39  [PushExpression(
40    StackTypes.Exec,
41    "EXEC.Y",
42    "Inserts beneath the top item of the EXEC stack a new item of the form \"( EXEC.Y <TopItem> )\"",
43    requiredBlockCount: 1)]
44  [StorableClass]
45  public class ExecYExpression : StatelessExpression {
46    public ExecYExpression() { }
47    [StorableConstructor]
48    protected ExecYExpression(bool deserializing) : base(deserializing) { }
49
50    public override bool IsNoop(IInternalPushInterpreter interpreter) {
51      return interpreter.ExecStack.Count == 0 ||
52             interpreter.Configuration.MaxProgramLength < 2 ||
53             (interpreter.ExecStack.Top.IsProgram && ((PushProgram)interpreter.ExecStack.Top).Depth == interpreter.Configuration.MaxDepth);
54    }
55
56    public override void Eval(IInternalPushInterpreter interpreter) {
57      var top = interpreter.ExecStack.Top;
58      var execYExpression = ExpressionTable.GetStatelessExpression<ExecYExpression>();
59      var expressions = interpreter.PoolContainer.ExpressionListPool.Get();
60
61      expressions.Add(top);
62      expressions.Add(execYExpression);
63
64      var result = PushProgram.Create(interpreter.PoolContainer.PushProgramPool, expressions);
65
66      interpreter.ExecStack.Top = result;
67      interpreter.ExecStack.Add(top);
68    }
69  }
70
71  /// <summary>
72  /// Removes the second item on the EXEC stack.
73  /// </summary>
74  [PushExpression(
75    StackTypes.Exec,
76    "EXEC.K",
77    "Removes the second item on the EXEC stack.",
78    requiredBlockCount: 2)]
79  [StorableClass]
80  public class ExecKExpression : StatelessExpression {
81    public ExecKExpression() { }
82    [StorableConstructor]
83    protected ExecKExpression(bool deserializing) : base(deserializing) { }
84
85    public override bool IsNoop(IInternalPushInterpreter interpreter) {
86      return interpreter.ExecStack.Count < 2;
87    }
88
89    public override void Eval(IInternalPushInterpreter interpreter) {
90      var top = interpreter.ExecStack.Pop();
91      interpreter.ExecStack.Top = top;
92    }
93  }
94
95  /// <summary>
96  /// Pops 3 items from the EXEC stack, which we will call A, B, and C
97  /// (with A being the first one popped). Then pushes a list containing B and C back onto the EXEC stack, followed by
98  /// another instance of C, followed by another instance of A.
99  /// </summary>
100  [PushExpression(
101    StackTypes.Exec,
102    "EXEC.S",
103    "Pops 3 items from the EXEC stack called A, B, and C. Then pushes a list containing B and C back onto the EXEC stack, followed by another instance of C, followed by another instance of A.",
104    requiredBlockCount: 3)]
105  [StorableClass]
106  public class ExecSExpression : StatelessExpression {
107    public ExecSExpression() { }
108    [StorableConstructor]
109    protected ExecSExpression(bool deserializing) : base(deserializing) { }
110
111    public override bool IsNoop(IInternalPushInterpreter interpreter) {
112      return interpreter.ExecStack.Count < 3 ||
113            (interpreter.ExecStack.Top is PushProgram && ((PushProgram)interpreter.ExecStack.Top).Depth == interpreter.Configuration.MaxDepth) ||
114            (interpreter.ExecStack[1] is PushProgram && ((PushProgram)interpreter.ExecStack[1]).Depth == interpreter.Configuration.MaxDepth);
115    }
116
117    public override void Eval(IInternalPushInterpreter interpreter) {
118      var a = interpreter.ExecStack.Top;
119      var b = interpreter.ExecStack[1];
120      var c = interpreter.ExecStack[2];
121      interpreter.ExecStack.Remove(2);
122
123      var expressions = interpreter.PoolContainer.ExpressionListPool.Get();
124      expressions.Add(c);
125      expressions.Add(b);
126
127      var newTop = PushProgram.Create(interpreter.PoolContainer.PushProgramPool, expressions);
128
129      interpreter.ExecStack.Top = newTop;
130      interpreter.ExecStack.Push(c, a);
131    }
132  }
133
134  /// <summary>
135  /// Does nothing.
136  /// </summary>
137  ///
138  [StorableClass]
139  [Serializable]
140  [PushExpression(
141    StackTypes.Exec,
142    "EXEC.NOOP",
143    "Does nothing.")]
144  public class ExecNoopExpression : StatelessExpression {
145    public ExecNoopExpression() { }
146    [StorableConstructor]
147    protected ExecNoopExpression(bool deserializing) : base(deserializing) { }
148
149    public override bool IsNoop(IInternalPushInterpreter interpreter) {
150      return true;
151    }
152
153    public override void Eval(IInternalPushInterpreter interpreter) {
154      // do nothing
155    }
156  }
157
158  /// <summary>
159  /// Executes top EXEC item recursively as long top BOOLEAN is TRUE.
160  /// </summary>
161  [PushExpression(
162    StackTypes.Exec,
163    "EXEC.WHILE",
164    "Executes top EXEC item recursively as long top BOOLEAN is TRUE.",
165    StackTypes.Boolean, requiredBlockCount: 1)]
166  [StorableClass]
167  public class ExecWhileExpression : StatelessExpression {
168    public ExecWhileExpression() { }
169    [StorableConstructor]
170    protected ExecWhileExpression(bool deserializing) : base(deserializing) { }
171
172    public override bool IsNoop(IInternalPushInterpreter interpreter) {
173      return interpreter.ExecStack.IsEmpty;
174    }
175
176    public override void Eval(IInternalPushInterpreter interpreter) {
177      if (interpreter.BooleanStack.IsEmpty ||
178          !interpreter.BooleanStack.Pop()) {
179        interpreter.ExecStack.Pop();
180        return;
181      }
182
183      interpreter.ExecStack.Push(this, interpreter.ExecStack.Top);
184    }
185  }
186
187  /// <summary>
188  /// Executes top EXEC item recursively until max. amount of eval. expressions is reached or top EXEC item removes the the recursive call.
189  /// </summary>
190  [PushExpression(
191    StackTypes.Exec,
192    "EXEC.DO*WHILE",
193    "Executes top EXEC item recursively until max. amount of eval. expressions is reached or top EXEC item removes the the recursive call.",
194    requiredBlockCount: 1)]
195  [StorableClass]
196  public class ExecDoWhileExpression : StatelessExpression {
197    public ExecDoWhileExpression() { }
198    [StorableConstructor]
199    protected ExecDoWhileExpression(bool deserializing) : base(deserializing) { }
200
201    public override bool IsNoop(IInternalPushInterpreter interpreter) {
202      return interpreter.ExecStack.IsEmpty;
203    }
204
205    public override void Eval(IInternalPushInterpreter interpreter) {
206      interpreter.ExecStack.Push(this, interpreter.ExecStack.Top);
207    }
208  }
209
210  /// <summary>
211  /// Top EXEC item is only executed, if top BOOLEAN is TRUE.
212  /// </summary>
213  [PushExpression(
214    StackTypes.Exec,
215    "EXEC.WHEN",
216    "Top EXEC item is only executed, if top BOOLEAN is TRUE.",
217    StackTypes.Boolean,
218    requiredBlockCount: 1)]
219  [StorableClass]
220  public class ExecWhenExpression : StatelessExpression {
221    public ExecWhenExpression() { }
222    [StorableConstructor]
223    protected ExecWhenExpression(bool deserializing) : base(deserializing) { }
224
225    public override bool IsNoop(IInternalPushInterpreter interpreter) {
226      return interpreter.ExecStack.IsEmpty ||
227             interpreter.BooleanStack.IsEmpty;
228    }
229
230    public override void Eval(IInternalPushInterpreter interpreter) {
231      var when = interpreter.BooleanStack.Pop();
232
233      if (!when) {
234        interpreter.ExecStack.Pop();
235      }
236    }
237  }
238}
Note: See TracBrowser for help on using the repository browser.