Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/DoCountExpressions.cs @ 18079

Last change on this file since 18079 was 15771, checked in by bburlacu, 6 years ago

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

File size: 4.1 KB
Line 
1using System;
2using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
3
4namespace HeuristicLab.Problems.ProgramSynthesis {
5  /// <summary>
6  /// An iteration instruction that performs a loop (the body of which is taken from the CODE stack) the number of times
7  /// indicated by the INTEGER argument, pushing an index (which runs from zero to one less than the number of iterations) onto the
8  /// INTEGER stack prior to each execution of the loop body. This should be implemented as a macro that expands into a call to
9  /// CODE.DO*RANGE. CODE.DO*COUNT takes a single INTEGER argument (the number of times that the loop will be executed) and a single
10  /// CODE argument (the body of the loop). If the provided INTEGER argument is negative or zero then this becomes a NOOP. Otherwise it
11  /// expands into: ( 0 '1 - IntegerArg>' CODE.QUOTE 'CodeArg' CODE.DO*RANGE )
12  /// </summary>
13  [Serializable]
14  [StorableClass]
15  [PushExpression(
16    StackTypes.Code,
17    "CODE.DO*COUNT",
18    "An iteration instruction that performs a loop (the body of which is taken from the CODE stack) the number of times indicated by the INTEGER argument, pushing an index (which runs from zero to one less than the number of iterations) onto the INTEGER stack prior to each execution of the loop body.",
19    StackTypes.Integer)]
20  public class CodeDoCountExpression : StatelessExpression {
21
22    public CodeDoCountExpression() { }
23    [StorableConstructor]
24    protected CodeDoCountExpression(bool deserializing) : base(deserializing) { }
25    public override void Eval(IInternalPushInterpreter interpreter) {
26      var destinationIndex = interpreter.IntegerStack.Top;
27      interpreter.IntegerStack.Top = 0;
28      interpreter.IntegerStack.Push(destinationIndex);
29
30      interpreter.ExecStack.Push(ExpressionTable.GetStatelessExpression<CodeDoRangeExpression>());
31    }
32
33    public override bool IsNoop(IInternalPushInterpreter interpreter) {
34      return interpreter.IntegerStack.IsEmpty ||
35             interpreter.IntegerStack.Top < 1 ||
36             interpreter.CodeStack.IsEmpty;
37    }
38  }
39
40  /// <summary>
41  /// An iteration instruction that performs a loop (the body of which is taken from the EXEC stack) the number of times
42  /// indicated by the INTEGER argument, pushing an index (which runs from zero to one less than the number of iterations) onto the
43  /// INTEGER stack prior to each execution of the loop body. This is similar to CODE.DO*COUNT except that it takes its code
44  /// argument from the EXEC stack. This should be implemented as a macro that expands into a call to EXEC.DO*RANGE. EXEC.DO*COUNT
45  /// takes a single INTEGER argument (the number of times that the loop will be executed) and a single EXEC argument (the body of the
46  /// loop). If the provided INTEGER argument is negative or zero then this becomes a NOOP. Otherwise it expands into:
47  /// ( 0 '1 - IntegerArg' EXEC.DO* RANGE'ExecArg' )
48  /// </summary>
49  [Serializable]
50  [StorableClass]
51  [PushExpression(
52    StackTypes.Exec,
53    "EXEC.DO*COUNT",
54    "An iteration instruction that performs a loop (the body of which is taken from the EXEC stack) the number of times indicated by the INTEGER argument, pushing an index (which runs from zero to one less than the number of iterations) onto the INTEGER stack prior to each execution of the loop body.",
55    StackTypes.Integer,
56    requiredBlockCount: 1)]
57  public class ExecDoCountExpression : StatelessExpression {
58    public ExecDoCountExpression() { }
59    [StorableConstructor]
60    protected ExecDoCountExpression(bool deserializing) : base(deserializing) { }
61
62    public override void Eval(IInternalPushInterpreter interpreter) {
63      var destinationIndex = interpreter.IntegerStack.Top;
64      interpreter.IntegerStack.Top = 0;
65      interpreter.IntegerStack.Push(destinationIndex);
66
67      interpreter.ExecStack.Push(ExpressionTable.GetStatelessExpression<ExecDoRangeExpression>());
68    }
69
70    public override bool IsNoop(IInternalPushInterpreter interpreter) {
71      return interpreter.IntegerStack.IsEmpty ||
72             interpreter.IntegerStack.Top < 1 ||
73             interpreter.ExecStack.IsEmpty;
74    }
75  }
76}
Note: See TracBrowser for help on using the repository browser.