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

Last change on this file since 10014 was 10014, checked in by jkarder, 9 years ago

#2069:

  • refactored grammar and symbols
  • changed RobocodeProblem to use the ProbabilisticTreeCreator by default
File size: 9.4 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.Linq;
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    private const string EventsName = "Events";
33    private const string ExpressionsName = "Expressions";
34    private const string ControlStatementsName = "Control Statements";
35    private const string RobocodeFunctionsName = "Robocode Functions";
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 ifStat = new IfStat();
60      var elseStat = new ElseStat();
61      var whileStat = new WhileStat();
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 setAdjustGunForRobotTurn = new SetAdjustGunForRobotTurn();
98      //var setAdjustRadarForGunTurn = new SetAdjustRadarForGunTurn();
99      //var setAdjustRadarForRobotTurn = new SetAdjustRadarForRobotTurn();
100      //var independent = new Independent();
101
102      var turnLeft = new TurnLeft();
103      var turnRight = new TurnRight();
104      var turnGunLeft = new TurnGunLeft();
105      var turnGunRight = new TurnGunRight();
106      var turnRadarLeft = new TurnRadarLeft();
107      var turnRadarRight = new TurnRadarRight();
108
109      var onBulletHit = new OnBulletHit();
110      var onBulletMissed = new OnBulletMissed();
111      var onHitByBullet = new OnHitByBullet();
112      var onHitRobot = new OnHitRobot();
113      var onHitWall = new OnHitWall();
114      var onScannedRobot = new OnScannedRobot();
115
116      var run = new Run();
117      var tank = new Tank();
118
119      var doNothing = new DoNothing();
120      var emptyEvent = new EmptyEvent();
121      #endregion
122
123      #region Symbol Collections
124      var controlSymbols = new ISymbol[] { ifStat, whileStat };
125      var actionSymbols = new ISymbol[] {
126        ahead, back, fire, turnGunLeft, turnGunRight, turnLeft, turnRadarLeft, turnRadarRight, turnRight
127        //setAdjustGunForRobotTurn, setAdjustRadarForGunTurn, setAdjustRadarForRobotTurn,
128      };
129      var functionSymbols = new ISymbol[] {
130        getEnergy, getGunHeading, getHeading, getRadarHeading, getX, getY
131      };
132
133      var events = new GroupSymbol(EventsName, new ISymbol[] { onScannedRobot, onBulletHit, onBulletMissed, onHitByBullet, onHitRobot, onHitWall });
134      var controlStatements = new GroupSymbol(ControlStatementsName, controlSymbols.Concat(new[] { elseStat }));
135      var expressions = new GroupSymbol(ExpressionsName, new ISymbol[] { logicalExpr, numericalExpr });
136      var robocodeFunctions = new GroupSymbol(RobocodeFunctionsName, actionSymbols.Concat(functionSymbols));
137      var relationalOperators = new GroupSymbol(RelationalOperatorsName, new ISymbol[] { equal, lessThan, lessThanOrEqual, greaterThan, greaterThanOrEqual });
138      var logicalOperators = new GroupSymbol(LogicalOperators, new ISymbol[] { conjunction, disjunction, negation });
139      var numericalOperators = new GroupSymbol(NumericalOperatorsName, new ISymbol[] { addition, subtraction, multiplication, division, modulus });
140      #endregion
141
142      #region Adding Symbols
143      AddSymbol(tank);
144      AddSymbol(run);
145      AddSymbol(stat);
146      AddSymbol(block);
147      AddSymbol(shotPower);
148      AddSymbol(number);
149      AddSymbol(logicalVal);
150      AddSymbol(events);
151      AddSymbol(expressions);
152      AddSymbol(controlStatements);
153      AddSymbol(robocodeFunctions);
154      AddSymbol(relationalOperators);
155      AddSymbol(logicalOperators);
156      AddSymbol(numericalOperators);
157      AddSymbol(emptyEvent);
158      AddSymbol(doNothing);
159      #endregion
160
161      #region Grammar Definition
162      // StartSymbol
163      AddAllowedChildSymbol(StartSymbol, tank);
164
165      // Tank
166      AddAllowedChildSymbol(tank, run, 0);
167      AddAllowedChildSymbol(tank, onScannedRobot, 1);
168      AddAllowedChildSymbol(tank, onBulletHit, 2);
169      AddAllowedChildSymbol(tank, onBulletMissed, 3);
170      AddAllowedChildSymbol(tank, onHitByBullet, 4);
171      AddAllowedChildSymbol(tank, onHitRobot, 5);
172      AddAllowedChildSymbol(tank, onHitWall, 6);
173
174      // Run
175      AddAllowedChildSymbol(run, stat);
176
177      // Event
178      foreach (var s in events.Symbols)
179        AddAllowedChildSymbol(s, stat);
180
181      // Block
182      AddAllowedChildSymbol(block, stat);
183
184      // Stat
185      AddAllowedChildSymbol(stat, stat);
186      AddAllowedChildSymbol(stat, block);
187      foreach (var s in controlSymbols)
188        AddAllowedChildSymbol(stat, s);
189      foreach (var s in robocodeFunctions.Symbols)
190        AddAllowedChildSymbol(stat, s);
191      AddAllowedChildSymbol(stat, emptyEvent);
192      AddAllowedChildSymbol(stat, doNothing);
193
194      // IfStat
195      AddAllowedChildSymbol(ifStat, logicalExpr, 0);
196      AddAllowedChildSymbol(ifStat, stat, 1);
197      AddAllowedChildSymbol(ifStat, emptyEvent, 1);
198      AddAllowedChildSymbol(ifStat, doNothing, 1);
199      AddAllowedChildSymbol(ifStat, elseStat, 2);
200      AddAllowedChildSymbol(ifStat, emptyEvent, 2);
201      AddAllowedChildSymbol(ifStat, doNothing, 2);
202
203      // ElseStat
204      AddAllowedChildSymbol(elseStat, stat);
205      AddAllowedChildSymbol(elseStat, emptyEvent);
206      AddAllowedChildSymbol(elseStat, doNothing);
207
208      // WhileStat
209      AddAllowedChildSymbol(whileStat, logicalExpr, 0);
210      AddAllowedChildSymbol(whileStat, stat, 1);
211      AddAllowedChildSymbol(whileStat, emptyEvent, 1);
212      AddAllowedChildSymbol(whileStat, doNothing, 1);
213
214      // Numerical Expressions
215      foreach (var s in functionSymbols.Concat(new[] { number }))
216        AddAllowedChildSymbol(numericalExpr, s);
217      foreach (var s in numericalOperators.Symbols) {
218        AddAllowedChildSymbol(numericalExpr, s);
219        foreach (var ne in functionSymbols.Concat(new[] { number }))
220          AddAllowedChildSymbol(s, ne);
221        foreach (var no in numericalOperators.Symbols) {
222          AddAllowedChildSymbol(s, no);
223        }
224      }
225
226      // Logical Expressions
227      AddAllowedChildSymbol(logicalExpr, logicalVal);
228      foreach (var s in logicalOperators.Symbols) {
229        AddAllowedChildSymbol(logicalExpr, s);
230        AddAllowedChildSymbol(s, logicalVal);
231        foreach (var lo in logicalOperators.Symbols)
232          AddAllowedChildSymbol(s, lo);
233        foreach (var ro in relationalOperators.Symbols)
234          AddAllowedChildSymbol(s, ro);
235      }
236      foreach (var s in relationalOperators.Symbols)
237        AddAllowedChildSymbol(s, numericalExpr);
238
239      // Functions
240      foreach (var f in robocodeFunctions.Symbols) {
241        if (f is Fire) AddAllowedChildSymbol(f, shotPower);
242        else AddAllowedChildSymbol(f, numericalExpr);
243      }
244      #endregion
245    }
246  }
247}
Note: See TracBrowser for help on using the repository browser.