Changeset 10014
- Timestamp:
- 10/01/13 16:39:35 (11 years ago)
- Location:
- branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3
- Files:
-
- 17 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Grammar.cs
r10011 r10014 20 20 #endregion 21 21 22 using System.Linq; 22 23 using HeuristicLab.Common; 23 24 using HeuristicLab.Core; … … 29 30 [Item("Robocode Grammar", "The grammar for the Robocode GP problem.")] 30 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 31 40 [StorableConstructor] 32 41 protected Grammar(bool deserializing) : base(deserializing) { } … … 113 122 114 123 #region Symbol Collections 115 var eventSymbols = new ISymbol[] { onScannedRobot, onBulletHit, onBulletMissed, onHitByBullet, onHitRobot, onHitWall };116 124 var controlSymbols = new ISymbol[] { ifStat, whileStat }; 117 var functionSymbols = new ISymbol[] {125 var actionSymbols = new ISymbol[] { 118 126 ahead, back, fire, turnGunLeft, turnGunRight, turnLeft, turnRadarLeft, turnRadarRight, turnRight 119 127 //setAdjustGunForRobotTurn, setAdjustRadarForGunTurn, setAdjustRadarForRobotTurn, 120 128 }; 121 var relationalOperators = new ISymbol[] { equal, lessThan, lessThanOrEqual, greaterThan, greaterThanOrEqual }; 122 var logicalOperators = new ISymbol[] { conjunction, disjunction, negation }; 123 var numericalExpressions = new ISymbol[] { number, getEnergy, getGunHeading, getHeading, getRadarHeading, getX, getY }; 124 var numericalOperators = new ISymbol[] { addition, subtraction, multiplication, division, modulus }; 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 }); 125 140 #endregion 126 141 127 142 #region Adding Symbols 128 foreach (var s in eventSymbols) 129 AddSymbol(s); 130 foreach (var s in controlSymbols) 131 AddSymbol(s); 132 foreach (var s in functionSymbols) 133 AddSymbol(s); 134 foreach (var s in relationalOperators) 135 AddSymbol(s); 136 foreach (var s in logicalOperators) 137 AddSymbol(s); 138 foreach (var s in numericalExpressions) 139 AddSymbol(s); 140 foreach (var s in numericalOperators) 141 AddSymbol(s); 142 143 AddSymbol(tank); 144 AddSymbol(run); 145 AddSymbol(stat); 143 146 AddSymbol(block); 144 AddSymbol(stat);145 AddSymbol(elseStat);146 147 AddSymbol(shotPower); 148 AddSymbol(number); 147 149 AddSymbol(logicalVal); 148 AddSymbol(logicalExpr); 149 AddSymbol(numericalExpr); 150 AddSymbol(run); 151 AddSymbol(tank); 150 AddSymbol(events); 151 AddSymbol(expressions); 152 AddSymbol(controlStatements); 153 AddSymbol(robocodeFunctions); 154 AddSymbol(relationalOperators); 155 AddSymbol(logicalOperators); 156 AddSymbol(numericalOperators); 152 157 AddSymbol(emptyEvent); 153 158 AddSymbol(doNothing); … … 171 176 172 177 // Event 173 foreach (var s in event Symbols)178 foreach (var s in events.Symbols) 174 179 AddAllowedChildSymbol(s, stat); 175 180 … … 182 187 foreach (var s in controlSymbols) 183 188 AddAllowedChildSymbol(stat, s); 184 foreach (var s in functionSymbols)189 foreach (var s in robocodeFunctions.Symbols) 185 190 AddAllowedChildSymbol(stat, s); 186 191 AddAllowedChildSymbol(stat, emptyEvent); … … 208 213 209 214 // Numerical Expressions 210 foreach (var s in numericalExpressions)215 foreach (var s in functionSymbols.Concat(new[] { number })) 211 216 AddAllowedChildSymbol(numericalExpr, s); 212 foreach (var s in numericalOperators ) {217 foreach (var s in numericalOperators.Symbols) { 213 218 AddAllowedChildSymbol(numericalExpr, s); 214 foreach (var ne in numericalExpressions)219 foreach (var ne in functionSymbols.Concat(new[] { number })) 215 220 AddAllowedChildSymbol(s, ne); 216 foreach (var no in numericalOperators ) {221 foreach (var no in numericalOperators.Symbols) { 217 222 AddAllowedChildSymbol(s, no); 218 223 } … … 221 226 // Logical Expressions 222 227 AddAllowedChildSymbol(logicalExpr, logicalVal); 223 foreach (var s in logicalOperators ) {228 foreach (var s in logicalOperators.Symbols) { 224 229 AddAllowedChildSymbol(logicalExpr, s); 225 230 AddAllowedChildSymbol(s, logicalVal); 226 foreach (var lo in logicalOperators )231 foreach (var lo in logicalOperators.Symbols) 227 232 AddAllowedChildSymbol(s, lo); 228 foreach (var ro in relationalOperators )233 foreach (var ro in relationalOperators.Symbols) 229 234 AddAllowedChildSymbol(s, ro); 230 235 } 231 foreach (var s in relationalOperators )236 foreach (var s in relationalOperators.Symbols) 232 237 AddAllowedChildSymbol(s, numericalExpr); 233 238 234 239 // Functions 235 foreach (var f in functionSymbols) { 236 if (f is Fire) 237 AddAllowedChildSymbol(f, shotPower); 238 else 239 AddAllowedChildSymbol(f, numericalExpr); 240 foreach (var f in robocodeFunctions.Symbols) { 241 if (f is Fire) AddAllowedChildSymbol(f, shotPower); 242 else AddAllowedChildSymbol(f, numericalExpr); 240 243 } 241 244 #endregion -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/RobocodeProblem.cs
r10011 r10014 76 76 77 77 public RobocodeProblem() 78 : base(new RobocodeEvaluator(), new RampedHalfAndHalfTreeCreator()) {78 : base(new RobocodeEvaluator(), new ProbabilisticTreeCreator()) { 79 79 DirectoryValue robocodeDir = new DirectoryValue(); 80 80 robocodeDir.Value = @"C:\robocode"; -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Event Methods/Events/EmptyEvent.cs
r10011 r10014 48 48 49 49 public override string Interpret(ISymbolicExpressionTreeNode node, IEnumerable<ISymbolicExpressionTreeNode> children) { 50 return ";";50 return string.Empty; 51 51 } 52 52 } -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Stat.cs
r10011 r10014 43 43 protected Stat(Stat original, Cloner cloner) : base(original, cloner) { } 44 44 45 public Stat() : base("Statement", "A statement.") { } 45 public Stat() 46 : base("Statement", "A statement.") { 47 Suffix = ";"; 48 } 46 49 47 50 public override IDeepCloneable Clone(Cloner cloner) { … … 50 53 51 54 public override string Interpret(ISymbolicExpressionTreeNode node, IEnumerable<ISymbolicExpressionTreeNode> children) { 52 return children.Aggregate(string.Empty, (current, c) => current + (Environment.NewLine + ((CodeNode)c.Symbol).Interpret(c, c.Subtrees))) ;55 return children.Aggregate(string.Empty, (current, c) => current + (Environment.NewLine + ((CodeNode)c.Symbol).Interpret(c, c.Subtrees))) + Suffix; 53 56 } 54 57 } -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Statements/ElseStat.cs
r10011 r10014 44 44 45 45 public ElseStat() 46 : base("ElseStat ement", "An ElseStatement.") {47 Prefix = " else";48 Suffix = " ";46 : base("ElseStat", "An else branch.") { 47 Prefix = " else {"; 48 Suffix = "}"; 49 49 } 50 50 … … 55 55 public override string Interpret(ISymbolicExpressionTreeNode node, IEnumerable<ISymbolicExpressionTreeNode> children) { 56 56 string result = children.Aggregate(string.Empty, (current, c) => current + (Environment.NewLine + ((CodeNode)c.Symbol).Interpret(c, c.Subtrees))); 57 return string.IsNullOrEmpty(result) 58 ? string.Empty 59 : Prefix + Environment.NewLine + result + Environment.NewLine + Suffix; 57 return string.IsNullOrEmpty(result) ? string.Empty : Prefix + Environment.NewLine + result + Environment.NewLine + Suffix; 60 58 } 61 59 } -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Statements/IfStat.cs
r10011 r10014 43 43 44 44 public IfStat() 45 : base("IfStat ement", "An if statement.") {45 : base("IfStat", "An if statement.") { 46 46 Prefix = "if ("; 47 Suffix = ") ";47 Suffix = ") {"; 48 48 } 49 49 … … 63 63 else throw new ArgumentException("Unexpected children.", "children"); 64 64 } 65 return Prefix + condition + Suffix + Environment.NewLine + ifTrue + Environment.NewLine + ifElse + Environment.NewLine; 65 string result = Prefix + condition + Suffix + Environment.NewLine + ifTrue + Environment.NewLine + "}"; 66 result += string.IsNullOrEmpty(ifElse) ? Environment.NewLine : ifElse; 67 return result; 66 68 } 67 69 } -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Statements/WhileStat.cs
r10011 r10014 45 45 : base("WhileStat", "A while statement.") { 46 46 Prefix = "while ("; 47 Suffix = ") ";47 Suffix = ") {"; 48 48 } 49 49 … … 61 61 else throw new ArgumentException("Unexpected children.", "children"); 62 62 } 63 return Prefix + condition + Suffix + Environment.NewLine + ifTrue + Environment.NewLine ;63 return Prefix + condition + Suffix + Environment.NewLine + ifTrue + Environment.NewLine + "}" + Environment.NewLine; 64 64 } 65 65 } -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Void Methods/Ahead.cs
r10011 r10014 44 44 : base("Ahead", "Immediately moves your robot ahead (forward) by distance measured in pixels.") { 45 45 Prefix = "setAhead("; 46 Suffix = ") ;";46 Suffix = ")"; 47 47 } 48 48 -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Void Methods/Back.cs
r10011 r10014 44 44 : base("Back", "Immediately moves your robot backward by distance measured in pixels.") { 45 45 Prefix = "setBack("; 46 Suffix = ") ;";46 Suffix = ")"; 47 47 } 48 48 -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Void Methods/DoNothing.cs
r10011 r10014 48 48 49 49 public override string Interpret(ISymbolicExpressionTreeNode node, IEnumerable<ISymbolicExpressionTreeNode> children) { 50 return ";";50 return string.Empty; 51 51 } 52 52 } -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Void Methods/Fire.cs
r10011 r10014 44 44 : base("Fire", "Immediately fires a bullet.") { 45 45 Prefix = "setFire("; 46 Suffix = ") ;";46 Suffix = ")"; 47 47 } 48 48 -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Void Methods/TurnGunLeft.cs
r10011 r10014 44 44 : base("TurnGunLeft", "Immediately turns the robot's gun to the left by degrees.") { 45 45 Prefix = "setTurnGunLeft("; 46 Suffix = ") ;";46 Suffix = ")"; 47 47 } 48 48 -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Void Methods/TurnGunRight.cs
r10011 r10014 44 44 : base("TurnGunRight", "Immediately turns the robot's gun to the right by degrees.") { 45 45 Prefix = "setTurnGunRight("; 46 Suffix = ") ;";46 Suffix = ")"; 47 47 } 48 48 -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Void Methods/TurnLeft.cs
r10011 r10014 44 44 : base("TurnLeft", "Immediately turns the robot's body to the left by degrees.") { 45 45 Prefix = "setTurnLeft("; 46 Suffix = ") ;";46 Suffix = ")"; 47 47 } 48 48 -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Void Methods/TurnRadarLeft.cs
r10011 r10014 44 44 : base("TurnRadarLeft", "Immediately turns the robot's radar to the left by degrees.") { 45 45 Prefix = "setTurnRadarLeft("; 46 Suffix = ") ;";46 Suffix = ")"; 47 47 } 48 48 -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Void Methods/TurnRadarRight.cs
r10011 r10014 44 44 : base("TurnRadarRight", "Immediately turns the robot's radar to the right by degrees.") { 45 45 Prefix = "setTurnRadarRight("; 46 Suffix = ") ;";46 Suffix = ")"; 47 47 } 48 48 -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Void Methods/TurnRight.cs
r10011 r10014 44 44 : base("TurnRight", "Immediately turns the robot's body to the right by degrees.") { 45 45 Prefix = "setTurnRight("; 46 Suffix = ") ;";46 Suffix = ")"; 47 47 } 48 48
Note: See TracChangeset
for help on using the changeset viewer.