Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9889 was 9889, checked in by ascheibe, 11 years ago

#2069 cleaned up views

File size: 5.5 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 HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28
29namespace HeuristicLab.Problems.Robocode {
30  public class Interpreter {
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);
36      string formattedPath = path.Replace("/", "\\");
37
38      try {
39        File.WriteAllText(path + "/robots/Evaluation/" + robotName + ".java", interpretedProgram, System.Text.Encoding.Default);
40      }
41      catch (Exception ex) {
42        throw;
43      }
44
45      ProcessStartInfo javaCompileInfo = new ProcessStartInfo();
46      javaCompileInfo.FileName = "cmd.exe";
47      javaCompileInfo.Arguments = "/C javac -cp " + formattedPath + "\\libs\\robocode.jar " +
48                                  formattedPath + "\\robots\\Evaluation\\" + robotName + ".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, robotName);
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 +
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      evaluateCodeInfo.RedirectStandardOutput = true;
81      evaluateCodeInfo.RedirectStandardError = true;
82      evaluateCodeInfo.UseShellExecute = false;
83      evaluateCodeInfo.CreateNoWindow = true;
84
85      Process evaluateCode = new Process();
86      evaluateCode.StartInfo = evaluateCodeInfo;
87      evaluateCode.Start();
88      evaluateCode.WaitForExit();
89
90      if (evaluateCode.ExitCode != 0) {
91        DeleteRobotFiles(path, robotName);
92        throw new Exception("Error running Robocode: " + evaluateCode.StandardError.ReadToEnd());
93      }
94
95      string scoreString = "";
96      double evaluation = -1.0;
97      try {
98        scoreString = evaluateCode.StandardOutput.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Last();
99        evaluation = Double.Parse(scoreString, CultureInfo.InvariantCulture);
100      }
101      catch (Exception ex) {
102        throw new Exception("Error parsing score string: " + ex.ToString());
103      }
104      finally {
105        DeleteRobotFiles(path, robotName);
106      }
107
108      return evaluation;
109    }
110
111    private static void DeleteRobotFiles(string path, string outputname) {
112      try {
113        File.Delete(path + "/robots/Evaluation/" + outputname + ".java");
114        File.Delete(path + "/robots/Evaluation/" + outputname + ".class");
115      }
116      catch { }
117    }
118
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) {
133      var tankNode = node;
134      while (!(tankNode.Symbol is Tank))
135        tankNode = tankNode.GetSubtree(0);
136
137      string result = ((CodeNode)tankNode.Symbol).Interpret(tankNode, tankNode.Subtrees);
138      result = result.Replace("class output", "class " + robotName);
139      return result;
140    }
141  }
142}
Note: See TracBrowser for help on using the repository browser.