Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2665 PooledPushProgram reduces memory usage and increases performance

File size: 3.3 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      var result = PushProgram.Create(interpreter.PushProgramPool, top, execYExpression);
44
45      interpreter.ExecStack.SetTop(result);
46      interpreter.ExecStack.Add(top);
47
48      return true;
49    }
50  }
51
52  /// <summary>
53  ///     Removes the second item on the EXEC stack.
54  /// </summary>
55  [PushExpression(StackType.Exec, "EXEC.K")]
56  public class ExecKExpression : StatelessExpression {
57    public override bool Eval(IPushInterpreter interpreter) {
58      if (interpreter.ExecStack.Count < 2) return false;
59
60      var top = interpreter.ExecStack.Pop();
61      interpreter.ExecStack.SetTop(top);
62
63      return true;
64    }
65  }
66
67  /// <summary>
68  ///     Pops 3 items from the EXEC stack, which we will call A, B, and C
69  ///     (with A being the first one popped). Then pushes a list containing B and C back onto the EXEC stack, followed by
70  ///     another instance of C, followed by another instance of A.
71  /// </summary>
72  [PushExpression(StackType.Exec, "EXEC.S")]
73  public class ExecSExpression : StatelessExpression {
74    public override bool Eval(IPushInterpreter interpreter) {
75      if (interpreter.ExecStack.Count < 3) return false;
76
77      var expression = interpreter.ExecStack.Pop(2);
78      var a = expression[1];
79      var b = expression[0];
80      var c = interpreter.ExecStack.Top;
81
82      var newTop = PushProgram.Create(interpreter.PushProgramPool, c, b);
83
84      interpreter.ExecStack.SetTop(newTop);
85      interpreter.ExecStack.Push(c, a);
86
87      return true;
88    }
89  }
90}
Note: See TracBrowser for help on using the repository browser.