Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/BooleanExpressions.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: 6.7 KB
Line 
1using System;
2using HeuristicLab.Common;
3using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
4
5namespace HeuristicLab.Problems.ProgramSynthesis {
6
7  /// <summary>
8  ///     Pushes the logical AND of the top two BOOLEANs.
9  /// </summary>
10  [PushExpression(StackTypes.Boolean, "BOOLEAN.AND", "Pushes the logical AND of the top two BOOLEANs.")]
11  [StorableClass]
12  public class BooleanAndExpression : StatelessExpression {
13    public BooleanAndExpression() { }
14    [StorableConstructor]
15    public BooleanAndExpression(bool deserializing) : base(deserializing) { }
16
17    public override bool IsNoop(IInternalPushInterpreter interpreter) {
18      return interpreter.BooleanStack.Count < 2;
19    }
20
21    public override void Eval(IInternalPushInterpreter interpreter) {
22      var first = interpreter.BooleanStack.Pop();
23      var second = interpreter.BooleanStack.Top;
24      var result = second && first;
25
26      interpreter.BooleanStack.Top = result;
27    }
28  }
29
30  /// <summary>
31  ///     Pushes the logical OR of the top two BOOLEANs.
32  /// </summary>
33  [PushExpression(StackTypes.Boolean, "BOOLEAN.OR", "Pushes the logical OR of the top two BOOLEANs.")]
34  [StorableClass]
35  public class BooleanOrExpression : StatelessExpression {
36    public BooleanOrExpression() { }
37    [StorableConstructor]
38    public BooleanOrExpression(bool deserializing) : base(deserializing) { }
39
40    public override bool IsNoop(IInternalPushInterpreter interpreter) {
41      return interpreter.BooleanStack.Count < 2;
42    }
43
44    public override void Eval(IInternalPushInterpreter interpreter) {
45      var first = interpreter.BooleanStack.Pop();
46      var second = interpreter.BooleanStack.Top;
47      var result = second || first;
48
49      interpreter.BooleanStack.Top = result;
50    }
51  }
52
53  /// <summary>
54  ///     Pushes the logical NOT of the top BOOLEAN.
55  /// </summary>
56  [PushExpression(StackTypes.Boolean, "BOOLEAN.NOT", "Pushes the logical NOT of the top BOOLEAN.")]
57  [StorableClass]
58  public class BooleanNotExpression : StatelessExpression {
59    public BooleanNotExpression() { }
60    [StorableConstructor]
61    public BooleanNotExpression(bool deserializing) : base(deserializing) { }
62
63    public override bool IsNoop(IInternalPushInterpreter interpreter) {
64      return interpreter.BooleanStack.IsEmpty;
65    }
66
67    public override void Eval(IInternalPushInterpreter interpreter) {
68      interpreter.BooleanStack.Top = !interpreter.BooleanStack.Top;
69    }
70  }
71
72  /// <summary>
73  ///     Pushes FALSE if the top FLOAT is 0.0, or TRUE otherwise.
74  /// </summary>
75  [PushExpression(StackTypes.Boolean, "BOOLEAN.FROMFLOAT", "Pushes FALSE if the top FLOAT is 0.0, or TRUE otherwise.", StackTypes.Float)]
76  [StorableClass]
77  public class BooleanFromFloatExpression : StatelessExpression {
78    public BooleanFromFloatExpression() { }
79    [StorableConstructor]
80    public BooleanFromFloatExpression(bool deserializing) : base(deserializing) { }
81
82    public override bool IsNoop(IInternalPushInterpreter interpreter) {
83      return interpreter.FloatStack.Count == 0;
84    }
85
86    public override void Eval(IInternalPushInterpreter interpreter) {
87      var value = !Math.Abs(interpreter.FloatStack.Pop()).IsAlmost(0);
88      interpreter.BooleanStack.Push(value);
89    }
90  }
91
92  /// <summary>
93  ///     Pushes FALSE if the top INTEGER is 0, or TRUE otherwise.
94  /// </summary>
95  [PushExpression(StackTypes.Boolean, "BOOLEAN.FROMINTEGER", "Pushes FALSE if the top INTEGER is 0, or TRUE otherwise.", StackTypes.Integer)]
96  [StorableClass]
97  public class BooleanFromIntegerExpression : StatelessExpression {
98    public BooleanFromIntegerExpression() { }
99    [StorableConstructor]
100    public BooleanFromIntegerExpression(bool deserializing) : base(deserializing) { }
101
102    public override bool IsNoop(IInternalPushInterpreter interpreter) {
103      return interpreter.IntegerStack.Count == 0;
104    }
105
106    public override void Eval(IInternalPushInterpreter interpreter) {
107      var value = interpreter.IntegerStack.Pop() != 0;
108      interpreter.BooleanStack.Push(value);
109    }
110  }
111
112  /// <summary>
113  ///     Pushes the logical AND of the top two BOOLEANs, whereby the first value is inverted first.
114  /// </summary>
115  [PushExpression(StackTypes.Boolean, "BOOLEAN.INVERT_FIRST_THEN_AND", "Pushes the logical AND of the top two BOOLEANs, whereby the first value is inverted first.")]
116  [StorableClass]
117  public class BooleanInvertFirstThenAnd : StatelessExpression {
118    public BooleanInvertFirstThenAnd() { }
119    [StorableConstructor]
120    public BooleanInvertFirstThenAnd(bool deserializing) : base(deserializing) { }
121
122    public override bool IsNoop(IInternalPushInterpreter interpreter) {
123      return interpreter.BooleanStack.Count < 2;
124    }
125
126    public override void Eval(IInternalPushInterpreter interpreter) {
127      var first = interpreter.BooleanStack.Pop();
128      var second = interpreter.BooleanStack.Top;
129      var result = second && !first;
130
131      interpreter.BooleanStack.Top = result;
132    }
133  }
134
135  /// <summary>
136  ///     Pushes the logical AND of the top two BOOLEANs, whereby the second value is inverted first.
137  /// </summary>
138  [PushExpression(StackTypes.Boolean, "BOOLEAN.INVERT_SECOND_THEN_AND", "Pushes the logical AND of the top two BOOLEANs, whereby the second value is inverted first.")]
139  [StorableClass]
140  public class BooleanInvertSecondThenAnd : StatelessExpression {
141    public BooleanInvertSecondThenAnd() { }
142    [StorableConstructor]
143    public BooleanInvertSecondThenAnd(bool deserializing) : base(deserializing) { }
144
145    public override bool IsNoop(IInternalPushInterpreter interpreter) {
146      return interpreter.BooleanStack.Count < 2;
147    }
148
149    public override void Eval(IInternalPushInterpreter interpreter) {
150      var first = interpreter.BooleanStack.Pop();
151      var second = interpreter.BooleanStack.Top;
152      var result = !second && first;
153
154      interpreter.BooleanStack.Top = result;
155    }
156  }
157
158
159  /// <summary>
160  ///     Pushes the xor of the top tow BOOLEANs
161  /// </summary>
162  [PushExpression(StackTypes.Boolean, "BOOLEAN.XOR", "Pushes the xor of the top tow BOOLEANs.")]
163  [StorableClass]
164  public class BooleanXorExpression : StatelessExpression {
165    public BooleanXorExpression() { }
166    [StorableConstructor]
167    public BooleanXorExpression(bool deserializing) : base(deserializing) { }
168
169    public override bool IsNoop(IInternalPushInterpreter interpreter) {
170      return interpreter.BooleanStack.Count < 2;
171    }
172
173    public override void Eval(IInternalPushInterpreter interpreter) {
174      var first = interpreter.BooleanStack.Pop();
175      var second = interpreter.BooleanStack.Top;
176      var result = second ^ first;
177
178      interpreter.BooleanStack.Top = result;
179    }
180  }
181}
Note: See TracBrowser for help on using the repository browser.