[9565] | 1 | using System.Collections.Generic;
|
---|
| 2 | using HeuristicLab.Common;
|
---|
| 3 | using HeuristicLab.Core;
|
---|
| 4 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 5 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 6 |
|
---|
| 7 | namespace HeuristicLab.Problems.Robocode
|
---|
| 8 | {
|
---|
| 9 | [StorableClass]
|
---|
| 10 | [Item("Robocode Grammar", "The grammar for the Robocode GP problem.")]
|
---|
| 11 | public class Grammar : SymbolicExpressionGrammar
|
---|
| 12 | {
|
---|
| 13 | [StorableConstructor]
|
---|
| 14 | private Grammar(bool deserializing) : base(deserializing) { }
|
---|
| 15 | private Grammar(Grammar original, Cloner cloner)
|
---|
| 16 | : base(original, cloner)
|
---|
| 17 | {
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | public Grammar()
|
---|
| 21 | : base("Robocode Grammar", "The grammar for the Robocode GP problem.")
|
---|
| 22 | {
|
---|
| 23 | Initialize();
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | public override IDeepCloneable Clone(Cloner cloner)
|
---|
| 27 | {
|
---|
| 28 | return new Grammar(this, cloner);
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | // initialize set of allowed symbols and define
|
---|
| 32 | // the allowed combinations of symbols
|
---|
| 33 | private void Initialize()
|
---|
| 34 | {
|
---|
| 35 | #region Symbols
|
---|
| 36 | var block = new Block();
|
---|
| 37 |
|
---|
| 38 | var ifStmt = new IfStatement();
|
---|
| 39 | var elseStmt = new ElseStatement();
|
---|
| 40 | var whileLoop = new WhileLoop();
|
---|
| 41 |
|
---|
| 42 | var numExpr = new NumericalExpression();
|
---|
| 43 | var number = new Number();
|
---|
| 44 | var numOp = new NumericalOperation();
|
---|
| 45 | var add = new Addition();
|
---|
| 46 | var sub = new Subtraction();
|
---|
| 47 | var mult = new Multiplication();
|
---|
| 48 | var div = new Division();
|
---|
| 49 | var mod = new Modulus();
|
---|
| 50 |
|
---|
| 51 | var logicExpr = new LogicalExpression();
|
---|
| 52 | var logicComp = new LogicalComparison();
|
---|
| 53 | var numComp = new NumericalComparison();
|
---|
| 54 | var logicVal = new LogicalValue();
|
---|
| 55 | var not = new Negation();
|
---|
| 56 | var and = new Conjunction();
|
---|
| 57 | var or = new Disjunction();
|
---|
| 58 | var eq = new Equal();
|
---|
| 59 | var lt = new LessThan();
|
---|
| 60 | var gt = new GreaterThan();
|
---|
| 61 | var lteq = new LessThanOrEqual();
|
---|
| 62 | var gteq = new GreaterThanOrEqual();
|
---|
| 63 |
|
---|
| 64 | var ahead = new Ahead();
|
---|
| 65 | var back = new Back();
|
---|
| 66 | var constant = new Constant();
|
---|
| 67 | var doNothing = new DoNothing();
|
---|
| 68 | var fire = new Fire();
|
---|
| 69 | var shotPower = new ShotPower();
|
---|
| 70 |
|
---|
| 71 | var getEnergy = new GetEnergy();
|
---|
| 72 | var getGunHeading = new GetGunHeading();
|
---|
| 73 | var getHeading = new GetHeading();
|
---|
| 74 | var getRadarHeading = new GetRadarHeading();
|
---|
| 75 | var getX = new GetX();
|
---|
| 76 | var getY = new GetY();
|
---|
| 77 |
|
---|
| 78 | //var setAdjustGunForRobotTurn = new SetAdjustGunForRobotTurn();
|
---|
| 79 | //var setAdjustRadarForGunTurn = new SetAdjustRadarForGunTurn();
|
---|
| 80 | //var setAdjustRadarForRobotTurn = new SetAdjustRadarForRobotTurn();
|
---|
| 81 | //var independent = new Independent();
|
---|
| 82 |
|
---|
| 83 | var turnLeft = new TurnLeft();
|
---|
| 84 | var turnRight = new TurnRight();
|
---|
| 85 | var turnGunLeft = new TurnGunLeft();
|
---|
| 86 | var turnGunRight = new TurnGunRight();
|
---|
| 87 | var turnRadarLeft = new TurnRadarLeft();
|
---|
| 88 | var turnRadarRight = new TurnRadarRight();
|
---|
| 89 |
|
---|
| 90 | var onBulletHit = new OnBulletHit();
|
---|
| 91 | var onBulletMissed = new OnBulletMissed();
|
---|
| 92 | var onHitByBullet = new OnHitByBullet();
|
---|
| 93 | var onHitRobot = new OnHitRobot();
|
---|
| 94 | var onHitWall = new OnHitWall();
|
---|
| 95 | var onScannedRobot = new OnScannedRobot();
|
---|
| 96 |
|
---|
| 97 | var run = new Run();
|
---|
| 98 | var tank = new Tank();
|
---|
| 99 | var program = new Program();
|
---|
| 100 | #endregion
|
---|
| 101 |
|
---|
| 102 | #region Symbol Collections
|
---|
| 103 | var numericalExpressions = new List<ISymbol>()
|
---|
| 104 | {
|
---|
| 105 | number, numOp, getEnergy, getGunHeading, getHeading, getRadarHeading, getX, getY
|
---|
| 106 | };
|
---|
| 107 |
|
---|
| 108 | var numericalOperators = new List<ISymbol>()
|
---|
| 109 | {
|
---|
| 110 | add, sub, mult, div, mod
|
---|
| 111 | };
|
---|
| 112 |
|
---|
| 113 | var logicalExpressions = new List<ISymbol>()
|
---|
| 114 | {
|
---|
| 115 | logicVal, logicComp, numComp, not
|
---|
| 116 | };
|
---|
| 117 |
|
---|
| 118 | var logicalComparators = new List<ISymbol>()
|
---|
| 119 | {
|
---|
| 120 | and, or
|
---|
| 121 | };
|
---|
| 122 |
|
---|
| 123 | var numericalComparators = new List<ISymbol>()
|
---|
| 124 | {
|
---|
| 125 | lt, gt, lteq, gteq, eq
|
---|
| 126 | };
|
---|
| 127 |
|
---|
| 128 | var terminalSymbols = new List<ISymbol>()
|
---|
| 129 | {
|
---|
| 130 | doNothing, //getEnergy, getGunHeading, getHeading, getRadarHeading, getX, getY
|
---|
| 131 | };
|
---|
| 132 |
|
---|
| 133 | var functionSymbols = new List<ISymbol>()
|
---|
| 134 | {
|
---|
| 135 | ahead, back, fire,
|
---|
| 136 | //setAdjustGunForRobotTurn, setAdjustRadarForGunTurn, setAdjustRadarForRobotTurn,
|
---|
| 137 | turnGunLeft, turnGunRight, turnLeft, turnRadarLeft, turnRadarRight, turnRight
|
---|
| 138 | };
|
---|
| 139 |
|
---|
| 140 | var constantSymbols = new List<ISymbol>()
|
---|
| 141 | {
|
---|
| 142 | shotPower, /*independent,*/ constant
|
---|
| 143 | };
|
---|
| 144 |
|
---|
| 145 | var eventSymbols = new List<ISymbol>()
|
---|
| 146 | {
|
---|
| 147 | run, onBulletHit, onBulletMissed, onHitByBullet, onHitRobot, onHitWall, onScannedRobot, block
|
---|
| 148 | };
|
---|
| 149 | #endregion
|
---|
| 150 |
|
---|
| 151 | #region Adding Symbols
|
---|
| 152 | // add all symbols to the grammar
|
---|
| 153 |
|
---|
| 154 | AddSymbol(ifStmt);
|
---|
| 155 | AddSymbol(elseStmt);
|
---|
| 156 | AddSymbol(whileLoop);
|
---|
| 157 |
|
---|
| 158 | AddSymbol(numExpr);
|
---|
| 159 | foreach (var s in numericalExpressions)
|
---|
| 160 | AddSymbol(s);
|
---|
| 161 | foreach (var s in numericalOperators)
|
---|
| 162 | AddSymbol(s);
|
---|
| 163 |
|
---|
| 164 | AddSymbol(logicExpr);
|
---|
| 165 | foreach (var s in logicalExpressions)
|
---|
| 166 | AddSymbol(s);
|
---|
| 167 | foreach (var s in logicalComparators)
|
---|
| 168 | AddSymbol(s);
|
---|
| 169 | foreach (var s in numericalComparators)
|
---|
| 170 | AddSymbol(s);
|
---|
| 171 |
|
---|
| 172 | foreach (var s in terminalSymbols)
|
---|
| 173 | AddSymbol(s);
|
---|
| 174 | foreach (var s in functionSymbols)
|
---|
| 175 | AddSymbol(s);
|
---|
| 176 | foreach (var s in constantSymbols)
|
---|
| 177 | AddSymbol(s);
|
---|
| 178 | foreach (var s in eventSymbols)
|
---|
| 179 | AddSymbol(s);
|
---|
| 180 | AddSymbol(tank);
|
---|
| 181 | #endregion
|
---|
| 182 |
|
---|
| 183 | #region Defining Grammar
|
---|
| 184 | // define grammar rules
|
---|
| 185 |
|
---|
| 186 | // Branches
|
---|
| 187 | AddAllowedChildSymbol(ifStmt, logicExpr, 0);
|
---|
| 188 | AddAllowedChildSymbol(ifStmt, block, 1);
|
---|
| 189 | AddAllowedChildSymbol(ifStmt, elseStmt, 2);
|
---|
| 190 | AddAllowedChildSymbol(ifStmt, doNothing, 2);
|
---|
| 191 | AddAllowedChildSymbol(elseStmt, block);
|
---|
| 192 | AddAllowedChildSymbol(whileLoop, logicExpr, 0);
|
---|
| 193 | AddAllowedChildSymbol(whileLoop, block, 1);
|
---|
| 194 |
|
---|
| 195 | // Numerical Expressions
|
---|
| 196 | foreach (var s in numericalExpressions)
|
---|
| 197 | {
|
---|
| 198 | AddAllowedChildSymbol(numExpr, s);
|
---|
| 199 | AddAllowedChildSymbol(numOp, s, 1);
|
---|
| 200 | AddAllowedChildSymbol(numOp, s, 2);
|
---|
| 201 | }
|
---|
| 202 | foreach (var s in numericalOperators)
|
---|
| 203 | AddAllowedChildSymbol(numOp, s, 0);
|
---|
| 204 |
|
---|
| 205 | // Logical Expressions
|
---|
| 206 | foreach (var s in logicalExpressions)
|
---|
| 207 | {
|
---|
| 208 | AddAllowedChildSymbol(logicExpr, s);
|
---|
| 209 | AddAllowedChildSymbol(not, s);
|
---|
| 210 | AddAllowedChildSymbol(logicComp, s, 1);
|
---|
| 211 | AddAllowedChildSymbol(logicComp, s, 2);
|
---|
| 212 | }
|
---|
| 213 | foreach (var s in logicalComparators)
|
---|
| 214 | AddAllowedChildSymbol(logicComp, s, 0);
|
---|
| 215 | foreach (var s in numericalExpressions)
|
---|
| 216 | {
|
---|
| 217 | AddAllowedChildSymbol(numComp, s, 1);
|
---|
| 218 | AddAllowedChildSymbol(numComp, s, 2);
|
---|
| 219 | }
|
---|
| 220 | foreach (var s in numericalComparators)
|
---|
| 221 | AddAllowedChildSymbol(numComp, s, 0);
|
---|
| 222 |
|
---|
| 223 |
|
---|
| 224 | // All Void Statements can appear in run or in an event handler or inside a block
|
---|
| 225 | foreach (var e in eventSymbols)
|
---|
| 226 | {
|
---|
| 227 | AddAllowedChildSymbol(e, ifStmt);
|
---|
| 228 | AddAllowedChildSymbol(e, whileLoop);
|
---|
| 229 | AddAllowedChildSymbol(e, doNothing);
|
---|
| 230 | foreach (var f in functionSymbols)
|
---|
| 231 | AddAllowedChildSymbol(e, f);
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | // Add the appropriate parameters as children of their respective functions
|
---|
| 235 | foreach (var f in functionSymbols)
|
---|
| 236 | {
|
---|
| 237 | if (f is SetAdjustGunForRobotTurn ||
|
---|
| 238 | f is SetAdjustRadarForGunTurn ||
|
---|
| 239 | f is SetAdjustRadarForRobotTurn)
|
---|
| 240 | AddAllowedChildSymbol(f, logicExpr);
|
---|
| 241 | else if (f is Fire)
|
---|
| 242 | AddAllowedChildSymbol(f, shotPower);
|
---|
| 243 | else
|
---|
| 244 | AddAllowedChildSymbol(f, numExpr);
|
---|
| 245 |
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | AddAllowedChildSymbol(tank, run, 0);
|
---|
| 249 | AddAllowedChildSymbol(tank, onBulletHit, 1);
|
---|
| 250 | AddAllowedChildSymbol(tank, onBulletMissed, 2);
|
---|
| 251 | AddAllowedChildSymbol(tank, onHitByBullet, 3);
|
---|
| 252 | AddAllowedChildSymbol(tank, onHitRobot, 4);
|
---|
| 253 | AddAllowedChildSymbol(tank, onHitWall, 5);
|
---|
| 254 | AddAllowedChildSymbol(tank, onScannedRobot, 6);
|
---|
| 255 |
|
---|
| 256 | AddAllowedChildSymbol(StartSymbol, tank, 0);
|
---|
| 257 | #endregion
|
---|
| 258 | }
|
---|
| 259 | }
|
---|
| 260 | } |
---|