Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/01/13 12:08:25 (11 years ago)
Author:
jkarder
Message:

#2069:

  • refactored grammar and symbols
  • fixed cloning and storable ctors
  • fixed plugin dependencies
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  
    2020#endregion
    2121
     22using System.Collections.Generic;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     
    3738
    3839    [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) { }
    4342
    44     public Number()
    45       : base("Number", "A Number.") {
    46       this.Prefix = "";
    47       this.Suffix = "";
    48     }
     43    public Number() : base("Number", "A number.") { }
    4944
    5045    public override ISymbolicExpressionTreeNode CreateTreeNode() {
    51 
    5246      return new NumberTreeNode();
    5347    }
     
    5751    }
    5852
    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();
    6155    }
    6256  }
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Numerical Expressions/NumberTreeNode.cs

    r9790 r10011  
    3636
    3737    [StorableConstructor]
    38     private NumberTreeNode(bool deserializing) : base(deserializing) { }
    39     private NumberTreeNode(NumberTreeNode original, Cloner cloner)
     38    protected NumberTreeNode(bool deserializing) : base(deserializing) { }
     39    protected NumberTreeNode(NumberTreeNode original, Cloner cloner)
    4040      : base(original, cloner) {
    4141      this.value = original.value;
    4242    }
    4343
    44     public NumberTreeNode()
    45       : base(new Number()) {
    46     }
     44    public NumberTreeNode() : base(new Number()) { }
    4745
    4846    public override IDeepCloneable Clone(Cloner cloner) {
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Numerical Expressions/Numerical Operators/Addition.cs

    r9790 r10011  
    2020#endregion
    2121
     22using System;
     23using System.Collections.Generic;
     24using System.Linq;
    2225using HeuristicLab.Common;
    2326using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     
    2629namespace HeuristicLab.Problems.Robocode {
    2730  [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; } }
    3134
    3235    [Storable]
     
    3740
    3841    [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) { }
    4444
    4545    public Addition()
    4646      : base("Addition", "Addition operator.") {
    47       this.Prefix = "+";
    48       this.Suffix = "";
     47      Prefix = "(";
     48      Suffix = ")";
    4949    }
    5050
     
    5353    }
    5454
    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;
    5761    }
    5862  }
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Numerical Expressions/Numerical Operators/Division.cs

    r9790 r10011  
    2020#endregion
    2121
     22using System;
     23using System.Collections.Generic;
     24using System.Linq;
    2225using HeuristicLab.Common;
    2326using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     
    2629namespace HeuristicLab.Problems.Robocode {
    2730  [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; } }
    3134
    3235    [Storable]
     
    3740
    3841    [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) { }
    4444
    4545    public Division()
    4646      : base("Division", "Division operator.") {
    47       this.Prefix = "/";
    48       this.Suffix = "";
     47      Prefix = "(";
     48      Suffix = ")";
    4949    }
    5050
     
    5353    }
    5454
    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;
    5761    }
    5862  }
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Numerical Expressions/Numerical Operators/Modulus.cs

    r9790 r10011  
    2020#endregion
    2121
     22using System;
     23using System.Collections.Generic;
     24using System.Linq;
    2225using HeuristicLab.Common;
    2326using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     
    2629namespace HeuristicLab.Problems.Robocode {
    2730  [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; } }
    3134
    3235    [Storable]
     
    3740
    3841    [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) { }
    4444
    4545    public Modulus()
    4646      : base("Modulus", "Modulus operator.") {
    47       this.Prefix = "%";
    48       this.Suffix = "";
     47      Prefix = "(";
     48      Suffix = ")";
    4949    }
    5050
     
    5353    }
    5454
    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;
    5761    }
    5862  }
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Numerical Expressions/Numerical Operators/Multiplication.cs

    r9790 r10011  
    2020#endregion
    2121
     22using System;
     23using System.Collections.Generic;
     24using System.Linq;
    2225using HeuristicLab.Common;
    2326using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     
    2629namespace HeuristicLab.Problems.Robocode {
    2730  [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; } }
    3134
    3235    [Storable]
     
    3740
    3841    [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) { }
    4444
    4545    public Multiplication()
    4646      : base("Multiplication", "Multiplication operator.") {
    47       this.Prefix = "*";
    48       this.Suffix = "";
     47      Prefix = "(";
     48      Suffix = ")";
    4949    }
    5050
     
    5353    }
    5454
    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;
    5761    }
    5862  }
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Numerical Expressions/Numerical Operators/Subtraction.cs

    r9790 r10011  
    2020#endregion
    2121
     22using System;
     23using System.Collections.Generic;
     24using System.Linq;
    2225using HeuristicLab.Common;
    2326using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     
    2629namespace HeuristicLab.Problems.Robocode {
    2730  [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; } }
    3134
    3235    [Storable]
     
    3740
    3841    [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) { }
    4444
    4545    public Subtraction()
    4646      : base("Subtraction", "Subtraction operator.") {
    47       this.Prefix = "-";
    48       this.Suffix = "";
     47      Prefix = "(";
     48      Suffix = ")";
    4949    }
    5050
     
    5353    }
    5454
    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;
    5761    }
    5862  }
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Numerical Expressions/NumericalExpression.cs

    r9790 r10011  
    2020#endregion
    2121
     22using System.Collections.Generic;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     
    3738
    3839    [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) { }
    4342
    44     public NumericalExpression()
    45       : base("NumericalExpression", "A NumericalExpression.") {
    46       this.Prefix = "";
    47       this.Suffix = "";
    48     }
     43    public NumericalExpression() : base("NumericalExpression", "A numerical expression.") { }
    4944
    5045    public override IDeepCloneable Clone(Cloner cloner) {
     
    5247    }
    5348
    54     public override string Interpret(ISymbolicExpressionTreeNode node, System.Collections.Generic.IEnumerable<ISymbolicExpressionTreeNode> children) {
     49    public override string Interpret(ISymbolicExpressionTreeNode node, IEnumerable<ISymbolicExpressionTreeNode> children) {
    5550      var enumerator = children.GetEnumerator();
     51      if (!enumerator.MoveNext()) throw new System.Exception("NumericalExpression was not given a child.");
    5652
    57       if (!enumerator.MoveNext()) throw new System.Exception("NumericalExpression was not given a child.");
    5853      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) + ".");
    6559
    6660      string result = ((CodeNode)symbol).Interpret(enumerator.Current, enumerator.Current.Subtrees);
    6761      if (enumerator.MoveNext()) throw new System.Exception("NumericalExpression was given more than one child.");
    6862
    69       return this.Prefix + result + this.Suffix;
     63      return result;
    7064    }
    7165  }
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Numerical Expressions/ShotPower.cs

    r9790 r10011  
    2020#endregion
    2121
     22using System.Collections.Generic;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     
    3738
    3839    [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) { }
    4342
    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.") { }
    4744
    4845    public override ISymbolicExpressionTreeNode CreateTreeNode() {
     
    5451    }
    5552
    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();
    5855    }
    5956  }
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Numerical Expressions/ShotPowerTreeNode.cs

    r9790 r10011  
    2020#endregion
    2121
     22using System;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
     
    3637
    3738    [StorableConstructor]
    38     private ShotPowerTreeNode(bool deserializing) : base(deserializing) { }
    39     private ShotPowerTreeNode(ShotPowerTreeNode original, Cloner cloner)
     39    protected ShotPowerTreeNode(bool deserializing) : base(deserializing) { }
     40    protected ShotPowerTreeNode(ShotPowerTreeNode original, Cloner cloner)
    4041      : base(original, cloner) {
    4142      this.value = original.value;
    4243    }
    4344
    44     public ShotPowerTreeNode()
    45       : base(new ShotPower()) {
    46     }
     45    public ShotPowerTreeNode() : base(new ShotPower()) { }
    4746
    4847    public override IDeepCloneable Clone(Cloner cloner) {
     
    5554
    5655    public override void ResetLocalParameters(IRandom random) {
    57       value = (double)random.Next(1, 3);
     56      value = Math.Max(0.1, random.NextDouble() * 3);
    5857    }
    5958  }
Note: See TracChangeset for help on using the changeset viewer.