Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/YankDuplicateExpressions.cs @ 16958

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

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

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