Changeset 9880
- Timestamp:
- 08/13/13 12:11:18 (11 years ago)
- Location:
- branches/Robocode.TrunkInt
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode.Views/3.3/SolutionCodeView.cs
r9790 r9880 42 42 : base() { 43 43 InitializeComponent(); 44 this.Path = " F:/robocode/";44 this.Path = "c:/robocode/"; 45 45 this.programCode.Text = ""; 46 46 } -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/BestSolutionAnalyzer.cs
r9790 r9880 20 20 #endregion 21 21 22 23 22 using HeuristicLab.Common; 24 23 using HeuristicLab.Core; … … 32 31 namespace HeuristicLab.Problems.Robocode { 33 32 [StorableClass] 34 [Item("Best Tank program Analyzer",33 [Item("Best Tank Program Analyzer", 35 34 "Analyzer that stores the best tank program.")] 36 public class BestSolutionAnalyzer : SingleSuccessorOperator, 37 ISymbolicExpressionTreeAnalyzer { 38 #region parameter names 35 public class BestSolutionAnalyzer : SingleSuccessorOperator, ISymbolicExpressionTreeAnalyzer { 39 36 private const string QualityParameterName = "Quality"; 40 private const string SymbolicExpressionTreeParameterName = 41 "TankProgram"; 42 private const string MovesParameterName = "Moves"; 43 private const string ShotsParameterName = "Shots"; 37 private const string SymbolicExpressionTreeParameterName = "TankProgram"; 44 38 private const string BestSolutionParameterName = "Best solution"; 45 39 private const string ResultsParameterName = "Results"; 46 40 private const string RobocodePathParamaterName = "Path"; 47 private const string CoevolutionParameterName = "Coevolution";48 #endregion49 41 42 public bool EnabledByDefault { 43 get { return true; } 44 } 50 45 51 #region parameters46 #region Parameters 52 47 public IScopeTreeLookupParameter<DoubleValue> QualityParameter { 53 get { 54 return (IScopeTreeLookupParameter<DoubleValue>) 55 Parameters[QualityParameterName]; 56 } 48 get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters[QualityParameterName]; } 57 49 } 58 50 public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter { 59 get { 60 return (IScopeTreeLookupParameter<ISymbolicExpressionTree>) 61 Parameters[SymbolicExpressionTreeParameterName]; 62 } 51 get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; } 63 52 } 64 //public ILookupParameter<IntValue> MovesParameter65 //{66 // get67 // {68 // return (ILookupParameter<IntValue>)69 // Parameters[MovesParameterName];70 // }71 //}72 //public ILookupParameter<IntValue> ShotsParameter73 //{74 // get75 // {76 // return (ILookupParameter<IntValue>)77 // Parameters[ShotsParameterName];78 // }79 //}80 53 public ILookupParameter<Solution> BestSolutionParameter { 81 get { 82 return (ILookupParameter<Solution>) 83 Parameters[BestSolutionParameterName]; 84 } 54 get { return (ILookupParameter<Solution>)Parameters[BestSolutionParameterName]; } 85 55 } 86 56 public ILookupParameter<ResultCollection> ResultParameter { 87 get { 88 return (ILookupParameter<ResultCollection>) 89 Parameters[ResultsParameterName]; 90 } 57 get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; } 91 58 } 92 59 public ILookupParameter<StringValue> RobocodePathParameter { 93 get { 94 return (ILookupParameter<StringValue>) 95 Parameters[RobocodePathParamaterName]; 96 } 97 } 98 public ILookupParameter<BoolValue> CoevolutionParameter { 99 get { 100 return (ILookupParameter<BoolValue>) 101 Parameters[CoevolutionParameterName]; 102 } 60 get { return (ILookupParameter<StringValue>)Parameters[RobocodePathParamaterName]; } 103 61 } 104 62 #endregion … … 106 64 [StorableConstructor] 107 65 protected BestSolutionAnalyzer(bool deserializing) : base(deserializing) { } 108 protected BestSolutionAnalyzer(BestSolutionAnalyzer original, 109 Cloner cloner) 110 : base(original, cloner) { 111 } 66 protected BestSolutionAnalyzer(BestSolutionAnalyzer original, Cloner cloner) 67 : base(original, cloner) { } 112 68 113 69 public BestSolutionAnalyzer() { 114 Parameters.Add( 115 new ScopeTreeLookupParameter<DoubleValue>( 116 QualityParameterName, 117 "The solution quality of the tank program.")); 118 Parameters.Add( 119 new ScopeTreeLookupParameter<ISymbolicExpressionTree>( 120 SymbolicExpressionTreeParameterName, 121 "The tank program to evaluate represented " + 122 "as symbolic expression tree.")); 123 Parameters.Add( 124 new LookupParameter<Solution>( 125 BestSolutionParameterName, "The best tank program.")); 126 //Parameters.Add( 127 // new LookupParameter<IntValue>( 128 // MovesParameterName, "The number of moves made.")); 129 //Parameters.Add( 130 // new LookupParameter<IntValue>( 131 // ShotsParameterName, "The shots made.")); 132 Parameters.Add( 133 new LookupParameter<ResultCollection>( 134 ResultsParameterName, "The result collection of the algorithm.")); 135 Parameters.Add( 136 new LookupParameter<StringValue>( 137 RobocodePathParamaterName, 138 "Path of the Robocode installation.")); 139 Parameters.Add( 140 new LookupParameter<BoolValue>( 141 CoevolutionParameterName, 142 "Use Coevolution")); 70 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(QualityParameterName, "The solution quality of the tank program.")); 71 Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The tank program to evaluate represented as symbolic expression tree.")); 72 Parameters.Add(new LookupParameter<Solution>(BestSolutionParameterName, "The best tank program.")); 73 Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The result collection of the algorithm.")); 74 Parameters.Add(new LookupParameter<StringValue>(RobocodePathParamaterName, "Path of the Robocode installation.")); 143 75 } 144 76 … … 159 91 } 160 92 161 var coevolution = CoevolutionParameter.ActualValue.Value;162 double actualQuality = 0;163 164 if (coevolution)165 actualQuality = Interpreter.EvaluateTankProgram(bestTree, null, RobocodePathParameter.ActualValue.Value);166 167 93 // create a solution instance 168 //int shots = ShotsParameter.ActualValue.Value; 169 //int moves = MovesParameter.ActualValue.Value; 170 var bestSolution = new Solution(bestTree, RobocodePathParameter.ActualValue.Value);//, moves, shots); 94 var bestSolution = new Solution(bestTree, RobocodePathParameter.ActualValue.Value); 171 95 // store the new solution in the best solution parameter 172 96 BestSolutionParameter.ActualValue = bestSolution; … … 176 100 var resultCollection = ResultParameter.ActualValue; 177 101 if (!resultCollection.ContainsKey(BestSolutionParameterName)) { 178 resultCollection.Add( 179 new Result(BestSolutionParameterName, 180 "The best tank program", bestSolution)); 181 //if(coevolution) 182 // resultCollection.Add( 183 // new Result("Actual Quality", 184 // "The actual quality of the best program", new DoubleValue(actualQuality))); 102 resultCollection.Add(new Result(BestSolutionParameterName, "The best tank program", bestSolution)); 185 103 } else { 186 104 resultCollection[BestSolutionParameterName].Value = bestSolution; 187 105 } 188 106 189 if (coevolution) {190 if (!resultCollection.ContainsKey("Actual Quality")) {191 resultCollection.Add(192 new Result("Actual Quality",193 "The actual quality of the best program", new DoubleValue(actualQuality)));194 } else {195 resultCollection["Actual Quality"].Value = new DoubleValue(actualQuality);196 }197 }198 199 // important return base.Apply() to make sure the200 // next operator is queued for execution201 107 return base.Apply(); 202 108 } … … 205 111 return new BestSolutionAnalyzer(this, cloner); 206 112 } 207 208 // override this property to indicate that this analyzer209 // should be enabled by default in the algorithm210 public bool EnabledByDefault {211 get { return true; }212 }213 113 } 214 114 } -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Interpreter.cs
r9879 r9880 25 25 using System.Linq; 26 26 using System.Text; 27 using System.Xml;28 using HeuristicLab.Core;29 27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 30 28 … … 32 30 public class Interpreter { 33 31 34 private struct TankStats {35 public int moves, shots;36 public TankStats(int moves, int shots) {37 this.moves = moves;38 this.shots = shots;39 }40 }41 32 static string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 42 33 43 public static string BuildTankProgram(ISymbolicExpressionTree tree) { 44 XmlDocument solutionCandidateRoot = new XmlDocument(); 45 solutionCandidateRoot.Load("../tank.xml"); 46 47 TankStats tankStats = EvaluateTankProgram(tree.Root, solutionCandidateRoot.FirstChild, null); 48 return solutionCandidateRoot.InnerText; 49 } 50 51 public static double EvaluateTankProgram(ISymbolicExpressionTree tree, XmlNode solutionCandidateRoot, string path) { 34 public static double EvaluateTankProgram(ISymbolicExpressionTree tree, string path) { 52 35 //TankStats tankStats = EvaluateTankProgram(tree.Root, solutionCandidateRoot, null); 53 36 //Evaluator.tankStatLog += "\r\n" + tankStats.shots + " / " + tankStats.moves; … … 153 136 evaluation += rand.NextDouble() / 100; 154 137 155 /*Evaluator.tankStatLog = "=====================================================\r\n";156 Evaluator.tankStatLog += "START OF TREE - Score: " + evaluation + " (" + tankStats.shots + "/" + tankStats.moves + ")" + "\r\n" + " Compilation Output: " + cmdOutput + "\r\n";157 Evaluator.tankStatLog += "=====================================================\r\n";158 159 Evaluator.tankStatLog += solutionCandidateRoot.InnerText + "\r\n";160 161 Evaluator.tankStatLog += "=====================================================\r\n";162 Evaluator.tankStatLog += "END OF TREE\r\n";163 Evaluator.tankStatLog += "=====================================================\r\n";164 165 File.AppendAllText("../TankLog.txt", Evaluator.tankStatLog);*/166 138 return evaluation; 167 }168 169 public static double EvaluateTankProgram(ISymbolicExpressionTree tree, ScopeList coevolutionIndividuals, XmlNode solutionCandidateRoot, string path) {170 Random random = new Random();171 string formattedPath = path.Replace("/", "\\");172 173 // Interpret and compile main tree174 //XmlNode mainTreeNode = solutionCandidateRoot.Clone();175 //EvaluateTankProgram(tree.Root, mainTreeNode, null);176 string interpretedMainTree = InterpretProgramTree(tree.Root);177 string mainTreeName = writeTreeToFile(interpretedMainTree, path, random);178 compileCodefile(/*mainTreeName,*/ mainTreeName, formattedPath);179 180 // Interpret and compile coevolutionIndividuals181 String[] coevolutionIndividualsNames = new String[coevolutionIndividuals.Count];182 for (int i = 0; i < coevolutionIndividuals.Count; i++) {183 //XmlNode individualTreeNode = solutionCandidateRoot.Clone();184 //EvaluateTankProgram(((ISymbolicExpressionTree)coevolutionIndividuals[i].Variables.ToArray()[0].Value).Root, individualTreeNode, null);185 string interpretedIndividual = InterpretProgramTree(((ISymbolicExpressionTree)coevolutionIndividuals[i].Variables.ToArray()[0].Value).Root);186 coevolutionIndividualsNames[i] = writeTreeToFile(/*individualTreeNode,*/ interpretedIndividual, path, random);187 compileCodefile(coevolutionIndividualsNames[i], formattedPath);188 }189 190 // Force Robocode to update its robot database by deleting the existing one191 try { File.Delete(path + "/robots/robot.database"); }192 catch (Exception e) { }193 194 // Run evaluator195 double evaluation = runEvaluator(mainTreeName, coevolutionIndividualsNames, formattedPath);196 try {197 File.Delete(path + "/robots/Evaluation/" + mainTreeName + ".java");198 File.Delete(path + "/robots/Evaluation/" + mainTreeName + ".class");199 foreach (string s in coevolutionIndividualsNames) {200 File.Delete(path + "/robots/Evaluation/" + s + ".java");201 File.Delete(path + "/robots/Evaluation/" + s + ".class");202 }203 }204 catch (Exception e) { }205 Random rand = new Random();206 evaluation += rand.NextDouble() / 100;207 208 return evaluation;209 }210 211 private static string writeTreeToFile(/*XmlNode node,*/ string interpretedTree, string path, Random random) {212 string outputName = "";213 string outputString = "";214 bool noError = true;215 do {216 try {217 outputName = new string(Enumerable.Repeat(chars, 8).Select(s => s[random.Next(s.Length)]).ToArray());218 outputString = /*node.InnerText*/interpretedTree.Replace("class output", "class " + outputName);219 File.WriteAllText(path + "/robots/Evaluation/" + outputName + ".java", outputString, System.Text.Encoding.Default);220 noError = true;221 }222 catch (Exception e) { noError = false; }223 } while (!noError);224 225 return outputName;226 }227 228 private static bool compileCodefile(string filename, string formattedPath) {229 ProcessStartInfo javaCompileInfo = new ProcessStartInfo();230 javaCompileInfo.FileName = "cmd.exe";231 javaCompileInfo.Arguments = "/C javac -cp " + formattedPath + "\\libs\\robocode.jar " + formattedPath + "\\robots\\Evaluation\\" + filename + ".java";232 javaCompileInfo.RedirectStandardOutput = true;233 javaCompileInfo.RedirectStandardError = true;234 javaCompileInfo.UseShellExecute = false;235 javaCompileInfo.CreateNoWindow = true;236 237 Process javaCompile = new Process();238 javaCompile.StartInfo = javaCompileInfo;239 javaCompile.Start();240 241 string cmdOutput = javaCompile.StandardOutput.ReadToEnd();242 cmdOutput += javaCompile.StandardError.ReadToEnd();243 244 javaCompile.WaitForExit();245 246 if (cmdOutput.Equals(""))247 return true;248 return false;249 }250 251 private static double runEvaluator(string mainTreeFilename, string[] coevolutionIndividualsNames, string formattedPath) {252 ProcessStartInfo evaluateCodeInfo = new ProcessStartInfo();253 evaluateCodeInfo.FileName = "cmd.exe";254 //javaCompileInfo.Arguments = "/C javac -cp C:\\robocode\\libs\\robocode.jar \"" + path + "\\Spaced Up\\output.java\"";255 evaluateCodeInfo.Arguments = "/C java -classpath " + formattedPath + "\\libs;" + formattedPath + "\\libs\\robocode.core-1.8.1.0.jar;" + formattedPath + "\\libs\\robocode.jar;" + formattedPath + "\\libs\\picocontainer-2.14.2.jar BattleRunner Evaluation." + mainTreeFilename + "* ";256 if (coevolutionIndividualsNames != null) {257 foreach (string s in coevolutionIndividualsNames)258 evaluateCodeInfo.Arguments += "Evaluation." + s + "* ";259 } else260 evaluateCodeInfo.Arguments += "false";261 //Console.WriteLine(javaCompileInfo.Arguments);262 evaluateCodeInfo.RedirectStandardOutput = true;263 evaluateCodeInfo.RedirectStandardError = true;264 evaluateCodeInfo.UseShellExecute = false;265 evaluateCodeInfo.CreateNoWindow = true;266 267 Process evaluateCode = new Process();268 evaluateCode.StartInfo = evaluateCodeInfo;269 evaluateCode.Start();270 271 StringBuilder q = new StringBuilder();272 string scoreString = "";273 while (!evaluateCode.HasExited) {274 scoreString = evaluateCode.StandardOutput.ReadToEnd().Split(275 new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Last();276 q.Append("Output: " + scoreString + "\r\n");277 q.Append("Error: " + evaluateCode.StandardError.ReadToEnd() + "\r\n");278 }279 string cmdOutput = q.ToString();280 try {281 double evaluation = Double.Parse(scoreString);282 return evaluation;283 }284 catch (Exception e) { }285 286 return -900;287 }288 289 private static TankStats EvaluateTankProgram(ISymbolicExpressionTreeNode node, XmlNode docNode, string method) {290 TankStats tankStats = new TankStats(0, 0);291 292 //string log = "Doing node: \r\n";293 /*if (node.Symbol is ProgramRootSymbol ||294 node.Symbol is StartSymbol)295 tankStats = EvaluateTankProgram(node.GetSubtree(0), ref docNode, null);296 else*/297 if (node.Symbol is ShotPower)298 docNode[method]["code"].InnerText += " " + ((ShotPowerTreeNode)node).Value + " ";299 else if (node.Symbol is LogicalValue)300 docNode[method]["code"].InnerText += " " + ((BooleanTreeNode)node).Value.ToString().ToLower() + " ";301 else if (node.Symbol is Block) {302 docNode[method]["code"].InnerText += "{\r\n";303 foreach (ISymbolicExpressionTreeNode n in node.Subtrees)304 EvaluateTankProgram(n, docNode, method);305 docNode[method]["code"].InnerText += "}\r\n";306 } else if (node.Symbol is IfStatement) {307 docNode[method]["code"].InnerText += "if(";308 EvaluateTankProgram(node.GetSubtree(0), docNode, method);309 docNode[method]["code"].InnerText += ")\r\n";310 EvaluateTankProgram(node.GetSubtree(1), docNode, method);311 if (node.SubtreeCount == 3)312 EvaluateTankProgram(node.GetSubtree(2), docNode, method);313 } else if (node.Symbol is ElseStatement) {314 docNode[method]["code"].InnerText += "else\r\n";315 if (node.SubtreeCount == 1)316 EvaluateTankProgram(node.GetSubtree(0), docNode, method);317 } else if (node.Symbol is WhileLoop) {318 docNode[method]["code"].InnerText += "While(";319 EvaluateTankProgram(node.GetSubtree(0), docNode, method);320 docNode[method]["code"].InnerText += ")\r\n";321 EvaluateTankProgram(node.GetSubtree(1), docNode, method);322 } else if (node.Symbol is Ahead) {323 docNode[method]["code"].InnerText += "setAhead(";324 EvaluateTankProgram(node.GetSubtree(0), docNode, method);325 docNode[method]["code"].InnerText += ");\r\n";326 tankStats.moves = 1;327 } else if (node.Symbol is Back) {328 docNode[method]["code"].InnerText += "setBack(";329 EvaluateTankProgram(node.GetSubtree(0), docNode, method);330 docNode[method]["code"].InnerText += ");\r\n";331 tankStats.moves = 1;332 } else if (node.Symbol is TurnGunLeft) {333 docNode[method]["code"].InnerText += "setTurnGunLeft(";334 EvaluateTankProgram(node.GetSubtree(0), docNode, method);335 docNode[method]["code"].InnerText += ");\r\n";336 tankStats.moves = 1;337 } else if (node.Symbol is TurnGunRight) {338 docNode[method]["code"].InnerText += "setTurnGunRight(";339 EvaluateTankProgram(node.GetSubtree(0), docNode, method);340 docNode[method]["code"].InnerText += ");\r\n";341 tankStats.moves = 1;342 } else if (node.Symbol is TurnLeft) {343 docNode[method]["code"].InnerText += "setTurnLeft(";344 EvaluateTankProgram(node.GetSubtree(0), docNode, method);345 docNode[method]["code"].InnerText += ");\r\n";346 tankStats.moves = 1;347 } else if (node.Symbol is TurnRadarLeft) {348 docNode[method]["code"].InnerText += "setTurnRadarLeft(";349 EvaluateTankProgram(node.GetSubtree(0), docNode, method);350 docNode[method]["code"].InnerText += ");\r\n";351 tankStats.moves = 1;352 } else if (node.Symbol is TurnRadarRight) {353 docNode[method]["code"].InnerText += "setTurnRadarRight(";354 EvaluateTankProgram(node.GetSubtree(0), docNode, method);355 docNode[method]["code"].InnerText += ");\r\n";356 tankStats.moves = 1;357 } else if (node.Symbol is TurnRight) {358 docNode[method]["code"].InnerText += "setTurnRight(";359 EvaluateTankProgram(node.GetSubtree(0), docNode, method);360 docNode[method]["code"].InnerText += ");\r\n";361 tankStats.moves = 1;362 } else if (node.Symbol is Fire) {363 docNode[method]["code"].InnerText += "setFire(";364 EvaluateTankProgram(node.GetSubtree(0), docNode, method);365 docNode[method]["code"].InnerText += ");\r\n";366 tankStats.shots = 1;367 } else if (node.Symbol is Run ||368 node.Symbol is OnBulletHit ||369 node.Symbol is OnBulletMissed ||370 node.Symbol is OnHitByBullet ||371 node.Symbol is OnHitRobot ||372 node.Symbol is OnHitWall ||373 node.Symbol is OnScannedRobot) {374 tankStats = new TankStats();375 foreach (SymbolicExpressionTreeNode n in node.Subtrees) {376 string methodName = node.Symbol.GetType().ToString().Split('.').Last();377 try {378 var tempStats = EvaluateTankProgram(n, docNode, methodName);379 tankStats.moves += tempStats.moves;380 tankStats.shots += tempStats.shots;381 }382 catch (Exception e) {383 //log += "NULL EXCEPTION ON:::::::: -" + methodName + "-\r\n";384 //foreach (XmlNode f in docNode[methodName])385 // log += f.Name + "\r\n";386 }387 }388 } else if (node.Symbol is Tank) {389 foreach (SymbolicExpressionTreeNode n in node.Subtrees) {390 try {391 var tempStats = EvaluateTankProgram(n, docNode, method);392 tankStats.moves += tempStats.moves;393 tankStats.shots += tempStats.shots;394 }395 catch (Exception e) { }396 }397 } else398 tankStats = EvaluateTankProgram(node.GetSubtree(0), docNode, null);399 400 //log += tankStats.shots + " / " + tankStats.moves + "\r\n";401 //File.AppendAllText("../TankLog.txt", log);402 403 return tankStats;404 139 } 405 140 -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/RobocodeEvaluator.cs
r9879 r9880 20 20 #endregion 21 21 22 23 using System;24 22 using System.Threading; 25 using System.Xml;26 23 using HeuristicLab.Common; 27 24 using HeuristicLab.Core; … … 36 33 [StorableClass] 37 34 [Item("Robocode Evaluator", "Evaluator for the Robocode GP problem.")] 38 public class RobocodeEvaluator : SingleSuccessorOperator, 39 ISingleObjectiveEvaluator { 40 #region parameter names 35 public class RobocodeEvaluator : SingleSuccessorOperator, ISingleObjectiveEvaluator { 41 36 private const string QualityParameterName = "Quality"; 42 37 private const string TankProgramParameterName = "TankProgram"; 43 38 private const string RobocodePathParamaterName = "Path"; 44 private const string CoevolutionParameterName = "Coevolution";45 39 40 //TODO: this should be removed 41 public static SemaphoreSlim semaphore = new SemaphoreSlim(10); 46 42 47 private static string solutionTemplatePath = ""; 48 public static string SolutionTemplate { 49 get { return solutionTemplatePath; } 50 set { 51 solutionTemplatePath = value; 52 } 53 } 54 55 public static SemaphoreSlim semaphore = new SemaphoreSlim(10); 56 #endregion 57 58 59 #region parameters 43 #region Parameters 60 44 public ILookupParameter<DoubleValue> QualityParameter { 61 get { 62 return (ILookupParameter<DoubleValue>) 63 Parameters[QualityParameterName]; 64 } 45 get { return (ILookupParameter<DoubleValue>)Parameters[QualityParameterName]; } 65 46 } 66 47 public ILookupParameter<ISymbolicExpressionTree> TankProgramParameter { 67 get { 68 return (ILookupParameter<ISymbolicExpressionTree>) 69 Parameters[TankProgramParameterName]; 70 } 48 get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[TankProgramParameterName]; } 71 49 } 72 50 public ILookupParameter<StringValue> RobocodePathParameter { 73 get { 74 return (ILookupParameter<StringValue>) 75 Parameters[RobocodePathParamaterName]; 76 } 77 } 78 public ILookupParameter<BoolValue> CoevolutionParameter { 79 get { 80 return (ILookupParameter<BoolValue>) 81 Parameters[CoevolutionParameterName]; 82 } 51 get { return (ILookupParameter<StringValue>)Parameters[RobocodePathParamaterName]; } 83 52 } 84 53 #endregion 85 86 54 87 55 [StorableConstructor] 88 56 protected RobocodeEvaluator(bool deserializing) : base(deserializing) { } 89 57 protected RobocodeEvaluator(RobocodeEvaluator original, Cloner cloner) 90 : base(original, cloner) { 58 : base(original, cloner) { } 59 60 public RobocodeEvaluator() { 61 Parameters.Add(new LookupParameter<DoubleValue>(QualityParameterName, "The solution quality of the Robocode tank program.")); 62 Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(TankProgramParameterName, "The Robocode tank program to evaluate represented as a symbolic expression tree.")); 63 Parameters.Add(new LookupParameter<StringValue>(RobocodePathParamaterName, "Path of the Robocode installation.")); 91 64 } 92 65 93 // default constructor for the evaluator94 public RobocodeEvaluator() {95 Parameters.Add(96 new LookupParameter<DoubleValue>(97 QualityParameterName,98 "The solution quality of the Robocode tank program."));99 Parameters.Add(100 new LookupParameter<ISymbolicExpressionTree>(101 TankProgramParameterName,102 "The Robocode tank program to evaluate represented as a " +103 "symbolic expression tree."));104 Parameters.Add(105 new LookupParameter<StringValue>(106 RobocodePathParamaterName,107 "Path of the Robocode installation."));108 Parameters.Add(109 new LookupParameter<BoolValue>(110 CoevolutionParameterName,111 "Use Coevolution"));112 113 SolutionTemplate = "../tank.xml";114 }115 116 // override the apply method to change the behaviour of the operator117 // here we take trees as inputs and calculate qualities118 // (fitness evaluation)119 66 public override IOperation Apply() { 120 67 semaphore.Wait(); 121 68 122 69 ISymbolicExpressionTree tree = TankProgramParameter.ActualValue; 123 //XmlDocument doc = new XmlDocument();124 //doc.Load("../tank.xml");125 XmlNode node = null;126 70 string path = RobocodePathParameter.ActualValue.Value; 127 128 if (CoevolutionParameter.ActualValue.Value) { 129 var trees = ExecutionContext.Parent.Scope.SubScopes;//SymbolicExpressionTreeParameter.ActualValue; 130 if (trees.Count == 2) { 131 trees = trees[0].SubScopes; 132 //treeCount = trees[0].SubScopes.Count; 133 //int selTreeCount = trees[1].SubScopes.Count; 134 } 135 136 Random random = new Random(); 137 ScopeList chosenScopes = new ScopeList(5); 138 for (int i = 0; i < 5; i++) { 139 var newScope = trees.ToArray()[random.Next(0, trees.Count - 1)]; 140 chosenScopes.Add(newScope); 141 } 142 143 //if (evaluations % 99 == 0) 144 // evaluations = evaluations; 145 146 QualityParameter.ActualValue = new DoubleValue(Interpreter.EvaluateTankProgram(tree, chosenScopes, node, path)); 147 } else 148 QualityParameter.ActualValue = new DoubleValue(Interpreter.EvaluateTankProgram(tree, node, path)); 71 QualityParameter.ActualValue = new DoubleValue(Interpreter.EvaluateTankProgram(tree, path)); 149 72 150 73 semaphore.Release(); 151 74 152 // important: return base.Apply() to make sure that the153 // next operator is queued for execution154 75 return base.Apply(); 155 76 } … … 158 79 return new RobocodeEvaluator(this, cloner); 159 80 } 160 161 public bool EnabledByDefault {162 get { return true; }163 }164 81 } 165 82 } -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/RobocodeProblem.cs
r9879 r9880 43 43 private const string TankGrammarParameterName = "Grammar"; 44 44 private const string RobocodePathParamaterName = "Path"; 45 private const string CoevolutionParameterName = "Coevolution";46 45 #endregion 47 46 48 47 #region Parameters 49 48 public IFixedValueParameter<IntValue> MaxTankProgramLengthParameter { 50 get { 51 return (IFixedValueParameter<IntValue>) 52 Parameters[MaxTankProgramLengthParameterName]; 53 } 49 get { return (IFixedValueParameter<IntValue>)Parameters[MaxTankProgramLengthParameterName]; } 54 50 } 55 51 public IFixedValueParameter<IntValue> MaxTankProgramDepthParameter { 56 get { 57 return (IFixedValueParameter<IntValue>) 58 Parameters[MaxTankProgramDepthParameterName]; 59 } 52 get { return (IFixedValueParameter<IntValue>)Parameters[MaxTankProgramDepthParameterName]; } 60 53 } 61 54 public IValueParameter<Grammar> GrammarParameter { 62 get { 63 return (IValueParameter<Grammar>) 64 Parameters[TankGrammarParameterName]; 65 } 55 get { return (IValueParameter<Grammar>)Parameters[TankGrammarParameterName]; } 66 56 } 67 57 public IFixedValueParameter<StringValue> RobocodePathParameter { 68 get { 69 return (IFixedValueParameter<StringValue>) 70 Parameters[RobocodePathParamaterName]; 71 } 72 } 73 public IFixedValueParameter<BoolValue> CoevolutionParameter { 74 get { 75 return (IFixedValueParameter<BoolValue>) 76 Parameters[CoevolutionParameterName]; 77 } 58 get { return (IFixedValueParameter<StringValue>)Parameters[RobocodePathParamaterName]; } 78 59 } 79 60 #endregion … … 87 68 } 88 69 89 // default constructor for the problem90 // also creates the fitness evaluation operator (Evaluator),91 // and the tree creation operator (RampedHalfAndHalfTreeCreator)92 70 public RobocodeProblem() 93 71 : base(new RobocodeEvaluator(), new RampedHalfAndHalfTreeCreator()) { 94 Parameters.Add( 95 new FixedValueParameter<IntValue>(MaxTankProgramDepthParameterName, 96 "Maximal depth of the Robocode tank program.", 97 new IntValue(6))); 98 Parameters.Add(new FixedValueParameter<IntValue>(MaxTankProgramLengthParameterName, 99 "Maximal length of the tank program.", new IntValue(1000))); 100 Parameters.Add( 101 new ValueParameter<Grammar>(TankGrammarParameterName, 102 "Grammar for the tank program.", 103 new Grammar())); 104 Parameters.Add(new FixedValueParameter<StringValue>(RobocodePathParamaterName, 105 "Path of the Robocode installation.", 106 new StringValue("F:/robocode2"))); 107 Parameters.Add(new FixedValueParameter<BoolValue>(CoevolutionParameterName, 108 "Use Coevolution", new BoolValue(false))); 72 Parameters.Add(new FixedValueParameter<IntValue>(MaxTankProgramDepthParameterName, "Maximal depth of the Robocode tank program.", new IntValue(6))); 73 Parameters.Add(new FixedValueParameter<IntValue>(MaxTankProgramLengthParameterName, "Maximal length of the tank program.", new IntValue(1000))); 74 Parameters.Add(new ValueParameter<Grammar>(TankGrammarParameterName, "Grammar for the tank program.", new Grammar())); 75 Parameters.Add(new FixedValueParameter<StringValue>(RobocodePathParamaterName, "Path of the Robocode installation.", new StringValue("C:/robocode"))); 109 76 110 77 Maximization.Value = true;
Note: See TracChangeset
for help on using the changeset viewer.