Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/VectorSetExpressions.cs @ 14834

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

#2665 LexicaseSelector, Performance improvements, UI Fixes, Debugger only shows used stacks, fixed Debugger stepping, Added vector expressions, ERCOptions,

File size: 2.9 KB
Line 
1using System.Collections.Generic;
2
3namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
4  using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
5  using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
6  using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
7
8  /// <summary>
9  /// Replaces, in the top type vector, item at index(from integer stack) with the first lit-type item.
10  /// </summary>
11  /// <typeparam name="T"></typeparam>
12  public abstract class VectorSetExpression<T> : StatelessExpression {
13    protected bool Eval(
14      IInternalPushInterpreter interpreter,
15      IPushStack<List<T>> vectorStack,
16      IPushStack<T> literalStack,
17      bool isLiteralTypeInteger = false) {
18      if (vectorStack.IsEmpty ||
19          literalStack.IsEmpty ||
20          (isLiteralTypeInteger && interpreter.IntegerStack.Count < 2) ||
21          (!isLiteralTypeInteger && interpreter.IntegerStack.IsEmpty))
22        return false;
23
24      T literal;
25      int index;
26
27      if (isLiteralTypeInteger) {
28        literal = literalStack.Top;
29        index = (int)interpreter.IntegerStack.ReverseElementAt(1);
30        interpreter.IntegerStack.Remove(2);
31      } else {
32        literal = literalStack.Pop();
33        index = (int)interpreter.IntegerStack.Pop();
34      }
35
36      var vector = vectorStack.Pop();
37      index = index % vector.Count;
38
39      if (index < 0)
40        index = vector.Count - index;
41
42      vector[index] = literal;
43      return true;
44    }
45  }
46
47  [PushExpression(StackTypes.IntegerVector, "INTEGER[].SET", StackTypes.Integer)]
48  public class IntegerVectorSetExpression : VectorSetExpression<long> {
49    public override bool Eval(IInternalPushInterpreter interpreter) {
50      return Eval(interpreter, interpreter.IntegerVectorStack, interpreter.IntegerStack, true);
51    }
52  }
53
54  [PushExpression(StackTypes.FloatVector, "FLOAT[].SET", StackTypes.Float | StackTypes.Integer)]
55  public class FloatVectorSetExpression : VectorSetExpression<double> {
56    public override bool Eval(IInternalPushInterpreter interpreter) {
57      return Eval(interpreter, interpreter.FloatVectorStack, interpreter.FloatStack);
58    }
59  }
60
61  [PushExpression(StackTypes.BooleanVector, "BOOLEAN[].SET", StackTypes.Boolean | StackTypes.Integer)]
62  public class BooleanVectorSetExpression : VectorSetExpression<bool> {
63    public override bool Eval(IInternalPushInterpreter interpreter) {
64      return Eval(interpreter, interpreter.BooleanVectorStack, interpreter.BooleanStack);
65    }
66  }
67
68  [PushExpression(StackTypes.StringVector, "STRING[].SET", StackTypes.String | StackTypes.Integer)]
69  public class StringVectorSetExpression : VectorSetExpression<string> {
70    public override bool Eval(IInternalPushInterpreter interpreter) {
71      return Eval(interpreter, interpreter.StringVectorStack, interpreter.StringStack);
72    }
73  }
74}
Note: See TracBrowser for help on using the repository browser.