Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/VectorFirstExpressions.cs @ 16801

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

#2665 Fixed bias 0 issue, PushExpressionFrequencyAnalyzer, Fixed probability for ERC settings, Fixed enable/disable instructions, Added expression descriptions

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