Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/01/13 16:39:35 (11 years ago)
Author:
jkarder
Message:

#2069:

  • refactored grammar and symbols
  • changed RobocodeProblem to use the ProbabilisticTreeCreator by default
Location:
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3
Files:
17 edited

Legend:

Unmodified
Added
Removed
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Grammar.cs

    r10011 r10014  
    2020#endregion
    2121
     22using System.Linq;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
     
    2930  [Item("Robocode Grammar", "The grammar for the Robocode GP problem.")]
    3031  public class Grammar : SymbolicExpressionGrammar {
     32    private const string EventsName = "Events";
     33    private const string ExpressionsName = "Expressions";
     34    private const string ControlStatementsName = "Control Statements";
     35    private const string RobocodeFunctionsName = "Robocode Functions";
     36    private const string RelationalOperatorsName = "Relational Operators";
     37    private const string LogicalOperators = "Logical Operators";
     38    private const string NumericalOperatorsName = "Numerical Operators";
     39
    3140    [StorableConstructor]
    3241    protected Grammar(bool deserializing) : base(deserializing) { }
     
    113122
    114123      #region Symbol Collections
    115       var eventSymbols = new ISymbol[] { onScannedRobot, onBulletHit, onBulletMissed, onHitByBullet, onHitRobot, onHitWall };
    116124      var controlSymbols = new ISymbol[] { ifStat, whileStat };
    117       var functionSymbols = new ISymbol[] {
     125      var actionSymbols = new ISymbol[] {
    118126        ahead, back, fire, turnGunLeft, turnGunRight, turnLeft, turnRadarLeft, turnRadarRight, turnRight
    119127        //setAdjustGunForRobotTurn, setAdjustRadarForGunTurn, setAdjustRadarForRobotTurn,
    120128      };
    121       var relationalOperators = new ISymbol[] { equal, lessThan, lessThanOrEqual, greaterThan, greaterThanOrEqual };
    122       var logicalOperators = new ISymbol[] { conjunction, disjunction, negation };
    123       var numericalExpressions = new ISymbol[] { number, getEnergy, getGunHeading, getHeading, getRadarHeading, getX, getY };
    124       var numericalOperators = new ISymbol[] { addition, subtraction, multiplication, division, modulus };
     129      var functionSymbols = new ISymbol[] {
     130        getEnergy, getGunHeading, getHeading, getRadarHeading, getX, getY
     131      };
     132
     133      var events = new GroupSymbol(EventsName, new ISymbol[] { onScannedRobot, onBulletHit, onBulletMissed, onHitByBullet, onHitRobot, onHitWall });
     134      var controlStatements = new GroupSymbol(ControlStatementsName, controlSymbols.Concat(new[] { elseStat }));
     135      var expressions = new GroupSymbol(ExpressionsName, new ISymbol[] { logicalExpr, numericalExpr });
     136      var robocodeFunctions = new GroupSymbol(RobocodeFunctionsName, actionSymbols.Concat(functionSymbols));
     137      var relationalOperators = new GroupSymbol(RelationalOperatorsName, new ISymbol[] { equal, lessThan, lessThanOrEqual, greaterThan, greaterThanOrEqual });
     138      var logicalOperators = new GroupSymbol(LogicalOperators, new ISymbol[] { conjunction, disjunction, negation });
     139      var numericalOperators = new GroupSymbol(NumericalOperatorsName, new ISymbol[] { addition, subtraction, multiplication, division, modulus });
    125140      #endregion
    126141
    127142      #region Adding Symbols
    128       foreach (var s in eventSymbols)
    129         AddSymbol(s);
    130       foreach (var s in controlSymbols)
    131         AddSymbol(s);
    132       foreach (var s in functionSymbols)
    133         AddSymbol(s);
    134       foreach (var s in relationalOperators)
    135         AddSymbol(s);
    136       foreach (var s in logicalOperators)
    137         AddSymbol(s);
    138       foreach (var s in numericalExpressions)
    139         AddSymbol(s);
    140       foreach (var s in numericalOperators)
    141         AddSymbol(s);
    142 
     143      AddSymbol(tank);
     144      AddSymbol(run);
     145      AddSymbol(stat);
    143146      AddSymbol(block);
    144       AddSymbol(stat);
    145       AddSymbol(elseStat);
    146147      AddSymbol(shotPower);
     148      AddSymbol(number);
    147149      AddSymbol(logicalVal);
    148       AddSymbol(logicalExpr);
    149       AddSymbol(numericalExpr);
    150       AddSymbol(run);
    151       AddSymbol(tank);
     150      AddSymbol(events);
     151      AddSymbol(expressions);
     152      AddSymbol(controlStatements);
     153      AddSymbol(robocodeFunctions);
     154      AddSymbol(relationalOperators);
     155      AddSymbol(logicalOperators);
     156      AddSymbol(numericalOperators);
    152157      AddSymbol(emptyEvent);
    153158      AddSymbol(doNothing);
     
    171176
    172177      // Event
    173       foreach (var s in eventSymbols)
     178      foreach (var s in events.Symbols)
    174179        AddAllowedChildSymbol(s, stat);
    175180
     
    182187      foreach (var s in controlSymbols)
    183188        AddAllowedChildSymbol(stat, s);
    184       foreach (var s in functionSymbols)
     189      foreach (var s in robocodeFunctions.Symbols)
    185190        AddAllowedChildSymbol(stat, s);
    186191      AddAllowedChildSymbol(stat, emptyEvent);
     
    208213
    209214      // Numerical Expressions
    210       foreach (var s in numericalExpressions)
     215      foreach (var s in functionSymbols.Concat(new[] { number }))
    211216        AddAllowedChildSymbol(numericalExpr, s);
    212       foreach (var s in numericalOperators) {
     217      foreach (var s in numericalOperators.Symbols) {
    213218        AddAllowedChildSymbol(numericalExpr, s);
    214         foreach (var ne in numericalExpressions)
     219        foreach (var ne in functionSymbols.Concat(new[] { number }))
    215220          AddAllowedChildSymbol(s, ne);
    216         foreach (var no in numericalOperators) {
     221        foreach (var no in numericalOperators.Symbols) {
    217222          AddAllowedChildSymbol(s, no);
    218223        }
     
    221226      // Logical Expressions
    222227      AddAllowedChildSymbol(logicalExpr, logicalVal);
    223       foreach (var s in logicalOperators) {
     228      foreach (var s in logicalOperators.Symbols) {
    224229        AddAllowedChildSymbol(logicalExpr, s);
    225230        AddAllowedChildSymbol(s, logicalVal);
    226         foreach (var lo in logicalOperators)
     231        foreach (var lo in logicalOperators.Symbols)
    227232          AddAllowedChildSymbol(s, lo);
    228         foreach (var ro in relationalOperators)
     233        foreach (var ro in relationalOperators.Symbols)
    229234          AddAllowedChildSymbol(s, ro);
    230235      }
    231       foreach (var s in relationalOperators)
     236      foreach (var s in relationalOperators.Symbols)
    232237        AddAllowedChildSymbol(s, numericalExpr);
    233238
    234239      // Functions
    235       foreach (var f in functionSymbols) {
    236         if (f is Fire)
    237           AddAllowedChildSymbol(f, shotPower);
    238         else
    239           AddAllowedChildSymbol(f, numericalExpr);
     240      foreach (var f in robocodeFunctions.Symbols) {
     241        if (f is Fire) AddAllowedChildSymbol(f, shotPower);
     242        else AddAllowedChildSymbol(f, numericalExpr);
    240243      }
    241244      #endregion
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/RobocodeProblem.cs

    r10011 r10014  
    7676
    7777    public RobocodeProblem()
    78       : base(new RobocodeEvaluator(), new RampedHalfAndHalfTreeCreator()) {
     78      : base(new RobocodeEvaluator(), new ProbabilisticTreeCreator()) {
    7979      DirectoryValue robocodeDir = new DirectoryValue();
    8080      robocodeDir.Value = @"C:\robocode";
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Event Methods/Events/EmptyEvent.cs

    r10011 r10014  
    4848
    4949    public override string Interpret(ISymbolicExpressionTreeNode node, IEnumerable<ISymbolicExpressionTreeNode> children) {
    50       return ";";
     50      return string.Empty;
    5151    }
    5252  }
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Stat.cs

    r10011 r10014  
    4343    protected Stat(Stat original, Cloner cloner) : base(original, cloner) { }
    4444
    45     public Stat() : base("Statement", "A statement.") { }
     45    public Stat()
     46      : base("Statement", "A statement.") {
     47      Suffix = ";";
     48    }
    4649
    4750    public override IDeepCloneable Clone(Cloner cloner) {
     
    5053
    5154    public override string Interpret(ISymbolicExpressionTreeNode node, IEnumerable<ISymbolicExpressionTreeNode> children) {
    52       return children.Aggregate(string.Empty, (current, c) => current + (Environment.NewLine + ((CodeNode)c.Symbol).Interpret(c, c.Subtrees)));
     55      return children.Aggregate(string.Empty, (current, c) => current + (Environment.NewLine + ((CodeNode)c.Symbol).Interpret(c, c.Subtrees))) + Suffix;
    5356    }
    5457  }
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Statements/ElseStat.cs

    r10011 r10014  
    4444
    4545    public ElseStat()
    46       : base("ElseStatement", "An ElseStatement.") {
    47       Prefix = "else";
    48       Suffix = "";
     46      : base("ElseStat", "An else branch.") {
     47      Prefix = " else {";
     48      Suffix = "}";
    4949    }
    5050
     
    5555    public override string Interpret(ISymbolicExpressionTreeNode node, IEnumerable<ISymbolicExpressionTreeNode> children) {
    5656      string result = children.Aggregate(string.Empty, (current, c) => current + (Environment.NewLine + ((CodeNode)c.Symbol).Interpret(c, c.Subtrees)));
    57       return string.IsNullOrEmpty(result)
    58         ? string.Empty
    59         : Prefix + Environment.NewLine + result + Environment.NewLine + Suffix;
     57      return string.IsNullOrEmpty(result) ? string.Empty : Prefix + Environment.NewLine + result + Environment.NewLine + Suffix;
    6058    }
    6159  }
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Statements/IfStat.cs

    r10011 r10014  
    4343
    4444    public IfStat()
    45       : base("IfStatement", "An if statement.") {
     45      : base("IfStat", "An if statement.") {
    4646      Prefix = "if (";
    47       Suffix = ")";
     47      Suffix = ") {";
    4848    }
    4949
     
    6363        else throw new ArgumentException("Unexpected children.", "children");
    6464      }
    65       return Prefix + condition + Suffix + Environment.NewLine + ifTrue + Environment.NewLine + ifElse + Environment.NewLine;
     65      string result = Prefix + condition + Suffix + Environment.NewLine + ifTrue + Environment.NewLine + "}";
     66      result += string.IsNullOrEmpty(ifElse) ? Environment.NewLine : ifElse;
     67      return result;
    6668    }
    6769  }
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Statements/WhileStat.cs

    r10011 r10014  
    4545      : base("WhileStat", "A while statement.") {
    4646      Prefix = "while (";
    47       Suffix = ")";
     47      Suffix = ") {";
    4848    }
    4949
     
    6161        else throw new ArgumentException("Unexpected children.", "children");
    6262      }
    63       return Prefix + condition + Suffix + Environment.NewLine + ifTrue + Environment.NewLine;
     63      return Prefix + condition + Suffix + Environment.NewLine + ifTrue + Environment.NewLine + "}" + Environment.NewLine;
    6464    }
    6565  }
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Void Methods/Ahead.cs

    r10011 r10014  
    4444      : base("Ahead", "Immediately moves your robot ahead (forward) by distance measured in pixels.") {
    4545      Prefix = "setAhead(";
    46       Suffix = ");";
     46      Suffix = ")";
    4747    }
    4848
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Void Methods/Back.cs

    r10011 r10014  
    4444      : base("Back", "Immediately moves your robot backward by distance measured in pixels.") {
    4545      Prefix = "setBack(";
    46       Suffix = ");";
     46      Suffix = ")";
    4747    }
    4848
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Void Methods/DoNothing.cs

    r10011 r10014  
    4848
    4949    public override string Interpret(ISymbolicExpressionTreeNode node, IEnumerable<ISymbolicExpressionTreeNode> children) {
    50       return ";";
     50      return string.Empty;
    5151    }
    5252  }
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Void Methods/Fire.cs

    r10011 r10014  
    4444      : base("Fire", "Immediately fires a bullet.") {
    4545      Prefix = "setFire(";
    46       Suffix = ");";
     46      Suffix = ")";
    4747    }
    4848
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Void Methods/TurnGunLeft.cs

    r10011 r10014  
    4444      : base("TurnGunLeft", "Immediately turns the robot's gun to the left by degrees.") {
    4545      Prefix = "setTurnGunLeft(";
    46       Suffix = ");";
     46      Suffix = ")";
    4747    }
    4848
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Void Methods/TurnGunRight.cs

    r10011 r10014  
    4444      : base("TurnGunRight", "Immediately turns the robot's gun to the right by degrees.") {
    4545      Prefix = "setTurnGunRight(";
    46       Suffix = ");";
     46      Suffix = ")";
    4747    }
    4848
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Void Methods/TurnLeft.cs

    r10011 r10014  
    4444      : base("TurnLeft", "Immediately turns the robot's body to the left by degrees.") {
    4545      Prefix = "setTurnLeft(";
    46       Suffix = ");";
     46      Suffix = ")";
    4747    }
    4848
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Void Methods/TurnRadarLeft.cs

    r10011 r10014  
    4444      : base("TurnRadarLeft", "Immediately turns the robot's radar to the left by degrees.") {
    4545      Prefix = "setTurnRadarLeft(";
    46       Suffix = ");";
     46      Suffix = ")";
    4747    }
    4848
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Void Methods/TurnRadarRight.cs

    r10011 r10014  
    4444      : base("TurnRadarRight", "Immediately turns the robot's radar to the right by degrees.") {
    4545      Prefix = "setTurnRadarRight(";
    46       Suffix = ");";
     46      Suffix = ")";
    4747    }
    4848
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Void Methods/TurnRight.cs

    r10011 r10014  
    4444      : base("TurnRight", "Immediately turns the robot's body to the right by degrees.") {
    4545      Prefix = "setTurnRight(";
    46       Suffix = ");";
     46      Suffix = ")";
    4747    }
    4848
Note: See TracChangeset for help on using the changeset viewer.