Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/VectorReplaceFirstExpressions.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.5 KB
Line 
1using System.Collections.Generic;
2using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
3
4namespace HeuristicLab.Problems.ProgramSynthesis {
5  /// <summary>
6  /// Replace first occurrence 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 VectorReplaceFirstExpression<T> : StatelessExpression {
11    protected VectorReplaceFirstExpression() { }
12    [StorableConstructor]
13    protected VectorReplaceFirstExpression(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 index = -1;
26      for (var i = 0; i < vector.Count && index < 0; i++) {
27        if (vector[i].Equals(second)) {
28          index = i;
29          break;
30        }
31      }
32
33      if (index >= 0) {
34        var result = new List<T>(vector);
35        result[index] = first;
36        vectorStack.Top = result;
37      }
38    }
39  }
40
41  [StorableClass]
42  [PushExpression(
43    StackTypes.IntegerVector,
44    "INTEGER[].REPLACEFIRST",
45    "Replace first occurrence of the second INTEGER with the INTEGER in the top INTEGER[].",
46    StackTypes.Integer)]
47  public class IntegerVectorReplaceFirstExpression : VectorReplaceFirstExpression<long> {
48    public IntegerVectorReplaceFirstExpression() { }
49    [StorableConstructor]
50    protected IntegerVectorReplaceFirstExpression(bool deserializing) : base(deserializing) { }
51
52    public override bool IsNoop(IInternalPushInterpreter interpreter) {
53      return IsNoop(interpreter.IntegerVectorStack, interpreter.IntegerStack);
54    }
55
56    public override void Eval(IInternalPushInterpreter interpreter) {
57      Eval(interpreter.IntegerVectorStack, interpreter.IntegerStack);
58    }
59  }
60
61  [StorableClass]
62  [PushExpression(
63    StackTypes.FloatVector,
64    "FLOAT[].REPLACEFIRST",
65    "Replace first occurrence of the second FLOAT with the FLOAT in the top FLOAT[].",
66    StackTypes.Float)]
67  public class FloatVectorReplaceFirstExpression : VectorReplaceFirstExpression<double> {
68    public FloatVectorReplaceFirstExpression() { }
69    [StorableConstructor]
70    protected FloatVectorReplaceFirstExpression(bool deserializing) : base(deserializing) { }
71
72    public override bool IsNoop(IInternalPushInterpreter interpreter) {
73      return IsNoop(interpreter.FloatVectorStack, interpreter.FloatStack);
74    }
75
76    public override void Eval(IInternalPushInterpreter interpreter) {
77      Eval(interpreter.FloatVectorStack, interpreter.FloatStack);
78    }
79  }
80
81  [StorableClass]
82  [PushExpression(
83    StackTypes.BooleanVector,
84    "BOOLEAN[].REPLACEFIRST",
85    "Replace first occurrence of the second BOOLEAN with the BOOLEAN in the top BOOLEAN[].",
86    StackTypes.Boolean)]
87  public class BooleanVectorReplaceFirstExpression : VectorReplaceFirstExpression<bool> {
88    public BooleanVectorReplaceFirstExpression() { }
89    [StorableConstructor]
90    protected BooleanVectorReplaceFirstExpression(bool deserializing) : base(deserializing) { }
91
92    public override bool IsNoop(IInternalPushInterpreter interpreter) {
93      return IsNoop(interpreter.BooleanVectorStack, interpreter.BooleanStack);
94    }
95
96    public override void Eval(IInternalPushInterpreter interpreter) {
97      Eval(interpreter.BooleanVectorStack, interpreter.BooleanStack);
98    }
99  }
100
101  [StorableClass]
102  [PushExpression(
103    StackTypes.StringVector,
104    "STRING[].REPLACEFIRST",
105    "Replace first occurrence of the second STRING with the STRING in the top STRING[].",
106    StackTypes.String)]
107  public class StringVectorReplaceFirstExpression : VectorReplaceFirstExpression<string> {
108    public StringVectorReplaceFirstExpression() { }
109    [StorableConstructor]
110    protected StringVectorReplaceFirstExpression(bool deserializing) : base(deserializing) { }
111
112    public override bool IsNoop(IInternalPushInterpreter interpreter) {
113      return IsNoop(interpreter.StringVectorStack, interpreter.StringStack);
114    }
115
116    public override void Eval(IInternalPushInterpreter interpreter) {
117      Eval(interpreter.StringVectorStack, interpreter.StringStack);
118    }
119  }
120}
Note: See TracBrowser for help on using the repository browser.