Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/SwapExpressions.cs @ 15032

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

#2665 Fixed bias 0 issue, PushExpressionFrequencyAnalyzer, Fixed probability for ERC settings, Fixed enable/disable instructions, Added expression descriptions

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