Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/VectorFirstExpressions.cs @ 15771

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

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

File size: 3.6 KB
Line 
1using System.Collections.Generic;
2using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
3
4namespace HeuristicLab.Problems.ProgramSynthesis {
5  /// <summary>
6  /// Gets the first item from the type stack
7  /// </summary>
8  /// <typeparam name="T"></typeparam>
9  [StorableClass]
10  public abstract class VectorFirstExpression<T> : StatelessExpression {
11    protected VectorFirstExpression() { }
12    [StorableConstructor]
13    protected VectorFirstExpression(bool deserializing) : base(deserializing) { }
14
15    protected bool IsNoop(IPushStack<IReadOnlyList<T>> vectorStack) {
16      return vectorStack.IsEmpty || vectorStack.Top.Count == 0;
17    }
18
19    protected void Eval(IPushStack<IReadOnlyList<T>> vectorStack, IPushStack<T> literalStack) {
20      var vector = vectorStack.Pop();
21      literalStack.Push(vector[0]);
22    }
23  }
24
25  [StorableClass]
26  [PushExpression(
27    StackTypes.IntegerVector,
28    "INTEGER[].FIRST",
29    "Pushes the first INTEGER of the top INTEGER[].",
30    StackTypes.Integer)]
31  public class IntegerVectorFirstExpression : VectorFirstExpression<long> {
32    public IntegerVectorFirstExpression() { }
33    [StorableConstructor]
34    protected IntegerVectorFirstExpression(bool deserializing) : base(deserializing) { }
35
36    public override bool IsNoop(IInternalPushInterpreter interpreter) {
37      return IsNoop(interpreter.IntegerVectorStack);
38    }
39
40    public override void Eval(IInternalPushInterpreter interpreter) {
41      Eval(interpreter.IntegerVectorStack, interpreter.IntegerStack);
42    }
43  }
44
45  [StorableClass]
46  [PushExpression(
47    StackTypes.FloatVector,
48    "FLOAT[].FIRST",
49    "Pushes the first FLOAT of the top FLOAT[].",
50    StackTypes.Float)]
51  public class FloatVectorFirstExpression : VectorFirstExpression<double> {
52    public FloatVectorFirstExpression() { }
53    [StorableConstructor]
54    protected FloatVectorFirstExpression(bool deserializing) : base(deserializing) { }
55
56    public override bool IsNoop(IInternalPushInterpreter interpreter) {
57      return IsNoop(interpreter.FloatVectorStack);
58    }
59
60    public override void Eval(IInternalPushInterpreter interpreter) {
61      Eval(interpreter.FloatVectorStack, interpreter.FloatStack);
62    }
63  }
64
65  [StorableClass]
66  [PushExpression(
67    StackTypes.BooleanVector,
68    "BOOLEAN[].FIRST",
69    "Pushes the first BOOLEAN of the top BOOLEAN[].",
70    StackTypes.Boolean)]
71  public class BooleanVectorFirstExpression : VectorFirstExpression<bool> {
72    public BooleanVectorFirstExpression() { }
73    [StorableConstructor]
74    protected BooleanVectorFirstExpression(bool deserializing) : base(deserializing) { }
75
76    public override bool IsNoop(IInternalPushInterpreter interpreter) {
77      return IsNoop(interpreter.BooleanVectorStack);
78    }
79
80    public override void Eval(IInternalPushInterpreter interpreter) {
81      Eval(interpreter.BooleanVectorStack, interpreter.BooleanStack);
82    }
83  }
84
85  [StorableClass]
86  [PushExpression(
87    StackTypes.StringVector,
88    "STRING[].FIRST",
89    "Pushes the first BOOLEAN of the top BOOLEAN[].",
90    StackTypes.String)]
91  public class StringVectorFirstExpression : VectorFirstExpression<string> {
92    public StringVectorFirstExpression() { }
93    [StorableConstructor]
94    protected StringVectorFirstExpression(bool deserializing) : base(deserializing) { }
95
96    public override bool IsNoop(IInternalPushInterpreter interpreter) {
97      return IsNoop(interpreter.StringVectorStack);
98    }
99
100    public override void Eval(IInternalPushInterpreter interpreter) {
101      Eval(interpreter.StringVectorStack, interpreter.StringStack);
102    }
103  }
104}
105
Note: See TracBrowser for help on using the repository browser.