Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/VectorReplaceExpressions.cs @ 17021

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

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

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