Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/VectorSetExpressions.cs

Last change on this file was 15032, checked in by pkimmesw, 7 years ago

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

File size: 5.2 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.Extensions;
7  using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
8  using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
9
10  /// <summary>
11  /// Replaces, in the top type vector, item at index(from integer stack) with the first lit-type item.
12  /// </summary>
13  /// <typeparam name="T"></typeparam>
14  [StorableClass]
15  public abstract class VectorSetExpression<T> : StatelessExpression {
16    protected VectorSetExpression() { }
17    [StorableConstructor]
18    protected VectorSetExpression(bool deserializing) : base(deserializing) { }
19
20    protected bool IsNoop(IInternalPushInterpreter interpreter, IPushStack<IReadOnlyList<T>> vectorStack, IPushStack<T> literalStack, bool isLiteralTypeInteger = false) {
21      return vectorStack.IsEmpty ||
22             vectorStack.Top.Count == 0 ||
23             literalStack.IsEmpty ||
24             (isLiteralTypeInteger && interpreter.IntegerStack.Count < 2) ||
25             (!isLiteralTypeInteger && interpreter.IntegerStack.IsEmpty);
26    }
27
28    protected void Eval(
29      IInternalPushInterpreter interpreter,
30      IPushStack<IReadOnlyList<T>> vectorStack,
31      IPushStack<T> literalStack,
32      bool isLiteralTypeInteger = false) {
33      T literal;
34      long x;
35
36      if (isLiteralTypeInteger) {
37        literal = literalStack.Top;
38        x = interpreter.IntegerStack[1];
39        interpreter.IntegerStack.Remove(2);
40      } else {
41        literal = literalStack.Pop();
42        x = interpreter.IntegerStack.Pop();
43      }
44
45      var vector = vectorStack.Top;
46      var newTop = new List<T>(vector);
47      var index = x.AsInt(vector.Count);
48
49      if (index < 0)
50        index *= -1;
51
52      newTop[index] = literal;
53      vectorStack.Top = newTop;
54    }
55  }
56
57  [StorableClass]
58  [PushExpression(
59    StackTypes.IntegerVector,
60    "INTEGER[].SET",
61    "Replaces in the top INTEGER[] item at index (from INTEGER stack) with the first INTEGER item.",
62    StackTypes.Integer)]
63  public class IntegerVectorSetExpression : VectorSetExpression<long> {
64    public IntegerVectorSetExpression() { }
65    [StorableConstructor]
66    protected IntegerVectorSetExpression(bool deserializing) : base(deserializing) { }
67
68    public override bool IsNoop(IInternalPushInterpreter interpreter) {
69      return IsNoop(interpreter, interpreter.IntegerVectorStack, interpreter.IntegerStack, true);
70    }
71
72    public override void Eval(IInternalPushInterpreter interpreter) {
73      Eval(interpreter, interpreter.IntegerVectorStack, interpreter.IntegerStack, true);
74    }
75  }
76
77  [StorableClass]
78  [PushExpression(
79    StackTypes.FloatVector,
80    "FLOAT[].SET",
81    "Replaces in the top FLOAT[] item at index (from INTEGER stack) with the first FLOAT item.",
82    StackTypes.Float | StackTypes.Integer)]
83  public class FloatVectorSetExpression : VectorSetExpression<double> {
84    public FloatVectorSetExpression() { }
85    [StorableConstructor]
86    protected FloatVectorSetExpression(bool deserializing) : base(deserializing) { }
87
88    public override bool IsNoop(IInternalPushInterpreter interpreter) {
89      return IsNoop(interpreter, interpreter.FloatVectorStack, interpreter.FloatStack);
90    }
91
92    public override void Eval(IInternalPushInterpreter interpreter) {
93      Eval(interpreter, interpreter.FloatVectorStack, interpreter.FloatStack);
94    }
95  }
96
97  [StorableClass]
98  [PushExpression(
99    StackTypes.BooleanVector,
100    "BOOLEAN[].SET",
101    "Replaces in the top BOOLEAN[] item at index (from INTEGER stack) with the first BOOLEAN item.",
102    StackTypes.Boolean | StackTypes.Integer)]
103  public class BooleanVectorSetExpression : VectorSetExpression<bool> {
104    public BooleanVectorSetExpression() { }
105    [StorableConstructor]
106    protected BooleanVectorSetExpression(bool deserializing) : base(deserializing) { }
107
108    public override bool IsNoop(IInternalPushInterpreter interpreter) {
109      return IsNoop(interpreter, interpreter.BooleanVectorStack, interpreter.BooleanStack);
110    }
111
112    public override void Eval(IInternalPushInterpreter interpreter) {
113      Eval(interpreter, interpreter.BooleanVectorStack, interpreter.BooleanStack);
114    }
115  }
116
117  [StorableClass]
118  [PushExpression(
119    StackTypes.StringVector,
120    "STRING[].SET",
121    "Replaces in the top STRING[] item at index (from INTEGER stack) with the first STRING item.",
122    StackTypes.String | StackTypes.Integer)]
123  public class StringVectorSetExpression : VectorSetExpression<string> {
124    public StringVectorSetExpression() { }
125    [StorableConstructor]
126    protected StringVectorSetExpression(bool deserializing) : base(deserializing) { }
127
128    public override bool IsNoop(IInternalPushInterpreter interpreter) {
129      return IsNoop(interpreter, interpreter.StringVectorStack, interpreter.StringStack);
130    }
131
132    public override void Eval(IInternalPushInterpreter interpreter) {
133      Eval(interpreter, interpreter.StringVectorStack, interpreter.StringStack);
134    }
135  }
136}
Note: See TracBrowser for help on using the repository browser.