Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2665 Merged ExecExpandExpression and PushProgram due to performance reasons, Tested managed object pooling

File size: 3.2 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
2
3  using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
4  using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
5
6  using Interpreter;
7
8  /// <summary>
9  /// If the top item of the BOOLEAN stack is TRUE then this removes the second item on the EXEC stack, leaving the first
10  /// item to be executed. If it is false then it removes the first item, leaving the second to be executed. This is similar to
11  /// 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
12  /// one item on the BOOLEAN stack.
13  /// </summary>
14  [PushExpression(StackType.Exec, "EXEC.IF")]
15  public class ExecIfExpression : StatelessExpression {
16    public override bool Eval(IPushInterpreter interpreter) {
17      // not enough arguments on stack
18      if ((interpreter.BooleanStack.Count == 0) || (interpreter.ExecStack.Count < 2)) return false;
19
20      var condition = interpreter.BooleanStack.Pop();
21
22      if (condition) interpreter.ExecStack.RemoveAt(interpreter.ExecStack.Count - 2);
23      else interpreter.ExecStack.RemoveTop();
24
25      return true;
26    }
27  }
28
29  /// <summary>
30  ///     Inserts beneath the top item of the EXEC stack a new item of the form
31  ///     "( EXEC.Y <TopItem> )".
32  /// </summary>
33  [PushExpression(StackType.Exec, "EXEC.Y")]
34  public class ExecYExpression : StatelessExpression {
35    public override bool Eval(IPushInterpreter interpreter) {
36      // not enough arguments on stack
37      if (interpreter.ExecStack.Count == 0 ||
38          interpreter.Configuration.MaxPointsInProgram < 2)
39        return false;
40
41      var top = interpreter.ExecStack.Top;
42      var execYExpression = ExpressionTable.GetStatelessExpression<ExecYExpression>();
43
44      var result = new PushProgram(top, execYExpression);
45
46      interpreter.ExecStack.SetTop(result);
47      interpreter.ExecStack.Add(top);
48
49      return true;
50    }
51  }
52
53  /// <summary>
54  ///     Removes the second item on the EXEC stack.
55  /// </summary>
56  [PushExpression(StackType.Exec, "EXEC.K")]
57  public class ExecKExpression : StatelessExpression {
58    public override bool Eval(IPushInterpreter interpreter) {
59      if (interpreter.ExecStack.Count < 2) return false;
60
61      var top = interpreter.ExecStack.Pop();
62      interpreter.ExecStack.SetTop(top);
63
64      return true;
65    }
66  }
67
68  /// <summary>
69  ///     Pops 3 items from the EXEC stack, which we will call A, B, and C
70  ///     (with A being the first one popped). Then pushes a list containing B and C back onto the EXEC stack, followed by
71  ///     another instance of C, followed by another instance of A.
72  /// </summary>
73  [PushExpression(StackType.Exec, "EXEC.S")]
74  public class ExecSExpression : StatelessExpression {
75    public override bool Eval(IPushInterpreter interpreter) {
76      if (interpreter.ExecStack.Count < 3) return false;
77
78      var expression = interpreter.ExecStack.Pop(2);
79      var a = expression[1];
80      var b = expression[0];
81      var c = interpreter.ExecStack.Top;
82
83      var newTop = new PushProgram(c, b);
84
85      interpreter.ExecStack.SetTop(newTop);
86      interpreter.ExecStack.Push(c, a);
87
88      return true;
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.