Changeset 9881 for branches/Robocode.TrunkInt
- Timestamp:
- 08/13/13 15:27:42 (11 years ago)
- Location:
- branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Interpreter.cs
r9880 r9881 24 24 using System.IO; 25 25 using System.Linq; 26 using System.Text;27 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 28 27 29 28 namespace HeuristicLab.Problems.Robocode { 30 29 public class Interpreter { 31 32 static string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";33 34 30 public static double EvaluateTankProgram(ISymbolicExpressionTree tree, string path) { 35 //TankStats tankStats = EvaluateTankProgram(tree.Root, solutionCandidateRoot, null);36 //Evaluator.tankStatLog += "\r\n" + tankStats.shots + " / " + tankStats.moves;37 31 string interpretedProgram = InterpretProgramTree(tree.Root); 38 Random random = new Random();39 32 string formattedPath = path.Replace("/", "\\"); 40 33 string outputname = ""; 41 string output = "";42 bool noError = true;43 do {44 try {45 outputname = new string(Enumerable.Repeat(chars, 8).Select(s => s[random.Next(s.Length)]).ToArray());46 output = /*solutionCandidateRoot.InnerText*/interpretedProgram.Replace("class output", "class " + outputname);47 File.WriteAllText(path + "/robots/Evaluation/" + outputname + ".java", output, System.Text.Encoding.Default);48 noError = true;49 }50 catch (Exception e) { noError = false; }51 } while (!noError);52 34 53 //string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); 54 //path.Replace("\\", "\\\\"); 55 //path.Replace(" ", "\\ "); 56 #region Compile code file 35 try { 36 outputname = Guid.NewGuid().ToString(); 37 outputname = outputname.Replace('-', '_'); 38 outputname = "Robocode" + outputname; 39 string output = interpretedProgram.Replace("class output", "class " + outputname); 40 File.WriteAllText(path + "/robots/Evaluation/" + outputname + ".java", output, System.Text.Encoding.Default); 41 } 42 catch (Exception ex) { 43 throw; 44 } 57 45 58 46 ProcessStartInfo javaCompileInfo = new ProcessStartInfo(); … … 68 56 javaCompile.Start(); 69 57 70 /*Evaluator.tankStatLog = "=====================================================\r\n";71 Evaluator.tankStatLog += "Tree Compilation Output: \r\n";72 Evaluator.tankStatLog += "=====================================================\r\n";*/73 74 58 string cmdOutput = javaCompile.StandardOutput.ReadToEnd(); 75 59 cmdOutput += javaCompile.StandardError.ReadToEnd(); 76 60 77 //Evaluator.tankStatLog += cmdOutput;78 //Console.WriteLine(cmdOutput);79 //Console.ReadLine();80 81 /*Evaluator.tankStatLog = "=====================================================\r\n";82 Evaluator.tankStatLog += "End of Tree Compilation Output: \r\n";83 Evaluator.tankStatLog += "=====================================================\r\n";*/84 85 61 javaCompile.WaitForExit(); 86 62 if (javaCompile.ExitCode != 0) { 87 return -1000.0; 63 DeleteRobotFiles(path, outputname); 64 throw new Exception("Compile Error: " + cmdOutput); 88 65 } 89 #endregion90 66 91 67 //parallel execution of multiple robocode instances can sometimes lead to a damaged robot.database 92 68 try { File.Delete(path + "/robots/robot.database"); } 93 catch (Exception e) { } 94 95 #region evaluate code 69 catch { } 96 70 97 71 ProcessStartInfo evaluateCodeInfo = new ProcessStartInfo(); 98 72 evaluateCodeInfo.FileName = "cmd.exe"; 99 //javaCompileInfo.Arguments = "/C javac -cp C:\\robocode\\libs\\robocode.jar \"" + path + "\\Spaced Up\\output.java\"";100 73 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 + "* false"; 101 //Console.WriteLine(javaCompileInfo.Arguments);102 74 evaluateCodeInfo.RedirectStandardOutput = true; 103 75 evaluateCodeInfo.RedirectStandardError = true; … … 108 80 evaluateCode.StartInfo = evaluateCodeInfo; 109 81 evaluateCode.Start(); 110 111 StringBuilder q = new StringBuilder();112 string scoreString = "";113 82 evaluateCode.WaitForExit(); 114 115 try { 116 scoreString = evaluateCode.StandardOutput.ReadToEnd().Split( 117 new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Last(); 118 q.Append("Output: " + scoreString + "\r\n"); 119 q.Append("Error: " + evaluateCode.StandardError.ReadToEnd() + "\r\n"); 120 cmdOutput += q.ToString(); 121 } 122 catch { 123 return -1000.0; 83 if (evaluateCode.ExitCode != 0) { 84 DeleteRobotFiles(path, outputname); 85 throw new Exception("Error running Robocode: " + evaluateCode.StandardError.ReadToEnd()); 124 86 } 125 87 126 #endregion 88 string scoreString = ""; 89 double evaluation = -900; 90 try { 91 scoreString = evaluateCode.StandardOutput.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Last(); 92 evaluation = Double.Parse(scoreString); 93 } 94 catch (Exception ex) { 95 throw new Exception("Error parsing score string: " + ex.ToString()); 96 } 97 finally { 98 DeleteRobotFiles(path, outputname); 99 } 127 100 128 double evaluation = -900; 101 return evaluation; 102 } 103 104 private static void DeleteRobotFiles(string path, string outputname) { 129 105 try { 130 106 File.Delete(path + "/robots/Evaluation/" + outputname + ".java"); 131 107 File.Delete(path + "/robots/Evaluation/" + outputname + ".class"); 132 evaluation = Double.Parse(scoreString);133 108 } 134 catch (Exception e) { evaluation = -900; } 135 Random rand = new Random(); 136 evaluation += rand.NextDouble() / 100; 137 138 return evaluation; 109 catch { } 139 110 } 140 111 -
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/RobocodeEvaluator.cs
r9880 r9881 20 20 #endregion 21 21 22 using System.Threading;23 22 using HeuristicLab.Common; 24 23 using HeuristicLab.Core; … … 37 36 private const string TankProgramParameterName = "TankProgram"; 38 37 private const string RobocodePathParamaterName = "Path"; 39 40 //TODO: this should be removed41 public static SemaphoreSlim semaphore = new SemaphoreSlim(10);42 38 43 39 #region Parameters … … 65 61 66 62 public override IOperation Apply() { 67 semaphore.Wait();68 69 63 ISymbolicExpressionTree tree = TankProgramParameter.ActualValue; 70 64 string path = RobocodePathParameter.ActualValue.Value; 71 65 QualityParameter.ActualValue = new DoubleValue(Interpreter.EvaluateTankProgram(tree, path)); 72 73 semaphore.Release();74 66 75 67 return base.Apply();
Note: See TracChangeset
for help on using the changeset viewer.