Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/VectorOccurrenceOfExpressions.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.6 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  /// Counts the occurrences of the top lit-type item in the top type vector.
10  /// </summary>
11  /// <typeparam name="T"></typeparam>
12  public abstract class VectorOccurrenceOfExpression<T> : StatelessExpression {
13    protected bool Eval(
14      IInternalPushInterpreter interpreter,
15      IPushStack<List<T>> vectorStack,
16      IPushStack<T> literalStack,
17      bool canOverride = false) {
18      if (vectorStack.IsEmpty || literalStack.IsEmpty)
19        return false;
20
21      var vector = vectorStack.Pop();
22      var literal = canOverride ? literalStack.Top : literalStack.Pop();
23
24      var occurrence = 0;
25      for (var i = 0; i < vector.Count; i++) {
26        if (vector[i].Equals(literal)) occurrence++;
27      }
28
29      if (canOverride) interpreter.IntegerStack.SetTop(occurrence);
30      else interpreter.IntegerStack.Push(occurrence);
31      return true;
32    }
33  }
34
35  [PushExpression(StackTypes.IntegerVector, "INTEGER[].OCCURRENCEOF", StackTypes.Integer)]
36  public class IntegerVectorOccurrenceOfExpression : VectorOccurrenceOfExpression<long> {
37    public override bool Eval(IInternalPushInterpreter interpreter) {
38      return Eval(interpreter, interpreter.IntegerVectorStack, interpreter.IntegerStack, true);
39    }
40  }
41
42  [PushExpression(StackTypes.FloatVector, "FLOAT[].OCCURRENCEOF", StackTypes.Float | StackTypes.Integer)]
43  public class FloatVectorOccurrenceOfExpression : VectorOccurrenceOfExpression<double> {
44    public override bool Eval(IInternalPushInterpreter interpreter) {
45      return Eval(interpreter, interpreter.FloatVectorStack, interpreter.FloatStack);
46    }
47  }
48
49  [PushExpression(StackTypes.BooleanVector, "BOOLEAN[].OCCURRENCEOF", StackTypes.Boolean | StackTypes.Integer)]
50  public class BooleanVectorOccurrenceOfExpression : VectorOccurrenceOfExpression<bool> {
51    public override bool Eval(IInternalPushInterpreter interpreter) {
52      return Eval(interpreter, interpreter.BooleanVectorStack, interpreter.BooleanStack);
53    }
54  }
55
56  [PushExpression(StackTypes.StringVector, "STRING[].OCCURRENCEOF", StackTypes.String | StackTypes.Integer)]
57  public class StringVectorOccurrenceOfExpression : VectorOccurrenceOfExpression<string> {
58    public override bool Eval(IInternalPushInterpreter interpreter) {
59      return Eval(interpreter, interpreter.StringVectorStack, interpreter.StringStack);
60    }
61  }
62}
Note: See TracBrowser for help on using the repository browser.