Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/BooleanExpressions.cs @ 14952

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

#2665 Added IsNoop to Expression, Made Expressions storable, Fixed Debugger, Fixed and improved problem data and result visualisation, Added custom ErcOption view, Added problem difficulty to problem data name

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