1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Diagnostics;
|
---|
24 | using System.IO;
|
---|
25 | using System.Linq;
|
---|
26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Problems.Robocode {
|
---|
29 | public class Interpreter {
|
---|
30 | public static double EvaluateTankProgram(ISymbolicExpressionTree tree, string path) {
|
---|
31 | string interpretedProgram = InterpretProgramTree(tree.Root);
|
---|
32 | string formattedPath = path.Replace("/", "\\");
|
---|
33 | string outputname = "";
|
---|
34 |
|
---|
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 | }
|
---|
45 |
|
---|
46 | ProcessStartInfo javaCompileInfo = new ProcessStartInfo();
|
---|
47 | javaCompileInfo.FileName = "cmd.exe";
|
---|
48 | javaCompileInfo.Arguments = "/C javac -cp " + formattedPath + "\\libs\\robocode.jar " + formattedPath + "\\robots\\Evaluation\\" + outputname + ".java";
|
---|
49 | javaCompileInfo.RedirectStandardOutput = true;
|
---|
50 | javaCompileInfo.RedirectStandardError = true;
|
---|
51 | javaCompileInfo.UseShellExecute = false;
|
---|
52 | javaCompileInfo.CreateNoWindow = true;
|
---|
53 |
|
---|
54 | Process javaCompile = new Process();
|
---|
55 | javaCompile.StartInfo = javaCompileInfo;
|
---|
56 | javaCompile.Start();
|
---|
57 |
|
---|
58 | string cmdOutput = javaCompile.StandardOutput.ReadToEnd();
|
---|
59 | cmdOutput += javaCompile.StandardError.ReadToEnd();
|
---|
60 |
|
---|
61 | javaCompile.WaitForExit();
|
---|
62 | if (javaCompile.ExitCode != 0) {
|
---|
63 | DeleteRobotFiles(path, outputname);
|
---|
64 | throw new Exception("Compile Error: " + cmdOutput);
|
---|
65 | }
|
---|
66 |
|
---|
67 | //parallel execution of multiple robocode instances can sometimes lead to a damaged robot.database
|
---|
68 | try { File.Delete(path + "/robots/robot.database"); }
|
---|
69 | catch { }
|
---|
70 |
|
---|
71 | ProcessStartInfo evaluateCodeInfo = new ProcessStartInfo();
|
---|
72 | evaluateCodeInfo.FileName = "cmd.exe";
|
---|
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";
|
---|
74 | evaluateCodeInfo.RedirectStandardOutput = true;
|
---|
75 | evaluateCodeInfo.RedirectStandardError = true;
|
---|
76 | evaluateCodeInfo.UseShellExecute = false;
|
---|
77 | evaluateCodeInfo.CreateNoWindow = true;
|
---|
78 |
|
---|
79 | Process evaluateCode = new Process();
|
---|
80 | evaluateCode.StartInfo = evaluateCodeInfo;
|
---|
81 | evaluateCode.Start();
|
---|
82 | evaluateCode.WaitForExit();
|
---|
83 | if (evaluateCode.ExitCode != 0) {
|
---|
84 | DeleteRobotFiles(path, outputname);
|
---|
85 | throw new Exception("Error running Robocode: " + evaluateCode.StandardError.ReadToEnd());
|
---|
86 | }
|
---|
87 |
|
---|
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 | }
|
---|
100 |
|
---|
101 | return evaluation;
|
---|
102 | }
|
---|
103 |
|
---|
104 | private static void DeleteRobotFiles(string path, string outputname) {
|
---|
105 | try {
|
---|
106 | File.Delete(path + "/robots/Evaluation/" + outputname + ".java");
|
---|
107 | File.Delete(path + "/robots/Evaluation/" + outputname + ".class");
|
---|
108 | }
|
---|
109 | catch { }
|
---|
110 | }
|
---|
111 |
|
---|
112 | public static string InterpretProgramTree(ISymbolicExpressionTreeNode node) {
|
---|
113 | var tankNode = node;
|
---|
114 | while (!(tankNode.Symbol is Tank))
|
---|
115 | tankNode = tankNode.GetSubtree(0);
|
---|
116 | return ((CodeNode)tankNode.Symbol).Interpret(tankNode, tankNode.Subtrees);
|
---|
117 | }
|
---|
118 | }
|
---|
119 | } |
---|