Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/ExecExpressions.cs @ 16958

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

#2665 Testet Problems, Testet error functions, Small fixes, Created HL files

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