1 | using HeuristicLab.Common;
|
---|
2 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
3 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
4 |
|
---|
5 | namespace HeuristicLab.Problems.Robocode {
|
---|
6 | [StorableClass]
|
---|
7 | public class NumericalExpression : CodeNode {
|
---|
8 | public override int MinimumArity { get { return 1; } }
|
---|
9 | public override int MaximumArity { get { return 1; } }
|
---|
10 |
|
---|
11 | [Storable]
|
---|
12 | public override string Prefix { get; set; }
|
---|
13 |
|
---|
14 | [Storable]
|
---|
15 | public override string Suffix { get; set; }
|
---|
16 |
|
---|
17 | [StorableConstructor]
|
---|
18 | private NumericalExpression(bool deserializing) : base(deserializing) { }
|
---|
19 | private NumericalExpression(NumericalExpression original, Cloner cloner)
|
---|
20 | : base(original, cloner) {
|
---|
21 | }
|
---|
22 |
|
---|
23 | public NumericalExpression()
|
---|
24 | : base("NumericalExpression", "A NumericalExpression.") {
|
---|
25 | this.Prefix = "";
|
---|
26 | this.Suffix = "";
|
---|
27 | }
|
---|
28 |
|
---|
29 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
30 | return new NumericalExpression(this, cloner);
|
---|
31 | }
|
---|
32 |
|
---|
33 | public override string Interpret(ISymbolicExpressionTreeNode node, System.Collections.Generic.IEnumerable<ISymbolicExpressionTreeNode> children) {
|
---|
34 | var enumerator = children.GetEnumerator();
|
---|
35 |
|
---|
36 | if (!enumerator.MoveNext()) throw new System.Exception("NumericalExpression was not given a child.");
|
---|
37 | var symbol = enumerator.Current.Symbol;
|
---|
38 |
|
---|
39 | if (!(symbol is Number || symbol is NumericalOperation || symbol.GetType().GetInterface(typeof(INumericalMethod).FullName) != null))
|
---|
40 | throw new System.Exception("NumericalExpression was given a child of type " + symbol.GetType().ToString() +
|
---|
41 | ". The expected child must be of type " + typeof(Number).ToString() + " or " +
|
---|
42 | typeof(NumericalOperation).ToString() + " or " +
|
---|
43 | typeof(INumericalMethod).ToString() + ".");
|
---|
44 |
|
---|
45 | string result = ((CodeNode)symbol).Interpret(enumerator.Current, enumerator.Current.Subtrees);
|
---|
46 | if (enumerator.MoveNext()) throw new System.Exception("NumericalExpression was given more than one child.");
|
---|
47 |
|
---|
48 | return this.Prefix + result + this.Suffix;
|
---|
49 | }
|
---|
50 | }
|
---|
51 | }
|
---|