Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/YankExpressions.cs @ 16003

Last change on this file since 16003 was 15771, checked in by bburlacu, 7 years ago

#2895: Add solution skeleton for PushGP with genealogy analysis.

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