Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Grammar.cs @ 9844

Last change on this file since 9844 was 9790, checked in by ascheibe, 11 years ago

#2069

  • added license headers
  • corrected version information
  • fixed formatting
File size: 9.3 KB
Line 
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
22using System.Collections.Generic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Problems.Robocode {
29  [StorableClass]
30  [Item("Robocode Grammar", "The grammar for the Robocode GP problem.")]
31  public class Grammar : SymbolicExpressionGrammar {
32    [StorableConstructor]
33    private Grammar(bool deserializing) : base(deserializing) { }
34    private Grammar(Grammar original, Cloner cloner)
35      : base(original, cloner) {
36    }
37
38    public Grammar()
39      : base("Robocode Grammar", "The grammar for the Robocode GP problem.") {
40      Initialize();
41    }
42
43    public override IDeepCloneable Clone(Cloner cloner) {
44      return new Grammar(this, cloner);
45    }
46
47    // initialize set of allowed symbols and define
48    // the allowed combinations of symbols
49    private void Initialize() {
50      #region Symbols
51      var block = new Block();
52
53      var ifStmt = new IfStatement();
54      var elseStmt = new ElseStatement();
55      var whileLoop = new WhileLoop();
56
57      var numExpr = new NumericalExpression();
58      var number = new Number();
59      var numOp = new NumericalOperation();
60      var add = new Addition();
61      var sub = new Subtraction();
62      var mult = new Multiplication();
63      var div = new Division();
64      var mod = new Modulus();
65
66      var logicExpr = new LogicalExpression();
67      var logicComp = new LogicalComparison();
68      var numComp = new NumericalComparison();
69      var logicVal = new LogicalValue();
70      var not = new Negation();
71      var and = new Conjunction();
72      var or = new Disjunction();
73      var eq = new Equal();
74      var lt = new LessThan();
75      var gt = new GreaterThan();
76      var lteq = new LessThanOrEqual();
77      var gteq = new GreaterThanOrEqual();
78
79      var ahead = new Ahead();
80      var back = new Back();
81      var constant = new Constant();
82      var doNothing = new DoNothing();
83      var fire = new Fire();
84      var shotPower = new ShotPower();
85
86      var getEnergy = new GetEnergy();
87      var getGunHeading = new GetGunHeading();
88      var getHeading = new GetHeading();
89      var getRadarHeading = new GetRadarHeading();
90      var getX = new GetX();
91      var getY = new GetY();
92
93      //var setAdjustGunForRobotTurn = new SetAdjustGunForRobotTurn();
94      //var setAdjustRadarForGunTurn = new SetAdjustRadarForGunTurn();
95      //var setAdjustRadarForRobotTurn = new SetAdjustRadarForRobotTurn();
96      //var independent = new Independent();
97
98      var turnLeft = new TurnLeft();
99      var turnRight = new TurnRight();
100      var turnGunLeft = new TurnGunLeft();
101      var turnGunRight = new TurnGunRight();
102      var turnRadarLeft = new TurnRadarLeft();
103      var turnRadarRight = new TurnRadarRight();
104
105      var onBulletHit = new OnBulletHit();
106      var onBulletMissed = new OnBulletMissed();
107      var onHitByBullet = new OnHitByBullet();
108      var onHitRobot = new OnHitRobot();
109      var onHitWall = new OnHitWall();
110      var onScannedRobot = new OnScannedRobot();
111      var emptyEvent = new EmptyEvent();
112
113      var run = new Run();
114      var tank = new Tank();
115      var program = new Program();
116      #endregion
117
118      #region Symbol Collections
119      var numericalExpressions = new List<ISymbol>()
120            {
121                number, numOp, getEnergy, getGunHeading, getHeading, getRadarHeading, getX, getY
122            };
123
124      var numericalOperators = new List<ISymbol>()
125            {
126                add, sub, mult, div, mod
127            };
128
129      var logicalExpressions = new List<ISymbol>()
130            {
131                logicVal, logicComp, numComp, not
132            };
133
134      var logicalComparators = new List<ISymbol>()
135            {
136                and, or
137            };
138
139      var numericalComparators = new List<ISymbol>()
140            {
141                lt, gt, lteq, gteq, eq
142            };
143
144      var terminalSymbols = new List<ISymbol>()
145            {
146                doNothing, //getEnergy, getGunHeading, getHeading, getRadarHeading, getX, getY
147            };
148
149      var functionSymbols = new List<ISymbol>()
150            {
151                ahead, back, fire,
152                //setAdjustGunForRobotTurn, setAdjustRadarForGunTurn, setAdjustRadarForRobotTurn,
153                turnGunLeft, turnGunRight, turnLeft, turnRadarLeft, turnRadarRight, turnRight
154            };
155
156      var constantSymbols = new List<ISymbol>()
157            {
158                shotPower, /*independent,*/ constant
159            };
160
161      var eventSymbols = new List<ISymbol>()
162            {
163                run, onBulletHit, onBulletMissed, onHitByBullet,
164                onHitRobot, onHitWall, onScannedRobot, block, emptyEvent
165            };
166      #endregion
167
168      #region Adding Symbols
169      // add all symbols to the grammar
170
171      AddSymbol(ifStmt);
172      AddSymbol(elseStmt);
173      AddSymbol(whileLoop);
174
175      AddSymbol(numExpr);
176      foreach (var s in numericalExpressions)
177        AddSymbol(s);
178      foreach (var s in numericalOperators)
179        AddSymbol(s);
180
181      AddSymbol(logicExpr);
182      foreach (var s in logicalExpressions)
183        AddSymbol(s);
184      foreach (var s in logicalComparators)
185        AddSymbol(s);
186      foreach (var s in numericalComparators)
187        AddSymbol(s);
188
189      foreach (var s in terminalSymbols)
190        AddSymbol(s);
191      foreach (var s in functionSymbols)
192        AddSymbol(s);
193      foreach (var s in constantSymbols)
194        AddSymbol(s);
195      foreach (var s in eventSymbols)
196        AddSymbol(s);
197      AddSymbol(tank);
198      #endregion
199
200      #region Defining Grammar
201      // define grammar rules
202
203      // Branches
204      AddAllowedChildSymbol(ifStmt, logicExpr, 0);
205      AddAllowedChildSymbol(ifStmt, block, 1);
206      AddAllowedChildSymbol(ifStmt, elseStmt, 2);
207      AddAllowedChildSymbol(ifStmt, doNothing, 2);
208      AddAllowedChildSymbol(elseStmt, block);
209      AddAllowedChildSymbol(whileLoop, logicExpr, 0);
210      AddAllowedChildSymbol(whileLoop, block, 1);
211
212      // Numerical Expressions
213      foreach (var s in numericalExpressions) {
214        AddAllowedChildSymbol(numExpr, s);
215        AddAllowedChildSymbol(numOp, s, 1);
216        AddAllowedChildSymbol(numOp, s, 2);
217      }
218      foreach (var s in numericalOperators)
219        AddAllowedChildSymbol(numOp, s, 0);
220
221      // Logical Expressions
222      foreach (var s in logicalExpressions) {
223        AddAllowedChildSymbol(logicExpr, s);
224        AddAllowedChildSymbol(not, s);
225        AddAllowedChildSymbol(logicComp, s, 1);
226        AddAllowedChildSymbol(logicComp, s, 2);
227      }
228      foreach (var s in logicalComparators)
229        AddAllowedChildSymbol(logicComp, s, 0);
230      foreach (var s in numericalExpressions) {
231        AddAllowedChildSymbol(numComp, s, 1);
232        AddAllowedChildSymbol(numComp, s, 2);
233      }
234      foreach (var s in numericalComparators)
235        AddAllowedChildSymbol(numComp, s, 0);
236
237
238      // All Void Statements can appear in run or in an event handler or inside a block
239      foreach (var e in eventSymbols) {
240        AddAllowedChildSymbol(e, ifStmt);
241        AddAllowedChildSymbol(e, whileLoop);
242        AddAllowedChildSymbol(e, doNothing);
243        foreach (var f in functionSymbols)
244          AddAllowedChildSymbol(e, f);
245      }
246
247      // Add the appropriate parameters as children of their respective functions
248      foreach (var f in functionSymbols) {
249        if (f is SetAdjustGunForRobotTurn ||
250            f is SetAdjustRadarForGunTurn ||
251            f is SetAdjustRadarForRobotTurn)
252          AddAllowedChildSymbol(f, logicExpr);
253        else if (f is Fire)
254          AddAllowedChildSymbol(f, shotPower);
255        else
256          AddAllowedChildSymbol(f, numExpr);
257
258      }
259
260      // Add all Event Methods to Tank. Run and OnScannedEvent are not optional,
261      // so EmptyEvent cannot take their places
262
263      AddAllowedChildSymbol(tank, run, 0);
264      AddAllowedChildSymbol(tank, onScannedRobot, 1);
265      AddAllowedChildSymbol(tank, onBulletMissed, 2);
266      AddAllowedChildSymbol(tank, onHitByBullet, 3);
267      AddAllowedChildSymbol(tank, onHitRobot, 4);
268      AddAllowedChildSymbol(tank, onHitWall, 5);
269      AddAllowedChildSymbol(tank, onBulletHit, 6);
270      for (int i = 2; i < tank.MaximumArity; i++)
271        AddAllowedChildSymbol(tank, emptyEvent, i);
272
273      AddAllowedChildSymbol(StartSymbol, tank, 0);
274      #endregion
275    }
276  }
277}
Note: See TracBrowser for help on using the repository browser.