Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/VectorContainsExpressions.cs @ 17189

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

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

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