Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/ExecExpressions.cs @ 15341

Last change on this file since 15341 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
RevLine 
[14727]1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
[15017]2  using System;
3
[14952]4  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[14727]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>
[14952]16  [StorableClass]
[15032]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,
[15334]22    requiredBlockCount: 2)]
[14727]23  public class ExecIfExpression : StatelessExpression {
[14952]24    public ExecIfExpression() { }
25    [StorableConstructor]
26    protected ExecIfExpression(bool deserializing) : base(deserializing) { }
[14727]27
[14952]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) {
[14727]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>
[15032]41  /// Inserts beneath the top item of the EXEC stack a new item of the form "( EXEC.Y <TopItem> )".
[14727]42  /// </summary>
[15032]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> )\"",
[15334]47    requiredBlockCount: 1)]
[14952]48  [StorableClass]
[14727]49  public class ExecYExpression : StatelessExpression {
[14952]50    public ExecYExpression() { }
51    [StorableConstructor]
52    protected ExecYExpression(bool deserializing) : base(deserializing) { }
[14727]53
[14952]54    public override bool IsNoop(IInternalPushInterpreter interpreter) {
55      return interpreter.ExecStack.Count == 0 ||
[15334]56             interpreter.Configuration.MaxProgramLength < 2 ||
[14952]57             (interpreter.ExecStack.Top.IsProgram && ((PushProgram)interpreter.ExecStack.Top).Depth == interpreter.Configuration.MaxDepth);
58    }
59
60    public override void Eval(IInternalPushInterpreter interpreter) {
[14727]61      var top = interpreter.ExecStack.Top;
[14729]62      var execYExpression = ExpressionTable.GetStatelessExpression<ExecYExpression>();
[14777]63      var expressions = interpreter.PoolContainer.ExpressionListPool.Get();
[14744]64
[14777]65      expressions.Add(top);
66      expressions.Add(execYExpression);
67
68      var result = PushProgram.Create(interpreter.PoolContainer.PushProgramPool, expressions);
69
[15017]70      interpreter.ExecStack.Top = result;
[14727]71      interpreter.ExecStack.Add(top);
72    }
73  }
74
75  /// <summary>
[15032]76  /// Removes the second item on the EXEC stack.
[14727]77  /// </summary>
[15032]78  [PushExpression(
79    StackTypes.Exec,
80    "EXEC.K",
81    "Removes the second item on the EXEC stack.",
[15334]82    requiredBlockCount: 2)]
[14952]83  [StorableClass]
[14727]84  public class ExecKExpression : StatelessExpression {
[14952]85    public ExecKExpression() { }
86    [StorableConstructor]
87    protected ExecKExpression(bool deserializing) : base(deserializing) { }
[14727]88
[14952]89    public override bool IsNoop(IInternalPushInterpreter interpreter) {
90      return interpreter.ExecStack.Count < 2;
91    }
92
93    public override void Eval(IInternalPushInterpreter interpreter) {
[14727]94      var top = interpreter.ExecStack.Pop();
[15017]95      interpreter.ExecStack.Top = top;
[14727]96    }
97  }
98
99  /// <summary>
[15032]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.
[14727]103  /// </summary>
[15032]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.",
[15334]108    requiredBlockCount: 3)]
[14952]109  [StorableClass]
[14727]110  public class ExecSExpression : StatelessExpression {
[14952]111    public ExecSExpression() { }
112    [StorableConstructor]
113    protected ExecSExpression(bool deserializing) : base(deserializing) { }
[14727]114
[14952]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    }
[14777]120
[14952]121    public override void Eval(IInternalPushInterpreter interpreter) {
[14875]122      var a = interpreter.ExecStack.Top;
123      var b = interpreter.ExecStack[1];
124      var c = interpreter.ExecStack[2];
125      interpreter.ExecStack.Remove(2);
[14727]126
[14777]127      var expressions = interpreter.PoolContainer.ExpressionListPool.Get();
128      expressions.Add(c);
129      expressions.Add(b);
[14727]130
[14777]131      var newTop = PushProgram.Create(interpreter.PoolContainer.PushProgramPool, expressions);
132
[15017]133      interpreter.ExecStack.Top = newTop;
[14727]134      interpreter.ExecStack.Push(c, a);
135    }
136  }
[14777]137
138  /// <summary>
[15032]139  /// Does nothing.
[14777]140  /// </summary>
[15017]141  ///
142  [StorableClass]
143  [Serializable]
[15032]144  [PushExpression(
145    StackTypes.Exec,
146    "EXEC.NOOP",
147    "Does nothing.")]
[14777]148  public class ExecNoopExpression : StatelessExpression {
[14952]149    public ExecNoopExpression() { }
150    [StorableConstructor]
151    protected ExecNoopExpression(bool deserializing) : base(deserializing) { }
152
153    public override bool IsNoop(IInternalPushInterpreter interpreter) {
154      return true;
[14777]155    }
[14952]156
157    public override void Eval(IInternalPushInterpreter interpreter) {
158      // do nothing
159    }
[14777]160  }
161
[15032]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.",
[15334]169    StackTypes.Boolean, requiredBlockCount: 1)]
[14952]170  [StorableClass]
[14777]171  public class ExecWhileExpression : StatelessExpression {
[14952]172    public ExecWhileExpression() { }
173    [StorableConstructor]
174    protected ExecWhileExpression(bool deserializing) : base(deserializing) { }
[14777]175
[14952]176    public override bool IsNoop(IInternalPushInterpreter interpreter) {
177      return interpreter.ExecStack.IsEmpty;
178    }
179
180    public override void Eval(IInternalPushInterpreter interpreter) {
[15032]181      if (interpreter.BooleanStack.IsEmpty ||
182          !interpreter.BooleanStack.Pop()) {
[14777]183        interpreter.ExecStack.Pop();
[14952]184        return;
[14777]185      }
186
187      interpreter.ExecStack.Push(this, interpreter.ExecStack.Top);
188    }
189  }
190
[15032]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.",
[15334]198    requiredBlockCount: 1)]
[14952]199  [StorableClass]
[14777]200  public class ExecDoWhileExpression : StatelessExpression {
[14952]201    public ExecDoWhileExpression() { }
202    [StorableConstructor]
203    protected ExecDoWhileExpression(bool deserializing) : base(deserializing) { }
[14777]204
[14952]205    public override bool IsNoop(IInternalPushInterpreter interpreter) {
206      return interpreter.ExecStack.IsEmpty;
207    }
208
209    public override void Eval(IInternalPushInterpreter interpreter) {
[14777]210      interpreter.ExecStack.Push(this, interpreter.ExecStack.Top);
211    }
212  }
213
[15032]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,
[15334]222    requiredBlockCount: 1)]
[14952]223  [StorableClass]
[14777]224  public class ExecWhenExpression : StatelessExpression {
[14952]225    public ExecWhenExpression() { }
226    [StorableConstructor]
227    protected ExecWhenExpression(bool deserializing) : base(deserializing) { }
[14777]228
[14952]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) {
[14777]235      var when = interpreter.BooleanStack.Pop();
236
237      if (!when) {
238        interpreter.ExecStack.Pop();
239      }
240    }
241  }
[14727]242}
Note: See TracBrowser for help on using the repository browser.