Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10355 was 9642, checked in by melkaref, 12 years ago

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

File size: 8.5 KB
RevLine 
[9565]1using System.Collections.Generic;
2using HeuristicLab.Common;
3using HeuristicLab.Core;
4using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
5using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
6
[9631]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    }
[9565]16
[9631]17    public Grammar()
18      : base("Robocode Grammar", "The grammar for the Robocode GP problem.") {
19      Initialize();
20    }
[9565]21
[9631]22    public override IDeepCloneable Clone(Cloner cloner) {
23      return new Grammar(this, cloner);
24    }
[9565]25
[9631]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();
[9565]31
[9631]32      var ifStmt = new IfStatement();
33      var elseStmt = new ElseStatement();
34      var whileLoop = new WhileLoop();
[9565]35
[9631]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();
[9565]44
[9631]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();
[9565]57
[9631]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();
[9565]64
[9631]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();
[9565]71
[9631]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>()
[9565]99            {
100                number, numOp, getEnergy, getGunHeading, getHeading, getRadarHeading, getX, getY
101            };
102
[9631]103      var numericalOperators = new List<ISymbol>()
[9565]104            {
105                add, sub, mult, div, mod
106            };
107
[9631]108      var logicalExpressions = new List<ISymbol>()
[9565]109            {
110                logicVal, logicComp, numComp, not
111            };
112
[9631]113      var logicalComparators = new List<ISymbol>()
[9565]114            {
115                and, or
116            };
117
[9631]118      var numericalComparators = new List<ISymbol>()
[9565]119            {
120                lt, gt, lteq, gteq, eq
121            };
122
[9631]123      var terminalSymbols = new List<ISymbol>()
[9565]124            {
125                doNothing, //getEnergy, getGunHeading, getHeading, getRadarHeading, getX, getY
126            };
127
[9631]128      var functionSymbols = new List<ISymbol>()
[9565]129            {
130                ahead, back, fire,
131                //setAdjustGunForRobotTurn, setAdjustRadarForGunTurn, setAdjustRadarForRobotTurn,
132                turnGunLeft, turnGunRight, turnLeft, turnRadarLeft, turnRadarRight, turnRight
133            };
134
[9631]135      var constantSymbols = new List<ISymbol>()
[9565]136            {
137                shotPower, /*independent,*/ constant
138            };
139
[9631]140      var eventSymbols = new List<ISymbol>()
[9565]141            {
[9609]142                run, onBulletHit, onBulletMissed, onHitByBullet,
143                onHitRobot, onHitWall, onScannedRobot, block, emptyEvent
[9565]144            };
[9631]145      #endregion
[9565]146
[9631]147      #region Adding Symbols
148      // add all symbols to the grammar
[9565]149
[9631]150      AddSymbol(ifStmt);
151      AddSymbol(elseStmt);
152      AddSymbol(whileLoop);
[9565]153
[9631]154      AddSymbol(numExpr);
155      foreach (var s in numericalExpressions)
156        AddSymbol(s);
157      foreach (var s in numericalOperators)
158        AddSymbol(s);
[9565]159
[9631]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);
[9565]167
[9631]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
[9565]178
[9631]179      #region Defining Grammar
180      // define grammar rules
[9565]181
[9631]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);
[9565]190
[9631]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);
[9565]199
[9631]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);
[9565]215
216
[9631]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      }
[9565]225
[9631]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);
[9565]236
[9631]237      }
[9609]238
[9631]239      // Add all Event Methods to Tank. Run and OnScannedEvent are not optional,
240      // so EmptyEvent cannot take their places
[9565]241
[9631]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);
[9642]249      for (int i = 2; i < tank.MaximumArity; i++)
[9631]250        AddAllowedChildSymbol(tank, emptyEvent, i);
251
252      AddAllowedChildSymbol(StartSymbol, tank, 0);
253      #endregion
[9565]254    }
[9631]255  }
[9565]256}
Note: See TracBrowser for help on using the repository browser.