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