Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2069 cleaned up battlerunner

File size: 4.8 KB
RevLine 
[9790]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;
[9601]23using System.Diagnostics;
24using System.IO;
[9565]25using System.Linq;
[9601]26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
[9565]27
[9601]28namespace HeuristicLab.Problems.Robocode {
29  public class Interpreter {
[9880]30    public static double EvaluateTankProgram(ISymbolicExpressionTree tree, string path) {
[9601]31      string interpretedProgram = InterpretProgramTree(tree.Root);
32      string formattedPath = path.Replace("/", "\\");
33      string outputname = "";
[9565]34
[9881]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      }
[9565]45
[9601]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;
[9565]53
[9601]54      Process javaCompile = new Process();
55      javaCompile.StartInfo = javaCompileInfo;
56      javaCompile.Start();
[9565]57
[9601]58      string cmdOutput = javaCompile.StandardOutput.ReadToEnd();
59      cmdOutput += javaCompile.StandardError.ReadToEnd();
[9612]60
[9601]61      javaCompile.WaitForExit();
[9612]62      if (javaCompile.ExitCode != 0) {
[9881]63        DeleteRobotFiles(path, outputname);
64        throw new Exception("Compile Error: " + cmdOutput);
[9612]65      }
[9565]66
[9881]67      //parallel execution of multiple robocode instances can sometimes lead to a damaged robot.database
[9601]68      try { File.Delete(path + "/robots/robot.database"); }
[9881]69      catch { }
[9565]70
[9601]71      ProcessStartInfo evaluateCodeInfo = new ProcessStartInfo();
72      evaluateCodeInfo.FileName = "cmd.exe";
[9882]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 + "* " + formattedPath + " false";
[9601]74      evaluateCodeInfo.RedirectStandardOutput = true;
75      evaluateCodeInfo.RedirectStandardError = true;
76      evaluateCodeInfo.UseShellExecute = false;
77      evaluateCodeInfo.CreateNoWindow = true;
[9565]78
[9601]79      Process evaluateCode = new Process();
80      evaluateCode.StartInfo = evaluateCodeInfo;
81      evaluateCode.Start();
[9881]82      evaluateCode.WaitForExit();
83      if (evaluateCode.ExitCode != 0) {
84        DeleteRobotFiles(path, outputname);
85        throw new Exception("Error running Robocode: " + evaluateCode.StandardError.ReadToEnd());
86      }
[9565]87
[9601]88      string scoreString = "";
[9881]89      double evaluation = -900;
[9612]90      try {
[9881]91        scoreString = evaluateCode.StandardOutput.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Last();
92        evaluation = Double.Parse(scoreString);
[9790]93      }
[9881]94      catch (Exception ex) {
95        throw new Exception("Error parsing score string: " + ex.ToString());
[9601]96      }
[9881]97      finally {
98        DeleteRobotFiles(path, outputname);
99      }
[9565]100
[9881]101      return evaluation;
102    }
[9565]103
[9881]104    private static void DeleteRobotFiles(string path, string outputname) {
[9601]105      try {
106        File.Delete(path + "/robots/Evaluation/" + outputname + ".java");
107        File.Delete(path + "/robots/Evaluation/" + outputname + ".class");
108      }
[9881]109      catch { }
[9601]110    }
[9565]111
[9601]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  }
[9565]119}
Note: See TracBrowser for help on using the repository browser.