Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/VectorOccurrenceOfExpressions.cs @ 15032

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

#2665 Fixed bias 0 issue, PushExpressionFrequencyAnalyzer, Fixed probability for ERC settings, Fixed enable/disable instructions, Added expression descriptions

File size: 4.8 KB
Line 
1using System.Collections.Generic;
2
3namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
4  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
5  using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
6  using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
7  using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
8
9  /// <summary>
10  /// Counts the occurrences of the top lit-type item in the top type vector.
11  /// </summary>
12  /// <typeparam name="T"></typeparam>
13  [StorableClass]
14  public abstract class VectorOccurrenceOfExpression<T> : StatelessExpression {
15    protected VectorOccurrenceOfExpression() { }
16    [StorableConstructor]
17    protected VectorOccurrenceOfExpression(bool deserializing) : base(deserializing) { }
18
19    protected bool IsNoop(IPushStack<IReadOnlyList<T>> vectorStack, IPushStack<T> literalStack) {
20      return vectorStack.IsEmpty || literalStack.IsEmpty;
21    }
22
23    protected void Eval(
24      IInternalPushInterpreter interpreter,
25      IPushStack<IReadOnlyList<T>> vectorStack,
26      IPushStack<T> literalStack,
27      bool canOverride = false) {
28      var vector = vectorStack.Pop();
29      var literal = canOverride ? literalStack.Top : literalStack.Pop();
30
31      var occurrence = 0;
32      for (var i = 0; i < vector.Count; i++) {
33        if (vector[i].Equals(literal)) occurrence++;
34      }
35
36      if (canOverride) interpreter.IntegerStack.Top = occurrence;
37      else interpreter.IntegerStack.Push(occurrence);
38    }
39  }
40
41  [StorableClass]
42  [PushExpression(
43    StackTypes.IntegerVector,
44    "INTEGER[].OCCURRENCEOF",
45    "Pushes the amount of occurrences of the top INTEGER in the top INTEGER[] onto the INTEGER stack.",
46    StackTypes.Integer)]
47  public class IntegerVectorOccurrenceOfExpression : VectorOccurrenceOfExpression<long> {
48    public IntegerVectorOccurrenceOfExpression() { }
49    [StorableConstructor]
50    protected IntegerVectorOccurrenceOfExpression(bool deserializing) : base(deserializing) { }
51
52    public override bool IsNoop(IInternalPushInterpreter interpreter) {
53      return IsNoop(interpreter.IntegerVectorStack, interpreter.IntegerStack);
54    }
55
56    public override void Eval(IInternalPushInterpreter interpreter) {
57      Eval(interpreter, interpreter.IntegerVectorStack, interpreter.IntegerStack, true);
58    }
59  }
60
61  [StorableClass]
62  [PushExpression(
63    StackTypes.FloatVector,
64    "FLOAT[].OCCURRENCEOF",
65    "Pushes the amount of occurrences of the top FLOAT in the top FLOAT[] onto the INTEGER stack.",
66    StackTypes.Float | StackTypes.Integer)]
67  public class FloatVectorOccurrenceOfExpression : VectorOccurrenceOfExpression<double> {
68    public FloatVectorOccurrenceOfExpression() { }
69    [StorableConstructor]
70    protected FloatVectorOccurrenceOfExpression(bool deserializing) : base(deserializing) { }
71
72    public override bool IsNoop(IInternalPushInterpreter interpreter) {
73      return IsNoop(interpreter.FloatVectorStack, interpreter.FloatStack);
74    }
75
76    public override void Eval(IInternalPushInterpreter interpreter) {
77      Eval(interpreter, interpreter.FloatVectorStack, interpreter.FloatStack);
78    }
79  }
80
81  [StorableClass]
82  [PushExpression(
83    StackTypes.BooleanVector,
84    "BOOLEAN[].OCCURRENCEOF",
85    "Pushes the amount of occurrences of the top BOOLEAN in the top BOOLEAN[] onto the INTEGER stack.",
86    StackTypes.Boolean | StackTypes.Integer)]
87  public class BooleanVectorOccurrenceOfExpression : VectorOccurrenceOfExpression<bool> {
88    public BooleanVectorOccurrenceOfExpression() { }
89    [StorableConstructor]
90    protected BooleanVectorOccurrenceOfExpression(bool deserializing) : base(deserializing) { }
91
92    public override bool IsNoop(IInternalPushInterpreter interpreter) {
93      return IsNoop(interpreter.BooleanVectorStack, interpreter.BooleanStack);
94    }
95
96    public override void Eval(IInternalPushInterpreter interpreter) {
97      Eval(interpreter, interpreter.BooleanVectorStack, interpreter.BooleanStack);
98    }
99  }
100
101  [StorableClass]
102  [PushExpression(
103    StackTypes.StringVector,
104    "STRING[].OCCURRENCEOF",
105    "Pushes the amount of occurrences of the top STRING in the top STRING[] onto the INTEGER stack.",
106    StackTypes.String | StackTypes.Integer)]
107  public class StringVectorOccurrenceOfExpression : VectorOccurrenceOfExpression<string> {
108    public StringVectorOccurrenceOfExpression() { }
109    [StorableConstructor]
110    protected StringVectorOccurrenceOfExpression(bool deserializing) : base(deserializing) { }
111
112    public override bool IsNoop(IInternalPushInterpreter interpreter) {
113      return IsNoop(interpreter.StringVectorStack, interpreter.StringStack);
114    }
115
116    public override void Eval(IInternalPushInterpreter interpreter) {
117      Eval(interpreter, interpreter.StringVectorStack, interpreter.StringStack);
118    }
119  }
120}
Note: See TracBrowser for help on using the repository browser.