Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/YankDuplicateExpressions.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: 10.5 KB
Line 
1using System.Collections.Generic;
2using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
3
4namespace HeuristicLab.Problems.ProgramSynthesis {
5  /// <summary>
6  /// Pushes a copy of an indexed item "deep" in the stack onto the top of the stack, without removing the deep item.
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 YankDuplicateExpression<T> : StatelessExpression {
12    protected YankDuplicateExpression() { }
13    [StorableConstructor]
14    protected YankDuplicateExpression(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.Push(stack[index]);
24    }
25  }
26
27  [StorableClass]
28  [PushExpression(
29    StackTypes.Integer,
30    "INTEGER.YANKDUP",
31    "Pushes a copy of an indexed item \"deep\" in the stack onto the top of the stack, without removing the deep item.")]
32  public class IntegerYankDuplicateExpression : YankDuplicateExpression<long> {
33    public IntegerYankDuplicateExpression() { }
34    [StorableConstructor]
35    protected IntegerYankDuplicateExpression(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.YANKDUP",
50    "Pushes a copy of an indexed item \"deep\" in the stack onto the top of the stack, without removing the deep item.",
51    StackTypes.Integer)]
52  public class FloatYankDuplicateExpression : YankDuplicateExpression<double> {
53    public FloatYankDuplicateExpression() { }
54    [StorableConstructor]
55    protected FloatYankDuplicateExpression(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.YANKDUP",
70    "Pushes a copy of an indexed item \"deep\" in the stack onto the top of the stack, without removing the deep item.",
71    StackTypes.Integer)]
72  public class BooleanYankDuplicateExpression : YankDuplicateExpression<bool> {
73    public BooleanYankDuplicateExpression() { }
74    [StorableConstructor]
75    protected BooleanYankDuplicateExpression(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.YANKDUP",
90    "Pushes a copy of an indexed item \"deep\" in the stack onto the top of the stack, without removing the deep item.",
91    StackTypes.Integer)]
92  public class NameYankDuplicateExpression : YankDuplicateExpression<string> {
93    public NameYankDuplicateExpression() { }
94    [StorableConstructor]
95    protected NameYankDuplicateExpression(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.YANKDUP",
110    "Pushes a copy of an indexed item \"deep\" in the stack onto the top of the stack, without removing the deep item.",
111    StackTypes.Integer, requiredBlockCount: 0)]
112  public class ExecYankDuplicateExpression : YankDuplicateExpression<Expression> {
113    public ExecYankDuplicateExpression() { }
114    [StorableConstructor]
115    protected ExecYankDuplicateExpression(bool deserializing) : base(deserializing) { }
116
117    public override bool IsNoop(IInternalPushInterpreter interpreter) {
118      return IsNoop(interpreter.ExecStack, interpreter.IntegerStack);
119    }
120
121    public override void Eval(IInternalPushInterpreter interpreter) {
122      Eval(interpreter.ExecStack, interpreter.IntegerStack);
123    }
124  }
125
126  [StorableClass]
127  [PushExpression(
128    StackTypes.Code,
129    "CODE.YANKDUP",
130    "Pushes a copy of an indexed item \"deep\" in the stack onto the top of the stack, without removing the deep item.",
131    StackTypes.Integer)]
132  public class CodeYankDuplicateExpression : YankDuplicateExpression<Expression> {
133    public CodeYankDuplicateExpression() { }
134    [StorableConstructor]
135    protected CodeYankDuplicateExpression(bool deserializing) : base(deserializing) { }
136
137    public override bool IsNoop(IInternalPushInterpreter interpreter) {
138      return IsNoop(interpreter.CodeStack, interpreter.IntegerStack);
139    }
140
141    public override void Eval(IInternalPushInterpreter interpreter) {
142      Eval(interpreter.CodeStack, interpreter.IntegerStack);
143    }
144  }
145
146  [StorableClass]
147  [PushExpression(
148    StackTypes.Char,
149    "CHAR.YANKDUP",
150    "Pushes a copy of an indexed item \"deep\" in the stack onto the top of the stack, without removing the deep item.",
151    StackTypes.Integer)]
152  public class CharYankDuplicateExpression : YankDuplicateExpression<char> {
153    public CharYankDuplicateExpression() { }
154    [StorableConstructor]
155    protected CharYankDuplicateExpression(bool deserializing) : base(deserializing) { }
156
157    public override bool IsNoop(IInternalPushInterpreter interpreter) {
158      return IsNoop(interpreter.CharStack, interpreter.IntegerStack);
159    }
160
161    public override void Eval(IInternalPushInterpreter interpreter) {
162      Eval(interpreter.CharStack, interpreter.IntegerStack);
163    }
164  }
165
166  [StorableClass]
167  [PushExpression(
168    StackTypes.String,
169    "STRING.YANKDUP",
170    "Pushes a copy of an indexed item \"deep\" in the stack onto the top of the stack, without removing the deep item.",
171    StackTypes.Integer)]
172  public class StringYankDuplicateExpression : YankDuplicateExpression<string> {
173    public StringYankDuplicateExpression() { }
174    [StorableConstructor]
175    protected StringYankDuplicateExpression(bool deserializing) : base(deserializing) { }
176
177    public override bool IsNoop(IInternalPushInterpreter interpreter) {
178      return IsNoop(interpreter.StringStack, interpreter.IntegerStack);
179    }
180
181    public override void Eval(IInternalPushInterpreter interpreter) {
182      Eval(interpreter.StringStack, interpreter.IntegerStack);
183    }
184  }
185
186  [StorableClass]
187  [PushExpression(
188    StackTypes.IntegerVector,
189    "INTEGER[].YANKDUP",
190    "Pushes a copy of an indexed item \"deep\" in the stack onto the top of the stack, without removing the deep item.",
191    StackTypes.Integer)]
192  public class IntegerVectorYankDuplicateExpression : YankDuplicateExpression<IReadOnlyList<long>> {
193    public IntegerVectorYankDuplicateExpression() { }
194    [StorableConstructor]
195    protected IntegerVectorYankDuplicateExpression(bool deserializing) : base(deserializing) { }
196
197    public override bool IsNoop(IInternalPushInterpreter interpreter) {
198      return IsNoop(interpreter.IntegerVectorStack, interpreter.IntegerStack);
199    }
200
201    public override void Eval(IInternalPushInterpreter interpreter) {
202      Eval(interpreter.IntegerVectorStack, interpreter.IntegerStack);
203    }
204  }
205
206  [StorableClass]
207  [PushExpression(
208    StackTypes.FloatVector,
209    "FLOAT[].YANKDUP",
210    "Pushes a copy of an indexed item \"deep\" in the stack onto the top of the stack, without removing the deep item.",
211    StackTypes.Integer)]
212  public class FloatVectorYankDuplicateExpression : YankDuplicateExpression<IReadOnlyList<double>> {
213    public FloatVectorYankDuplicateExpression() { }
214    [StorableConstructor]
215    protected FloatVectorYankDuplicateExpression(bool deserializing) : base(deserializing) { }
216
217    public override bool IsNoop(IInternalPushInterpreter interpreter) {
218      return IsNoop(interpreter.FloatVectorStack, interpreter.IntegerStack);
219    }
220
221    public override void Eval(IInternalPushInterpreter interpreter) {
222      Eval(interpreter.FloatVectorStack, interpreter.IntegerStack);
223    }
224  }
225
226  [StorableClass]
227  [PushExpression(
228    StackTypes.BooleanVector,
229    "BOOLEAN[].YANKDUP",
230    "Pushes a copy of an indexed item \"deep\" in the stack onto the top of the stack, without removing the deep item.",
231    StackTypes.Integer)]
232  public class BooleanVectorYankDuplicateExpression : YankDuplicateExpression<IReadOnlyList<bool>> {
233    public BooleanVectorYankDuplicateExpression() { }
234    [StorableConstructor]
235    protected BooleanVectorYankDuplicateExpression(bool deserializing) : base(deserializing) { }
236
237    public override bool IsNoop(IInternalPushInterpreter interpreter) {
238      return IsNoop(interpreter.BooleanVectorStack, interpreter.IntegerStack);
239    }
240
241    public override void Eval(IInternalPushInterpreter interpreter) {
242      Eval(interpreter.BooleanVectorStack, interpreter.IntegerStack);
243    }
244  }
245
246  [StorableClass]
247  [PushExpression(
248    StackTypes.StringVector,
249    "STRING[].YANKDUP",
250    "Pushes a copy of an indexed item \"deep\" in the stack onto the top of the stack, without removing the deep item.",
251    StackTypes.Integer)]
252  public class StringVectorYankDuplicateExpression : YankDuplicateExpression<IReadOnlyList<string>> {
253    public StringVectorYankDuplicateExpression() { }
254    [StorableConstructor]
255    protected StringVectorYankDuplicateExpression(bool deserializing) : base(deserializing) { }
256
257    public override bool IsNoop(IInternalPushInterpreter interpreter) {
258      return IsNoop(interpreter.StringVectorStack, interpreter.IntegerStack);
259    }
260
261    public override void Eval(IInternalPushInterpreter interpreter) {
262      Eval(interpreter.StringVectorStack, interpreter.IntegerStack);
263    }
264  }
265}
Note: See TracBrowser for help on using the repository browser.