Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Robocode/HeuristicLab.Problems.Robocode/Grammar.cs @ 10160

Last change on this file since 10160 was 9642, checked in by melkaref, 11 years ago

#2069 - Changed back the Grammar back to require OnScannedRobot event

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