Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/DoTimesExpressions.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: 4.6 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
2  using System;
3  using Attributes;
4  using Interpreter;
5  using Persistence.Default.CompositeSerializers.Storable;
6  using Stack;
7
8  /// <summary>
9  ///     Like CODE.DO*COUNT but does not push the loop counter.
10  ///     This should be implemented as a macro that expands into CODE.DO*RANGE, similarly to the implementation
11  ///     of CODE.DO*COUNT, except that a call to INTEGER.POP should be tacked on to the front of the loop body code
12  ///     in the call to CODE.DO*RANGE.
13  /// </summary>
14  [Serializable]
15  [StorableClass]
16  [PushExpression(StackTypes.Code, "CODE.DO*TIMES", StackTypes.Integer)]
17  public class CodeDoTimesExpression : StatelessExpression {
18    public CodeDoTimesExpression() { }
19    [StorableConstructor]
20    protected CodeDoTimesExpression(bool deserializing) : base(deserializing) { }
21
22    public override void Eval(IInternalPushInterpreter interpreter) {
23      var destinationIndex = Math.Max(0, interpreter.IntegerStack.Top - 1);
24
25      if (destinationIndex == 0) {
26        interpreter.IntegerStack.Pop();
27        interpreter.ExecStack.Push((PushProgram)interpreter.CodeStack.Pop());
28        return;
29      }
30
31      interpreter.IntegerStack.Top = 0;
32      interpreter.IntegerStack.Push(destinationIndex);
33
34      var popExpressions = ExpressionTable.GetStatelessExpression<IntegerPopExpression>();
35      PushProgram program;
36
37      if (!interpreter.CodeStack.Top.IsProgram) {
38        var expressions = interpreter.PoolContainer.ExpressionListPool.Get();
39        expressions.Add(interpreter.ExecStack.Top);
40        expressions.Add(popExpressions);
41
42        program = PushProgram.Create(interpreter.PoolContainer.PushProgramPool, expressions);
43      } else {
44        program = PushProgram.Merge(
45          interpreter.PoolContainer.PushProgramPool,
46          interpreter.PoolContainer.ExpressionListPool,
47          (PushProgram)interpreter.CodeStack.Top,
48          popExpressions);
49      }
50
51      interpreter.CodeStack.Top = program;
52      interpreter.ExecStack.Push(ExpressionTable.GetStatelessExpression<CodeDoRangeExpression>());
53    }
54
55    public override bool IsNoop(IInternalPushInterpreter interpreter) {
56      return interpreter.IntegerStack.IsEmpty ||
57             interpreter.IntegerStack.Top < 0 ||
58             interpreter.CodeStack.IsEmpty;
59    }
60  }
61
62  /// <summary>
63  ///     Like EXEC.DO*COUNT but does not push the loop counter. This should be implemented as a macro that expands into
64  ///     EXEC.DO*RANGE,
65  ///     similarly to the implementation of EXEC.DO*COUNT, except that a call to INTEGER.POP should be tacked on to the
66  ///     front of the
67  ///     loop body code in the call to EXEC.DO*RANGE. This call to INTEGER.POP will remove the loop counter, which will have
68  ///     been
69  ///     pushed by EXEC.DO*RANGE, prior to the execution of the loop body.
70  /// </summary>
71  [Serializable]
72  [StorableClass]
73  [PushExpression(StackTypes.Exec, "EXEC.DO*TIMES", StackTypes.Integer, execIn: 1)]
74  public class ExecDoTimesExpression : StatelessExpression {
75    public ExecDoTimesExpression() { }
76    [StorableConstructor]
77    protected ExecDoTimesExpression(bool deserializing) : base(deserializing) { }
78
79    public override void Eval(IInternalPushInterpreter interpreter) {
80      var destinationIndex = Math.Max(0, interpreter.IntegerStack.Top - 1);
81
82      if (destinationIndex == 0) {
83        interpreter.IntegerStack.Pop();
84        return;
85      }
86
87      interpreter.IntegerStack.Top = 0;
88      interpreter.IntegerStack.Push(destinationIndex);
89
90      var popExpressions = ExpressionTable.GetStatelessExpression<IntegerPopExpression>();
91      PushProgram program;
92
93      if (!interpreter.ExecStack.Top.IsProgram) {
94        var expressions = interpreter.PoolContainer.ExpressionListPool.Get();
95        expressions.Add(interpreter.ExecStack.Top);
96        expressions.Add(popExpressions);
97
98        program = PushProgram.Create(interpreter.PoolContainer.PushProgramPool, expressions);
99      } else {
100        program = PushProgram.Merge(
101          interpreter.PoolContainer.PushProgramPool,
102          interpreter.PoolContainer.ExpressionListPool,
103          (PushProgram)interpreter.ExecStack.Top,
104          popExpressions);
105      }
106
107      interpreter.ExecStack.Top = program;
108      interpreter.ExecStack.Push(ExpressionTable.GetStatelessExpression<ExecDoRangeExpression>());
109    }
110
111    public override bool IsNoop(IInternalPushInterpreter interpreter) {
112      return interpreter.IntegerStack.IsEmpty ||
113             interpreter.IntegerStack.Top < 0 ||
114             interpreter.ExecStack.IsEmpty;
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.