Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/VectorNthExpressions.cs @ 17133

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

#2665 Fixed bias 0 issue, PushExpressionFrequencyAnalyzer, Fixed probability for ERC settings, Fixed enable/disable instructions, Added expression descriptions

File size: 4.6 KB
Line 
1using System.Collections.Generic;
2
3namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
4
5  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
6  using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
7  using HeuristicLab.Problems.ProgramSynthesis.Push.Extensions;
8  using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
9  using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
10
11  /// <summary>
12  /// Gets the nth item from the type stack
13  /// </summary>
14  /// <typeparam name="T"></typeparam>
15  [StorableClass]
16  public abstract class VectorNthExpression<T> : StatelessExpression {
17    protected VectorNthExpression() { }
18    [StorableConstructor]
19    protected VectorNthExpression(bool deserializing) : base(deserializing) { }
20
21    protected bool IsNoop(IInternalPushInterpreter interpreter, IPushStack<IReadOnlyList<T>> vectorStack) {
22      return vectorStack.IsEmpty ||
23             vectorStack.Top.Count == 0 ||
24             interpreter.IntegerStack.IsEmpty;
25    }
26
27    protected void Eval(
28      IInternalPushInterpreter interpreter,
29      IPushStack<IReadOnlyList<T>> vectorStack,
30      IPushStack<T> literalStack) {
31      var vector = vectorStack.Pop();
32
33      if (literalStack == interpreter.IntegerStack) {
34        var n = interpreter.IntegerStack.Top.AsInt(vector.Count);
35        literalStack.Top = vector[n];
36      } else {
37        var n = interpreter.IntegerStack.Pop().AsInt(vector.Count);
38        literalStack.Push(vector[n]);
39      }
40    }
41  }
42
43  [StorableClass]
44  [PushExpression(
45    StackTypes.IntegerVector,
46    "INTEGER[].NTH",
47    "Pushes the nth item from the top INTEGER[] onto the INTEGER stack, whereby n is taken from the INTEGER stack.",
48    StackTypes.Integer)]
49  public class IntegerVectorNthExpression : VectorNthExpression<long> {
50    public IntegerVectorNthExpression() { }
51    [StorableConstructor]
52    protected IntegerVectorNthExpression(bool deserializing) : base(deserializing) { }
53
54    public override bool IsNoop(IInternalPushInterpreter interpreter) {
55      return IsNoop(interpreter, interpreter.IntegerVectorStack);
56    }
57
58    public override void Eval(IInternalPushInterpreter interpreter) {
59      Eval(interpreter, interpreter.IntegerVectorStack, interpreter.IntegerStack);
60    }
61  }
62
63  [StorableClass]
64  [PushExpression(
65    StackTypes.FloatVector,
66    "FLOAT[].NTH",
67    "Pushes the nth item from the top FLOAT[] onto the FLOAT stack, whereby n is taken from the INTEGER stack.",
68    StackTypes.Float | StackTypes.Integer)]
69  public class FloatVectorNthExpression : VectorNthExpression<double> {
70    public FloatVectorNthExpression() { }
71    [StorableConstructor]
72    protected FloatVectorNthExpression(bool deserializing) : base(deserializing) { }
73
74    public override bool IsNoop(IInternalPushInterpreter interpreter) {
75      return IsNoop(interpreter, interpreter.FloatVectorStack);
76    }
77
78    public override void Eval(IInternalPushInterpreter interpreter) {
79      Eval(interpreter, interpreter.FloatVectorStack, interpreter.FloatStack);
80    }
81  }
82
83  [StorableClass]
84  [PushExpression(
85    StackTypes.BooleanVector,
86    "BOOLEAN[].NTH",
87    "Pushes the nth item from the top BOOLEAN[] onto the BOOLEAN stack, whereby n is taken from the INTEGER stack.",
88    StackTypes.Boolean | StackTypes.Integer)]
89  public class BooleanVectorNthExpression : VectorNthExpression<bool> {
90    public BooleanVectorNthExpression() { }
91    [StorableConstructor]
92    protected BooleanVectorNthExpression(bool deserializing) : base(deserializing) { }
93
94    public override bool IsNoop(IInternalPushInterpreter interpreter) {
95      return IsNoop(interpreter, interpreter.BooleanVectorStack);
96    }
97
98    public override void Eval(IInternalPushInterpreter interpreter) {
99      Eval(interpreter, interpreter.BooleanVectorStack, interpreter.BooleanStack);
100    }
101  }
102
103  [StorableClass]
104  [PushExpression(
105    StackTypes.StringVector,
106    "STRING[].NTH",
107    "Pushes the nth item from the top STRING[] onto the BOOLEAN stack, whereby n is taken from the INTEGER stack.",
108    StackTypes.String | StackTypes.Integer)]
109  public class StringVectorNthExpression : VectorNthExpression<string> {
110    public StringVectorNthExpression() { }
111    [StorableConstructor]
112    protected StringVectorNthExpression(bool deserializing) : base(deserializing) { }
113
114    public override bool IsNoop(IInternalPushInterpreter interpreter) {
115      return IsNoop(interpreter, interpreter.StringVectorStack);
116    }
117
118    public override void Eval(IInternalPushInterpreter interpreter) {
119      Eval(interpreter, interpreter.StringVectorStack, interpreter.StringStack);
120    }
121  }
122}
Note: See TracBrowser for help on using the repository browser.