Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/VectorConcatExpressions.cs @ 16958

Last change on this file since 16958 was 15032, checked in by pkimmesw, 8 years ago

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

File size: 4.0 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  /// Concatinates two vectors on the type stack.
11  /// </summary>
12  [StorableClass]
13  public abstract class VectorConcatExpression<T> : StatelessExpression {
14    protected VectorConcatExpression() { }
15    [StorableConstructor]
16    protected VectorConcatExpression(bool deserializing) : base(deserializing) { }
17
18    protected bool IsNoop(IInternalPushInterpreter interpreter, IPushStack<IReadOnlyList<T>> vectorStack) {
19      return vectorStack.Count < 2 ||
20             vectorStack.Top.Count + vectorStack[1].Count > interpreter.Configuration.MaxVectorLength;
21    }
22
23    protected void Eval(IInternalPushInterpreter interpter, IPushStack<IReadOnlyList<T>> vectorStack) {
24      var first = vectorStack.Pop();
25      var second = vectorStack.Top;
26      var result = new List<T>(second.Count + first.Count);
27      result.AddRange(second);
28      result.AddRange(first);
29
30      vectorStack.Top = result;
31    }
32  }
33
34  [StorableClass]
35  [PushExpression(
36    StackTypes.IntegerVector,
37    "INTEGER[].CONCAT",
38    "Concatenates the top two vectors on the INTEGER[] stack.")]
39  public class IntegerVectorConcatExpression : VectorConcatExpression<long> {
40    public IntegerVectorConcatExpression() { }
41    [StorableConstructor]
42    protected IntegerVectorConcatExpression(bool deserializing) : base(deserializing) { }
43
44    public override bool IsNoop(IInternalPushInterpreter interpreter) {
45      return IsNoop(interpreter, interpreter.IntegerVectorStack);
46    }
47
48    public override void Eval(IInternalPushInterpreter interpreter) {
49      Eval(interpreter, interpreter.IntegerVectorStack);
50    }
51  }
52
53  [StorableClass]
54  [PushExpression(
55    StackTypes.FloatVector,
56    "FLOAT[].CONCAT",
57    "Concatenates the top two vectors on the FLOAT[] stack.")]
58  public class FloatVectorConcatExpression : VectorConcatExpression<double> {
59    public FloatVectorConcatExpression() { }
60    [StorableConstructor]
61    protected FloatVectorConcatExpression(bool deserializing) : base(deserializing) { }
62
63    public override bool IsNoop(IInternalPushInterpreter interpreter) {
64      return IsNoop(interpreter, interpreter.FloatVectorStack);
65    }
66
67    public override void Eval(IInternalPushInterpreter interpreter) {
68      Eval(interpreter, interpreter.FloatVectorStack);
69    }
70  }
71
72  [StorableClass]
73  [PushExpression(
74    StackTypes.BooleanVector,
75    "BOOLEAN[].CONCAT",
76    "Concatenates the top two vectors on the BOOLEAN[] stack.")]
77  public class BooleanVectorConcatExpression : VectorConcatExpression<bool> {
78    public BooleanVectorConcatExpression() { }
79    [StorableConstructor]
80    protected BooleanVectorConcatExpression(bool deserializing) : base(deserializing) { }
81
82    public override bool IsNoop(IInternalPushInterpreter interpreter) {
83      return IsNoop(interpreter, interpreter.BooleanVectorStack);
84    }
85
86    public override void Eval(IInternalPushInterpreter interpreter) {
87      Eval(interpreter, interpreter.BooleanVectorStack);
88    }
89  }
90
91  [StorableClass]
92  [PushExpression(
93    StackTypes.StringVector,
94    "STRING[].CONCAT",
95    "Concatenates the top two vectors on the STRING[] stack.")]
96  public class StringVectorConcatExpression : VectorConcatExpression<string> {
97    public StringVectorConcatExpression() { }
98    [StorableConstructor]
99    protected StringVectorConcatExpression(bool deserializing) : base(deserializing) { }
100
101    public override bool IsNoop(IInternalPushInterpreter interpreter) {
102      return IsNoop(interpreter, interpreter.StringVectorStack);
103    }
104
105    public override void Eval(IInternalPushInterpreter interpreter) {
106      Eval(interpreter, interpreter.StringVectorStack);
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.