Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/SwapExpressions.cs @ 17021

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

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

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