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 | whileStat.Enabled = false;
|
---|
62 |
|
---|
63 | var logicalExpr = new LogicalExpression();
|
---|
64 | var numericalExpr = new NumericalExpression();
|
---|
65 |
|
---|
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 |
|
---|
82 | var number = new Number();
|
---|
83 | var logicalVal = new LogicalValue();
|
---|
84 |
|
---|
85 | var ahead = new Ahead();
|
---|
86 | var back = new Back();
|
---|
87 | var fire = new Fire();
|
---|
88 | var shotPower = new ShotPower();
|
---|
89 |
|
---|
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();
|
---|
96 |
|
---|
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();
|
---|
113 |
|
---|
114 | var doNothing = new DoNothing();
|
---|
115 | var emptyEvent = new EmptyEvent();
|
---|
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[] { logicalExpr, 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, 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);
|
---|
192 |
|
---|
193 | // WhileStat
|
---|
194 | AddAllowedChildSymbol(whileStat, logicalExpr, 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(logicalExpr, logicalVal);
|
---|
209 | AddAllowedChildSymbol(logicalExpr, 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 is Fire) AddAllowedChildSymbol(a, shotPower);
|
---|
219 | else AddAllowedChildSymbol(a, numericalExpr);
|
---|
220 | }
|
---|
221 | #endregion
|
---|
222 | }
|
---|
223 | }
|
---|
224 | } |
---|