Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/BooleanExpressions.cs @ 16752

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

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

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