Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 7.8 KB
RevLine 
[15771]1using System.Collections.Generic;
2using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
3
4namespace HeuristicLab.Problems.ProgramSynthesis {
[14727]5  /// <summary>
6  ///     Swaps the top two values.
7  /// </summary>
8  /// <typeparam name="T">Stacktype</typeparam>
[14952]9  [StorableClass]
[14727]10  public abstract class SwapExpression<T> : StatelessExpression {
[14952]11    protected SwapExpression() { }
12    [StorableConstructor]
13    protected SwapExpression(bool deserializing) : base(deserializing) { }
[14727]14
[14952]15    protected void Eval(IPushStack<T> stack) {
[14727]16      stack.Swap(2);
17    }
18  }
19
[14952]20  [StorableClass]
[15032]21  [PushExpression(
22    StackTypes.Integer,
23    "INTEGER.SWAP",
24    "Swaps the top two items on the stack.")]
[14727]25  public class IntegerSwapExpression : SwapExpression<long> {
[14952]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;
[14727]32    }
[14952]33
34    public override void Eval(IInternalPushInterpreter interpreter) {
35      Eval(interpreter.IntegerStack);
36    }
[14727]37  }
38
[14952]39  [StorableClass]
[15032]40  [PushExpression(
41    StackTypes.Float,
42    "FLOAT.SWAP",
43    "Swaps the top two items on the stack.")]
[14727]44  public class FloatSwapExpression : SwapExpression<double> {
[14952]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;
[14727]51    }
[14952]52
53    public override void Eval(IInternalPushInterpreter interpreter) {
54      Eval(interpreter.FloatStack);
55    }
[14727]56  }
57
[14952]58  [StorableClass]
[15032]59  [PushExpression(
60    StackTypes.Boolean,
61    "BOOLEAN.SWAP",
62    "Swaps the top two items on the stack.")]
[14727]63  public class BooleanSwapExpression : SwapExpression<bool> {
[14952]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;
[14727]70    }
[14952]71
72    public override void Eval(IInternalPushInterpreter interpreter) {
73      Eval(interpreter.BooleanStack);
74    }
[14727]75  }
76
[14952]77  [StorableClass]
[15032]78  [PushExpression(
79    StackTypes.Name,
80    "NAME.SWAP",
81    "Swaps the top two items on the stack.")]
[14727]82  public class NameSwapExpression : SwapExpression<string> {
[14952]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;
[14727]89    }
[14952]90
91    public override void Eval(IInternalPushInterpreter interpreter) {
92      Eval(interpreter.NameStack);
93    }
[14727]94  }
95
[14952]96  [StorableClass]
[15032]97  [PushExpression(
98    StackTypes.Exec,
99    "EXEC.SWAP",
100    "Swaps the top two items on the stack.",
[15334]101    requiredBlockCount: 2)]
[14727]102  public class ExecSwapExpression : SwapExpression<Expression> {
[14952]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;
[14727]109    }
[14952]110
111    public override void Eval(IInternalPushInterpreter interpreter) {
112      Eval(interpreter.ExecStack);
113    }
[14727]114  }
115
[14952]116  [StorableClass]
[15032]117  [PushExpression(
118    StackTypes.Code,
119    "CODE.SWAP",
120    "Swaps the top two items on the stack.")]
[14727]121  public class CodeSwapExpression : SwapExpression<Expression> {
[14952]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;
[14727]128    }
[14952]129
130    public override void Eval(IInternalPushInterpreter interpreter) {
131      Eval(interpreter.CodeStack);
132    }
[14727]133  }
[14777]134
[14952]135  [StorableClass]
[15032]136  [PushExpression(
137    StackTypes.Char,
138    "CHAR.SWAP",
139    "Swaps the top two items on the stack.")]
[14777]140  public class CharSwapExpression : SwapExpression<char> {
[14952]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;
[14777]147    }
[14952]148
149    public override void Eval(IInternalPushInterpreter interpreter) {
150      Eval(interpreter.CharStack);
151    }
[14777]152  }
153
[14952]154  [StorableClass]
[15032]155  [PushExpression(
156    StackTypes.String,
157    "STRING.SWAP",
158    "Swaps the top two items on the stack.")]
[14777]159  public class StringSwapExpression : SwapExpression<string> {
[14952]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;
[14777]166    }
[14952]167
168    public override void Eval(IInternalPushInterpreter interpreter) {
169      Eval(interpreter.StringStack);
170    }
[14777]171  }
[14834]172
[14952]173  [StorableClass]
[15032]174  [PushExpression(
175    StackTypes.IntegerVector,
176    "INTEGER[].SWAP",
177    "Swaps the top two items on the stack.")]
[15017]178  public class IntegerVectorSwapExpression : SwapExpression<IReadOnlyList<long>> {
[14952]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;
[14834]185    }
[14952]186
187    public override void Eval(IInternalPushInterpreter interpreter) {
188      Eval(interpreter.IntegerVectorStack);
189    }
[14834]190  }
[14875]191
[14952]192  [StorableClass]
[15032]193  [PushExpression(
194    StackTypes.FloatVector,
195    "FLOAT[].SWAP",
196    "Swaps the top two items on the stack.")]
[15017]197  public class FloatVectorSwapExpression : SwapExpression<IReadOnlyList<double>> {
[14952]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;
[14875]204    }
[14952]205
206    public override void Eval(IInternalPushInterpreter interpreter) {
207      Eval(interpreter.FloatVectorStack);
208    }
[14875]209  }
210
[14952]211  [StorableClass]
[15032]212  [PushExpression(
213    StackTypes.BooleanVector,
214    "BOOLEAN[].SWAP",
215    "Swaps the top two items on the stack.")]
[15017]216  public class BooleanVectorSwapExpression : SwapExpression<IReadOnlyList<bool>> {
[14952]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;
[14875]223    }
[14952]224
225    public override void Eval(IInternalPushInterpreter interpreter) {
226      Eval(interpreter.BooleanVectorStack);
227    }
[14875]228  }
229
[14952]230  [StorableClass]
[15032]231  [PushExpression(
232    StackTypes.StringVector,
233    "STRING[].SWAP",
234    "Swaps the top two items on the stack.")]
[15017]235  public class StringVectorSwapExpression : SwapExpression<IReadOnlyList<string>> {
[14952]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;
[14875]242    }
[14952]243
244    public override void Eval(IInternalPushInterpreter interpreter) {
245      Eval(interpreter.StringVectorStack);
246    }
[14875]247  }
[14727]248}
Note: See TracBrowser for help on using the repository browser.