- Timestamp:
- 10/15/15 16:39:12 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Interpreter.cs
r13011 r13013 26 26 using System.Linq; 27 27 using System.Reflection; 28 using HeuristicLab.Core;29 using HeuristicLab.Data;30 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 31 29 32 namespace HeuristicLab.Problems.GeneticProgramming.Robo Code {30 namespace HeuristicLab.Problems.GeneticProgramming.Robocode { 33 31 public static class Interpreter { 34 32 public static double EvaluateTankProgram(ISymbolicExpressionTree tree, string path, EnemyCollection enemies, string robotName = null, bool showUI = false, int nrOfRounds = 3) { … … 47 45 File.WriteAllText(srcRobotPath, interpretedProgram, System.Text.Encoding.Default); 48 46 47 // compile java source to class file 49 48 ProcessStartInfo javaCompileInfo = new ProcessStartInfo(); 50 49 javaCompileInfo.FileName = "cmd.exe"; … … 55 54 javaCompileInfo.CreateNoWindow = true; 56 55 57 Process javaCompile = new Process();58 javaCompile.StartInfo = javaCompileInfo;59 javaCompile.Start();56 using (Process javaCompile = new Process()) { 57 javaCompile.StartInfo = javaCompileInfo; 58 javaCompile.Start(); 60 59 61 string cmdOutput = javaCompile.StandardOutput.ReadToEnd();62 cmdOutput += javaCompile.StandardError.ReadToEnd();60 string cmdOutput = javaCompile.StandardOutput.ReadToEnd(); 61 cmdOutput += javaCompile.StandardError.ReadToEnd(); 63 62 64 javaCompile.WaitForExit(); 65 if (javaCompile.ExitCode != 0) { 66 DeleteRobotFiles(path, robotName); 67 throw new Exception("Compile Error: " + cmdOutput); 63 javaCompile.WaitForExit(); 64 if (javaCompile.ExitCode != 0) { 65 DeleteRobotFiles(path, robotName); 66 throw new Exception("Compile Error: " + cmdOutput); 67 } 68 68 } 69 69 70 70 //parallel execution of multiple robocode instances can sometimes lead to a damaged robot.database 71 try { File.Delete(path + @"\robots\robot.database"); } 72 catch { } 71 var robotsDbFileName = Path.Combine(path, "robots", "robot.database"); 72 if (File.Exists(robotsDbFileName)) 73 File.Delete(robotsDbFileName); 73 74 74 75 ProcessStartInfo evaluateCodeInfo = new ProcessStartInfo(); 75 76 77 // execute a battle with numberOfRounds against a number of enemies 78 // TODO: seems there is a bug when selecting multiple enemies 76 79 evaluateCodeInfo.FileName = "cmd.exe"; 77 evaluateCodeInfo.Arguments = "/C java -cp " + battleRunnerPath + ";" + robocodeCoreJar + ";" + robocodeJar + 78 ";" + picocontainerJar + ";" + " BattleRunner Evaluation." + robotName + " " + path; 79 80 if (showUI) { 81 evaluateCodeInfo.Arguments += " true " + nrOfRounds + " "; 82 } else { 83 evaluateCodeInfo.Arguments += " false " + nrOfRounds + " "; 84 } 85 86 foreach (var enemy in enemies.CheckedItems) 87 evaluateCodeInfo.Arguments += enemy.Value + " "; 80 var classpath = string.Join(";", new[] { battleRunnerPath, robocodeCoreJar, robocodeJar, picocontainerJar }); 81 var enemyRobotNames = string.Join(" ", enemies.CheckedItems.Select(i => i.Value)); 82 evaluateCodeInfo.Arguments = string.Format("/C java -cp {0} BattleRunner Evaluation.{1} {2} {3} {4} {5}", classpath, robotName, path, showUI, nrOfRounds, enemyRobotNames); 88 83 89 84 evaluateCodeInfo.RedirectStandardOutput = true; … … 92 87 evaluateCodeInfo.CreateNoWindow = true; 93 88 94 Process evaluateCode = new Process(); 95 evaluateCode.StartInfo = evaluateCodeInfo; 96 evaluateCode.Start(); 97 evaluateCode.WaitForExit(); 89 double evaluation; 90 using (Process evaluateCode = new Process()) { 91 evaluateCode.StartInfo = evaluateCodeInfo; 92 evaluateCode.Start(); 93 evaluateCode.WaitForExit(); 98 94 99 if (evaluateCode.ExitCode != 0) { 100 DeleteRobotFiles(path, robotName); 101 throw new Exception("Error running Robocode: " + evaluateCode.StandardError.ReadToEnd()); 102 } 95 if (evaluateCode.ExitCode != 0) { 96 DeleteRobotFiles(path, robotName); 97 throw new Exception("Error running Robocode: " + evaluateCode.StandardError.ReadToEnd() + Environment.NewLine + 98 evaluateCode.StandardOutput.ReadToEnd()); 99 } 103 100 104 string scoreString = ""; 105 double evaluation = -1.0; 106 try { 107 scoreString = evaluateCode.StandardOutput.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Last(); 108 evaluation = Double.Parse(scoreString, CultureInfo.InvariantCulture); 109 } 110 catch (Exception ex) { 111 throw new Exception("Error parsing score string: " + ex.ToString()); 112 } 113 finally { 114 DeleteRobotFiles(path, robotName); 101 try { 102 string scoreString = 103 evaluateCode.StandardOutput.ReadToEnd() 104 .Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries) 105 .Last(); 106 evaluation = Double.Parse(scoreString, CultureInfo.InvariantCulture); 107 } 108 catch (Exception ex) { 109 throw new Exception("Error parsing score string: " + ex); 110 } 111 finally { 112 DeleteRobotFiles(path, robotName); 113 } 115 114 } 116 115 … … 119 118 120 119 private static void DeleteRobotFiles(string path, string outputname) { 121 try { 122 File.Delete(path + @"\robots\Evaluation\" + outputname + ".java"); 123 File.Delete(path + @"\robots\Evaluation\" + outputname + ".class"); 124 } 125 catch { } 120 File.Delete(path + @"\robots\Evaluation\" + outputname + ".java"); 121 File.Delete(path + @"\robots\Evaluation\" + outputname + ".class"); 126 122 } 127 123 … … 132 128 } 133 129 catch { 134 throw new Exception("Error finding required Robo Code files.");130 throw new Exception("Error finding required Robocode files."); 135 131 } 136 132 return fileName;
Note: See TracChangeset
for help on using the changeset viewer.