Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2069

  • qualities are now scaled between 0 and 1
  • fixed naming of robots
File size: 5.0 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.IO;
25using System.Linq;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27
28namespace 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.Remove(8, 1);
38        outputname = outputname.Remove(12, 1);
39        outputname = outputname.Remove(16, 1);
40        outputname = outputname.Remove(20, 1);
41        outputname = outputname.Remove(0, 1);
42        outputname = outputname.Insert(0, "R");
43
44        string output = interpretedProgram.Replace("class output", "class " + outputname);
45        File.WriteAllText(path + "/robots/Evaluation/" + outputname + ".java", output, System.Text.Encoding.Default);
46      }
47      catch (Exception ex) {
48        throw;
49      }
50
51      ProcessStartInfo javaCompileInfo = new ProcessStartInfo();
52      javaCompileInfo.FileName = "cmd.exe";
53      javaCompileInfo.Arguments = "/C javac -cp " + formattedPath + "\\libs\\robocode.jar " + formattedPath + "\\robots\\Evaluation\\" + outputname + ".java";
54      javaCompileInfo.RedirectStandardOutput = true;
55      javaCompileInfo.RedirectStandardError = true;
56      javaCompileInfo.UseShellExecute = false;
57      javaCompileInfo.CreateNoWindow = true;
58
59      Process javaCompile = new Process();
60      javaCompile.StartInfo = javaCompileInfo;
61      javaCompile.Start();
62
63      string cmdOutput = javaCompile.StandardOutput.ReadToEnd();
64      cmdOutput += javaCompile.StandardError.ReadToEnd();
65
66      javaCompile.WaitForExit();
67      if (javaCompile.ExitCode != 0) {
68        DeleteRobotFiles(path, outputname);
69        throw new Exception("Compile Error: " + cmdOutput);
70      }
71
72      //parallel execution of multiple robocode instances can sometimes lead to a damaged robot.database
73      try { File.Delete(path + "/robots/robot.database"); }
74      catch { }
75
76      ProcessStartInfo evaluateCodeInfo = new ProcessStartInfo();
77      evaluateCodeInfo.FileName = "cmd.exe";
78      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";
79      evaluateCodeInfo.RedirectStandardOutput = true;
80      evaluateCodeInfo.RedirectStandardError = true;
81      evaluateCodeInfo.UseShellExecute = false;
82      evaluateCodeInfo.CreateNoWindow = true;
83
84      Process evaluateCode = new Process();
85      evaluateCode.StartInfo = evaluateCodeInfo;
86      evaluateCode.Start();
87      evaluateCode.WaitForExit();
88      if (evaluateCode.ExitCode != 0) {
89        DeleteRobotFiles(path, outputname);
90        throw new Exception("Error running Robocode: " + evaluateCode.StandardError.ReadToEnd());
91      }
92
93      string scoreString = "";
94      double evaluation = -1.0;
95      try {
96        scoreString = evaluateCode.StandardOutput.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Last();
97        evaluation = Double.Parse(scoreString);
98      }
99      catch (Exception ex) {
100        throw new Exception("Error parsing score string: " + ex.ToString());
101      }
102      finally {
103        DeleteRobotFiles(path, outputname);
104      }
105
106      return evaluation;
107    }
108
109    private static void DeleteRobotFiles(string path, string outputname) {
110      try {
111        File.Delete(path + "/robots/Evaluation/" + outputname + ".java");
112        File.Delete(path + "/robots/Evaluation/" + outputname + ".class");
113      }
114      catch { }
115    }
116
117    public static string InterpretProgramTree(ISymbolicExpressionTreeNode node) {
118      var tankNode = node;
119      while (!(tankNode.Symbol is Tank))
120        tankNode = tankNode.GetSubtree(0);
121      return ((CodeNode)tankNode.Symbol).Interpret(tankNode, tankNode.Subtrees);
122    }
123  }
124}
Note: See TracBrowser for help on using the repository browser.