Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/VectorIterateExpressions.cs @ 16724

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

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

File size: 5.3 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  /// Iterates in reverse order (due to performance reasons) over the type vector using the code on the exec stack.If the vector isn't empty, expands to:
9  /// ((first vector) (top-item :exec state) (rest vector) exec_do*vector_type (top-item :exec state) rest_of_program)
10  /// </summary>
11  /// <typeparam name="T"></typeparam>
12  [StorableClass]
13  public abstract class VectorIterateExpression<T> : StatelessExpression {
14    protected VectorIterateExpression() { }
15    [StorableConstructor]
16    protected VectorIterateExpression(bool deserializing) : base(deserializing) { }
17
18    protected bool IsNoop(IInternalPushInterpreter interpter, IPushStack<IReadOnlyList<T>> vectorStack) {
19      return vectorStack.IsEmpty || interpter.ExecStack.IsEmpty;
20    }
21
22    protected void Eval(IInternalPushInterpreter interpter, IPushStack<IReadOnlyList<T>> vectorStack, IPushStack<T> literalStack) {
23      var vector = vectorStack.Top;
24
25      if (vector.Count == 0) {
26        interpter.ExecStack.Pop();
27        vectorStack.Pop();
28      } else if (vector.Count == 1) {
29        literalStack.Push(vector[0]);
30        vectorStack.Pop();
31      } else {
32        interpter.ExecStack.Push(this, interpter.ExecStack.Top);
33
34        var last = vector.Count - 1;
35        literalStack.Push(vector[last]);
36
37        var list = vector as List<T>;
38        if (list != null) {
39          var newTop = new T[last];
40          list.CopyTo(0, newTop, 0, last);
41          vectorStack.Top = newTop;
42          return;
43        }
44
45        var array = vector as T[];
46        if (array != null) {
47          var newTop = new T[last];
48          Array.Copy(array, 0, newTop, 0, last);
49          vectorStack.Top = newTop;
50          return;
51        }
52
53        vectorStack.Top = vector.Take(last).ToList();
54      }
55    }
56  }
57
58  [StorableClass]
59  [PushExpression(
60    StackTypes.IntegerVector,
61    "INTEGER[].ITERATE",
62    "Iterates in reverse order (due to performance reasons) over the top INTEGER[] using the top item of the EXEC stack.",
63    StackTypes.Integer | StackTypes.Exec,
64    requiredBlockCount: 1)]
65  public class IntegerVectorIterateExpression : VectorIterateExpression<long> {
66    public IntegerVectorIterateExpression() { }
67    [StorableConstructor]
68    protected IntegerVectorIterateExpression(bool deserializing) : base(deserializing) { }
69
70    public override bool IsNoop(IInternalPushInterpreter interpreter) {
71      return IsNoop(interpreter, interpreter.IntegerVectorStack);
72    }
73
74    public override void Eval(IInternalPushInterpreter interpreter) {
75      Eval(interpreter, interpreter.IntegerVectorStack, interpreter.IntegerStack);
76    }
77  }
78
79  [StorableClass]
80  [PushExpression(
81    StackTypes.FloatVector,
82    "FLOAT[].ITERATE",
83    "Iterates in reverse order (due to performance reasons) over the top FLOAT[] using the top item of the EXEC stack.",
84    StackTypes.Float | StackTypes.Exec, requiredBlockCount: 1)]
85  public class FloatVectorIterateExpression : VectorIterateExpression<double> {
86    public FloatVectorIterateExpression() { }
87    [StorableConstructor]
88    protected FloatVectorIterateExpression(bool deserializing) : base(deserializing) { }
89
90    public override bool IsNoop(IInternalPushInterpreter interpreter) {
91      return IsNoop(interpreter, interpreter.FloatVectorStack);
92    }
93
94    public override void Eval(IInternalPushInterpreter interpreter) {
95      Eval(interpreter, interpreter.FloatVectorStack, interpreter.FloatStack);
96    }
97  }
98
99  [StorableClass]
100  [PushExpression(
101    StackTypes.BooleanVector,
102    "BOOLEAN[].ITERATE",
103    "Iterates in reverse order (due to performance reasons) over the top BOOLEAN[] using the top item of the EXEC stack.",
104    StackTypes.Boolean | StackTypes.Exec, requiredBlockCount: 1)]
105  public class BooleanVectorIterateExpression : VectorIterateExpression<bool> {
106    public BooleanVectorIterateExpression() { }
107    [StorableConstructor]
108    protected BooleanVectorIterateExpression(bool deserializing) : base(deserializing) { }
109
110    public override bool IsNoop(IInternalPushInterpreter interpreter) {
111      return IsNoop(interpreter, interpreter.BooleanVectorStack);
112    }
113
114    public override void Eval(IInternalPushInterpreter interpreter) {
115      Eval(interpreter, interpreter.BooleanVectorStack, interpreter.BooleanStack);
116    }
117  }
118
119  [StorableClass]
120  [PushExpression(
121    StackTypes.StringVector,
122    "STRING[].ITERATE",
123    "Iterates in reverse order (due to performance reasons) over the top STRING[] using the top item of the EXEC stack.",
124    StackTypes.String | StackTypes.Exec, requiredBlockCount: 1)]
125  public class StringVectorIterateExpression : VectorIterateExpression<string> {
126    public StringVectorIterateExpression() { }
127    [StorableConstructor]
128    protected StringVectorIterateExpression(bool deserializing) : base(deserializing) { }
129
130    public override bool IsNoop(IInternalPushInterpreter interpreter) {
131      return IsNoop(interpreter, interpreter.StringVectorStack);
132    }
133
134    public override void Eval(IInternalPushInterpreter interpreter) {
135      Eval(interpreter, interpreter.StringVectorStack, interpreter.StringStack);
136    }
137  }
138}
Note: See TracBrowser for help on using the repository browser.