[9790] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2013 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 |
|
---|
[9565] | 22 | using HeuristicLab.Common;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 26 |
|
---|
[9631] | 27 | namespace HeuristicLab.Problems.Robocode {
|
---|
| 28 | [StorableClass]
|
---|
| 29 | [Item("Robocode Grammar", "The grammar for the Robocode GP problem.")]
|
---|
| 30 | public class Grammar : SymbolicExpressionGrammar {
|
---|
[10014] | 31 | private const string EventsName = "Events";
|
---|
| 32 | private const string ExpressionsName = "Expressions";
|
---|
| 33 | private const string ControlStatementsName = "Control Statements";
|
---|
| 34 | private const string RobocodeFunctionsName = "Robocode Functions";
|
---|
[10043] | 35 | private const string RobocodeActionsName = "Robocode Actions";
|
---|
[10014] | 36 | private const string RelationalOperatorsName = "Relational Operators";
|
---|
| 37 | private const string LogicalOperators = "Logical Operators";
|
---|
| 38 | private const string NumericalOperatorsName = "Numerical Operators";
|
---|
| 39 |
|
---|
[9631] | 40 | [StorableConstructor]
|
---|
[10011] | 41 | protected Grammar(bool deserializing) : base(deserializing) { }
|
---|
| 42 | protected Grammar(Grammar original, Cloner cloner) : base(original, cloner) { }
|
---|
[9565] | 43 |
|
---|
[9631] | 44 | public Grammar()
|
---|
| 45 | : base("Robocode Grammar", "The grammar for the Robocode GP problem.") {
|
---|
| 46 | Initialize();
|
---|
| 47 | }
|
---|
[9565] | 48 |
|
---|
[9631] | 49 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 50 | return new Grammar(this, cloner);
|
---|
| 51 | }
|
---|
[9565] | 52 |
|
---|
[9631] | 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();
|
---|
[10011] | 58 | var stat = new Stat();
|
---|
[10028] | 59 | var ifThenElseStat = new IfThenElseStat();
|
---|
[10011] | 60 | var whileStat = new WhileStat();
|
---|
[9565] | 61 |
|
---|
[10011] | 62 | var logicalExpr = new LogicalExpression();
|
---|
| 63 | var numericalExpr = new NumericalExpression();
|
---|
[9565] | 64 |
|
---|
[10011] | 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 |
|
---|
[9631] | 81 | var number = new Number();
|
---|
[10011] | 82 | var logicalVal = new LogicalValue();
|
---|
[9565] | 83 |
|
---|
[9631] | 84 | var ahead = new Ahead();
|
---|
| 85 | var back = new Back();
|
---|
| 86 | var fire = new Fire();
|
---|
| 87 | var shotPower = new ShotPower();
|
---|
[9565] | 88 |
|
---|
[9631] | 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();
|
---|
[9565] | 95 |
|
---|
[9631] | 96 | var turnLeft = new TurnLeft();
|
---|
| 97 | var turnRight = new TurnRight();
|
---|
| 98 | var turnGunLeft = new TurnGunLeft();
|
---|
| 99 | var turnGunRight = new TurnGunRight();
|
---|
| 100 | var turnRadarLeft = new TurnRadarLeft();
|
---|
| 101 | var turnRadarRight = new TurnRadarRight();
|
---|
| 102 |
|
---|
| 103 | var onBulletHit = new OnBulletHit();
|
---|
| 104 | var onBulletMissed = new OnBulletMissed();
|
---|
| 105 | var onHitByBullet = new OnHitByBullet();
|
---|
| 106 | var onHitRobot = new OnHitRobot();
|
---|
| 107 | var onHitWall = new OnHitWall();
|
---|
| 108 | var onScannedRobot = new OnScannedRobot();
|
---|
| 109 |
|
---|
| 110 | var run = new Run();
|
---|
| 111 | var tank = new Tank();
|
---|
[10011] | 112 |
|
---|
| 113 | var doNothing = new DoNothing();
|
---|
| 114 | var emptyEvent = new EmptyEvent();
|
---|
[9631] | 115 | #endregion
|
---|
| 116 |
|
---|
| 117 | #region Symbol Collections
|
---|
[10028] | 118 | var controlSymbols = new ISymbol[] { ifThenElseStat, whileStat };
|
---|
[10014] | 119 | var actionSymbols = new ISymbol[] {
|
---|
[10011] | 120 | ahead, back, fire, turnGunLeft, turnGunRight, turnLeft, turnRadarLeft, turnRadarRight, turnRight
|
---|
| 121 | };
|
---|
[10014] | 122 | var functionSymbols = new ISymbol[] {
|
---|
| 123 | getEnergy, getGunHeading, getHeading, getRadarHeading, getX, getY
|
---|
| 124 | };
|
---|
| 125 |
|
---|
[10043] | 126 | var events = new GroupSymbol(EventsName, new ISymbol[] { run, onScannedRobot, onBulletHit, onBulletMissed, onHitByBullet, onHitRobot, onHitWall });
|
---|
[10028] | 127 | var controlStatements = new GroupSymbol(ControlStatementsName, controlSymbols);
|
---|
[10014] | 128 | var expressions = new GroupSymbol(ExpressionsName, new ISymbol[] { logicalExpr, numericalExpr });
|
---|
[10043] | 129 | var robocodeFunctions = new GroupSymbol(RobocodeFunctionsName, functionSymbols);
|
---|
| 130 | var robocodeActions = new GroupSymbol(RobocodeActionsName, actionSymbols);
|
---|
[10014] | 131 | var relationalOperators = new GroupSymbol(RelationalOperatorsName, new ISymbol[] { equal, lessThan, lessThanOrEqual, greaterThan, greaterThanOrEqual });
|
---|
| 132 | var logicalOperators = new GroupSymbol(LogicalOperators, new ISymbol[] { conjunction, disjunction, negation });
|
---|
| 133 | var numericalOperators = new GroupSymbol(NumericalOperatorsName, new ISymbol[] { addition, subtraction, multiplication, division, modulus });
|
---|
[9631] | 134 | #endregion
|
---|
[9565] | 135 |
|
---|
[9631] | 136 | #region Adding Symbols
|
---|
[10014] | 137 | AddSymbol(tank);
|
---|
| 138 | AddSymbol(stat);
|
---|
[10011] | 139 | AddSymbol(block);
|
---|
| 140 | AddSymbol(shotPower);
|
---|
[10014] | 141 | AddSymbol(number);
|
---|
[10011] | 142 | AddSymbol(logicalVal);
|
---|
[10014] | 143 | AddSymbol(events);
|
---|
| 144 | AddSymbol(expressions);
|
---|
| 145 | AddSymbol(controlStatements);
|
---|
| 146 | AddSymbol(robocodeFunctions);
|
---|
[10043] | 147 | AddSymbol(robocodeActions);
|
---|
[10014] | 148 | AddSymbol(relationalOperators);
|
---|
| 149 | AddSymbol(logicalOperators);
|
---|
| 150 | AddSymbol(numericalOperators);
|
---|
[10011] | 151 | AddSymbol(emptyEvent);
|
---|
| 152 | AddSymbol(doNothing);
|
---|
[9631] | 153 | #endregion
|
---|
[9565] | 154 |
|
---|
[10011] | 155 | #region Grammar Definition
|
---|
| 156 | // StartSymbol
|
---|
| 157 | AddAllowedChildSymbol(StartSymbol, tank);
|
---|
[9565] | 158 |
|
---|
[10011] | 159 | // Tank
|
---|
| 160 | AddAllowedChildSymbol(tank, run, 0);
|
---|
| 161 | AddAllowedChildSymbol(tank, onScannedRobot, 1);
|
---|
| 162 | AddAllowedChildSymbol(tank, onBulletHit, 2);
|
---|
| 163 | AddAllowedChildSymbol(tank, onBulletMissed, 3);
|
---|
| 164 | AddAllowedChildSymbol(tank, onHitByBullet, 4);
|
---|
| 165 | AddAllowedChildSymbol(tank, onHitRobot, 5);
|
---|
| 166 | AddAllowedChildSymbol(tank, onHitWall, 6);
|
---|
[9565] | 167 |
|
---|
[10043] | 168 | // Events
|
---|
| 169 | AddAllowedChildSymbol(events, stat);
|
---|
[10011] | 170 |
|
---|
| 171 | // Block
|
---|
| 172 | AddAllowedChildSymbol(block, stat);
|
---|
| 173 |
|
---|
| 174 | // Stat
|
---|
| 175 | AddAllowedChildSymbol(stat, stat);
|
---|
| 176 | AddAllowedChildSymbol(stat, block);
|
---|
[10043] | 177 | AddAllowedChildSymbol(stat, controlStatements);
|
---|
| 178 | AddAllowedChildSymbol(stat, robocodeFunctions);
|
---|
| 179 | AddAllowedChildSymbol(stat, robocodeActions);
|
---|
[10011] | 180 | AddAllowedChildSymbol(stat, emptyEvent);
|
---|
| 181 | AddAllowedChildSymbol(stat, doNothing);
|
---|
| 182 |
|
---|
| 183 | // IfStat
|
---|
[10028] | 184 | AddAllowedChildSymbol(ifThenElseStat, logicalExpr, 0);
|
---|
| 185 | AddAllowedChildSymbol(ifThenElseStat, stat, 1);
|
---|
| 186 | AddAllowedChildSymbol(ifThenElseStat, emptyEvent, 1);
|
---|
| 187 | AddAllowedChildSymbol(ifThenElseStat, doNothing, 1);
|
---|
| 188 | AddAllowedChildSymbol(ifThenElseStat, stat, 2);
|
---|
| 189 | AddAllowedChildSymbol(ifThenElseStat, emptyEvent, 2);
|
---|
| 190 | AddAllowedChildSymbol(ifThenElseStat, doNothing, 2);
|
---|
[10011] | 191 |
|
---|
| 192 | // WhileStat
|
---|
| 193 | AddAllowedChildSymbol(whileStat, logicalExpr, 0);
|
---|
| 194 | AddAllowedChildSymbol(whileStat, stat, 1);
|
---|
| 195 | AddAllowedChildSymbol(whileStat, emptyEvent, 1);
|
---|
| 196 | AddAllowedChildSymbol(whileStat, doNothing, 1);
|
---|
| 197 |
|
---|
[9631] | 198 | // Numerical Expressions
|
---|
[10043] | 199 | AddAllowedChildSymbol(numericalExpr, number);
|
---|
| 200 | AddAllowedChildSymbol(numericalExpr, robocodeFunctions);
|
---|
| 201 | AddAllowedChildSymbol(numericalExpr, numericalOperators);
|
---|
| 202 | AddAllowedChildSymbol(numericalOperators, number);
|
---|
| 203 | AddAllowedChildSymbol(numericalOperators, robocodeFunctions);
|
---|
| 204 | AddAllowedChildSymbol(numericalOperators, numericalOperators);
|
---|
[9565] | 205 |
|
---|
[9631] | 206 | // Logical Expressions
|
---|
[10011] | 207 | AddAllowedChildSymbol(logicalExpr, logicalVal);
|
---|
[10043] | 208 | AddAllowedChildSymbol(logicalExpr, logicalOperators);
|
---|
| 209 | AddAllowedChildSymbol(logicalOperators, logicalVal);
|
---|
| 210 | AddAllowedChildSymbol(logicalOperators, logicalOperators);
|
---|
| 211 | AddAllowedChildSymbol(logicalOperators, relationalOperators);
|
---|
| 212 | AddAllowedChildSymbol(relationalOperators, numericalExpr);
|
---|
[9565] | 213 |
|
---|
[10043] | 214 | // Functions and Actions
|
---|
| 215 | AddAllowedChildSymbol(robocodeFunctions, numericalExpr);
|
---|
| 216 | foreach (var a in robocodeActions.Symbols) {
|
---|
| 217 | if (a is Fire) AddAllowedChildSymbol(a, shotPower);
|
---|
| 218 | else AddAllowedChildSymbol(a, numericalExpr);
|
---|
[9631] | 219 | }
|
---|
| 220 | #endregion
|
---|
[9565] | 221 | }
|
---|
[9631] | 222 | }
|
---|
[9565] | 223 | } |
---|