1  #region License Information


2  /* HeuristicLab


3  * Copyright (C) 20022013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)


4  *


5  * This file is part of HeuristicLab.


6  *


7  * HeuristicLab is free software: you can redistribute it and/or modify


8  * it under the terms of the GNU General Public License as published by


9  * the Free Software Foundation, either version 3 of the License, or


10  * (at your option) any later version.


11  *


12  * HeuristicLab is distributed in the hope that it will be useful,


13  * but WITHOUT ANY WARRANTY; without even the implied warranty of


14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the


15  * GNU General Public License for more details.


16  *


17  * You should have received a copy of the GNU General Public License


18  * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.


19  */


20  #endregion


21 


22  using System.Linq;


23  using HeuristicLab.Common;


24  using HeuristicLab.Core;


25  using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;


26  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;


27 


28  namespace HeuristicLab.Problems.Robocode {


29  [StorableClass]


30  [Item("Robocode Grammar", "The grammar for the Robocode GP problem.")]


31  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 


40  [StorableConstructor]


41  protected Grammar(bool deserializing) : base(deserializing) { }


42  protected Grammar(Grammar original, Cloner cloner) : base(original, cloner) { }


43 


44  public Grammar()


45  : base("Robocode Grammar", "The grammar for the Robocode GP problem.") {


46  Initialize();


47  }


48 


49  public override IDeepCloneable Clone(Cloner cloner) {


50  return new Grammar(this, cloner);


51  }


52 


53  // initialize set of allowed symbols and define


54  // the allowed combinations of symbols


55  private void Initialize() {


56  #region Symbols


57  var block = new Block();


58  var stat = new Stat();


59  var ifThenElseStat = new IfThenElseStat();


60  var whileStat = new WhileStat();


61 


62  var logicalExpr = new LogicalExpression();


63  var numericalExpr = new NumericalExpression();


64 


65  var equal = new Equal();


66  var lessThan = new LessThan();


67  var lessThanOrEqual = new LessThanOrEqual();


68  var greaterThan = new GreaterThan();


69  var greaterThanOrEqual = new GreaterThanOrEqual();


70 


71  var conjunction = new Conjunction();


72  var disjunction = new Disjunction();


73  var negation = new Negation();


74 


75  var addition = new Addition();


76  var subtraction = new Subtraction();


77  var multiplication = new Multiplication();


78  var division = new Division();


79  var modulus = new Modulus();


80 


81  var number = new Number();


82  var logicalVal = new LogicalValue();


83 


84  var ahead = new Ahead();


85  var back = new Back();


86  var fire = new Fire();


87  var shotPower = new ShotPower();


88 


89  var getEnergy = new GetEnergy();


90  var getGunHeading = new GetGunHeading();


91  var getHeading = new GetHeading();


92  var getRadarHeading = new GetRadarHeading();


93  var getX = new GetX();


94  var getY = new GetY();


95 


96  //var setAdjustGunForRobotTurn = new SetAdjustGunForRobotTurn();


97  //var setAdjustRadarForGunTurn = new SetAdjustRadarForGunTurn();


98  //var setAdjustRadarForRobotTurn = new SetAdjustRadarForRobotTurn();


99  //var independent = new Independent();


100 


101  var turnLeft = new TurnLeft();


102  var turnRight = new TurnRight();


103  var turnGunLeft = new TurnGunLeft();


104  var turnGunRight = new TurnGunRight();


105  var turnRadarLeft = new TurnRadarLeft();


106  var turnRadarRight = new TurnRadarRight();


107 


108  var onBulletHit = new OnBulletHit();


109  var onBulletMissed = new OnBulletMissed();


110  var onHitByBullet = new OnHitByBullet();


111  var onHitRobot = new OnHitRobot();


112  var onHitWall = new OnHitWall();


113  var onScannedRobot = new OnScannedRobot();


114 


115  var run = new Run();


116  var tank = new Tank();


117 


118  var doNothing = new DoNothing();


119  var emptyEvent = new EmptyEvent();


120  #endregion


121 


122  #region Symbol Collections


123  var controlSymbols = new ISymbol[] { ifThenElseStat, whileStat };


124  var actionSymbols = new ISymbol[] {


125  ahead, back, fire, turnGunLeft, turnGunRight, turnLeft, turnRadarLeft, turnRadarRight, turnRight


126  //setAdjustGunForRobotTurn, setAdjustRadarForGunTurn, setAdjustRadarForRobotTurn,


127  };


128  var functionSymbols = new ISymbol[] {


129  getEnergy, getGunHeading, getHeading, getRadarHeading, getX, getY


130  };


131 


132  var events = new GroupSymbol(EventsName, new ISymbol[] { onScannedRobot, onBulletHit, onBulletMissed, onHitByBullet, onHitRobot, onHitWall });


133  var controlStatements = new GroupSymbol(ControlStatementsName, controlSymbols);


134  var expressions = new GroupSymbol(ExpressionsName, new ISymbol[] { logicalExpr, numericalExpr });


135  var robocodeFunctions = new GroupSymbol(RobocodeFunctionsName, actionSymbols.Concat(functionSymbols));


136  var relationalOperators = new GroupSymbol(RelationalOperatorsName, new ISymbol[] { equal, lessThan, lessThanOrEqual, greaterThan, greaterThanOrEqual });


137  var logicalOperators = new GroupSymbol(LogicalOperators, new ISymbol[] { conjunction, disjunction, negation });


138  var numericalOperators = new GroupSymbol(NumericalOperatorsName, new ISymbol[] { addition, subtraction, multiplication, division, modulus });


139  #endregion


140 


141  #region Adding Symbols


142  AddSymbol(tank);


143  AddSymbol(run);


144  AddSymbol(stat);


145  AddSymbol(block);


146  AddSymbol(shotPower);


147  AddSymbol(number);


148  AddSymbol(logicalVal);


149  AddSymbol(events);


150  AddSymbol(expressions);


151  AddSymbol(controlStatements);


152  AddSymbol(robocodeFunctions);


153  AddSymbol(relationalOperators);


154  AddSymbol(logicalOperators);


155  AddSymbol(numericalOperators);


156  AddSymbol(emptyEvent);


157  AddSymbol(doNothing);


158  #endregion


159 


160  #region Grammar Definition


161  // StartSymbol


162  AddAllowedChildSymbol(StartSymbol, tank);


163 


164  // Tank


165  AddAllowedChildSymbol(tank, run, 0);


166  AddAllowedChildSymbol(tank, onScannedRobot, 1);


167  AddAllowedChildSymbol(tank, onBulletHit, 2);


168  AddAllowedChildSymbol(tank, onBulletMissed, 3);


169  AddAllowedChildSymbol(tank, onHitByBullet, 4);


170  AddAllowedChildSymbol(tank, onHitRobot, 5);


171  AddAllowedChildSymbol(tank, onHitWall, 6);


172 


173  // Run


174  AddAllowedChildSymbol(run, stat);


175 


176  // Event


177  foreach (var s in events.Symbols)


178  AddAllowedChildSymbol(s, stat);


179 


180  // Block


181  AddAllowedChildSymbol(block, stat);


182 


183  // Stat


184  AddAllowedChildSymbol(stat, stat);


185  AddAllowedChildSymbol(stat, block);


186  foreach (var s in controlSymbols)


187  AddAllowedChildSymbol(stat, s);


188  foreach (var s in robocodeFunctions.Symbols)


189  AddAllowedChildSymbol(stat, s);


190  AddAllowedChildSymbol(stat, emptyEvent);


191  AddAllowedChildSymbol(stat, doNothing);


192 


193  // IfStat


194  AddAllowedChildSymbol(ifThenElseStat, logicalExpr, 0);


195  AddAllowedChildSymbol(ifThenElseStat, stat, 1);


196  AddAllowedChildSymbol(ifThenElseStat, emptyEvent, 1);


197  AddAllowedChildSymbol(ifThenElseStat, doNothing, 1);


198  AddAllowedChildSymbol(ifThenElseStat, stat, 2);


199  AddAllowedChildSymbol(ifThenElseStat, emptyEvent, 2);


200  AddAllowedChildSymbol(ifThenElseStat, doNothing, 2);


201 


202  // WhileStat


203  AddAllowedChildSymbol(whileStat, logicalExpr, 0);


204  AddAllowedChildSymbol(whileStat, stat, 1);


205  AddAllowedChildSymbol(whileStat, emptyEvent, 1);


206  AddAllowedChildSymbol(whileStat, doNothing, 1);


207 


208  // Numerical Expressions


209  foreach (var s in functionSymbols.Concat(new[] { number }))


210  AddAllowedChildSymbol(numericalExpr, s);


211  foreach (var s in numericalOperators.Symbols) {


212  AddAllowedChildSymbol(numericalExpr, s);


213  foreach (var ne in functionSymbols.Concat(new[] { number }))


214  AddAllowedChildSymbol(s, ne);


215  foreach (var no in numericalOperators.Symbols) {


216  AddAllowedChildSymbol(s, no);


217  }


218  }


219 


220  // Logical Expressions


221  AddAllowedChildSymbol(logicalExpr, logicalVal);


222  foreach (var s in logicalOperators.Symbols) {


223  AddAllowedChildSymbol(logicalExpr, s);


224  AddAllowedChildSymbol(s, logicalVal);


225  foreach (var lo in logicalOperators.Symbols)


226  AddAllowedChildSymbol(s, lo);


227  foreach (var ro in relationalOperators.Symbols)


228  AddAllowedChildSymbol(s, ro);


229  }


230  foreach (var s in relationalOperators.Symbols)


231  AddAllowedChildSymbol(s, numericalExpr);


232 


233  // Functions


234  foreach (var f in robocodeFunctions.Symbols) {


235  if (f is Fire) AddAllowedChildSymbol(f, shotPower);


236  else AddAllowedChildSymbol(f, numericalExpr);


237  }


238  #endregion


239  }


240  }


241  } 
