Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 8.7 KB
RevLine 
[15771]1using System;
2using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
3
4namespace HeuristicLab.Problems.ProgramSynthesis {
[15017]5
[14727]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>
[14952]12  [StorableClass]
[15032]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,
[15334]18    requiredBlockCount: 2)]
[14727]19  public class ExecIfExpression : StatelessExpression {
[14952]20    public ExecIfExpression() { }
21    [StorableConstructor]
22    protected ExecIfExpression(bool deserializing) : base(deserializing) { }
[14727]23
[14952]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) {
[14727]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>
[15032]37  /// Inserts beneath the top item of the EXEC stack a new item of the form "( EXEC.Y <TopItem> )".
[14727]38  /// </summary>
[15032]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> )\"",
[15334]43    requiredBlockCount: 1)]
[14952]44  [StorableClass]
[14727]45  public class ExecYExpression : StatelessExpression {
[14952]46    public ExecYExpression() { }
47    [StorableConstructor]
48    protected ExecYExpression(bool deserializing) : base(deserializing) { }
[14727]49
[14952]50    public override bool IsNoop(IInternalPushInterpreter interpreter) {
51      return interpreter.ExecStack.Count == 0 ||
[15334]52             interpreter.Configuration.MaxProgramLength < 2 ||
[14952]53             (interpreter.ExecStack.Top.IsProgram && ((PushProgram)interpreter.ExecStack.Top).Depth == interpreter.Configuration.MaxDepth);
54    }
55
56    public override void Eval(IInternalPushInterpreter interpreter) {
[14727]57      var top = interpreter.ExecStack.Top;
[14729]58      var execYExpression = ExpressionTable.GetStatelessExpression<ExecYExpression>();
[14777]59      var expressions = interpreter.PoolContainer.ExpressionListPool.Get();
[14744]60
[14777]61      expressions.Add(top);
62      expressions.Add(execYExpression);
63
64      var result = PushProgram.Create(interpreter.PoolContainer.PushProgramPool, expressions);
65
[15017]66      interpreter.ExecStack.Top = result;
[14727]67      interpreter.ExecStack.Add(top);
68    }
69  }
70
71  /// <summary>
[15032]72  /// Removes the second item on the EXEC stack.
[14727]73  /// </summary>
[15032]74  [PushExpression(
75    StackTypes.Exec,
76    "EXEC.K",
77    "Removes the second item on the EXEC stack.",
[15334]78    requiredBlockCount: 2)]
[14952]79  [StorableClass]
[14727]80  public class ExecKExpression : StatelessExpression {
[14952]81    public ExecKExpression() { }
82    [StorableConstructor]
83    protected ExecKExpression(bool deserializing) : base(deserializing) { }
[14727]84
[14952]85    public override bool IsNoop(IInternalPushInterpreter interpreter) {
86      return interpreter.ExecStack.Count < 2;
87    }
88
89    public override void Eval(IInternalPushInterpreter interpreter) {
[14727]90      var top = interpreter.ExecStack.Pop();
[15017]91      interpreter.ExecStack.Top = top;
[14727]92    }
93  }
94
95  /// <summary>
[15032]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.
[14727]99  /// </summary>
[15032]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.",
[15334]104    requiredBlockCount: 3)]
[14952]105  [StorableClass]
[14727]106  public class ExecSExpression : StatelessExpression {
[14952]107    public ExecSExpression() { }
108    [StorableConstructor]
109    protected ExecSExpression(bool deserializing) : base(deserializing) { }
[14727]110
[14952]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    }
[14777]116
[14952]117    public override void Eval(IInternalPushInterpreter interpreter) {
[14875]118      var a = interpreter.ExecStack.Top;
119      var b = interpreter.ExecStack[1];
120      var c = interpreter.ExecStack[2];
121      interpreter.ExecStack.Remove(2);
[14727]122
[14777]123      var expressions = interpreter.PoolContainer.ExpressionListPool.Get();
124      expressions.Add(c);
125      expressions.Add(b);
[14727]126
[14777]127      var newTop = PushProgram.Create(interpreter.PoolContainer.PushProgramPool, expressions);
128
[15017]129      interpreter.ExecStack.Top = newTop;
[14727]130      interpreter.ExecStack.Push(c, a);
131    }
132  }
[14777]133
134  /// <summary>
[15032]135  /// Does nothing.
[14777]136  /// </summary>
[15017]137  ///
138  [StorableClass]
139  [Serializable]
[15032]140  [PushExpression(
141    StackTypes.Exec,
142    "EXEC.NOOP",
143    "Does nothing.")]
[14777]144  public class ExecNoopExpression : StatelessExpression {
[14952]145    public ExecNoopExpression() { }
146    [StorableConstructor]
147    protected ExecNoopExpression(bool deserializing) : base(deserializing) { }
148
149    public override bool IsNoop(IInternalPushInterpreter interpreter) {
150      return true;
[14777]151    }
[14952]152
153    public override void Eval(IInternalPushInterpreter interpreter) {
154      // do nothing
155    }
[14777]156  }
157
[15032]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.",
[15334]165    StackTypes.Boolean, requiredBlockCount: 1)]
[14952]166  [StorableClass]
[14777]167  public class ExecWhileExpression : StatelessExpression {
[14952]168    public ExecWhileExpression() { }
169    [StorableConstructor]
170    protected ExecWhileExpression(bool deserializing) : base(deserializing) { }
[14777]171
[14952]172    public override bool IsNoop(IInternalPushInterpreter interpreter) {
173      return interpreter.ExecStack.IsEmpty;
174    }
175
176    public override void Eval(IInternalPushInterpreter interpreter) {
[15032]177      if (interpreter.BooleanStack.IsEmpty ||
178          !interpreter.BooleanStack.Pop()) {
[14777]179        interpreter.ExecStack.Pop();
[14952]180        return;
[14777]181      }
182
183      interpreter.ExecStack.Push(this, interpreter.ExecStack.Top);
184    }
185  }
186
[15032]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.",
[15334]194    requiredBlockCount: 1)]
[14952]195  [StorableClass]
[14777]196  public class ExecDoWhileExpression : StatelessExpression {
[14952]197    public ExecDoWhileExpression() { }
198    [StorableConstructor]
199    protected ExecDoWhileExpression(bool deserializing) : base(deserializing) { }
[14777]200
[14952]201    public override bool IsNoop(IInternalPushInterpreter interpreter) {
202      return interpreter.ExecStack.IsEmpty;
203    }
204
205    public override void Eval(IInternalPushInterpreter interpreter) {
[14777]206      interpreter.ExecStack.Push(this, interpreter.ExecStack.Top);
207    }
208  }
209
[15032]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,
[15334]218    requiredBlockCount: 1)]
[14952]219  [StorableClass]
[14777]220  public class ExecWhenExpression : StatelessExpression {
[14952]221    public ExecWhenExpression() { }
222    [StorableConstructor]
223    protected ExecWhenExpression(bool deserializing) : base(deserializing) { }
[14777]224
[14952]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) {
[14777]231      var when = interpreter.BooleanStack.Pop();
232
233      if (!when) {
234        interpreter.ExecStack.Pop();
235      }
236    }
237  }
[14727]238}
Note: See TracBrowser for help on using the repository browser.