[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();
|
---|
[10046] | 61 | whileStat.Enabled = false;
|
---|
[9565] | 62 |
|
---|
[10011] | 63 | var logicalExpr = new LogicalExpression();
|
---|
| 64 | var numericalExpr = new NumericalExpression();
|
---|
[9565] | 65 |
|
---|
[10011] | 66 | var equal = new Equal();
|
---|
| 67 | var lessThan = new LessThan();
|
---|
| 68 | var lessThanOrEqual = new LessThanOrEqual();
|
---|
| 69 | var greaterThan = new GreaterThan();
|
---|
| 70 | var greaterThanOrEqual = new GreaterThanOrEqual();
|
---|
| 71 |
|
---|
| 72 | var conjunction = new Conjunction();
|
---|
| 73 | var disjunction = new Disjunction();
|
---|
| 74 | var negation = new Negation();
|
---|
| 75 |
|
---|
| 76 | var addition = new Addition();
|
---|
| 77 | var subtraction = new Subtraction();
|
---|
| 78 | var multiplication = new Multiplication();
|
---|
| 79 | var division = new Division();
|
---|
| 80 | var modulus = new Modulus();
|
---|
| 81 |
|
---|
[9631] | 82 | var number = new Number();
|
---|
[10011] | 83 | var logicalVal = new LogicalValue();
|
---|
[9565] | 84 |
|
---|
[9631] | 85 | var ahead = new Ahead();
|
---|
| 86 | var back = new Back();
|
---|
| 87 | var fire = new Fire();
|
---|
| 88 | var shotPower = new ShotPower();
|
---|
[9565] | 89 |
|
---|
[9631] | 90 | var getEnergy = new GetEnergy();
|
---|
| 91 | var getGunHeading = new GetGunHeading();
|
---|
| 92 | var getHeading = new GetHeading();
|
---|
| 93 | var getRadarHeading = new GetRadarHeading();
|
---|
| 94 | var getX = new GetX();
|
---|
| 95 | var getY = new GetY();
|
---|
[9565] | 96 |
|
---|
[9631] | 97 | var turnLeft = new TurnLeft();
|
---|
| 98 | var turnRight = new TurnRight();
|
---|
| 99 | var turnGunLeft = new TurnGunLeft();
|
---|
| 100 | var turnGunRight = new TurnGunRight();
|
---|
| 101 | var turnRadarLeft = new TurnRadarLeft();
|
---|
| 102 | var turnRadarRight = new TurnRadarRight();
|
---|
| 103 |
|
---|
| 104 | var onBulletHit = new OnBulletHit();
|
---|
| 105 | var onBulletMissed = new OnBulletMissed();
|
---|
| 106 | var onHitByBullet = new OnHitByBullet();
|
---|
| 107 | var onHitRobot = new OnHitRobot();
|
---|
| 108 | var onHitWall = new OnHitWall();
|
---|
| 109 | var onScannedRobot = new OnScannedRobot();
|
---|
| 110 |
|
---|
| 111 | var run = new Run();
|
---|
| 112 | var tank = new Tank();
|
---|
[10011] | 113 |
|
---|
| 114 | var doNothing = new DoNothing();
|
---|
| 115 | var emptyEvent = new EmptyEvent();
|
---|
[9631] | 116 | #endregion
|
---|
| 117 |
|
---|
| 118 | #region Symbol Collections
|
---|
[10028] | 119 | var controlSymbols = new ISymbol[] { ifThenElseStat, whileStat };
|
---|
[10014] | 120 | var actionSymbols = new ISymbol[] {
|
---|
[10011] | 121 | ahead, back, fire, turnGunLeft, turnGunRight, turnLeft, turnRadarLeft, turnRadarRight, turnRight
|
---|
| 122 | };
|
---|
[10014] | 123 | var functionSymbols = new ISymbol[] {
|
---|
| 124 | getEnergy, getGunHeading, getHeading, getRadarHeading, getX, getY
|
---|
| 125 | };
|
---|
| 126 |
|
---|
[10043] | 127 | var events = new GroupSymbol(EventsName, new ISymbol[] { run, onScannedRobot, onBulletHit, onBulletMissed, onHitByBullet, onHitRobot, onHitWall });
|
---|
[10028] | 128 | var controlStatements = new GroupSymbol(ControlStatementsName, controlSymbols);
|
---|
[10014] | 129 | var expressions = new GroupSymbol(ExpressionsName, new ISymbol[] { logicalExpr, numericalExpr });
|
---|
[10043] | 130 | var robocodeFunctions = new GroupSymbol(RobocodeFunctionsName, functionSymbols);
|
---|
| 131 | var robocodeActions = new GroupSymbol(RobocodeActionsName, actionSymbols);
|
---|
[10014] | 132 | var relationalOperators = new GroupSymbol(RelationalOperatorsName, new ISymbol[] { equal, lessThan, lessThanOrEqual, greaterThan, greaterThanOrEqual });
|
---|
| 133 | var logicalOperators = new GroupSymbol(LogicalOperators, new ISymbol[] { conjunction, disjunction, negation });
|
---|
| 134 | var numericalOperators = new GroupSymbol(NumericalOperatorsName, new ISymbol[] { addition, subtraction, multiplication, division, modulus });
|
---|
[9631] | 135 | #endregion
|
---|
[9565] | 136 |
|
---|
[9631] | 137 | #region Adding Symbols
|
---|
[10014] | 138 | AddSymbol(tank);
|
---|
| 139 | AddSymbol(stat);
|
---|
[10011] | 140 | AddSymbol(block);
|
---|
| 141 | AddSymbol(shotPower);
|
---|
[10014] | 142 | AddSymbol(number);
|
---|
[10011] | 143 | AddSymbol(logicalVal);
|
---|
[10014] | 144 | AddSymbol(events);
|
---|
| 145 | AddSymbol(expressions);
|
---|
| 146 | AddSymbol(controlStatements);
|
---|
| 147 | AddSymbol(robocodeFunctions);
|
---|
[10043] | 148 | AddSymbol(robocodeActions);
|
---|
[10014] | 149 | AddSymbol(relationalOperators);
|
---|
| 150 | AddSymbol(logicalOperators);
|
---|
| 151 | AddSymbol(numericalOperators);
|
---|
[10011] | 152 | AddSymbol(emptyEvent);
|
---|
| 153 | AddSymbol(doNothing);
|
---|
[9631] | 154 | #endregion
|
---|
[9565] | 155 |
|
---|
[10011] | 156 | #region Grammar Definition
|
---|
| 157 | // StartSymbol
|
---|
| 158 | AddAllowedChildSymbol(StartSymbol, tank);
|
---|
[9565] | 159 |
|
---|
[10011] | 160 | // Tank
|
---|
| 161 | AddAllowedChildSymbol(tank, run, 0);
|
---|
| 162 | AddAllowedChildSymbol(tank, onScannedRobot, 1);
|
---|
| 163 | AddAllowedChildSymbol(tank, onBulletHit, 2);
|
---|
| 164 | AddAllowedChildSymbol(tank, onBulletMissed, 3);
|
---|
| 165 | AddAllowedChildSymbol(tank, onHitByBullet, 4);
|
---|
| 166 | AddAllowedChildSymbol(tank, onHitRobot, 5);
|
---|
| 167 | AddAllowedChildSymbol(tank, onHitWall, 6);
|
---|
[9565] | 168 |
|
---|
[10043] | 169 | // Events
|
---|
| 170 | AddAllowedChildSymbol(events, stat);
|
---|
[10011] | 171 |
|
---|
| 172 | // Block
|
---|
| 173 | AddAllowedChildSymbol(block, stat);
|
---|
| 174 |
|
---|
| 175 | // Stat
|
---|
| 176 | AddAllowedChildSymbol(stat, stat);
|
---|
| 177 | AddAllowedChildSymbol(stat, block);
|
---|
[10043] | 178 | AddAllowedChildSymbol(stat, controlStatements);
|
---|
| 179 | AddAllowedChildSymbol(stat, robocodeFunctions);
|
---|
| 180 | AddAllowedChildSymbol(stat, robocodeActions);
|
---|
[10011] | 181 | AddAllowedChildSymbol(stat, emptyEvent);
|
---|
| 182 | AddAllowedChildSymbol(stat, doNothing);
|
---|
| 183 |
|
---|
| 184 | // IfStat
|
---|
[10028] | 185 | AddAllowedChildSymbol(ifThenElseStat, logicalExpr, 0);
|
---|
| 186 | AddAllowedChildSymbol(ifThenElseStat, stat, 1);
|
---|
| 187 | AddAllowedChildSymbol(ifThenElseStat, emptyEvent, 1);
|
---|
| 188 | AddAllowedChildSymbol(ifThenElseStat, doNothing, 1);
|
---|
| 189 | AddAllowedChildSymbol(ifThenElseStat, stat, 2);
|
---|
| 190 | AddAllowedChildSymbol(ifThenElseStat, emptyEvent, 2);
|
---|
| 191 | AddAllowedChildSymbol(ifThenElseStat, doNothing, 2);
|
---|
[10011] | 192 |
|
---|
| 193 | // WhileStat
|
---|
| 194 | AddAllowedChildSymbol(whileStat, logicalExpr, 0);
|
---|
| 195 | AddAllowedChildSymbol(whileStat, stat, 1);
|
---|
| 196 | AddAllowedChildSymbol(whileStat, emptyEvent, 1);
|
---|
| 197 | AddAllowedChildSymbol(whileStat, doNothing, 1);
|
---|
| 198 |
|
---|
[9631] | 199 | // Numerical Expressions
|
---|
[10043] | 200 | AddAllowedChildSymbol(numericalExpr, number);
|
---|
| 201 | AddAllowedChildSymbol(numericalExpr, robocodeFunctions);
|
---|
| 202 | AddAllowedChildSymbol(numericalExpr, numericalOperators);
|
---|
| 203 | AddAllowedChildSymbol(numericalOperators, number);
|
---|
| 204 | AddAllowedChildSymbol(numericalOperators, robocodeFunctions);
|
---|
| 205 | AddAllowedChildSymbol(numericalOperators, numericalOperators);
|
---|
[9565] | 206 |
|
---|
[9631] | 207 | // Logical Expressions
|
---|
[10011] | 208 | AddAllowedChildSymbol(logicalExpr, logicalVal);
|
---|
[10043] | 209 | AddAllowedChildSymbol(logicalExpr, logicalOperators);
|
---|
| 210 | AddAllowedChildSymbol(logicalOperators, logicalVal);
|
---|
| 211 | AddAllowedChildSymbol(logicalOperators, logicalOperators);
|
---|
| 212 | AddAllowedChildSymbol(logicalOperators, relationalOperators);
|
---|
| 213 | AddAllowedChildSymbol(relationalOperators, numericalExpr);
|
---|
[9565] | 214 |
|
---|
[10043] | 215 | // Functions and Actions
|
---|
| 216 | AddAllowedChildSymbol(robocodeFunctions, numericalExpr);
|
---|
| 217 | foreach (var a in robocodeActions.Symbols) {
|
---|
| 218 | if (a is Fire) AddAllowedChildSymbol(a, shotPower);
|
---|
| 219 | else AddAllowedChildSymbol(a, numericalExpr);
|
---|
[9631] | 220 | }
|
---|
| 221 | #endregion
|
---|
[9565] | 222 | }
|
---|
[9631] | 223 | }
|
---|
[9565] | 224 | } |
---|