Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/BooleanExpressions.cs @ 14398

Last change on this file since 14398 was 14398, checked in by pkimmesw, 8 years ago

#2665 Expressions are splitted into StatefullExpressions and StatelessExpressions, Added traits for tests

File size: 2.5 KB
Line 
1using HeuristicLab.Algorithms.PushGP.Interpreter;
2
3namespace HeuristicLab.Algorithms.PushGP.Expressions
4{
5    /// <summary>
6    /// Pushes the logical AND of the top two BOOLEANs.
7    /// </summary>
8    public class BooleanAndExpression : PushResultExpression<bool>
9    {
10        protected override string InitStringRepresentation()
11        {
12            return "BOOLEAN.AND";
13        }
14
15        public override void Eval(IInterpreter interpreter)
16        {
17            Eval(interpreter.BooleanStack, 2, values => values[0] && values[1]);
18        }
19    }
20
21    /// <summary>
22    /// Pushes the logical OR of the top two BOOLEANs.
23    /// </summary>
24    public class BooleanOrExpression : PushResultExpression<bool>
25    {
26        protected override string InitStringRepresentation()
27        {
28            return "BOOLEAN.OR";
29        }
30
31        public override void Eval(IInterpreter interpreter)
32        {
33            Eval(interpreter.BooleanStack, 2, values => values[0] || values[1]);
34        }
35    }
36
37    /// <summary>
38    /// Pushes the logical NOT of the top BOOLEAN.
39    /// </summary>
40    public class BooleanNotExpression : PushResultExpression<bool>
41    {
42        protected override string InitStringRepresentation() { return "BOOLEAN.NOT"; }
43
44        public override void Eval(IInterpreter interpreter)
45        {
46            Eval(interpreter.BooleanStack, 1, values => !values[0]);
47        }
48    }
49
50    /// <summary>
51    /// Pushes FALSE if the top FLOAT is 0.0, or TRUE otherwise.
52    /// </summary>
53    public class BooleanFromFloatExpression : StatelessExpression
54    {
55        protected override string InitStringRepresentation() { return "BOOLEAN.FROMFLOAT"; }
56
57        public override void Eval(IInterpreter interpreter)
58        {
59            if (interpreter.FloatStack.Count == 0)
60                return;
61
62            var value = interpreter.FloatStack.Pop() != 0.0;
63
64            interpreter.BooleanStack.Push(value);
65        }
66    }
67
68    /// <summary>
69    /// Pushes FALSE if the top INTEGER is 0, or TRUE otherwise.
70    /// </summary>
71    public class BooleanFromIntegerExpression : StatelessExpression
72    {
73        protected override string InitStringRepresentation() { return "BOOLEAN.FROMINTEGER"; }
74
75        public override void Eval(IInterpreter interpreter)
76        {
77            if (interpreter.IntegerStack.Count == 0)
78                return;
79
80            var value = interpreter.IntegerStack.Pop() != 0;
81
82            interpreter.BooleanStack.Push(value);
83        }
84    }
85}
Note: See TracBrowser for help on using the repository browser.