Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2665 Fixed Benchmark Problem Definition, Converted LoopExpressions to stateless expressions, Added several unit test to ensure funcionality, Fixed UI bugs

File size: 7.6 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(StackTypes.Exec, "EXEC.IF", StackTypes.Boolean, execIn: 2)]
18  public class ExecIfExpression : StatelessExpression {
19    public ExecIfExpression() { }
20    [StorableConstructor]
21    protected ExecIfExpression(bool deserializing) : base(deserializing) { }
22
23    public override bool IsNoop(IInternalPushInterpreter interpreter) {
24      return (interpreter.BooleanStack.Count == 0) || (interpreter.ExecStack.Count < 2);
25    }
26
27    public override void Eval(IInternalPushInterpreter interpreter) {
28      var condition = interpreter.BooleanStack.Pop();
29
30      if (condition) interpreter.ExecStack.RemoveAt(interpreter.ExecStack.Count - 2);
31      else interpreter.ExecStack.RemoveTop();
32    }
33  }
34
35  /// <summary>
36  ///     Inserts beneath the top item of the EXEC stack a new item of the form
37  ///     "( EXEC.Y <TopItem> )".
38  /// </summary>
39  [PushExpression(StackTypes.Exec, "EXEC.Y", execIn: 1)]
40  [StorableClass]
41  public class ExecYExpression : StatelessExpression {
42    public ExecYExpression() { }
43    [StorableConstructor]
44    protected ExecYExpression(bool deserializing) : base(deserializing) { }
45
46    public override bool IsNoop(IInternalPushInterpreter interpreter) {
47      return interpreter.ExecStack.Count == 0 ||
48             interpreter.Configuration.MaxPointsInProgram < 2 ||
49             (interpreter.ExecStack.Top.IsProgram && ((PushProgram)interpreter.ExecStack.Top).Depth == interpreter.Configuration.MaxDepth);
50    }
51
52    public override void Eval(IInternalPushInterpreter interpreter) {
53      var top = interpreter.ExecStack.Top;
54      var execYExpression = ExpressionTable.GetStatelessExpression<ExecYExpression>();
55      var expressions = interpreter.PoolContainer.ExpressionListPool.Get();
56
57      expressions.Add(top);
58      expressions.Add(execYExpression);
59
60      var result = PushProgram.Create(interpreter.PoolContainer.PushProgramPool, expressions);
61
62      interpreter.ExecStack.Top = result;
63      interpreter.ExecStack.Add(top);
64    }
65  }
66
67  /// <summary>
68  ///     Removes the second item on the EXEC stack.
69  /// </summary>
70  [PushExpression(StackTypes.Exec, "EXEC.K", execIn: 2)]
71  [StorableClass]
72  public class ExecKExpression : StatelessExpression {
73    public ExecKExpression() { }
74    [StorableConstructor]
75    protected ExecKExpression(bool deserializing) : base(deserializing) { }
76
77    public override bool IsNoop(IInternalPushInterpreter interpreter) {
78      return interpreter.ExecStack.Count < 2;
79    }
80
81    public override void Eval(IInternalPushInterpreter interpreter) {
82      var top = interpreter.ExecStack.Pop();
83      interpreter.ExecStack.Top = top;
84    }
85  }
86
87  /// <summary>
88  ///     Pops 3 items from the EXEC stack, which we will call A, B, and C
89  ///     (with A being the first one popped). Then pushes a list containing B and C back onto the EXEC stack, followed by
90  ///     another instance of C, followed by another instance of A.
91  /// </summary>
92  [PushExpression(StackTypes.Exec, "EXEC.S", execIn: 3)]
93  [StorableClass]
94  public class ExecSExpression : StatelessExpression {
95    public ExecSExpression() { }
96    [StorableConstructor]
97    protected ExecSExpression(bool deserializing) : base(deserializing) { }
98
99    public override bool IsNoop(IInternalPushInterpreter interpreter) {
100      return interpreter.ExecStack.Count < 3 ||
101            (interpreter.ExecStack.Top is PushProgram && ((PushProgram)interpreter.ExecStack.Top).Depth == interpreter.Configuration.MaxDepth) ||
102            (interpreter.ExecStack[1] is PushProgram && ((PushProgram)interpreter.ExecStack[1]).Depth == interpreter.Configuration.MaxDepth);
103    }
104
105    public override void Eval(IInternalPushInterpreter interpreter) {
106      var a = interpreter.ExecStack.Top;
107      var b = interpreter.ExecStack[1];
108      var c = interpreter.ExecStack[2];
109      interpreter.ExecStack.Remove(2);
110
111      var expressions = interpreter.PoolContainer.ExpressionListPool.Get();
112      expressions.Add(c);
113      expressions.Add(b);
114
115      var newTop = PushProgram.Create(interpreter.PoolContainer.PushProgramPool, expressions);
116
117      interpreter.ExecStack.Top = newTop;
118      interpreter.ExecStack.Push(c, a);
119    }
120  }
121
122  /// <summary>
123  ///     Does nothing.
124  /// </summary>
125  ///
126  [StorableClass]
127  [Serializable]
128  [PushExpression(StackTypes.Exec, "EXEC.NOOP")]
129  public class ExecNoopExpression : StatelessExpression {
130    public ExecNoopExpression() { }
131    [StorableConstructor]
132    protected ExecNoopExpression(bool deserializing) : base(deserializing) { }
133
134    public override bool IsNoop(IInternalPushInterpreter interpreter) {
135      return true;
136    }
137
138    public override void Eval(IInternalPushInterpreter interpreter) {
139      // do nothing
140    }
141  }
142
143  [PushExpression(StackTypes.Exec, "EXEC.WHILE", StackTypes.Boolean, execIn: 1)]
144  [StorableClass]
145  public class ExecWhileExpression : StatelessExpression {
146    public ExecWhileExpression() { }
147    [StorableConstructor]
148    protected ExecWhileExpression(bool deserializing) : base(deserializing) { }
149
150    public override bool IsNoop(IInternalPushInterpreter interpreter) {
151      return interpreter.ExecStack.IsEmpty;
152    }
153
154    public override void Eval(IInternalPushInterpreter interpreter) {
155      if (interpreter.BooleanStack.IsEmpty) {
156        interpreter.ExecStack.Pop();
157        return;
158      }
159
160      var booleanTop = interpreter.BooleanStack.Pop();
161
162      if (!booleanTop) {
163        interpreter.ExecStack.Pop();
164        return;
165      }
166
167      interpreter.ExecStack.Push(this, interpreter.ExecStack.Top);
168    }
169  }
170
171  [PushExpression(StackTypes.Exec, "EXEC.DO*WHILE", execIn: 1)]
172  [StorableClass]
173  public class ExecDoWhileExpression : StatelessExpression {
174    public ExecDoWhileExpression() { }
175    [StorableConstructor]
176    protected ExecDoWhileExpression(bool deserializing) : base(deserializing) { }
177
178    public override bool IsNoop(IInternalPushInterpreter interpreter) {
179      return interpreter.ExecStack.IsEmpty;
180    }
181
182    public override void Eval(IInternalPushInterpreter interpreter) {
183      interpreter.ExecStack.Push(this, interpreter.ExecStack.Top);
184    }
185  }
186
187  [PushExpression(StackTypes.Exec, "EXEC.WHEN", StackTypes.Boolean, execIn: 1)]
188  [StorableClass]
189  public class ExecWhenExpression : StatelessExpression {
190    public ExecWhenExpression() { }
191    [StorableConstructor]
192    protected ExecWhenExpression(bool deserializing) : base(deserializing) { }
193
194    public override bool IsNoop(IInternalPushInterpreter interpreter) {
195      return interpreter.ExecStack.IsEmpty ||
196             interpreter.BooleanStack.IsEmpty;
197    }
198
199    public override void Eval(IInternalPushInterpreter interpreter) {
200      var when = interpreter.BooleanStack.Pop();
201
202      if (!when) {
203        interpreter.ExecStack.Pop();
204      }
205    }
206  }
207}
Note: See TracBrowser for help on using the repository browser.