Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/VectorLengthExpressions.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: 3.6 KB
Line 
1using System.Collections.Generic;
2using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
3
4namespace HeuristicLab.Problems.ProgramSynthesis {
5  /// <summary>
6  /// Takes the length of the top item on the type stack
7  /// </summary>
8  /// <typeparam name="T"></typeparam>
9  [StorableClass]
10  public abstract class VectorLengthExpression<T> : StatelessExpression {
11    protected VectorLengthExpression() { }
12    [StorableConstructor]
13    protected VectorLengthExpression(bool deserializing) : base(deserializing) { }
14
15    protected bool IsNoop(IPushStack<IReadOnlyList<T>> vectorStack) {
16      return vectorStack.IsEmpty;
17    }
18
19    protected void Eval(IInternalPushInterpreter interpreter, IPushStack<IReadOnlyList<T>> vectorStack) {
20      var length = vectorStack.Pop().Count;
21      interpreter.IntegerStack.Push(length);
22    }
23  }
24
25  [StorableClass]
26  [PushExpression(
27    StackTypes.IntegerVector,
28    "INTEGER[].LENGTH",
29    "Pushes the length of the top INTEGER[] on the INTEGER stack",
30    StackTypes.Integer)]
31  public class IntegerVectorLengthExpression : VectorLengthExpression<long> {
32    public IntegerVectorLengthExpression() { }
33    [StorableConstructor]
34    protected IntegerVectorLengthExpression(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, interpreter.IntegerVectorStack);
42    }
43  }
44
45  [StorableClass]
46  [PushExpression(
47    StackTypes.FloatVector,
48    "FLOAT[].LENGTH",
49    "Pushes the length of the top FLOAT[] on the INTEGER stack",
50    StackTypes.Integer)]
51  public class FloatVectorLengthExpression : VectorLengthExpression<double> {
52    public FloatVectorLengthExpression() { }
53    [StorableConstructor]
54    protected FloatVectorLengthExpression(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, interpreter.FloatVectorStack);
62    }
63  }
64
65  [StorableClass]
66  [PushExpression(
67    StackTypes.BooleanVector,
68    "BOOLEAN[].LENGTH",
69    "Pushes the length of the top BOOLEAN[] on the INTEGER stack",
70    StackTypes.Integer)]
71  public class BooleanVectorLengthExpression : VectorLengthExpression<bool> {
72    public BooleanVectorLengthExpression() { }
73    [StorableConstructor]
74    protected BooleanVectorLengthExpression(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, interpreter.BooleanVectorStack);
82    }
83  }
84
85  [StorableClass]
86  [PushExpression(
87    StackTypes.StringVector,
88    "STRING[].LENGTH",
89    "Pushes the length of the top STRING[] on the INTEGER stack",
90    StackTypes.Integer)]
91  public class StringVectorLengthExpression : VectorLengthExpression<string> {
92    public StringVectorLengthExpression() { }
93    [StorableConstructor]
94    protected StringVectorLengthExpression(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, interpreter.StringVectorStack);
102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.