Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/VectorReverseExpressions.cs @ 16724

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

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

File size: 3.4 KB
Line 
1using System.Collections.Generic;
2using System.Linq;
3using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
4
5namespace HeuristicLab.Problems.ProgramSynthesis {
6  /// <summary>
7  /// Takes the reverse of the top item on the type stack.
8  /// </summary>
9  /// <param name="vectorStack"></param>
10  /// <returns></returns>
11  [StorableClass]
12  public abstract class VectorReverseExpression<T> : StatelessExpression {
13    protected VectorReverseExpression() { }
14    [StorableConstructor]
15    protected VectorReverseExpression(bool deserializing) : base(deserializing) { }
16
17    protected bool IsNoop(IPushStack<IReadOnlyList<T>> vectorStack) {
18      return vectorStack.IsEmpty || vectorStack.Top.Count < 2;
19    }
20
21    protected void Eval(IPushStack<IReadOnlyList<T>> vectorStack) {
22      vectorStack.Top = vectorStack.Top.Reverse().ToList();
23    }
24  }
25
26  [StorableClass]
27  [PushExpression(
28    StackTypes.IntegerVector,
29    "INTEGER[].REVERSE",
30    "Reverses the top INTEGER[].")]
31  public class IntegerVectorReverseExpression : VectorReverseExpression<long> {
32    public IntegerVectorReverseExpression() { }
33    [StorableConstructor]
34    protected IntegerVectorReverseExpression(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);
42    }
43  }
44
45  [StorableClass]
46  [PushExpression(
47    StackTypes.FloatVector,
48    "FLOAT[].REVERSE",
49    "Reverses the top FLOAT[].")]
50  public class FloatVectorReverseExpression : VectorReverseExpression<double> {
51    public FloatVectorReverseExpression() { }
52    [StorableConstructor]
53    protected FloatVectorReverseExpression(bool deserializing) : base(deserializing) { }
54
55    public override bool IsNoop(IInternalPushInterpreter interpreter) {
56      return IsNoop(interpreter.FloatVectorStack);
57    }
58
59    public override void Eval(IInternalPushInterpreter interpreter) {
60      Eval(interpreter.FloatVectorStack);
61    }
62  }
63
64  [StorableClass]
65  [PushExpression(
66    StackTypes.BooleanVector,
67    "BOOLEAN[].REVERSE",
68    "Reverses the top BOOLEAN[].")]
69  public class BooleanVectorReverseExpression : VectorReverseExpression<bool> {
70    public BooleanVectorReverseExpression() { }
71    [StorableConstructor]
72    protected BooleanVectorReverseExpression(bool deserializing) : base(deserializing) { }
73
74    public override bool IsNoop(IInternalPushInterpreter interpreter) {
75      return IsNoop(interpreter.BooleanVectorStack);
76    }
77
78    public override void Eval(IInternalPushInterpreter interpreter) {
79      Eval(interpreter.BooleanVectorStack);
80    }
81  }
82
83  [StorableClass]
84  [PushExpression(
85    StackTypes.StringVector,
86    "STRING[].REVERSE",
87    "Reverses the top STRING[].")]
88  public class StringVectorReverseExpression : VectorReverseExpression<string> {
89    public StringVectorReverseExpression() { }
90    [StorableConstructor]
91    protected StringVectorReverseExpression(bool deserializing) : base(deserializing) { }
92
93    public override bool IsNoop(IInternalPushInterpreter interpreter) {
94      return IsNoop(interpreter.StringVectorStack);
95    }
96
97    public override void Eval(IInternalPushInterpreter interpreter) {
98      Eval(interpreter.StringVectorStack);
99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.