Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Robocode/HeuristicLab.Problems.Robocode/Symbols/Numerical Expressions/NumericalOperation.cs @ 9584

Last change on this file since 9584 was 9565, checked in by melkaref, 12 years ago

Robocode Plugin code without Mutation Operators

File size: 3.3 KB
Line 
1using HeuristicLab.Common;
2using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
3using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
4
5namespace HeuristicLab.Problems.Robocode
6{
7    [StorableClass]
8    public class NumericalOperation : CodeNode
9    {
10        public override int MinimumArity { get { return 3; } }
11        public override int MaximumArity { get { return 3; } }
12
13        [Storable]
14        public override string Prefix { get; set; }
15
16        [Storable]
17        public override string Suffix { get; set; }
18
19        [StorableConstructor]
20        private NumericalOperation(bool deserializing) : base(deserializing) { }
21        private NumericalOperation(NumericalOperation original, Cloner cloner)
22            : base(original, cloner)
23        {
24            this.Prefix = "(";
25            this.Suffix = ")";
26        }
27
28        public NumericalOperation()
29            : base("NumericalOperation", "An expression consisting of two numbers and an operator.")
30        {
31            this.Prefix = "(";
32            this.Suffix = ")";
33        }
34
35        public override IDeepCloneable Clone(Cloner cloner)
36        {
37            return new NumericalOperation(this, cloner);
38        }
39
40        public override string Interpret(ISymbolicExpressionTreeNode node, System.Collections.Generic.IEnumerable<ISymbolicExpressionTreeNode> children)
41        {
42            ISymbolicExpressionTreeNode condition = null, lhs = null, rhs = null;
43            var enumerator = children.GetEnumerator();
44            int childCount = 0;
45            while (enumerator.MoveNext())
46            {
47                childCount++;
48                switch (childCount)
49                {
50                    case 1: condition = enumerator.Current; break;
51                    case 2: lhs = enumerator.Current; break;
52                    case 3: rhs = enumerator.Current; break;
53                    default: throw new System.Exception("Unexpected number of children. Expected 3 children.");
54                }
55            }
56            if (childCount < 3) throw new System.Exception("Unexpected number of children. Expected 3 children.");
57
58            if (!(condition.Symbol is Addition || condition.Symbol is Division ||
59                condition.Symbol is Modulus || condition.Symbol is Multiplication || condition.Symbol is Subtraction))
60                throw new System.Exception("Unexpected first child for NumericalOperation of type: " +
61                    condition.GetType().ToString() + ". Expected " + typeof(Addition).ToString() +
62                    " or " + typeof(Division).ToString() +
63                    " or " + typeof(Modulus).ToString() +
64                    " or " + typeof(Multiplication).ToString() +
65                    " or " + typeof(Subtraction).ToString() + ".");
66
67            // TODO Check children 2 & 3 for correct types
68
69            string[] result = new string[]
70            {
71                ((CodeNode)condition.Symbol).Interpret(condition, condition.Subtrees),
72                ((CodeNode)lhs.Symbol).Interpret(lhs, lhs.Subtrees),
73                ((CodeNode)rhs.Symbol).Interpret(rhs, rhs.Subtrees)
74            };
75
76            return this.Prefix + " " + result[1] + " " + result[0] + " " + result[2] + " " + this.Suffix;
77        }
78    }
79}
Note: See TracBrowser for help on using the repository browser.