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 |
|
---|
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.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 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 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();
|
---|
112 |
|
---|
113 | var doNothing = new DoNothing();
|
---|
114 | var emptyEvent = new EmptyEvent();
|
---|
115 | #endregion
|
---|
116 |
|
---|
117 | #region Symbol Collections
|
---|
118 | var controlSymbols = new ISymbol[] { ifThenElseStat, whileStat };
|
---|
119 | var actionSymbols = new ISymbol[] {
|
---|
120 | ahead, back, fire, turnGunLeft, turnGunRight, turnLeft, turnRadarLeft, turnRadarRight, turnRight
|
---|
121 | };
|
---|
122 | var functionSymbols = new ISymbol[] {
|
---|
123 | getEnergy, getGunHeading, getHeading, getRadarHeading, getX, getY
|
---|
124 | };
|
---|
125 |
|
---|
126 | var events = new GroupSymbol(EventsName, new ISymbol[] { run, onScannedRobot, onBulletHit, onBulletMissed, onHitByBullet, onHitRobot, onHitWall });
|
---|
127 | var controlStatements = new GroupSymbol(ControlStatementsName, controlSymbols);
|
---|
128 | var expressions = new GroupSymbol(ExpressionsName, new ISymbol[] { logicalExpr, numericalExpr });
|
---|
129 | var robocodeFunctions = new GroupSymbol(RobocodeFunctionsName, functionSymbols);
|
---|
130 | var robocodeActions = new GroupSymbol(RobocodeActionsName, actionSymbols);
|
---|
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 });
|
---|
134 | #endregion
|
---|
135 |
|
---|
136 | #region Adding Symbols
|
---|
137 | AddSymbol(tank);
|
---|
138 | AddSymbol(stat);
|
---|
139 | AddSymbol(block);
|
---|
140 | AddSymbol(shotPower);
|
---|
141 | AddSymbol(number);
|
---|
142 | AddSymbol(logicalVal);
|
---|
143 | AddSymbol(events);
|
---|
144 | AddSymbol(expressions);
|
---|
145 | AddSymbol(controlStatements);
|
---|
146 | AddSymbol(robocodeFunctions);
|
---|
147 | AddSymbol(robocodeActions);
|
---|
148 | AddSymbol(relationalOperators);
|
---|
149 | AddSymbol(logicalOperators);
|
---|
150 | AddSymbol(numericalOperators);
|
---|
151 | AddSymbol(emptyEvent);
|
---|
152 | AddSymbol(doNothing);
|
---|
153 | #endregion
|
---|
154 |
|
---|
155 | #region Grammar Definition
|
---|
156 | // StartSymbol
|
---|
157 | AddAllowedChildSymbol(StartSymbol, tank);
|
---|
158 |
|
---|
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);
|
---|
167 |
|
---|
168 | // Events
|
---|
169 | AddAllowedChildSymbol(events, stat);
|
---|
170 |
|
---|
171 | // Block
|
---|
172 | AddAllowedChildSymbol(block, stat);
|
---|
173 |
|
---|
174 | // Stat
|
---|
175 | AddAllowedChildSymbol(stat, stat);
|
---|
176 | AddAllowedChildSymbol(stat, block);
|
---|
177 | AddAllowedChildSymbol(stat, controlStatements);
|
---|
178 | AddAllowedChildSymbol(stat, robocodeFunctions);
|
---|
179 | AddAllowedChildSymbol(stat, robocodeActions);
|
---|
180 | AddAllowedChildSymbol(stat, emptyEvent);
|
---|
181 | AddAllowedChildSymbol(stat, doNothing);
|
---|
182 |
|
---|
183 | // IfStat
|
---|
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);
|
---|
191 |
|
---|
192 | // WhileStat
|
---|
193 | AddAllowedChildSymbol(whileStat, logicalExpr, 0);
|
---|
194 | AddAllowedChildSymbol(whileStat, stat, 1);
|
---|
195 | AddAllowedChildSymbol(whileStat, emptyEvent, 1);
|
---|
196 | AddAllowedChildSymbol(whileStat, doNothing, 1);
|
---|
197 |
|
---|
198 | // Numerical Expressions
|
---|
199 | AddAllowedChildSymbol(numericalExpr, number);
|
---|
200 | AddAllowedChildSymbol(numericalExpr, robocodeFunctions);
|
---|
201 | AddAllowedChildSymbol(numericalExpr, numericalOperators);
|
---|
202 | AddAllowedChildSymbol(numericalOperators, number);
|
---|
203 | AddAllowedChildSymbol(numericalOperators, robocodeFunctions);
|
---|
204 | AddAllowedChildSymbol(numericalOperators, numericalOperators);
|
---|
205 |
|
---|
206 | // Logical Expressions
|
---|
207 | AddAllowedChildSymbol(logicalExpr, logicalVal);
|
---|
208 | AddAllowedChildSymbol(logicalExpr, logicalOperators);
|
---|
209 | AddAllowedChildSymbol(logicalOperators, logicalVal);
|
---|
210 | AddAllowedChildSymbol(logicalOperators, logicalOperators);
|
---|
211 | AddAllowedChildSymbol(logicalOperators, relationalOperators);
|
---|
212 | AddAllowedChildSymbol(relationalOperators, numericalExpr);
|
---|
213 |
|
---|
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);
|
---|
219 | }
|
---|
220 | #endregion
|
---|
221 | }
|
---|
222 | }
|
---|
223 | } |
---|