- Timestamp:
- 08/21/13 14:38:09 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Interpreter.cs
r9888 r9889 29 29 namespace HeuristicLab.Problems.Robocode { 30 30 public class Interpreter { 31 public static double EvaluateTankProgram(ISymbolicExpressionTree tree, string path) { 32 string interpretedProgram = InterpretProgramTree(tree.Root); 31 public static double EvaluateTankProgram(ISymbolicExpressionTree tree, string path, string robotName = null, bool showUI = false) { 32 if (robotName == null) 33 robotName = GenerateRobotName(); 34 35 string interpretedProgram = InterpretProgramTree(tree.Root, robotName); 33 36 string formattedPath = path.Replace("/", "\\"); 34 string outputname = "";35 37 36 38 try { 37 outputname = Guid.NewGuid().ToString(); 38 outputname = outputname.Remove(8, 1); 39 outputname = outputname.Remove(12, 1); 40 outputname = outputname.Remove(16, 1); 41 outputname = outputname.Remove(20, 1); 42 outputname = outputname.Remove(0, 1); 43 outputname = outputname.Insert(0, "R"); 44 45 string output = interpretedProgram.Replace("class output", "class " + outputname); 46 File.WriteAllText(path + "/robots/Evaluation/" + outputname + ".java", output, System.Text.Encoding.Default); 39 File.WriteAllText(path + "/robots/Evaluation/" + robotName + ".java", interpretedProgram, System.Text.Encoding.Default); 47 40 } 48 41 catch (Exception ex) { … … 52 45 ProcessStartInfo javaCompileInfo = new ProcessStartInfo(); 53 46 javaCompileInfo.FileName = "cmd.exe"; 54 javaCompileInfo.Arguments = "/C javac -cp " + formattedPath + "\\libs\\robocode.jar " + formattedPath + "\\robots\\Evaluation\\" + outputname + ".java"; 47 javaCompileInfo.Arguments = "/C javac -cp " + formattedPath + "\\libs\\robocode.jar " + 48 formattedPath + "\\robots\\Evaluation\\" + robotName + ".java"; 55 49 javaCompileInfo.RedirectStandardOutput = true; 56 50 javaCompileInfo.RedirectStandardError = true; … … 67 61 javaCompile.WaitForExit(); 68 62 if (javaCompile.ExitCode != 0) { 69 DeleteRobotFiles(path, outputname);63 DeleteRobotFiles(path, robotName); 70 64 throw new Exception("Compile Error: " + cmdOutput); 71 65 } … … 77 71 ProcessStartInfo evaluateCodeInfo = new ProcessStartInfo(); 78 72 evaluateCodeInfo.FileName = "cmd.exe"; 79 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." + outputname + "* " + formattedPath + " false"; 73 evaluateCodeInfo.Arguments = "/C java -classpath " + formattedPath + "\\libs;" + formattedPath + 74 "\\libs\\robocode.core-1.8.1.0.jar;" + formattedPath + "\\libs\\robocode.jar;" + 75 formattedPath + "\\libs\\picocontainer-2.14.2.jar BattleRunner Evaluation." + 76 robotName + "* " + formattedPath; 77 if (showUI) 78 evaluateCodeInfo.Arguments += " true"; 79 80 80 evaluateCodeInfo.RedirectStandardOutput = true; 81 81 evaluateCodeInfo.RedirectStandardError = true; … … 87 87 evaluateCode.Start(); 88 88 evaluateCode.WaitForExit(); 89 89 90 if (evaluateCode.ExitCode != 0) { 90 DeleteRobotFiles(path, outputname);91 DeleteRobotFiles(path, robotName); 91 92 throw new Exception("Error running Robocode: " + evaluateCode.StandardError.ReadToEnd()); 92 93 } … … 102 103 } 103 104 finally { 104 DeleteRobotFiles(path, outputname);105 DeleteRobotFiles(path, robotName); 105 106 } 106 107 … … 116 117 } 117 118 118 public static string InterpretProgramTree(ISymbolicExpressionTreeNode node) { 119 private static string GenerateRobotName() { 120 // Robocode class names are 32 char max and 121 // Java class names have to start with a letter 122 string outputname = Guid.NewGuid().ToString(); 123 outputname = outputname.Remove(8, 1); 124 outputname = outputname.Remove(12, 1); 125 outputname = outputname.Remove(16, 1); 126 outputname = outputname.Remove(20, 1); 127 outputname = outputname.Remove(0, 1); 128 outputname = outputname.Insert(0, "R"); 129 return outputname; 130 } 131 132 public static string InterpretProgramTree(ISymbolicExpressionTreeNode node, string robotName) { 119 133 var tankNode = node; 120 134 while (!(tankNode.Symbol is Tank)) 121 135 tankNode = tankNode.GetSubtree(0); 122 return ((CodeNode)tankNode.Symbol).Interpret(tankNode, tankNode.Subtrees); 136 137 string result = ((CodeNode)tankNode.Symbol).Interpret(tankNode, tankNode.Subtrees); 138 result = result.Replace("class output", "class " + robotName); 139 return result; 123 140 } 124 141 }
Note: See TracChangeset
for help on using the changeset viewer.