Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/VectorSubExpressions.cs @ 18242

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

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

File size: 4.4 KB
Line 
1using System.Collections.Generic;
2using System;
3using System.Linq;
4using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
5
6namespace HeuristicLab.Problems.ProgramSynthesis {
7  /// <summary>
8  /// Takes the subvec of the top item on the type stack.
9  /// </summary>
10  /// <typeparam name="T"></typeparam>
11  [StorableClass]
12  public abstract class VectorSubExpression<T> : StatelessExpression {
13    protected VectorSubExpression() { }
14    [StorableConstructor]
15    protected VectorSubExpression(bool deserializing) : base(deserializing) { }
16
17    protected bool IsNoop(IInternalPushInterpreter interpreter, IPushStack<IReadOnlyList<T>> vectorStack) {
18      return vectorStack.IsEmpty || interpreter.IntegerStack.Count < 2;
19    }
20
21    protected void Eval(IInternalPushInterpreter interpreter, IPushStack<IReadOnlyList<T>> vectorStack) {
22      var first = (int)Math.Min(vectorStack.Top.Count, Math.Max(0, interpreter.IntegerStack[1]));
23      var second = (int)Math.Min(vectorStack.Top.Count, Math.Max(first, interpreter.IntegerStack.Top));
24      interpreter.IntegerStack.Remove(2);
25
26      if (vectorStack.Top.Count == 0)
27        return;
28
29      var vector = vectorStack.Top;
30      var list = vector as List<T>;
31      var lenght = second - first;
32
33      if (list != null) {
34        var newTop = new T[lenght];
35        list.CopyTo(first, newTop, 0, lenght);
36        vectorStack.Top = newTop;
37        return;
38      }
39
40      var array = vector as T[];
41      if (array != null) {
42        var newTop = new T[lenght];
43        Array.Copy(array, first, newTop, 0, lenght);
44        vectorStack.Top = newTop;
45        return;
46      }
47
48      vectorStack.Top = vector.Skip(first).Take(lenght).ToList();
49    }
50  }
51
52  [StorableClass]
53  [PushExpression(
54    StackTypes.IntegerVector,
55    "INTEGER[].SUB",
56    "Pushes the sub vector of the top INTEGER[].",
57    StackTypes.Integer)]
58  public class IntegerVectorSubExpression : VectorSubExpression<long> {
59    public IntegerVectorSubExpression() { }
60    [StorableConstructor]
61    protected IntegerVectorSubExpression(bool deserializing) : base(deserializing) { }
62
63    public override bool IsNoop(IInternalPushInterpreter interpreter) {
64      return IsNoop(interpreter, interpreter.IntegerVectorStack);
65    }
66
67    public override void Eval(IInternalPushInterpreter interpreter) {
68      Eval(interpreter, interpreter.IntegerVectorStack);
69    }
70  }
71
72  [StorableClass]
73  [PushExpression(
74    StackTypes.FloatVector,
75    "FLOAT[].SUB",
76    "Pushes the sub vector of the top FLOAT[].",
77    StackTypes.Integer)]
78  public class FloatVectorSubExpression : VectorSubExpression<double> {
79    public FloatVectorSubExpression() { }
80    [StorableConstructor]
81    protected FloatVectorSubExpression(bool deserializing) : base(deserializing) { }
82
83    public override bool IsNoop(IInternalPushInterpreter interpreter) {
84      return IsNoop(interpreter, interpreter.FloatVectorStack);
85    }
86
87    public override void Eval(IInternalPushInterpreter interpreter) {
88      Eval(interpreter, interpreter.FloatVectorStack);
89    }
90  }
91
92  [StorableClass]
93  [PushExpression(
94    StackTypes.BooleanVector,
95    "BOOLEAN[].SUB",
96    "Pushes the sub vector of the top BOOLEAN[].",
97    StackTypes.Integer)]
98  public class BooleanVectorSubExpression : VectorSubExpression<bool> {
99    public BooleanVectorSubExpression() { }
100    [StorableConstructor]
101    protected BooleanVectorSubExpression(bool deserializing) : base(deserializing) { }
102
103    public override bool IsNoop(IInternalPushInterpreter interpreter) {
104      return IsNoop(interpreter, interpreter.BooleanVectorStack);
105    }
106
107    public override void Eval(IInternalPushInterpreter interpreter) {
108      Eval(interpreter, interpreter.BooleanVectorStack);
109    }
110  }
111
112  [StorableClass]
113  [PushExpression(
114    StackTypes.StringVector,
115    "STRING[].SUB",
116    "Pushes the sub vector of the top STRING[].",
117    StackTypes.Integer)]
118  public class StringVectorSubExpression : VectorSubExpression<string> {
119    public StringVectorSubExpression() { }
120    [StorableConstructor]
121    protected StringVectorSubExpression(bool deserializing) : base(deserializing) { }
122
123    public override bool IsNoop(IInternalPushInterpreter interpreter) {
124      return IsNoop(interpreter, interpreter.StringVectorStack);
125    }
126
127    public override void Eval(IInternalPushInterpreter interpreter) {
128      Eval(interpreter, interpreter.StringVectorStack);
129    }
130  }
131}
Note: See TracBrowser for help on using the repository browser.