Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/VectorRemoveExpressions.cs @ 17021

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

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

File size: 3.9 KB
Line 
1using System.Collections.Generic;
2using System.Linq;
3using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
4
5namespace HeuristicLab.Problems.ProgramSynthesis {
6  /// <summary>
7  /// Removes all occurrences of the first lit-type item in the top type vector.
8  /// </summary>
9  /// <typeparam name="T"></typeparam>
10  [StorableClass]
11  public abstract class VectorRemoveExpression<T> : StatelessExpression {
12    protected VectorRemoveExpression() { }
13    [StorableConstructor]
14    protected VectorRemoveExpression(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(IPushStack<IReadOnlyList<T>> vectorStack, IPushStack<T> literalStack) {
21      var literal = literalStack.Pop();
22      vectorStack.Top = vectorStack.Top.Where(x => !x.Equals(literal)).ToList();
23    }
24  }
25
26  [StorableClass]
27  [PushExpression(
28    StackTypes.IntegerVector,
29    "INTEGER[].REMOVE",
30    "Removes all occurrences of the top INTEGER in the top INTEGER[].",
31    StackTypes.Integer)]
32  public class IntegerVectorRemoveExpression : VectorRemoveExpression<long> {
33    public IntegerVectorRemoveExpression() { }
34    [StorableConstructor]
35    protected IntegerVectorRemoveExpression(bool deserializing) : base(deserializing) { }
36
37    public override bool IsNoop(IInternalPushInterpreter interpreter) {
38      return IsNoop(interpreter.IntegerVectorStack, interpreter.IntegerStack);
39    }
40
41    public override void Eval(IInternalPushInterpreter interpreter) {
42      Eval(interpreter.IntegerVectorStack, interpreter.IntegerStack);
43    }
44  }
45
46  [StorableClass]
47  [PushExpression(
48    StackTypes.FloatVector,
49    "FLOAT[].REMOVE",
50    "Removes all occurrences of the top FLOAT in the top FLOAT[].",
51    StackTypes.Float)]
52  public class FloatVectorRemoveExpression : VectorRemoveExpression<double> {
53    public FloatVectorRemoveExpression() { }
54    [StorableConstructor]
55    protected FloatVectorRemoveExpression(bool deserializing) : base(deserializing) { }
56
57    public override bool IsNoop(IInternalPushInterpreter interpreter) {
58      return IsNoop(interpreter.FloatVectorStack, interpreter.FloatStack);
59    }
60
61    public override void Eval(IInternalPushInterpreter interpreter) {
62      Eval(interpreter.FloatVectorStack, interpreter.FloatStack);
63    }
64  }
65
66  [StorableClass]
67  [PushExpression(
68    StackTypes.BooleanVector,
69    "BOOLEAN[].REMOVE",
70    "Removes all occurrences of the top BOOLEAN in the top BOOLEAN[].",
71    StackTypes.Boolean)]
72  public class BooleanVectorRemoveExpression : VectorRemoveExpression<bool> {
73    public BooleanVectorRemoveExpression() { }
74    [StorableConstructor]
75    protected BooleanVectorRemoveExpression(bool deserializing) : base(deserializing) { }
76
77    public override bool IsNoop(IInternalPushInterpreter interpreter) {
78      return IsNoop(interpreter.BooleanVectorStack, interpreter.BooleanStack);
79    }
80
81    public override void Eval(IInternalPushInterpreter interpreter) {
82      Eval(interpreter.BooleanVectorStack, interpreter.BooleanStack);
83    }
84  }
85
86  [StorableClass]
87  [PushExpression(
88    StackTypes.StringVector,
89    "STRING[].REMOVE",
90    "Removes all occurrences of the top STRING in the top STRING[].",
91    StackTypes.String)]
92  public class StringVectorRemoveExpression : VectorRemoveExpression<string> {
93    public StringVectorRemoveExpression() { }
94    [StorableConstructor]
95    protected StringVectorRemoveExpression(bool deserializing) : base(deserializing) { }
96
97    public override bool IsNoop(IInternalPushInterpreter interpreter) {
98      return IsNoop(interpreter.StringVectorStack, interpreter.StringStack);
99    }
100
101    public override void Eval(IInternalPushInterpreter interpreter) {
102      Eval(interpreter.StringVectorStack, interpreter.StringStack);
103    }
104  }
105}
Note: See TracBrowser for help on using the repository browser.