Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/VectorLengthExpressions.cs @ 15017

Last change on this file since 15017 was 15017, checked in by pkimmesw, 7 years ago

#2665 Fixed Benchmark Problem Definition, Converted LoopExpressions to stateless expressions, Added several unit test to ensure funcionality, Fixed UI bugs

File size: 3.5 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
2  using System.Collections.Generic;
3
4  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
5  using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
6  using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
7  using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
8
9  /// <summary>
10  /// Takes the length of the top item on the type stack
11  /// </summary>
12  /// <typeparam name="T"></typeparam>
13  [StorableClass]
14  public abstract class VectorLengthExpression<T> : StatelessExpression {
15    protected VectorLengthExpression() { }
16    [StorableConstructor]
17    protected VectorLengthExpression(bool deserializing) : base(deserializing) { }
18
19    protected bool IsNoop(IPushStack<IReadOnlyList<T>> vectorStack) {
20      return vectorStack.IsEmpty;
21    }
22
23    protected void Eval(IInternalPushInterpreter interpreter, IPushStack<IReadOnlyList<T>> vectorStack) {
24      var length = vectorStack.Pop().Count;
25      interpreter.IntegerStack.Push(length);
26    }
27  }
28
29  [StorableClass]
30  [PushExpression(StackTypes.IntegerVector, "INTEGER[].LENGTH", 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(StackTypes.FloatVector, "FLOAT[].LENGTH", StackTypes.Integer)]
47  public class FloatVectorLengthExpression : VectorLengthExpression<double> {
48    public FloatVectorLengthExpression() { }
49    [StorableConstructor]
50    protected FloatVectorLengthExpression(bool deserializing) : base(deserializing) { }
51
52    public override bool IsNoop(IInternalPushInterpreter interpreter) {
53      return IsNoop(interpreter.FloatVectorStack);
54    }
55
56    public override void Eval(IInternalPushInterpreter interpreter) {
57      Eval(interpreter, interpreter.FloatVectorStack);
58    }
59  }
60
61  [StorableClass]
62  [PushExpression(StackTypes.BooleanVector, "BOOLEAN[].LENGTH", StackTypes.Integer)]
63  public class BooleanVectorLengthExpression : VectorLengthExpression<bool> {
64    public BooleanVectorLengthExpression() { }
65    [StorableConstructor]
66    protected BooleanVectorLengthExpression(bool deserializing) : base(deserializing) { }
67
68    public override bool IsNoop(IInternalPushInterpreter interpreter) {
69      return IsNoop(interpreter.BooleanVectorStack);
70    }
71
72    public override void Eval(IInternalPushInterpreter interpreter) {
73      Eval(interpreter, interpreter.BooleanVectorStack);
74    }
75  }
76
77  [StorableClass]
78  [PushExpression(StackTypes.StringVector, "STRING[].LENGTH", StackTypes.Integer)]
79  public class StringVectorLengthExpression : VectorLengthExpression<string> {
80    public StringVectorLengthExpression() { }
81    [StorableConstructor]
82    protected StringVectorLengthExpression(bool deserializing) : base(deserializing) { }
83
84    public override bool IsNoop(IInternalPushInterpreter interpreter) {
85      return IsNoop(interpreter.StringVectorStack);
86    }
87
88    public override void Eval(IInternalPushInterpreter interpreter) {
89      Eval(interpreter, interpreter.StringVectorStack);
90    }
91  }
92}
Note: See TracBrowser for help on using the repository browser.