Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/VectorIndexOfExpressions.cs @ 18242

Last change on this file since 18242 was 15771, checked in by bburlacu, 7 years ago

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

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