Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2069

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