Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/VectorSetExpressions.cs @ 16003

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

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

File size: 4.9 KB
Line 
1using System.Collections.Generic;
2using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
3
4namespace HeuristicLab.Problems.ProgramSynthesis {
5  /// <summary>
6  /// Replaces, in the top type vector, item at index(from integer stack) with the first lit-type item.
7  /// </summary>
8  /// <typeparam name="T"></typeparam>
9  [StorableClass]
10  public abstract class VectorSetExpression<T> : StatelessExpression {
11    protected VectorSetExpression() { }
12    [StorableConstructor]
13    protected VectorSetExpression(bool deserializing) : base(deserializing) { }
14
15    protected bool IsNoop(IInternalPushInterpreter interpreter, IPushStack<IReadOnlyList<T>> vectorStack, IPushStack<T> literalStack, bool isLiteralTypeInteger = false) {
16      return vectorStack.IsEmpty ||
17             vectorStack.Top.Count == 0 ||
18             literalStack.IsEmpty ||
19             (isLiteralTypeInteger && interpreter.IntegerStack.Count < 2) ||
20             (!isLiteralTypeInteger && interpreter.IntegerStack.IsEmpty);
21    }
22
23    protected void Eval(
24      IInternalPushInterpreter interpreter,
25      IPushStack<IReadOnlyList<T>> vectorStack,
26      IPushStack<T> literalStack,
27      bool isLiteralTypeInteger = false) {
28      T literal;
29      long x;
30
31      if (isLiteralTypeInteger) {
32        literal = literalStack.Top;
33        x = interpreter.IntegerStack[1];
34        interpreter.IntegerStack.Remove(2);
35      } else {
36        literal = literalStack.Pop();
37        x = interpreter.IntegerStack.Pop();
38      }
39
40      var vector = vectorStack.Top;
41      var newTop = new List<T>(vector);
42      var index = x.AsInt(vector.Count);
43
44      if (index < 0)
45        index *= -1;
46
47      newTop[index] = literal;
48      vectorStack.Top = newTop;
49    }
50  }
51
52  [StorableClass]
53  [PushExpression(
54    StackTypes.IntegerVector,
55    "INTEGER[].SET",
56    "Replaces in the top INTEGER[] item at index (from INTEGER stack) with the first INTEGER item.",
57    StackTypes.Integer)]
58  public class IntegerVectorSetExpression : VectorSetExpression<long> {
59    public IntegerVectorSetExpression() { }
60    [StorableConstructor]
61    protected IntegerVectorSetExpression(bool deserializing) : base(deserializing) { }
62
63    public override bool IsNoop(IInternalPushInterpreter interpreter) {
64      return IsNoop(interpreter, interpreter.IntegerVectorStack, interpreter.IntegerStack, true);
65    }
66
67    public override void Eval(IInternalPushInterpreter interpreter) {
68      Eval(interpreter, interpreter.IntegerVectorStack, interpreter.IntegerStack, true);
69    }
70  }
71
72  [StorableClass]
73  [PushExpression(
74    StackTypes.FloatVector,
75    "FLOAT[].SET",
76    "Replaces in the top FLOAT[] item at index (from INTEGER stack) with the first FLOAT item.",
77    StackTypes.Float | StackTypes.Integer)]
78  public class FloatVectorSetExpression : VectorSetExpression<double> {
79    public FloatVectorSetExpression() { }
80    [StorableConstructor]
81    protected FloatVectorSetExpression(bool deserializing) : base(deserializing) { }
82
83    public override bool IsNoop(IInternalPushInterpreter interpreter) {
84      return IsNoop(interpreter, interpreter.FloatVectorStack, interpreter.FloatStack);
85    }
86
87    public override void Eval(IInternalPushInterpreter interpreter) {
88      Eval(interpreter, interpreter.FloatVectorStack, interpreter.FloatStack);
89    }
90  }
91
92  [StorableClass]
93  [PushExpression(
94    StackTypes.BooleanVector,
95    "BOOLEAN[].SET",
96    "Replaces in the top BOOLEAN[] item at index (from INTEGER stack) with the first BOOLEAN item.",
97    StackTypes.Boolean | StackTypes.Integer)]
98  public class BooleanVectorSetExpression : VectorSetExpression<bool> {
99    public BooleanVectorSetExpression() { }
100    [StorableConstructor]
101    protected BooleanVectorSetExpression(bool deserializing) : base(deserializing) { }
102
103    public override bool IsNoop(IInternalPushInterpreter interpreter) {
104      return IsNoop(interpreter, interpreter.BooleanVectorStack, interpreter.BooleanStack);
105    }
106
107    public override void Eval(IInternalPushInterpreter interpreter) {
108      Eval(interpreter, interpreter.BooleanVectorStack, interpreter.BooleanStack);
109    }
110  }
111
112  [StorableClass]
113  [PushExpression(
114    StackTypes.StringVector,
115    "STRING[].SET",
116    "Replaces in the top STRING[] item at index (from INTEGER stack) with the first STRING item.",
117    StackTypes.String | StackTypes.Integer)]
118  public class StringVectorSetExpression : VectorSetExpression<string> {
119    public StringVectorSetExpression() { }
120    [StorableConstructor]
121    protected StringVectorSetExpression(bool deserializing) : base(deserializing) { }
122
123    public override bool IsNoop(IInternalPushInterpreter interpreter) {
124      return IsNoop(interpreter, interpreter.StringVectorStack, interpreter.StringStack);
125    }
126
127    public override void Eval(IInternalPushInterpreter interpreter) {
128      Eval(interpreter, interpreter.StringVectorStack, interpreter.StringStack);
129    }
130  }
131}
Note: See TracBrowser for help on using the repository browser.