Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/YankExpressions.cs @ 17603

Last change on this file since 17603 was 15334, checked in by pkimmesw, 7 years ago

#2665 Testet Problems, Testet error functions, Small fixes, Created HL files

File size: 9.9 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
2  using System.Collections.Generic;
3
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  /// Removes an indexed item from "deep" in the stack and pushes it on top of the stack.
12  /// The index is taken from the INTEGER stack, and the indexing is done after the index is removed.
13  /// </summary>
14  /// <typeparam name="T">Stacktype</typeparam>
15  [StorableClass]
16  public abstract class YankExpression<T> : StatelessExpression {
17    protected YankExpression() { }
18    [StorableConstructor]
19    protected YankExpression(bool deserializing) : base(deserializing) { }
20
21    protected bool IsNoop(IPushStack<T> stack, IPushStack<long> integerStack) {
22      return (stack == integerStack && integerStack.Count < 3) ||
23             (stack != integerStack && (integerStack.IsEmpty || stack.Count < 2));
24    }
25
26    protected void Eval(IPushStack<T> stack, IPushStack<long> integerStack) {
27      var index = integerStack.Pop().AsInt(stack.Count);
28      stack.Yank(index);
29    }
30  }
31
32  [StorableClass]
33  [PushExpression(
34    StackTypes.Integer,
35    "INTEGER.YANK",
36    "Removes an indexed item from \"deep\" in the stack and pushes it on top of the stack.")]
37  public class IntegerYankExpression : YankExpression<long> {
38    public IntegerYankExpression() { }
39    [StorableConstructor]
40    protected IntegerYankExpression(bool deserializing) : base(deserializing) { }
41
42    public override bool IsNoop(IInternalPushInterpreter interpreter) {
43      return IsNoop(interpreter.IntegerStack, interpreter.IntegerStack);
44    }
45
46    public override void Eval(IInternalPushInterpreter interpreter) {
47      Eval(interpreter.IntegerStack, interpreter.IntegerStack);
48    }
49  }
50
51  [StorableClass]
52  [PushExpression(
53    StackTypes.Float,
54    "FLOAT.YANK",
55    "Removes an indexed item from \"deep\" in the stack and pushes it on top of the stack.",
56    StackTypes.Integer)]
57  public class FloatYankExpression : YankExpression<double> {
58    public FloatYankExpression() { }
59    [StorableConstructor]
60    protected FloatYankExpression(bool deserializing) : base(deserializing) { }
61
62    public override bool IsNoop(IInternalPushInterpreter interpreter) {
63      return IsNoop(interpreter.FloatStack, interpreter.IntegerStack);
64    }
65
66    public override void Eval(IInternalPushInterpreter interpreter) {
67      Eval(interpreter.FloatStack, interpreter.IntegerStack);
68    }
69  }
70
71  [StorableClass]
72  [PushExpression(
73    StackTypes.Boolean,
74    "BOOLEAN.YANK",
75    "Removes an indexed item from \"deep\" in the stack and pushes it on top of the stack.",
76    StackTypes.Integer)]
77  public class BooleanYankExpression : YankExpression<bool> {
78    public BooleanYankExpression() { }
79    [StorableConstructor]
80    protected BooleanYankExpression(bool deserializing) : base(deserializing) { }
81
82    public override bool IsNoop(IInternalPushInterpreter interpreter) {
83      return IsNoop(interpreter.BooleanStack, interpreter.IntegerStack);
84    }
85
86    public override void Eval(IInternalPushInterpreter interpreter) {
87      Eval(interpreter.BooleanStack, interpreter.IntegerStack);
88    }
89  }
90
91  [StorableClass]
92  [PushExpression(
93    StackTypes.Name,
94    "NAME.YANK",
95    "Removes an indexed item from \"deep\" in the stack and pushes it on top of the stack.",
96    StackTypes.Integer)]
97  public class NameYankExpression : YankExpression<string> {
98    public NameYankExpression() { }
99    [StorableConstructor]
100    protected NameYankExpression(bool deserializing) : base(deserializing) { }
101
102    public override bool IsNoop(IInternalPushInterpreter interpreter) {
103      return IsNoop(interpreter.NameStack, interpreter.IntegerStack);
104    }
105
106    public override void Eval(IInternalPushInterpreter interpreter) {
107      Eval(interpreter.NameStack, interpreter.IntegerStack);
108    }
109  }
110
111  [StorableClass]
112  [PushExpression(
113    StackTypes.Exec,
114    "EXEC.YANK",
115    "Removes an indexed item from \"deep\" in the stack and pushes it on top of the stack.",
116    StackTypes.Integer,
117    requiredBlockCount: 0)]
118  public class ExecYankExpression : YankExpression<Expression> {
119    public ExecYankExpression() { }
120    [StorableConstructor]
121    protected ExecYankExpression(bool deserializing) : base(deserializing) { }
122
123    public override bool IsNoop(IInternalPushInterpreter interpreter) {
124      return IsNoop(interpreter.ExecStack, interpreter.IntegerStack);
125    }
126
127    public override void Eval(IInternalPushInterpreter interpreter) {
128      Eval(interpreter.ExecStack, interpreter.IntegerStack);
129    }
130  }
131
132  [StorableClass]
133  [PushExpression(
134    StackTypes.Code,
135    "CODE.YANK",
136    "Removes an indexed item from \"deep\" in the stack and pushes it on top of the stack.",
137    StackTypes.Integer)]
138  public class CodeYankExpression : YankExpression<Expression> {
139    public CodeYankExpression() { }
140    [StorableConstructor]
141    protected CodeYankExpression(bool deserializing) : base(deserializing) { }
142
143    public override bool IsNoop(IInternalPushInterpreter interpreter) {
144      return IsNoop(interpreter.CodeStack, interpreter.IntegerStack);
145    }
146
147    public override void Eval(IInternalPushInterpreter interpreter) {
148      Eval(interpreter.CodeStack, interpreter.IntegerStack);
149    }
150  }
151
152  [StorableClass]
153  [PushExpression(
154    StackTypes.Char,
155    "CHAR.YANK",
156    "Removes an indexed item from \"deep\" in the stack and pushes it on top of the stack.",
157    StackTypes.Integer)]
158  public class CharYankExpression : YankExpression<char> {
159    public CharYankExpression() { }
160    [StorableConstructor]
161    protected CharYankExpression(bool deserializing) : base(deserializing) { }
162
163    public override bool IsNoop(IInternalPushInterpreter interpreter) {
164      return IsNoop(interpreter.CharStack, interpreter.IntegerStack);
165    }
166
167    public override void Eval(IInternalPushInterpreter interpreter) {
168      Eval(interpreter.CharStack, interpreter.IntegerStack);
169    }
170  }
171
172  [StorableClass]
173  [PushExpression(
174    StackTypes.String,
175    "STRING.YANK",
176    "Removes an indexed item from \"deep\" in the stack and pushes it on top of the stack.",
177    StackTypes.Integer)]
178  public class StringYankExpression : YankExpression<string> {
179    public StringYankExpression() { }
180    [StorableConstructor]
181    protected StringYankExpression(bool deserializing) : base(deserializing) { }
182
183    public override bool IsNoop(IInternalPushInterpreter interpreter) {
184      return IsNoop(interpreter.StringStack, interpreter.IntegerStack);
185    }
186
187    public override void Eval(IInternalPushInterpreter interpreter) {
188      Eval(interpreter.StringStack, interpreter.IntegerStack);
189    }
190  }
191
192  [StorableClass]
193  [PushExpression(
194    StackTypes.IntegerVector,
195    "INTEGER[].YANK",
196    "Removes an indexed item from \"deep\" in the stack and pushes it on top of the stack.",
197    StackTypes.Integer)]
198  public class IntegerVectorYankExpression : YankExpression<IReadOnlyList<long>> {
199    public IntegerVectorYankExpression() { }
200    [StorableConstructor]
201    protected IntegerVectorYankExpression(bool deserializing) : base(deserializing) { }
202
203    public override bool IsNoop(IInternalPushInterpreter interpreter) {
204      return IsNoop(interpreter.IntegerVectorStack, interpreter.IntegerStack);
205    }
206
207    public override void Eval(IInternalPushInterpreter interpreter) {
208      Eval(interpreter.IntegerVectorStack, interpreter.IntegerStack);
209    }
210  }
211
212  [StorableClass]
213  [PushExpression(
214    StackTypes.FloatVector,
215    "FLOAT[].YANK",
216    "Removes an indexed item from \"deep\" in the stack and pushes it on top of the stack.",
217    StackTypes.Integer)]
218  public class FloatVectorYankExpression : YankExpression<IReadOnlyList<double>> {
219    public FloatVectorYankExpression() { }
220    [StorableConstructor]
221    protected FloatVectorYankExpression(bool deserializing) : base(deserializing) { }
222
223    public override bool IsNoop(IInternalPushInterpreter interpreter) {
224      return IsNoop(interpreter.FloatVectorStack, interpreter.IntegerStack);
225    }
226
227    public override void Eval(IInternalPushInterpreter interpreter) {
228      Eval(interpreter.FloatVectorStack, interpreter.IntegerStack);
229    }
230  }
231
232  [StorableClass]
233  [PushExpression(
234    StackTypes.BooleanVector,
235    "BOOLEAN[].YANK",
236    "Removes an indexed item from \"deep\" in the stack and pushes it on top of the stack.",
237    StackTypes.Integer)]
238  public class BooleanVectorYankExpression : YankExpression<IReadOnlyList<bool>> {
239    public BooleanVectorYankExpression() { }
240    [StorableConstructor]
241    protected BooleanVectorYankExpression(bool deserializing) : base(deserializing) { }
242
243    public override bool IsNoop(IInternalPushInterpreter interpreter) {
244      return IsNoop(interpreter.BooleanVectorStack, interpreter.IntegerStack);
245    }
246
247    public override void Eval(IInternalPushInterpreter interpreter) {
248      Eval(interpreter.BooleanVectorStack, interpreter.IntegerStack);
249    }
250  }
251
252  [StorableClass]
253  [PushExpression(
254    StackTypes.StringVector,
255    "STRING[].YANK",
256    "Removes an indexed item from \"deep\" in the stack and pushes it on top of the stack.",
257    StackTypes.Integer)]
258  public class StringVectorYankExpression : YankExpression<IReadOnlyList<string>> {
259    public StringVectorYankExpression() { }
260    [StorableConstructor]
261    protected StringVectorYankExpression(bool deserializing) : base(deserializing) { }
262
263    public override bool IsNoop(IInternalPushInterpreter interpreter) {
264      return IsNoop(interpreter.StringVectorStack, interpreter.IntegerStack);
265    }
266
267    public override void Eval(IInternalPushInterpreter interpreter) {
268      Eval(interpreter.StringVectorStack, interpreter.IntegerStack);
269    }
270  }
271}
Note: See TracBrowser for help on using the repository browser.