[13210] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15584] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[13210] | 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 HeuristicLab.Common;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 26 |
|
---|
| 27 | namespace HeuristicLab.Problems.GeneticProgramming.Robocode {
|
---|
| 28 | [StorableClass]
|
---|
| 29 | [Item("Robocode Grammar", "The grammar for the Robocode GP problem.")]
|
---|
| 30 | public class Grammar : SymbolicExpressionGrammar {
|
---|
| 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";
|
---|
| 35 | private const string RobocodeActionsName = "Robocode Actions";
|
---|
| 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 SimpleSymbol("Block", "A group of statements", 1, byte.MaxValue);
|
---|
| 58 | var stat = new SimpleSymbol("Statement", "A statement.", 1, 1);
|
---|
| 59 | var ifThenElseStat = new SimpleSymbol("IfThenElseStat", "An if statement.", 2, 3);
|
---|
| 60 | var whileStat = new SimpleSymbol("WhileStat", "A while statement.", 2, 2);
|
---|
| 61 | whileStat.Enabled = false;
|
---|
| 62 |
|
---|
| 63 | var boolExpr = new SimpleSymbol("BooleanExpression", "A Boolean expression.", 1, 1);
|
---|
| 64 | var numericalExpr = new SimpleSymbol("NumericalExpression", "A numerical expression.", 1, 1);
|
---|
| 65 |
|
---|
| 66 | var equal = new SimpleSymbol("Equal", "Equal comparator.", 2, 2);
|
---|
| 67 | var lessThan = new SimpleSymbol("LessThan", "LessThan comparator.", 2, 2);
|
---|
| 68 | var lessThanOrEqual = new SimpleSymbol("LessThanOrEqual", "LessThanOrEqual comparator.", 2, 2);
|
---|
| 69 | var greaterThan = new SimpleSymbol("GreaterThan", "GreaterThan comparator.", 2, 2);
|
---|
| 70 | var greaterThanOrEqual = new SimpleSymbol("GreaterThanOrEqual", "GreaterThanOrEqual comparator.", 2, 2);
|
---|
| 71 |
|
---|
| 72 | var conjunction = new SimpleSymbol("ConditionalAnd", "Conjunction comparator.", 2, byte.MaxValue);
|
---|
| 73 | var disjunction = new SimpleSymbol("ConditionalOr", "Disjunction comparator.", 2, byte.MaxValue);
|
---|
| 74 | var negation = new SimpleSymbol("Negation", "A negation.", 1, 1);
|
---|
| 75 |
|
---|
| 76 | var addition = new SimpleSymbol("Addition", "Addition operator.", 2, byte.MaxValue);
|
---|
| 77 | var subtraction = new SimpleSymbol("Subtraction", "Subtraction operator.", 2, byte.MaxValue);
|
---|
| 78 | var multiplication = new SimpleSymbol("Multiplication", "Multiplication operator.", 2, byte.MaxValue);
|
---|
| 79 | var division = new SimpleSymbol("Division", "Division operator.", 2, byte.MaxValue);
|
---|
| 80 | var modulus = new SimpleSymbol("Modulus", "Modulus operator.", 2, byte.MaxValue);
|
---|
| 81 |
|
---|
| 82 | var number = new Number();
|
---|
| 83 | var logicalVal = new BooleanValue();
|
---|
| 84 |
|
---|
| 85 | var ahead = new SimpleSymbol("Ahead", "Immediately moves your robot ahead (forward) by distance measured in pixels.", 1, 1);
|
---|
| 86 | var back = new SimpleSymbol("Back", "Immediately moves your robot backward by distance measured in pixels.", 1, 1);
|
---|
| 87 | var fire = new SimpleSymbol("Fire", "Immediately fires a bullet.", 1, 1);
|
---|
| 88 | var shotPower = new ShotPower();
|
---|
| 89 |
|
---|
| 90 | var getEnergy = new SimpleSymbol("GetEnergy", "Returns the robot's current energy.", 0, 0);
|
---|
| 91 | var getHeading = new SimpleSymbol("GetHeading", "Returns the direction that the robot's body is facing, in degrees.", 0, 0);
|
---|
| 92 | var getGunHeading = new SimpleSymbol("GetGunHeading", "Returns the direction that the robot's gun is facing, in degrees.", 0, 0);
|
---|
| 93 | var getRadarHeading = new SimpleSymbol("GetRadarHeading", "Returns the direction that the robot's radar is facing, in degrees.", 0, 0);
|
---|
| 94 | var getX = new SimpleSymbol("GetX", "Returns the X position of the robot. (0,0) is at the bottom left of the battlefield.", 0, 0);
|
---|
| 95 | var getY = new SimpleSymbol("GetY", "Returns the Y position of the robot. (0,0) is at the bottom left of the battlefield.", 0, 0);
|
---|
| 96 |
|
---|
| 97 | var turnLeft = new SimpleSymbol("TurnLeft", "Immediately turns the robot's body to the left by degrees.", 1, 1);
|
---|
| 98 | var turnRight = new SimpleSymbol("TurnRight", "Immediately turns the robot's body to the right by degrees.", 1, 1);
|
---|
| 99 | var turnGunLeft = new SimpleSymbol("TurnGunLeft", "Immediately turns the robot's gun to the left by degrees.", 1, 1);
|
---|
| 100 | var turnGunRight = new SimpleSymbol("TurnGunRight", "Immediately turns the robot's gun to the right by degrees.", 1, 1);
|
---|
| 101 | var turnRadarLeft = new SimpleSymbol("TurnRadarLeft", "Immediately turns the robot's radar to the left by degrees.", 1, 1);
|
---|
| 102 | var turnRadarRight = new SimpleSymbol("TurnRadarRight", "Immediately turns the robot's radar to the right by degrees.", 1, 1);
|
---|
| 103 |
|
---|
| 104 | var onBulletHit = new SimpleSymbol("OnBulletHit", "This method is called when one of your bullets hits another robot.", 2, 10);
|
---|
| 105 | var onBulletMissed = new SimpleSymbol("OnBulletMissed", "This method is called when one of your bullets misses, i.e. hits a wall.", 2, 10);
|
---|
| 106 | var onHitByBullet = new SimpleSymbol("OnHitByBullet", "This method is called when your robot is hit by a bullet.", 2, 10);
|
---|
| 107 | var onHitRobot = new SimpleSymbol("OnHitRobot", "This method is called when your robot collides with another robot.", 2, 10);
|
---|
| 108 | var onHitWall = new SimpleSymbol("OnHitWall", "This method is called when your robot collides with a wall.", 2, 10);
|
---|
| 109 | var onScannedRobot = new SimpleSymbol("OnScannedRobot", "This method is called when your robot sees another robot, i.e. when the robot's radar scan \"hits\" another robot.", 2, 10);
|
---|
| 110 |
|
---|
| 111 | var run = new SimpleSymbol("Run", "The main method in every robot.", 1, 10);
|
---|
| 112 | var tank = new SimpleSymbol("Tank", "The root of a Robocode Tank program.", 2, 7);
|
---|
| 113 |
|
---|
| 114 | var doNothing = new SimpleSymbol("DoNothing", "Do nothing this turn, meaning that the robot will skip its turn.", 0, 0);
|
---|
| 115 | var emptyEvent = new SimpleSymbol("EmptyEvent", "This is a placeholder for an empty event.", 0, 0);
|
---|
| 116 | #endregion
|
---|
| 117 |
|
---|
| 118 | #region Symbol Collections
|
---|
| 119 | var controlSymbols = new ISymbol[] { ifThenElseStat, whileStat };
|
---|
| 120 | var actionSymbols = new ISymbol[] {
|
---|
| 121 | ahead, back, fire, turnGunLeft, turnGunRight, turnLeft, turnRadarLeft, turnRadarRight, turnRight
|
---|
| 122 | };
|
---|
| 123 | var functionSymbols = new ISymbol[] {
|
---|
| 124 | getEnergy, getGunHeading, getHeading, getRadarHeading, getX, getY
|
---|
| 125 | };
|
---|
| 126 |
|
---|
| 127 | var events = new GroupSymbol(EventsName, new ISymbol[] { run, onScannedRobot, onBulletHit, onBulletMissed, onHitByBullet, onHitRobot, onHitWall });
|
---|
| 128 | var controlStatements = new GroupSymbol(ControlStatementsName, controlSymbols);
|
---|
| 129 | var expressions = new GroupSymbol(ExpressionsName, new ISymbol[] { boolExpr, numericalExpr });
|
---|
| 130 | var robocodeFunctions = new GroupSymbol(RobocodeFunctionsName, functionSymbols);
|
---|
| 131 | var robocodeActions = new GroupSymbol(RobocodeActionsName, actionSymbols);
|
---|
| 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 });
|
---|
| 135 | #endregion
|
---|
| 136 |
|
---|
| 137 | #region Adding Symbols
|
---|
| 138 | AddSymbol(tank);
|
---|
| 139 | AddSymbol(stat);
|
---|
| 140 | AddSymbol(block);
|
---|
| 141 | AddSymbol(shotPower);
|
---|
| 142 | AddSymbol(number);
|
---|
| 143 | AddSymbol(logicalVal);
|
---|
| 144 | AddSymbol(events);
|
---|
| 145 | AddSymbol(expressions);
|
---|
| 146 | AddSymbol(controlStatements);
|
---|
| 147 | AddSymbol(robocodeFunctions);
|
---|
| 148 | AddSymbol(robocodeActions);
|
---|
| 149 | AddSymbol(relationalOperators);
|
---|
| 150 | AddSymbol(logicalOperators);
|
---|
| 151 | AddSymbol(numericalOperators);
|
---|
| 152 | AddSymbol(emptyEvent);
|
---|
| 153 | AddSymbol(doNothing);
|
---|
| 154 | #endregion
|
---|
| 155 |
|
---|
| 156 | #region Grammar Definition
|
---|
| 157 | // StartSymbol
|
---|
| 158 | AddAllowedChildSymbol(StartSymbol, tank);
|
---|
| 159 |
|
---|
| 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);
|
---|
| 168 |
|
---|
| 169 | // Events
|
---|
| 170 | AddAllowedChildSymbol(events, stat);
|
---|
| 171 |
|
---|
| 172 | // Block
|
---|
| 173 | AddAllowedChildSymbol(block, stat);
|
---|
| 174 |
|
---|
| 175 | // Stat
|
---|
| 176 | AddAllowedChildSymbol(stat, stat);
|
---|
| 177 | AddAllowedChildSymbol(stat, block);
|
---|
| 178 | AddAllowedChildSymbol(stat, controlStatements);
|
---|
| 179 | AddAllowedChildSymbol(stat, robocodeFunctions);
|
---|
| 180 | AddAllowedChildSymbol(stat, robocodeActions);
|
---|
| 181 | AddAllowedChildSymbol(stat, emptyEvent);
|
---|
| 182 | AddAllowedChildSymbol(stat, doNothing);
|
---|
| 183 |
|
---|
| 184 | // IfStat
|
---|
| 185 | AddAllowedChildSymbol(ifThenElseStat, boolExpr, 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);
|
---|
| 192 |
|
---|
| 193 | // WhileStat
|
---|
| 194 | AddAllowedChildSymbol(whileStat, boolExpr, 0);
|
---|
| 195 | AddAllowedChildSymbol(whileStat, stat, 1);
|
---|
| 196 | AddAllowedChildSymbol(whileStat, emptyEvent, 1);
|
---|
| 197 | AddAllowedChildSymbol(whileStat, doNothing, 1);
|
---|
| 198 |
|
---|
| 199 | // Numerical Expressions
|
---|
| 200 | AddAllowedChildSymbol(numericalExpr, number);
|
---|
| 201 | AddAllowedChildSymbol(numericalExpr, robocodeFunctions);
|
---|
| 202 | AddAllowedChildSymbol(numericalExpr, numericalOperators);
|
---|
| 203 | AddAllowedChildSymbol(numericalOperators, number);
|
---|
| 204 | AddAllowedChildSymbol(numericalOperators, robocodeFunctions);
|
---|
| 205 | AddAllowedChildSymbol(numericalOperators, numericalOperators);
|
---|
| 206 |
|
---|
| 207 | // Logical Expressions
|
---|
| 208 | AddAllowedChildSymbol(boolExpr, logicalVal);
|
---|
| 209 | AddAllowedChildSymbol(boolExpr, logicalOperators);
|
---|
| 210 | AddAllowedChildSymbol(logicalOperators, logicalVal);
|
---|
| 211 | AddAllowedChildSymbol(logicalOperators, logicalOperators);
|
---|
| 212 | AddAllowedChildSymbol(logicalOperators, relationalOperators);
|
---|
| 213 | AddAllowedChildSymbol(relationalOperators, numericalExpr);
|
---|
| 214 |
|
---|
| 215 | // Functions and Actions
|
---|
| 216 | AddAllowedChildSymbol(robocodeFunctions, numericalExpr);
|
---|
| 217 | foreach (var a in robocodeActions.Symbols) {
|
---|
| 218 | if (a.Name == fire.Name) AddAllowedChildSymbol(a, shotPower);
|
---|
| 219 | else AddAllowedChildSymbol(a, numericalExpr);
|
---|
| 220 | }
|
---|
| 221 | #endregion
|
---|
| 222 | }
|
---|
| 223 | }
|
---|
| 224 | } |
---|