Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/DuplicateExpressions.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: 8.0 KB
Line 
1using System.Collections.Generic;
2using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
3
4namespace HeuristicLab.Problems.ProgramSynthesis {
5
6  /// <summary>
7  /// Duplicates the top item on the T stack. Does not pop its argument
8  /// (which, if it did, would negate the effect of the duplication!).
9  /// </summary>
10  /// <typeparam name="T">Stacktype</typeparam>
11  [StorableClass]
12  public abstract class DuplicateExpression<T> : StatelessExpression {
13    protected DuplicateExpression() { }
14    [StorableConstructor]
15    protected DuplicateExpression(bool deserializing) : base(deserializing) { }
16
17    protected void Eval(IPushStack<T> stack) {
18      stack.Push(stack.Top);
19    }
20  }
21
22  [StorableClass]
23  [PushExpression(StackTypes.Integer, "INTEGER.DUP", "Duplicates the top item on the INTEGER stack.")]
24  public class IntegerDuplicateExpression : DuplicateExpression<long> {
25    public IntegerDuplicateExpression() { }
26    [StorableConstructor]
27    protected IntegerDuplicateExpression(bool deserializing) : base(deserializing) { }
28
29    public override bool IsNoop(IInternalPushInterpreter interpreter) {
30      return interpreter.IntegerStack.IsEmpty;
31    }
32
33    public override void Eval(IInternalPushInterpreter interpreter) {
34      Eval(interpreter.IntegerStack);
35    }
36  }
37
38  [StorableClass]
39  [PushExpression(StackTypes.Float, "FLOAT.DUP", "Duplicates the top item on the FLOAT stack.")]
40  public class FloatDuplicateExpression : DuplicateExpression<double> {
41    public FloatDuplicateExpression() { }
42    [StorableConstructor]
43    protected FloatDuplicateExpression(bool deserializing) : base(deserializing) { }
44
45    public override bool IsNoop(IInternalPushInterpreter interpreter) {
46      return interpreter.FloatStack.IsEmpty;
47    }
48
49    public override void Eval(IInternalPushInterpreter interpreter) {
50      Eval(interpreter.FloatStack);
51    }
52  }
53
54  [StorableClass]
55  [PushExpression(StackTypes.Boolean, "BOOLEAN.DUP", "Duplicates the top item on the BOOLEAN stack.")]
56  public class BooleanDuplicateExpression : DuplicateExpression<bool> {
57    public BooleanDuplicateExpression() { }
58    [StorableConstructor]
59    protected BooleanDuplicateExpression(bool deserializing) : base(deserializing) { }
60
61    public override bool IsNoop(IInternalPushInterpreter interpreter) {
62      return interpreter.BooleanStack.IsEmpty;
63    }
64
65    public override void Eval(IInternalPushInterpreter interpreter) {
66      Eval(interpreter.BooleanStack);
67    }
68  }
69
70  [StorableClass]
71  [PushExpression(StackTypes.Name, "NAME.DUP", "Duplicates the top item on the NAME stack.")]
72  public class NameDuplicateExpression : DuplicateExpression<string> {
73    public NameDuplicateExpression() { }
74    [StorableConstructor]
75    protected NameDuplicateExpression(bool deserializing) : base(deserializing) { }
76
77    public override bool IsNoop(IInternalPushInterpreter interpreter) {
78      return interpreter.NameStack.IsEmpty;
79    }
80
81    public override void Eval(IInternalPushInterpreter interpreter) {
82      Eval(interpreter.NameStack);
83    }
84  }
85
86  [StorableClass]
87  [PushExpression(StackTypes.Exec, "EXEC.DUP", "Duplicates the top item on the EXEC stack.", requiredBlockCount: 1)]
88  public class ExecDuplicateExpression : DuplicateExpression<Expression> {
89    public ExecDuplicateExpression() { }
90    [StorableConstructor]
91    protected ExecDuplicateExpression(bool deserializing) : base(deserializing) { }
92
93    public override bool IsNoop(IInternalPushInterpreter interpreter) {
94      return interpreter.ExecStack.IsEmpty;
95    }
96
97    public override void Eval(IInternalPushInterpreter interpreter) {
98      Eval(interpreter.ExecStack);
99    }
100  }
101
102  [StorableClass]
103  [PushExpression(StackTypes.Code, "CODE.DUP", "Duplicates the top item on the CODE stack.")]
104  public class CodeDuplicateExpression : DuplicateExpression<Expression> {
105    public CodeDuplicateExpression() { }
106    [StorableConstructor]
107    protected CodeDuplicateExpression(bool deserializing) : base(deserializing) { }
108
109    public override bool IsNoop(IInternalPushInterpreter interpreter) {
110      return interpreter.CodeStack.IsEmpty;
111    }
112
113    public override void Eval(IInternalPushInterpreter interpreter) {
114      Eval(interpreter.CodeStack);
115    }
116  }
117
118  [StorableClass]
119  [PushExpression(StackTypes.Char, "CHAR.DUP", "Duplicates the top item on the CHAR stack.")]
120  public class CharDuplicateExpression : DuplicateExpression<char> {
121    public CharDuplicateExpression() { }
122    [StorableConstructor]
123    protected CharDuplicateExpression(bool deserializing) : base(deserializing) { }
124
125    public override bool IsNoop(IInternalPushInterpreter interpreter) {
126      return interpreter.CharStack.IsEmpty;
127    }
128
129    public override void Eval(IInternalPushInterpreter interpreter) {
130      Eval(interpreter.CharStack);
131    }
132  }
133
134  [StorableClass]
135  [PushExpression(StackTypes.String, "STRING.DUP", "Duplicates the top item on the STRING stack.")]
136  public class StringDuplicateExpression : DuplicateExpression<string> {
137    public StringDuplicateExpression() { }
138    [StorableConstructor]
139    protected StringDuplicateExpression(bool deserializing) : base(deserializing) { }
140
141    public override bool IsNoop(IInternalPushInterpreter interpreter) {
142      return interpreter.StringStack.IsEmpty;
143    }
144
145    public override void Eval(IInternalPushInterpreter interpreter) {
146      Eval(interpreter.StringStack);
147    }
148  }
149
150  [StorableClass]
151  [PushExpression(StackTypes.IntegerVector, "INTEGER[].DUP", "Duplicates the top item on the INTEGER[] stack.")]
152  public class IntegerVectorDuplicateExpression : DuplicateExpression<IReadOnlyList<long>> {
153    public IntegerVectorDuplicateExpression() { }
154    [StorableConstructor]
155    protected IntegerVectorDuplicateExpression(bool deserializing) : base(deserializing) { }
156
157    public override bool IsNoop(IInternalPushInterpreter interpreter) {
158      return interpreter.IntegerVectorStack.IsEmpty;
159    }
160
161    public override void Eval(IInternalPushInterpreter interpreter) {
162      Eval(interpreter.IntegerVectorStack);
163    }
164  }
165
166  [StorableClass]
167  [PushExpression(StackTypes.FloatVector, "FLOAT[].DUP", "Duplicates the top item on the FLOAT[] stack.")]
168  public class FloatVectorDuplicateExpression : DuplicateExpression<IReadOnlyList<double>> {
169    public FloatVectorDuplicateExpression() { }
170    [StorableConstructor]
171    protected FloatVectorDuplicateExpression(bool deserializing) : base(deserializing) { }
172
173    public override bool IsNoop(IInternalPushInterpreter interpreter) {
174      return interpreter.FloatVectorStack.IsEmpty;
175    }
176
177    public override void Eval(IInternalPushInterpreter interpreter) {
178      Eval(interpreter.FloatVectorStack);
179    }
180  }
181
182  [StorableClass]
183  [PushExpression(StackTypes.BooleanVector, "BOOLEAN[].DUP", "Duplicates the top item on the BOOLEAN[] stack.")]
184  public class BooleanVectorDuplicateExpression : DuplicateExpression<IReadOnlyList<bool>> {
185    public BooleanVectorDuplicateExpression() { }
186    [StorableConstructor]
187    protected BooleanVectorDuplicateExpression(bool deserializing) : base(deserializing) { }
188
189    public override bool IsNoop(IInternalPushInterpreter interpreter) {
190      return interpreter.BooleanVectorStack.IsEmpty;
191    }
192
193    public override void Eval(IInternalPushInterpreter interpreter) {
194      Eval(interpreter.BooleanVectorStack);
195    }
196  }
197
198  [StorableClass]
199  [PushExpression(StackTypes.StringVector, "STRING[].DUP", "Duplicates the top item on the STRING[] stack.")]
200  public class StringVectorDuplicateExpression : DuplicateExpression<IReadOnlyList<string>> {
201    public StringVectorDuplicateExpression() { }
202    [StorableConstructor]
203    protected StringVectorDuplicateExpression(bool deserializing) : base(deserializing) { }
204
205    public override bool IsNoop(IInternalPushInterpreter interpreter) {
206      return interpreter.StringVectorStack.IsEmpty;
207    }
208
209    public override void Eval(IInternalPushInterpreter interpreter) {
210      Eval(interpreter.StringVectorStack);
211    }
212  }
213}
Note: See TracBrowser for help on using the repository browser.