Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/VectorReplaceExpressions.cs @ 17460

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