Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Interpreter.cs @ 9891

Last change on this file since 9891 was 9891, checked in by ascheibe, 9 years ago

#2069 moved battle runner from Robocode directory to HL bin

File size: 5.6 KB
Line 
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
22using System;
23using System.Diagnostics;
24using System.Globalization;
25using System.IO;
26using System.Linq;
27using System.Reflection;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29
30namespace HeuristicLab.Problems.Robocode {
31  public class Interpreter {
32    public static double EvaluateTankProgram(ISymbolicExpressionTree tree, string path, string robotName = null, bool showUI = false) {
33      if (robotName == null)
34        robotName = GenerateRobotName();
35
36      string interpretedProgram = InterpretProgramTree(tree.Root, robotName);
37      string formattedPath = path.Replace("/", "\\");
38      string battleRunnerPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
39
40      try {
41        File.WriteAllText(path + "/robots/Evaluation/" + robotName + ".java", interpretedProgram, System.Text.Encoding.Default);
42      }
43      catch (Exception ex) {
44        throw;
45      }
46
47      ProcessStartInfo javaCompileInfo = new ProcessStartInfo();
48      javaCompileInfo.FileName = "cmd.exe";
49      javaCompileInfo.Arguments = "/C javac -cp " + formattedPath + "\\libs\\robocode.jar " +
50                                  formattedPath + "\\robots\\Evaluation\\" + robotName + ".java";
51      javaCompileInfo.RedirectStandardOutput = true;
52      javaCompileInfo.RedirectStandardError = true;
53      javaCompileInfo.UseShellExecute = false;
54      javaCompileInfo.CreateNoWindow = true;
55
56      Process javaCompile = new Process();
57      javaCompile.StartInfo = javaCompileInfo;
58      javaCompile.Start();
59
60      string cmdOutput = javaCompile.StandardOutput.ReadToEnd();
61      cmdOutput += javaCompile.StandardError.ReadToEnd();
62
63      javaCompile.WaitForExit();
64      if (javaCompile.ExitCode != 0) {
65        DeleteRobotFiles(path, robotName);
66        throw new Exception("Compile Error: " + cmdOutput);
67      }
68
69      //parallel execution of multiple robocode instances can sometimes lead to a damaged robot.database
70      try { File.Delete(path + "/robots/robot.database"); }
71      catch { }
72
73      ProcessStartInfo evaluateCodeInfo = new ProcessStartInfo();
74
75      evaluateCodeInfo.FileName = "cmd.exe";
76      evaluateCodeInfo.Arguments = "/C java -classpath " + battleRunnerPath + ";" + formattedPath + "\\libs\\robocode.core-1.8.1.0.jar;" + formattedPath + "\\libs\\robocode.jar;" +
77                                   formattedPath + "\\libs\\picocontainer-2.14.2.jar BattleRunner Evaluation." +
78                                   robotName + "* " + formattedPath;
79      if (showUI)
80        evaluateCodeInfo.Arguments += " true";
81
82      evaluateCodeInfo.RedirectStandardOutput = true;
83      evaluateCodeInfo.RedirectStandardError = true;
84      evaluateCodeInfo.UseShellExecute = false;
85      evaluateCodeInfo.CreateNoWindow = true;
86
87      Process evaluateCode = new Process();
88      evaluateCode.StartInfo = evaluateCodeInfo;
89      evaluateCode.Start();
90      evaluateCode.WaitForExit();
91
92      if (evaluateCode.ExitCode != 0) {
93        DeleteRobotFiles(path, robotName);
94        throw new Exception("Error running Robocode: " + evaluateCode.StandardError.ReadToEnd());
95      }
96
97      string scoreString = "";
98      double evaluation = -1.0;
99      try {
100        scoreString = evaluateCode.StandardOutput.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Last();
101        evaluation = Double.Parse(scoreString, CultureInfo.InvariantCulture);
102      }
103      catch (Exception ex) {
104        throw new Exception("Error parsing score string: " + ex.ToString());
105      }
106      finally {
107        DeleteRobotFiles(path, robotName);
108      }
109
110      return evaluation;
111    }
112
113    private static void DeleteRobotFiles(string path, string outputname) {
114      try {
115        File.Delete(path + "/robots/Evaluation/" + outputname + ".java");
116        File.Delete(path + "/robots/Evaluation/" + outputname + ".class");
117      }
118      catch { }
119    }
120
121    private static string GenerateRobotName() {
122      // Robocode class names are 32 char max and
123      // Java class names have to start with a letter
124      string outputname = Guid.NewGuid().ToString();
125      outputname = outputname.Remove(8, 1);
126      outputname = outputname.Remove(12, 1);
127      outputname = outputname.Remove(16, 1);
128      outputname = outputname.Remove(20, 1);
129      outputname = outputname.Remove(0, 1);
130      outputname = outputname.Insert(0, "R");
131      return outputname;
132    }
133
134    public static string InterpretProgramTree(ISymbolicExpressionTreeNode node, string robotName) {
135      var tankNode = node;
136      while (!(tankNode.Symbol is Tank))
137        tankNode = tankNode.GetSubtree(0);
138
139      string result = ((CodeNode)tankNode.Symbol).Interpret(tankNode, tankNode.Subtrees);
140      result = result.Replace("class output", "class " + robotName);
141      return result;
142    }
143  }
144}
Note: See TracBrowser for help on using the repository browser.