Changeset 13017
- Timestamp:
- 10/16/15 10:36:09 (9 years ago)
- Location:
- branches/Robocode.TrunkInt
- Files:
-
- 1 added
- 16 deleted
- 12 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode.Views/3.3/CodeNodeView.Designer.cs
r13013 r13017 112 112 // splitContainer.Panel1 113 113 // 114 this.splitContainer.Panel1.Controls.Add(this.prefixCode); 114 115 this.splitContainer.Panel1.Controls.Add(this.prefixLabel); 115 this.splitContainer.Panel1.Controls.Add(this.prefixCode);116 116 // 117 117 // splitContainer.Panel2 118 118 // 119 this.splitContainer.Panel2.Controls.Add(this.suffixCode); 119 120 this.splitContainer.Panel2.Controls.Add(this.suffixLabel); 120 this.splitContainer.Panel2.Controls.Add(this.suffixCode);121 121 this.splitContainer.Size = new System.Drawing.Size(582, 261); 122 122 this.splitContainer.SplitterDistance = 130; -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode.Views/3.3/CodeNodeView.cs
r13013 r13017 26 26 27 27 namespace HeuristicLab.Problems.GeneticProgramming.Views.Robocode { 28 [View("Code NodeView")]29 [Content(typeof(Code Node), IsDefaultView = true)]28 [View("CodeSymbol View")] 29 [Content(typeof(CodeSymbol), IsDefaultView = true)] 30 30 public partial class CodeNodeView : ItemView { 31 public new Code NodeContent {32 get { return (Code Node)base.Content; }31 public new CodeSymbol Content { 32 get { return (CodeSymbol)base.Content; } 33 33 set { base.Content = value; } 34 34 } -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/EnemyCollection.cs
r13013 r13017 51 51 52 52 public static EnemyCollection ReloadEnemies(string robocodeDir) { 53 EnemyCollection robotList = new EnemyCollection(); ;53 EnemyCollection robotList = new EnemyCollection(); 54 54 55 55 try { -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Grammar.cs
r13013 r13017 55 55 private void Initialize() { 56 56 #region Symbols 57 var block = new Block();58 var stat = new S tat();59 var ifThenElseStat = new IfThenElseStat();60 var whileStat = new WhileStat();57 var block = new SimpleSymbol("Block", "A group of statements", 1, byte.MaxValue); 58 var stat = new SimpleSymbol("Statement", "A statement.", 1, 1); 59 var ifThenElseStat = new SimpleSymbol("IfThenElseStat", "An if statement.", 2, 3); 60 var whileStat = new SimpleSymbol("WhileStat", "A while statement.", 2, 2); 61 61 whileStat.Enabled = false; 62 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 S ubtraction();78 var multiplication = new Multiplication();79 var division = new Division();80 var modulus = new Modulus();63 var boolExpr = new SimpleSymbol("BooleanExpression", "A Boolean expression.", 1, 1); 64 var numericalExpr = new SimpleSymbol("NumericalExpression", "A numerical expression.", 1, 1); 65 66 var equal = new SimpleSymbol("Equal", "Equal comparator.", 2, 2); 67 var lessThan = new SimpleSymbol("LessThan", "LessThan comparator.", 2, 2); 68 var lessThanOrEqual = new SimpleSymbol("LessThanOrEqual", "LessThanOrEqual comparator.", 2, 2); 69 var greaterThan = new SimpleSymbol("GreaterThan", "GreaterThan comparator.", 2, 2); 70 var greaterThanOrEqual = new SimpleSymbol("GreaterThanOrEqual", "GreaterThanOrEqual comparator.", 2, 2); 71 72 var conjunction = new SimpleSymbol("ConditionalAnd", "Conjunction comparator.", 2, byte.MaxValue); 73 var disjunction = new SimpleSymbol("ConditionalOr", "Disjunction comparator.", 2, byte.MaxValue); 74 var negation = new SimpleSymbol("Negation", "A negation.", 1, 1); 75 76 var addition = new SimpleSymbol("Addition", "Addition operator.", 2, byte.MaxValue); 77 var subtraction = new SimpleSymbol("Subtraction", "Subtraction operator.", 2, byte.MaxValue); 78 var multiplication = new SimpleSymbol("Multiplication", "Multiplication operator.", 2, byte.MaxValue); 79 var division = new SimpleSymbol("Division", "Division operator.", 2, byte.MaxValue); 80 var modulus = new SimpleSymbol("Modulus", "Modulus operator.", 2, byte.MaxValue); 81 81 82 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();83 var logicalVal = new BooleanValue(); 84 85 var ahead = new SimpleSymbol("Ahead", "Immediately moves your robot ahead (forward) by distance measured in pixels.", 1, 1); 86 var back = new SimpleSymbol("Back", "Immediately moves your robot backward by distance measured in pixels.", 1, 1); 87 var fire = new SimpleSymbol("Fire", "Immediately fires a bullet.", 1, 1); 88 88 var shotPower = new ShotPower(); 89 89 90 var getEnergy = new GetEnergy();91 var get GunHeading = new GetGunHeading();92 var get Heading = new GetHeading();93 var getRadarHeading = new GetRadarHeading();94 var getX = new GetX();95 var getY = new GetY();96 97 var turnLeft = new TurnLeft();98 var turnRight = new TurnRight();99 var turnGunLeft = new TurnGunLeft();100 var turnGunRight = new TurnGunRight();101 var turnRadarLeft = new TurnRadarLeft();102 var turnRadarRight = new TurnRadarRight();103 104 var onBulletHit = new OnBulletHit();105 var onBulletMissed = new OnBulletMissed();106 var onHitByBullet = new OnHitByBullet();107 var onHitRobot = new OnHitRobot();108 var onHitWall = new OnHitWall();109 var onScannedRobot = new OnScannedRobot();110 111 var run = new Run();112 var tank = new Tank();113 114 var doNothing = new DoNothing();115 var emptyEvent = new EmptyEvent();90 var getEnergy = new SimpleSymbol("GetEnergy", "Returns the robot's current energy.", 0, 0); 91 var getHeading = new SimpleSymbol("GetHeading", "Returns the direction that the robot's body is facing, in degrees.", 0, 0); 92 var getGunHeading = new SimpleSymbol("GetGunHeading", "Returns the direction that the robot's gun is facing, in degrees.", 0, 0); 93 var getRadarHeading = new SimpleSymbol("GetRadarHeading", "Returns the direction that the robot's radar is facing, in degrees.", 0, 0); 94 var getX = new SimpleSymbol("GetX", "Returns the X position of the robot. (0,0) is at the bottom left of the battlefield.", 0, 0); 95 var getY = new SimpleSymbol("GetY", "Returns the Y position of the robot. (0,0) is at the bottom left of the battlefield.", 0, 0); 96 97 var turnLeft = new SimpleSymbol("TurnLeft", "Immediately turns the robot's body to the left by degrees.", 1, 1); 98 var turnRight = new SimpleSymbol("TurnRight", "Immediately turns the robot's body to the right by degrees.", 1, 1); 99 var turnGunLeft = new SimpleSymbol("TurnGunLeft", "Immediately turns the robot's gun to the left by degrees.", 1, 1); 100 var turnGunRight = new SimpleSymbol("TurnGunRight", "Immediately turns the robot's gun to the right by degrees.", 1, 1); 101 var turnRadarLeft = new SimpleSymbol("TurnRadarLeft", "Immediately turns the robot's radar to the left by degrees.", 1, 1); 102 var turnRadarRight = new SimpleSymbol("TurnRadarRight", "Immediately turns the robot's radar to the right by degrees.", 1, 1); 103 104 var onBulletHit = new SimpleSymbol("OnBulletHit", "This method is called when one of your bullets hits another robot.", 2, 10); 105 var onBulletMissed = new SimpleSymbol("OnBulletMissed", "This method is called when one of your bullets misses, i.e. hits a wall.", 2, 10); 106 var onHitByBullet = new SimpleSymbol("OnHitByBullet", "This method is called when your robot is hit by a bullet.", 2, 10); 107 var onHitRobot = new SimpleSymbol("OnHitRobot", "This method is called when your robot collides with another robot.", 2, 10); 108 var onHitWall = new SimpleSymbol("OnHitWall", "This method is called when your robot collides with a wall.", 2, 10); 109 var onScannedRobot = new SimpleSymbol("OnScannedRobot", "This method is called when your robot sees another robot, i.e. when the robot's radar scan \"hits\" another robot.", 2, 10); 110 111 var run = new SimpleSymbol("Run", "The main method in every robot.", 1, 10); 112 var tank = new SimpleSymbol("Tank", "The root of a Robocode Tank program.", 2, 7); 113 114 var doNothing = new SimpleSymbol("DoNothing", "Do nothing this turn, meaning that the robot will skip its turn.", 0, 0); 115 var emptyEvent = new SimpleSymbol("EmptyEvent", "This is a placeholder for an empty event.", 0, 0); 116 116 #endregion 117 117 … … 127 127 var events = new GroupSymbol(EventsName, new ISymbol[] { run, onScannedRobot, onBulletHit, onBulletMissed, onHitByBullet, onHitRobot, onHitWall }); 128 128 var controlStatements = new GroupSymbol(ControlStatementsName, controlSymbols); 129 var expressions = new GroupSymbol(ExpressionsName, new ISymbol[] { logicalExpr, numericalExpr });129 var expressions = new GroupSymbol(ExpressionsName, new ISymbol[] { boolExpr, numericalExpr }); 130 130 var robocodeFunctions = new GroupSymbol(RobocodeFunctionsName, functionSymbols); 131 131 var robocodeActions = new GroupSymbol(RobocodeActionsName, actionSymbols); … … 183 183 184 184 // IfStat 185 AddAllowedChildSymbol(ifThenElseStat, logicalExpr, 0);185 AddAllowedChildSymbol(ifThenElseStat, boolExpr, 0); 186 186 AddAllowedChildSymbol(ifThenElseStat, stat, 1); 187 187 AddAllowedChildSymbol(ifThenElseStat, emptyEvent, 1); … … 192 192 193 193 // WhileStat 194 AddAllowedChildSymbol(whileStat, logicalExpr, 0);194 AddAllowedChildSymbol(whileStat, boolExpr, 0); 195 195 AddAllowedChildSymbol(whileStat, stat, 1); 196 196 AddAllowedChildSymbol(whileStat, emptyEvent, 1); … … 206 206 207 207 // Logical Expressions 208 AddAllowedChildSymbol( logicalExpr, logicalVal);209 AddAllowedChildSymbol( logicalExpr, logicalOperators);208 AddAllowedChildSymbol(boolExpr, logicalVal); 209 AddAllowedChildSymbol(boolExpr, logicalOperators); 210 210 AddAllowedChildSymbol(logicalOperators, logicalVal); 211 211 AddAllowedChildSymbol(logicalOperators, logicalOperators); … … 216 216 AddAllowedChildSymbol(robocodeFunctions, numericalExpr); 217 217 foreach (var a in robocodeActions.Symbols) { 218 if (a is Fire) AddAllowedChildSymbol(a, shotPower);218 if (a.Name == fire.Name) AddAllowedChildSymbol(a, shotPower); 219 219 else AddAllowedChildSymbol(a, numericalExpr); 220 220 } -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/HeuristicLab.Problems.Robocode-3.3.csproj
r13015 r13017 103 103 <Compile Include="Grammar.cs" /> 104 104 <Compile Include="Interpreter.cs" /> 105 <Compile Include=" RobocodeProblem.cs" />105 <Compile Include="Problem.cs" /> 106 106 <None Include="Properties\AssemblyInfo.cs.frame" /> 107 107 <Compile Include="Solution.cs" /> 108 <Compile Include="Symbols\Event Methods\Events\EmptyEvent.cs" /> 109 <Compile Include="Symbols\Logical Expressions\Logical Comparators\ILogicalComparator.cs" /> 110 <Compile Include="Symbols\Numerical Expressions\Numerical Operators\INumericalOperator.cs" /> 111 <Compile Include="Symbols\Numerical Methods\INumericalMethod.cs" /> 112 <Compile Include="Symbols\Stat.cs" /> 113 <Compile Include="Symbols\Void Methods\Ahead.cs" /> 114 <Compile Include="Symbols\Void Methods\Back.cs" /> 115 <Compile Include="Symbols\Block.cs" /> 116 <Compile Include="Symbols\Statements\IfThenElseStat.cs" /> 117 <Compile Include="Symbols\Statements\WhileStat.cs" /> 108 <Compile Include="Symbols\CodeNode.cs"> 109 <SubType>Code</SubType> 110 </Compile> 118 111 <Compile Include="Symbols\Logical Expressions\BooleanTreeNode.cs" /> 119 <Compile Include="Symbols\Logical Expressions\Conjunction.cs" /> 120 <Compile Include="Symbols\Logical Expressions\Disjunction.cs" /> 121 <Compile Include="Symbols\Logical Expressions\Logical Comparators\Equal.cs" /> 122 <Compile Include="Symbols\Logical Expressions\Logical Comparators\GreaterThan.cs" /> 123 <Compile Include="Symbols\Logical Expressions\Logical Comparators\GreaterThanOrEqual.cs" /> 124 <Compile Include="Symbols\Logical Expressions\Logical Comparators\LessThan.cs" /> 125 <Compile Include="Symbols\Logical Expressions\Logical Comparators\LessThanOrEqual.cs" /> 126 <Compile Include="Symbols\Logical Expressions\LogicalExpression.cs" /> 127 <Compile Include="Symbols\Logical Expressions\LogicalValue.cs" /> 128 <Compile Include="Symbols\Logical Expressions\Negation.cs" /> 112 <Compile Include="Symbols\Logical Expressions\BooleanValue.cs" /> 129 113 <Compile Include="Symbols\Numerical Expressions\NumberTreeNode.cs" /> 130 114 <Compile Include="Symbols\Numerical Expressions\Number.cs" /> 131 <Compile Include="Symbols\Numerical Expressions\Numerical Operators\Addition.cs" />132 <Compile Include="Symbols\Numerical Expressions\Numerical Operators\Division.cs" />133 <Compile Include="Symbols\Numerical Expressions\Numerical Operators\Modulus.cs" />134 <Compile Include="Symbols\Numerical Expressions\Numerical Operators\Multiplication.cs" />135 <Compile Include="Symbols\Numerical Expressions\Numerical Operators\Subtraction.cs" />136 <Compile Include="Symbols\Numerical Expressions\NumericalExpression.cs" />137 <Compile Include="Symbols\CodeNode.cs" />138 <Compile Include="Symbols\Tank.cs" />139 <Compile Include="Symbols\Void Methods\DoNothing.cs" />140 <Compile Include="Symbols\Event Methods\Events\OnBulletHit.cs" />141 <Compile Include="Symbols\Event Methods\Events\OnBulletMissed.cs" />142 <Compile Include="Symbols\Event Methods\Events\OnHitByBullet.cs" />143 <Compile Include="Symbols\Event Methods\Events\OnHitRobot.cs" />144 <Compile Include="Symbols\Event Methods\Events\OnHitWall.cs" />145 <Compile Include="Symbols\Event Methods\Events\OnScannedRobot.cs" />146 <Compile Include="Symbols\Void Methods\Fire.cs" />147 <Compile Include="Symbols\Numerical Methods\GetEnergy.cs" />148 <Compile Include="Symbols\Numerical Methods\GetGunHeading.cs" />149 <Compile Include="Symbols\Numerical Methods\GetHeading.cs" />150 115 <Compile Include="Plugin.cs" /> 151 116 <Compile Include="Properties\AssemblyInfo.cs" /> 152 <Compile Include="Symbols\Numerical Methods\GetRadarHeading.cs" />153 <Compile Include="Symbols\Numerical Methods\GetX.cs" />154 <Compile Include="Symbols\Numerical Methods\GetY.cs" />155 <Compile Include="Symbols\Program.cs" />156 <Compile Include="Symbols\Event Methods\Run.cs" />157 117 <Compile Include="Symbols\Numerical Expressions\ShotPower.cs" /> 158 118 <Compile Include="Symbols\Numerical Expressions\ShotPowerTreeNode.cs" /> 159 <Compile Include="Symbols\Void Methods\TurnGunLeft.cs" />160 <Compile Include="Symbols\Void Methods\TurnGunRight.cs" />161 <Compile Include="Symbols\Void Methods\TurnLeft.cs" />162 <Compile Include="Symbols\Void Methods\TurnRadarLeft.cs" />163 <Compile Include="Symbols\Void Methods\TurnRadarRight.cs" />164 <Compile Include="Symbols\Void Methods\TurnRight.cs" />165 119 </ItemGroup> 166 120 <ItemGroup> 167 121 <Content Include="BattleRunner.java" /> 122 </ItemGroup> 123 <ItemGroup> 124 <Folder Include="Symbols\Event Methods\Events\" /> 125 <Folder Include="Symbols\Logical Expressions\Logical Comparators\" /> 126 <Folder Include="Symbols\Numerical Expressions\Numerical Operators\" /> 127 <Folder Include="Symbols\Numerical Methods\" /> 128 <Folder Include="Symbols\Statements\" /> 129 <Folder Include="Symbols\Void Methods\" /> 168 130 </ItemGroup> 169 131 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Interpreter.cs
r13013 r13017 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using System.Diagnostics; 25 using System.Diagnostics.Contracts; 24 26 using System.Globalization; 25 27 using System.IO; 26 28 using System.Linq; 27 29 using System.Reflection; 30 using System.Runtime.Remoting.Messaging; 28 31 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 29 32 30 33 namespace HeuristicLab.Problems.GeneticProgramming.Robocode { 31 34 public static class Interpreter { 35 // TODO performance: it would probably be useful to implement the BattleRunner in such a way that we don't have to restart the java process each time, e.g. using console IO to load & run robots 32 36 public static double EvaluateTankProgram(ISymbolicExpressionTree tree, string path, EnemyCollection enemies, string robotName = null, bool showUI = false, int nrOfRounds = 3) { 33 37 if (robotName == null) … … 148 152 public static string InterpretProgramTree(ISymbolicExpressionTreeNode node, string robotName) { 149 153 var tankNode = node; 150 while ( !(tankNode.Symbol is Tank))154 while (tankNode.Symbol.Name != "Tank") 151 155 tankNode = tankNode.GetSubtree(0); 152 156 153 string result = ((CodeNode)tankNode.Symbol).Interpret(tankNode, tankNode.Subtrees);157 string result = Interpret(tankNode); 154 158 result = result.Replace("class output", "class " + robotName); 155 159 return result; 156 160 } 161 162 private static string Interpret(ISymbolicExpressionTreeNode node) { 163 switch (node.Symbol.Name) { 164 case "Block": return InterpretBlock(node); 165 case "Statement": return InterpretStat(node); 166 case "DoNothing": return string.Empty; 167 case "EmptyEvent": return string.Empty; 168 169 case "GetEnergy": return "getEnergy()"; 170 case "GetHeading": return "getHeading()"; 171 case "GetGunHeading": return "getGunHeading()"; 172 case "GetRadarHeading": return "getRadarHeading()"; 173 case "GetX": return "getX()"; 174 case "GetY": return "getY()"; 175 176 case "Addition": return InterpretBinaryOperator(" + ", node); 177 case "Subtraction": return InterpretBinaryOperator(" - ", node); 178 case "Multiplication": return InterpretBinaryOperator(" * ", node); 179 case "Division": return InterpretBinaryOperator(" / ", node); 180 case "Modulus": return InterpretBinaryOperator(" % ", node); 181 182 case "Equal": return InterpretComparison(" == ", node); 183 case "LessThan": return InterpretComparison(" < ", node); 184 case "LessThanOrEqual": return InterpretComparison(" <= ", node); 185 case "GreaterThan": return InterpretComparison(" >", node); 186 case "GreaterThanOrEqual": return InterpretComparison(" >= ", node); 187 case "ConditionalAnd": return InterpretBinaryOperator(" && ", node); 188 case "ConditionalOr": return InterpretBinaryOperator(" || ", node); 189 case "Negation": return InterpretFunc1("!", node); 190 191 case "IfThenElseStat": return InterpretIf(node); 192 case "WhileStat": return InterpretWhile(node); 193 case "BooleanExpression": return InterpretChild(node); 194 case "NumericalExpression": return InterpretChild(node); 195 case "Number": return InterpretNumber(node); 196 case "BooleanValue": return InterpretBoolValue(node); 197 198 199 case "Ahead": return InterpretFunc1("setAhead", node); 200 case "Back": return InterpretFunc1("setBack", node); 201 case "Fire": return InterpretFunc1("setFire", node); 202 case "TurnLeft": return InterpretFunc1("setTurnLeft", node); 203 case "TurnRight": return InterpretFunc1("setTurnRight", node); 204 case "TurnGunLeft": return InterpretFunc1("setTurnGunLeft", node); 205 case "TurnGunRight": return InterpretFunc1("setTurnGunRight", node); 206 case "TurnRadarLeft": return InterpretFunc1("setTurnRadarLeft", node); 207 case "TurnRadarRight": return InterpretFunc1("setTurnRadarRight", node); 208 case "ShotPower": return InterpetShotPower(node); 209 210 case "OnBulletHit": return InterpretOnBulletHit(node); 211 case "OnBulletMissed": return InterpretOnBulletMissed(node); 212 case "OnHitByBullet": return InterpretOnHitByBullet(node); 213 case "OnHitRobot": return InterpretOnHitRobot(node); 214 case "OnHitWall": return InterpretOnHitWall(node); 215 case "OnScannedRobot": return InterpretOnScannedRobot(node); 216 217 case "Run": return InterpretRun(node); 218 case "Tank": return InterpretTank(node); 219 case "CodeSymbol": return InterpretCodeSymbol(node); 220 221 default: throw new ArgumentException(string.Format("Found an unknown symbol {0} in a robocode solution", node.Symbol.Name)); 222 } 223 } 224 225 private static string InterpretCodeSymbol(ISymbolicExpressionTreeNode node) { 226 var sy = node.Symbol as CodeSymbol; 227 string code = string.Join(Environment.NewLine, node.Subtrees.Select(Interpret)); 228 return string.Format( 229 @"{0} 230 {1} 231 {2}", sy.Prefix, code, sy.Suffix); 232 } 233 234 private static string InterpretBoolValue(ISymbolicExpressionTreeNode node) { 235 var boolNode = node as BooleanTreeNode; 236 return string.Format(NumberFormatInfo.InvariantInfo, "{0}", boolNode.Value).ToLower(); 237 } 238 239 private static string InterpretNumber(ISymbolicExpressionTreeNode node) { 240 var numberNode = node as NumberTreeNode; 241 return string.Format(NumberFormatInfo.InvariantInfo, "{0}", numberNode.Value); 242 } 243 244 private static string InterpetShotPower(ISymbolicExpressionTreeNode node) { 245 var shotPowerNode = node as ShotPowerTreeNode; 246 return string.Format(NumberFormatInfo.InvariantInfo, "{0:E}", shotPowerNode.Value); 247 } 248 249 250 internal static string InterpretBlock(ISymbolicExpressionTreeNode node) { 251 string result = string.Join(Environment.NewLine, node.Subtrees.Select(Interpret)); 252 return string.Format("{{ {0} }}", result + Environment.NewLine); 253 } 254 255 internal static string InterpretStat(ISymbolicExpressionTreeNode node) { 256 // must only have one sub-tree 257 Contract.Assert(node.SubtreeCount == 1); 258 return Interpret(node.GetSubtree(0)) + " ;" + Environment.NewLine; 259 } 260 261 internal static string InterpretIf(ISymbolicExpressionTreeNode node) { 262 ISymbolicExpressionTreeNode condition = null, truePart = null, falsePart = null; 263 string[] parts = new string[3]; 264 265 if (node.SubtreeCount < 2 || node.SubtreeCount > 3) 266 throw new Exception("Unexpected number of children. Expected 2 or 3 children."); 267 268 condition = node.GetSubtree(0); 269 truePart = node.GetSubtree(1); 270 if (node.SubtreeCount == 3) 271 falsePart = node.GetSubtree(2); 272 273 parts[0] = Interpret(condition); 274 parts[1] = Interpret(truePart); 275 if (falsePart != null) parts[2] = Interpret(falsePart); 276 277 return string.Format("if ({0}) {{ {1} }} else {{ {2} }}", Interpret(condition), Interpret(truePart), 278 falsePart == null ? string.Empty : Interpret(falsePart)); 279 } 280 281 internal static string InterpretWhile(ISymbolicExpressionTreeNode node) { 282 var cond = Interpret(node.GetSubtree(0)); 283 var body = Interpret(node.GetSubtree(1)); 284 return string.Format("while ({0}) {{ {2} {1} {2} }} {2}", cond, body, Environment.NewLine); 285 } 286 287 public static string InterpretBinaryOperator(string opSy, ISymbolicExpressionTreeNode node) { 288 if (node.SubtreeCount < 2) 289 throw new ArgumentException(string.Format("Expected at least two children in {0}.", node.Symbol), "node"); 290 291 string result = string.Join(opSy, node.Subtrees.Select(Interpret)); 292 return "(" + result + ")"; 293 } 294 295 public static string InterpretChild(ISymbolicExpressionTreeNode node) { 296 if (node.SubtreeCount != 1) 297 throw new ArgumentException(string.Format("Expected exactly one child in {0}.", node.Symbol), "node"); 298 299 return Interpret(node.GetSubtree(0)); 300 } 301 302 public static string InterpretComparison(string compSy, ISymbolicExpressionTreeNode node) { 303 ISymbolicExpressionTreeNode lhs = null, rhs = null; 304 if (node.SubtreeCount != 2) 305 throw new ArgumentException(string.Format("Expected exactly two children in {0}.", node.Symbol), "node"); 306 307 lhs = node.GetSubtree(0); 308 rhs = node.GetSubtree(1); 309 310 return Interpret(lhs) + " == " + Interpret(rhs); 311 } 312 313 public static string InterpretFunc1(string functionId, ISymbolicExpressionTreeNode node) { 314 if (node.SubtreeCount != 1) 315 throw new ArgumentException(string.Format("Expected 1 child in {0}.", node.Symbol.Name), "node"); 316 317 return string.Format("{0}({1})", functionId, Interpret(node.GetSubtree(0))); 318 } 319 320 public static string InterpretOnScannedRobot(ISymbolicExpressionTreeNode node) { 321 string code = string.Join(Environment.NewLine, node.Subtrees.Select(Interpret)); 322 return string.Format( 323 @"public void onScannedRobot(ScannedRobotEvent e) {{ 324 double absoluteBearing = getHeading() + e.getBearing(); 325 double bearingFromGun = normalRelativeAngleDegrees(absoluteBearing - getGunHeading()); 326 setTurnGunRight(bearingFromGun); 327 {0} 328 execute(); 329 }}", code); 330 } 331 332 333 public static string InterpretOnHitWall(ISymbolicExpressionTreeNode node) { 334 string code = string.Join(Environment.NewLine, node.Subtrees.Select(Interpret)); 335 return string.Format( 336 @"public void onHitWall(HitWallEvent e) {{ 337 {0} 338 execute(); 339 }}", code); 340 } 341 342 public static string InterpretOnHitRobot(ISymbolicExpressionTreeNode node) { 343 string code = string.Join(Environment.NewLine, node.Subtrees.Select(Interpret)); 344 return string.Format( 345 @"public void onHitRobot(HitRobotEvent e) {{ 346 {0} 347 execute(); 348 }}", code); 349 } 350 351 public static string InterpretOnHitByBullet(ISymbolicExpressionTreeNode node) { 352 string code = string.Join(Environment.NewLine, node.Subtrees.Select(Interpret)); 353 return string.Format( 354 @"public void onHitByBullet(HitByBulletEvent e) {{ 355 {0} 356 execute(); 357 }}", code); 358 } 359 360 public static string InterpretOnBulletMissed(ISymbolicExpressionTreeNode node) { 361 string code = string.Join(Environment.NewLine, node.Subtrees.Select(Interpret)); 362 return string.Format( 363 @"public void onBulletMissed(BulletMissedEvent e) {{ 364 {0} 365 execute(); 366 }}", code); 367 } 368 369 public static string InterpretOnBulletHit(ISymbolicExpressionTreeNode node) { 370 var Prefix = "public void onBulletHit(BulletHitEvent e) {"; 371 var Suffix = 372 @"execute(); 373 }"; 374 string code = string.Join(Environment.NewLine, node.Subtrees.Select(Interpret)); 375 return Prefix + code + Environment.NewLine + Suffix; 376 } 377 378 public static string InterpretRun(ISymbolicExpressionTreeNode node) { 379 string code = string.Join(Environment.NewLine, node.Subtrees.Select(Interpret)); 380 return string.Format( 381 @"public void run() {{ 382 setAdjustGunForRobotTurn(true); 383 turnRadarRightRadians(Double.POSITIVE_INFINITY); 384 {0} 385 execute(); 386 }}", code); 387 } 388 389 public static string InterpretTank(ISymbolicExpressionTreeNode node) { 390 string code = string.Join(Environment.NewLine, node.Subtrees.Select(Interpret)); 391 return string.Format( 392 @"package Evaluation; 393 import robocode.*; 394 import robocode.Robot; 395 import robocode.util.*; 396 import static robocode.util.Utils.normalRelativeAngleDegrees; 397 import java.awt.*; 398 399 public class output extends AdvancedRobot {{ 400 {0} 401 }}", code); 402 } 157 403 } 158 404 } -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Problem.cs
r13016 r13017 32 32 [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 360)] 33 33 [Item("Robocode Problem", "Evolution of a robocode program in java using genetic programming.")] 34 public class RobocodeProblem : SymbolicExpressionTreeProblem {34 public class Problem : SymbolicExpressionTreeProblem { 35 35 #region Parameter Names 36 private const string MaxTankProgramLengthParameterName = "MaxProgramLength";37 private const string MaxTankProgramDepthParameterName = "MaxProgramDepth";38 36 private const string RobocodePathParamaterName = "RobocodePath"; 39 37 private const string NrOfRoundsParameterName = "NrOfRounds"; … … 42 40 43 41 #region Parameters 44 //public IFixedValueParameter<IntValue> MaxTankProgramLengthParameter {45 // get { return (IFixedValueParameter<IntValue>)Parameters[MaxTankProgramLengthParameterName]; }46 //}47 //public IFixedValueParameter<IntValue> MaxTankProgramDepthParameter {48 // get { return (IFixedValueParameter<IntValue>)Parameters[MaxTankProgramDepthParameterName]; }49 //}50 42 public IFixedValueParameter<DirectoryValue> RobocodePathParameter { 51 43 get { return (IFixedValueParameter<DirectoryValue>)Parameters[RobocodePathParamaterName]; } … … 57 49 get { return (IValueParameter<EnemyCollection>)Parameters[EnemiesParameterName]; } 58 50 } 59 60 //public int MaxTankProgramDepth {61 // get { return MaxTankProgramDepthParameter.Value.Value; }62 // set { MaxTankProgramDepthParameter.Value.Value = value; }63 //}64 //65 //public int MaxTankProgramLength {66 // get { return MaxTankProgramLengthParameter.Value.Value; }67 // set { MaxTankProgramLengthParameter.Value.Value = value; }68 //}69 51 70 52 public string RobocodePath { … … 85 67 86 68 [StorableConstructor] 87 protected RobocodeProblem(bool deserializing) : base(deserializing) { }88 protected RobocodeProblem(RobocodeProblem original, Cloner cloner)69 protected Problem(bool deserializing) : base(deserializing) { } 70 protected Problem(Problem original, Cloner cloner) 89 71 : base(original, cloner) { 90 72 RegisterEventHandlers(); 91 73 } 92 74 93 public RobocodeProblem()75 public Problem() 94 76 : base() { 95 DirectoryValue robocodeDir = new DirectoryValue { Value = @" C:\robocode" };77 DirectoryValue robocodeDir = new DirectoryValue { Value = @"robocode" }; 96 78 97 79 var robotList = EnemyCollection.ReloadEnemies(robocodeDir.Value); … … 111 93 112 94 public override IDeepCloneable Clone(Cloner cloner) { 113 return new RobocodeProblem(this, cloner);95 return new Problem(this, cloner); 114 96 } 115 97 -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/CodeNode.cs
r13013 r13017 27 27 namespace HeuristicLab.Problems.GeneticProgramming.Robocode { 28 28 [StorableClass] 29 public abstract class CodeNode : Symbol { 30 public abstract string Prefix { get; set; } 31 public abstract string Suffix { get; set; } 29 // a symbol that can represent any user-defined fragment of code 30 public sealed class CodeSymbol : Symbol { 31 public override int MinimumArity { get { return 1; } } 32 public override int MaximumArity { get { return 10; } } 33 34 public string Prefix { get; set; } 35 public string Suffix { get; set; } 36 37 public override bool CanChangeName { get { return false; } } // cannot change, otherwise we cannot detect these symbols in the interpreter 38 public override bool CanChangeDescription { get { return false; } } 32 39 33 40 [StorableConstructor] 34 pr otected CodeNode(bool deserializing) : base(deserializing) { }35 pr otected CodeNode(CodeNodeoriginal, Cloner cloner)41 private CodeSymbol(bool deserializing) : base(deserializing) { } 42 private CodeSymbol(CodeSymbol original, Cloner cloner) 36 43 : base(original, cloner) { 37 44 Prefix = original.Prefix; 38 45 Suffix = original.Suffix; 39 46 } 40 p rotected CodeNode(string name, string description)41 : base( name, description) {47 public CodeSymbol() 48 : base("CodeSymbol", "The CodeSymbol symbol can represent any user-defined fragment of code.") { 42 49 Prefix = string.Empty; 43 50 Suffix = string.Empty; 44 51 } 45 52 46 public abstract string Interpret(ISymbolicExpressionTreeNode node, IEnumerable<ISymbolicExpressionTreeNode> children); 53 public override IDeepCloneable Clone(Cloner cloner) { 54 return new CodeSymbol(this, cloner); 55 } 47 56 } 48 57 } -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Logical Expressions/BooleanTreeNode.cs
r13013 r13017 42 42 } 43 43 44 public BooleanTreeNode( ) : base(new LogicalValue()) { }44 public BooleanTreeNode(BooleanValue boolValueSy) : base(boolValueSy) { } 45 45 46 46 public override IDeepCloneable Clone(Cloner cloner) { -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Numerical Expressions/Number.cs
r13013 r13017 28 28 namespace HeuristicLab.Problems.GeneticProgramming.Robocode { 29 29 [StorableClass] 30 public class Number : CodeNode{30 public class Number : Symbol { 31 31 public override int MinimumArity { get { return 0; } } 32 32 public override int MaximumArity { get { return 0; } } 33 34 [Storable]35 public override string Prefix { get; set; }36 37 [Storable]38 public override string Suffix { get; set; }39 33 40 34 [StorableConstructor] … … 45 39 46 40 public override ISymbolicExpressionTreeNode CreateTreeNode() { 47 return new NumberTreeNode( );41 return new NumberTreeNode(this); 48 42 } 49 43 … … 52 46 } 53 47 54 public overridestring Interpret(ISymbolicExpressionTreeNode node, IEnumerable<ISymbolicExpressionTreeNode> children) {48 public string Interpret(ISymbolicExpressionTreeNode node, IEnumerable<ISymbolicExpressionTreeNode> children) { 55 49 return ((NumberTreeNode)node).Value.ToString(CultureInfo.InvariantCulture); 56 50 } -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Numerical Expressions/NumberTreeNode.cs
r13013 r13017 42 42 } 43 43 44 public NumberTreeNode( ) : base(new Number()) { }44 public NumberTreeNode(Number numberSy) : base(numberSy) { } 45 45 46 46 public override IDeepCloneable Clone(Cloner cloner) { -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Numerical Expressions/ShotPower.cs
r13013 r13017 28 28 namespace HeuristicLab.Problems.GeneticProgramming.Robocode { 29 29 [StorableClass] 30 public class ShotPower : CodeNode{30 public class ShotPower : Symbol { 31 31 public override int MinimumArity { get { return 0; } } 32 32 public override int MaximumArity { get { return 0; } } 33 34 [Storable]35 public override string Prefix { get; set; }36 37 [Storable]38 public override string Suffix { get; set; }39 33 40 34 [StorableConstructor] … … 45 39 46 40 public override ISymbolicExpressionTreeNode CreateTreeNode() { 47 return new ShotPowerTreeNode( );41 return new ShotPowerTreeNode(this); 48 42 } 49 43 … … 52 46 } 53 47 54 public overridestring Interpret(ISymbolicExpressionTreeNode node, IEnumerable<ISymbolicExpressionTreeNode> children) {48 public string Interpret(ISymbolicExpressionTreeNode node, IEnumerable<ISymbolicExpressionTreeNode> children) { 55 49 return ((ShotPowerTreeNode)node).Value.ToString(CultureInfo.InvariantCulture); 56 50 } -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Symbols/Numerical Expressions/ShotPowerTreeNode.cs
r13013 r13017 43 43 } 44 44 45 public ShotPowerTreeNode( ) : base(new ShotPower()) { }45 public ShotPowerTreeNode(ShotPower powerSy) : base(powerSy) { } 46 46 47 47 public override IDeepCloneable Clone(Cloner cloner) {
Note: See TracChangeset
for help on using the changeset viewer.