Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/VectorIndexOfExpressions.cs @ 16958

Last change on this file since 16958 was 15032, checked in by pkimmesw, 8 years ago

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

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