Changeset 10011 for branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Numerical Expressions
- Timestamp:
- 10/01/13 12:08:25 (11 years ago)
- Location:
- branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Numerical Expressions
- Files:
-
- 1 added
- 1 deleted
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Numerical Expressions/Number.cs
r9790 r10011 20 20 #endregion 21 21 22 using System.Collections.Generic; 22 23 using HeuristicLab.Common; 23 24 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; … … 37 38 38 39 [StorableConstructor] 39 private Number(bool deserializing) : base(deserializing) { } 40 private Number(Number original, Cloner cloner) 41 : base(original, cloner) { 42 } 40 protected Number(bool deserializing) : base(deserializing) { } 41 protected Number(Number original, Cloner cloner) : base(original, cloner) { } 43 42 44 public Number() 45 : base("Number", "A Number.") { 46 this.Prefix = ""; 47 this.Suffix = ""; 48 } 43 public Number() : base("Number", "A number.") { } 49 44 50 45 public override ISymbolicExpressionTreeNode CreateTreeNode() { 51 52 46 return new NumberTreeNode(); 53 47 } … … 57 51 } 58 52 59 public override string Interpret(ISymbolicExpressionTreeNode node, System.Collections.Generic.IEnumerable<ISymbolicExpressionTreeNode> children) {60 return " " + ((NumberTreeNode)node).Value + " ";53 public override string Interpret(ISymbolicExpressionTreeNode node, IEnumerable<ISymbolicExpressionTreeNode> children) { 54 return ((NumberTreeNode)node).Value.ToString(); 61 55 } 62 56 } -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Numerical Expressions/NumberTreeNode.cs
r9790 r10011 36 36 37 37 [StorableConstructor] 38 pr ivateNumberTreeNode(bool deserializing) : base(deserializing) { }39 pr ivateNumberTreeNode(NumberTreeNode original, Cloner cloner)38 protected NumberTreeNode(bool deserializing) : base(deserializing) { } 39 protected NumberTreeNode(NumberTreeNode original, Cloner cloner) 40 40 : base(original, cloner) { 41 41 this.value = original.value; 42 42 } 43 43 44 public NumberTreeNode() 45 : base(new Number()) { 46 } 44 public NumberTreeNode() : base(new Number()) { } 47 45 48 46 public override IDeepCloneable Clone(Cloner cloner) { -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Numerical Expressions/Numerical Operators/Addition.cs
r9790 r10011 20 20 #endregion 21 21 22 using System; 23 using System.Collections.Generic; 24 using System.Linq; 22 25 using HeuristicLab.Common; 23 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; … … 26 29 namespace HeuristicLab.Problems.Robocode { 27 30 [StorableClass] 28 public class Addition : CodeNode {29 public override int MinimumArity { get { return 0; } }30 public override int MaximumArity { get { return 0; } }31 public class Addition : CodeNode, INumericalOperator { 32 public override int MinimumArity { get { return 2; } } 33 public override int MaximumArity { get { return byte.MaxValue; } } 31 34 32 35 [Storable] … … 37 40 38 41 [StorableConstructor] 39 private Addition(bool deserializing) : base(deserializing) { } 40 private Addition(Addition original, Cloner cloner) 41 : base(original, cloner) { 42 43 } 42 protected Addition(bool deserializing) : base(deserializing) { } 43 protected Addition(Addition original, Cloner cloner) : base(original, cloner) { } 44 44 45 45 public Addition() 46 46 : base("Addition", "Addition operator.") { 47 this.Prefix = "+";48 this.Suffix = "";47 Prefix = "("; 48 Suffix = ")"; 49 49 } 50 50 … … 53 53 } 54 54 55 public override string Interpret(ISymbolicExpressionTreeNode node, System.Collections.Generic.IEnumerable<ISymbolicExpressionTreeNode> children) { 56 return this.Prefix + this.Suffix; 55 public override string Interpret(ISymbolicExpressionTreeNode node, IEnumerable<ISymbolicExpressionTreeNode> children) { 56 if (children.Count() < 2) 57 throw new ArgumentException("Expected at leaset 2 children.", "children"); 58 59 string result = string.Join(" + ", children.Select(c => ((CodeNode)c.Symbol).Interpret(c, c.Subtrees))); 60 return Prefix + result + Suffix; 57 61 } 58 62 } -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Numerical Expressions/Numerical Operators/Division.cs
r9790 r10011 20 20 #endregion 21 21 22 using System; 23 using System.Collections.Generic; 24 using System.Linq; 22 25 using HeuristicLab.Common; 23 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; … … 26 29 namespace HeuristicLab.Problems.Robocode { 27 30 [StorableClass] 28 public class Division : CodeNode {29 public override int MinimumArity { get { return 0; } }30 public override int MaximumArity { get { return 0; } }31 public class Division : CodeNode, INumericalOperator { 32 public override int MinimumArity { get { return 2; } } 33 public override int MaximumArity { get { return byte.MaxValue; } } 31 34 32 35 [Storable] … … 37 40 38 41 [StorableConstructor] 39 private Division(bool deserializing) : base(deserializing) { } 40 private Division(Division original, Cloner cloner) 41 : base(original, cloner) { 42 43 } 42 protected Division(bool deserializing) : base(deserializing) { } 43 protected Division(Division original, Cloner cloner) : base(original, cloner) { } 44 44 45 45 public Division() 46 46 : base("Division", "Division operator.") { 47 this.Prefix = "/";48 this.Suffix = "";47 Prefix = "("; 48 Suffix = ")"; 49 49 } 50 50 … … 53 53 } 54 54 55 public override string Interpret(ISymbolicExpressionTreeNode node, System.Collections.Generic.IEnumerable<ISymbolicExpressionTreeNode> children) { 56 return this.Prefix + this.Suffix; 55 public override string Interpret(ISymbolicExpressionTreeNode node, IEnumerable<ISymbolicExpressionTreeNode> children) { 56 if (children.Count() < 2) 57 throw new ArgumentException("Expected at leaset 2 children.", "children"); 58 59 string result = string.Join(" / ", children.Select(c => ((CodeNode)c.Symbol).Interpret(c, c.Subtrees))); 60 return Prefix + result + Suffix; 57 61 } 58 62 } -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Numerical Expressions/Numerical Operators/Modulus.cs
r9790 r10011 20 20 #endregion 21 21 22 using System; 23 using System.Collections.Generic; 24 using System.Linq; 22 25 using HeuristicLab.Common; 23 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; … … 26 29 namespace HeuristicLab.Problems.Robocode { 27 30 [StorableClass] 28 public class Modulus : CodeNode {29 public override int MinimumArity { get { return 0; } }30 public override int MaximumArity { get { return 0; } }31 public class Modulus : CodeNode, INumericalOperator { 32 public override int MinimumArity { get { return 2; } } 33 public override int MaximumArity { get { return byte.MaxValue; } } 31 34 32 35 [Storable] … … 37 40 38 41 [StorableConstructor] 39 private Modulus(bool deserializing) : base(deserializing) { } 40 private Modulus(Modulus original, Cloner cloner) 41 : base(original, cloner) { 42 43 } 42 protected Modulus(bool deserializing) : base(deserializing) { } 43 protected Modulus(Modulus original, Cloner cloner) : base(original, cloner) { } 44 44 45 45 public Modulus() 46 46 : base("Modulus", "Modulus operator.") { 47 this.Prefix = "%";48 this.Suffix = "";47 Prefix = "("; 48 Suffix = ")"; 49 49 } 50 50 … … 53 53 } 54 54 55 public override string Interpret(ISymbolicExpressionTreeNode node, System.Collections.Generic.IEnumerable<ISymbolicExpressionTreeNode> children) { 56 return this.Prefix + this.Suffix; 55 public override string Interpret(ISymbolicExpressionTreeNode node, IEnumerable<ISymbolicExpressionTreeNode> children) { 56 if (children.Count() < 2) 57 throw new ArgumentException("Expected at leaset 2 children.", "children"); 58 59 string result = string.Join(" % ", children.Select(c => ((CodeNode)c.Symbol).Interpret(c, c.Subtrees))); 60 return Prefix + result + Suffix; 57 61 } 58 62 } -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Numerical Expressions/Numerical Operators/Multiplication.cs
r9790 r10011 20 20 #endregion 21 21 22 using System; 23 using System.Collections.Generic; 24 using System.Linq; 22 25 using HeuristicLab.Common; 23 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; … … 26 29 namespace HeuristicLab.Problems.Robocode { 27 30 [StorableClass] 28 public class Multiplication : CodeNode {29 public override int MinimumArity { get { return 0; } }30 public override int MaximumArity { get { return 0; } }31 public class Multiplication : CodeNode, INumericalOperator { 32 public override int MinimumArity { get { return 2; } } 33 public override int MaximumArity { get { return byte.MaxValue; } } 31 34 32 35 [Storable] … … 37 40 38 41 [StorableConstructor] 39 private Multiplication(bool deserializing) : base(deserializing) { } 40 private Multiplication(Multiplication original, Cloner cloner) 41 : base(original, cloner) { 42 43 } 42 protected Multiplication(bool deserializing) : base(deserializing) { } 43 protected Multiplication(Multiplication original, Cloner cloner) : base(original, cloner) { } 44 44 45 45 public Multiplication() 46 46 : base("Multiplication", "Multiplication operator.") { 47 this.Prefix = "*";48 this.Suffix = "";47 Prefix = "("; 48 Suffix = ")"; 49 49 } 50 50 … … 53 53 } 54 54 55 public override string Interpret(ISymbolicExpressionTreeNode node, System.Collections.Generic.IEnumerable<ISymbolicExpressionTreeNode> children) { 56 return this.Prefix + this.Suffix; 55 public override string Interpret(ISymbolicExpressionTreeNode node, IEnumerable<ISymbolicExpressionTreeNode> children) { 56 if (children.Count() < 2) 57 throw new ArgumentException("Expected at leaset 2 children.", "children"); 58 59 string result = string.Join(" * ", children.Select(c => ((CodeNode)c.Symbol).Interpret(c, c.Subtrees))); 60 return Prefix + result + Suffix; 57 61 } 58 62 } -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Numerical Expressions/Numerical Operators/Subtraction.cs
r9790 r10011 20 20 #endregion 21 21 22 using System; 23 using System.Collections.Generic; 24 using System.Linq; 22 25 using HeuristicLab.Common; 23 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; … … 26 29 namespace HeuristicLab.Problems.Robocode { 27 30 [StorableClass] 28 public class Subtraction : CodeNode {29 public override int MinimumArity { get { return 0; } }30 public override int MaximumArity { get { return 0; } }31 public class Subtraction : CodeNode, INumericalOperator { 32 public override int MinimumArity { get { return 2; } } 33 public override int MaximumArity { get { return byte.MaxValue; } } 31 34 32 35 [Storable] … … 37 40 38 41 [StorableConstructor] 39 private Subtraction(bool deserializing) : base(deserializing) { } 40 private Subtraction(Subtraction original, Cloner cloner) 41 : base(original, cloner) { 42 43 } 42 protected Subtraction(bool deserializing) : base(deserializing) { } 43 protected Subtraction(Subtraction original, Cloner cloner) : base(original, cloner) { } 44 44 45 45 public Subtraction() 46 46 : base("Subtraction", "Subtraction operator.") { 47 this.Prefix = "-";48 this.Suffix = "";47 Prefix = "("; 48 Suffix = ")"; 49 49 } 50 50 … … 53 53 } 54 54 55 public override string Interpret(ISymbolicExpressionTreeNode node, System.Collections.Generic.IEnumerable<ISymbolicExpressionTreeNode> children) { 56 return this.Prefix + this.Suffix; 55 public override string Interpret(ISymbolicExpressionTreeNode node, IEnumerable<ISymbolicExpressionTreeNode> children) { 56 if (children.Count() < 2) 57 throw new ArgumentException("Expected at leaset 2 children.", "children"); 58 59 string result = string.Join(" - ", children.Select(c => ((CodeNode)c.Symbol).Interpret(c, c.Subtrees))); 60 return Prefix + result + Suffix; 57 61 } 58 62 } -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Numerical Expressions/NumericalExpression.cs
r9790 r10011 20 20 #endregion 21 21 22 using System.Collections.Generic; 22 23 using HeuristicLab.Common; 23 24 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; … … 37 38 38 39 [StorableConstructor] 39 private NumericalExpression(bool deserializing) : base(deserializing) { } 40 private NumericalExpression(NumericalExpression original, Cloner cloner) 41 : base(original, cloner) { 42 } 40 protected NumericalExpression(bool deserializing) : base(deserializing) { } 41 protected NumericalExpression(NumericalExpression original, Cloner cloner) : base(original, cloner) { } 43 42 44 public NumericalExpression() 45 : base("NumericalExpression", "A NumericalExpression.") { 46 this.Prefix = ""; 47 this.Suffix = ""; 48 } 43 public NumericalExpression() : base("NumericalExpression", "A numerical expression.") { } 49 44 50 45 public override IDeepCloneable Clone(Cloner cloner) { … … 52 47 } 53 48 54 public override string Interpret(ISymbolicExpressionTreeNode node, System.Collections.Generic.IEnumerable<ISymbolicExpressionTreeNode> children) {49 public override string Interpret(ISymbolicExpressionTreeNode node, IEnumerable<ISymbolicExpressionTreeNode> children) { 55 50 var enumerator = children.GetEnumerator(); 51 if (!enumerator.MoveNext()) throw new System.Exception("NumericalExpression was not given a child."); 56 52 57 if (!enumerator.MoveNext()) throw new System.Exception("NumericalExpression was not given a child.");58 53 var symbol = enumerator.Current.Symbol; 59 60 if (!(symbol is Number || symbol is NumericalOperation || symbol.GetType().GetInterface(typeof(INumericalMethod).FullName) != null)) 61 throw new System.Exception("NumericalExpression was given a child of type " + symbol.GetType().ToString() + 62 ". The expected child must be of type " + typeof(Number).ToString() + " or " + 63 typeof(NumericalOperation).ToString() + " or " + 64 typeof(INumericalMethod).ToString() + "."); 54 if (!(symbol is Number || symbol is INumericalMethod || symbol is INumericalOperator)) 55 throw new System.Exception("NumericalExpression was given a child of type " + symbol.GetType() + 56 ". The expected child must be of type " + typeof(Number) 57 + " or " + typeof(INumericalMethod) 58 + " or " + typeof(INumericalOperator) + "."); 65 59 66 60 string result = ((CodeNode)symbol).Interpret(enumerator.Current, enumerator.Current.Subtrees); 67 61 if (enumerator.MoveNext()) throw new System.Exception("NumericalExpression was given more than one child."); 68 62 69 return this.Prefix + result + this.Suffix;63 return result; 70 64 } 71 65 } -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Numerical Expressions/ShotPower.cs
r9790 r10011 20 20 #endregion 21 21 22 using System.Collections.Generic; 22 23 using HeuristicLab.Common; 23 24 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; … … 37 38 38 39 [StorableConstructor] 39 private ShotPower(bool deserializing) : base(deserializing) { } 40 private ShotPower(ShotPower original, Cloner cloner) 41 : base(original, cloner) { 42 } 40 protected ShotPower(bool deserializing) : base(deserializing) { } 41 protected ShotPower(ShotPower original, Cloner cloner) : base(original, cloner) { } 43 42 44 public ShotPower() 45 : base("ShotPower", "The power of a shot between 0.1 and 3.") { 46 } 43 public ShotPower() : base("ShotPower", "The power of a shot between 0.1 and 3.") { } 47 44 48 45 public override ISymbolicExpressionTreeNode CreateTreeNode() { … … 54 51 } 55 52 56 public override string Interpret(ISymbolicExpressionTreeNode node, System.Collections.Generic.IEnumerable<ISymbolicExpressionTreeNode> children) {57 return " " + ((ShotPowerTreeNode)node).Value + " ";53 public override string Interpret(ISymbolicExpressionTreeNode node, IEnumerable<ISymbolicExpressionTreeNode> children) { 54 return ((ShotPowerTreeNode)node).Value.ToString(); 58 55 } 59 56 } -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Numerical Expressions/ShotPowerTreeNode.cs
r9790 r10011 20 20 #endregion 21 21 22 using System; 22 23 using HeuristicLab.Common; 23 24 using HeuristicLab.Core; … … 36 37 37 38 [StorableConstructor] 38 pr ivateShotPowerTreeNode(bool deserializing) : base(deserializing) { }39 pr ivateShotPowerTreeNode(ShotPowerTreeNode original, Cloner cloner)39 protected ShotPowerTreeNode(bool deserializing) : base(deserializing) { } 40 protected ShotPowerTreeNode(ShotPowerTreeNode original, Cloner cloner) 40 41 : base(original, cloner) { 41 42 this.value = original.value; 42 43 } 43 44 44 public ShotPowerTreeNode() 45 : base(new ShotPower()) { 46 } 45 public ShotPowerTreeNode() : base(new ShotPower()) { } 47 46 48 47 public override IDeepCloneable Clone(Cloner cloner) { … … 55 54 56 55 public override void ResetLocalParameters(IRandom random) { 57 value = (double)random.Next(1,3);56 value = Math.Max(0.1, random.NextDouble() * 3); 58 57 } 59 58 }
Note: See TracChangeset
for help on using the changeset viewer.