[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 |
|
---|
| 22 | using System;
|
---|
[9601] | 23 | using System.Diagnostics;
|
---|
| 24 | using System.IO;
|
---|
[9565] | 25 | using System.Linq;
|
---|
[9601] | 26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[9565] | 27 |
|
---|
[9601] | 28 | namespace 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();
|
---|
[9884] | 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 |
|
---|
[9881] | 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 | }
|
---|
[9565] | 50 |
|
---|
[9601] | 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;
|
---|
[9565] | 58 |
|
---|
[9601] | 59 | Process javaCompile = new Process();
|
---|
| 60 | javaCompile.StartInfo = javaCompileInfo;
|
---|
| 61 | javaCompile.Start();
|
---|
[9565] | 62 |
|
---|
[9601] | 63 | string cmdOutput = javaCompile.StandardOutput.ReadToEnd();
|
---|
| 64 | cmdOutput += javaCompile.StandardError.ReadToEnd();
|
---|
[9612] | 65 |
|
---|
[9601] | 66 | javaCompile.WaitForExit();
|
---|
[9612] | 67 | if (javaCompile.ExitCode != 0) {
|
---|
[9881] | 68 | DeleteRobotFiles(path, outputname);
|
---|
| 69 | throw new Exception("Compile Error: " + cmdOutput);
|
---|
[9612] | 70 | }
|
---|
[9565] | 71 |
|
---|
[9881] | 72 | //parallel execution of multiple robocode instances can sometimes lead to a damaged robot.database
|
---|
[9601] | 73 | try { File.Delete(path + "/robots/robot.database"); }
|
---|
[9881] | 74 | catch { }
|
---|
[9565] | 75 |
|
---|
[9601] | 76 | ProcessStartInfo evaluateCodeInfo = new ProcessStartInfo();
|
---|
| 77 | evaluateCodeInfo.FileName = "cmd.exe";
|
---|
[9882] | 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";
|
---|
[9601] | 79 | evaluateCodeInfo.RedirectStandardOutput = true;
|
---|
| 80 | evaluateCodeInfo.RedirectStandardError = true;
|
---|
| 81 | evaluateCodeInfo.UseShellExecute = false;
|
---|
| 82 | evaluateCodeInfo.CreateNoWindow = true;
|
---|
[9565] | 83 |
|
---|
[9601] | 84 | Process evaluateCode = new Process();
|
---|
| 85 | evaluateCode.StartInfo = evaluateCodeInfo;
|
---|
| 86 | evaluateCode.Start();
|
---|
[9881] | 87 | evaluateCode.WaitForExit();
|
---|
| 88 | if (evaluateCode.ExitCode != 0) {
|
---|
| 89 | DeleteRobotFiles(path, outputname);
|
---|
| 90 | throw new Exception("Error running Robocode: " + evaluateCode.StandardError.ReadToEnd());
|
---|
| 91 | }
|
---|
[9565] | 92 |
|
---|
[9601] | 93 | string scoreString = "";
|
---|
[9884] | 94 | double evaluation = -1.0;
|
---|
[9612] | 95 | try {
|
---|
[9881] | 96 | scoreString = evaluateCode.StandardOutput.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Last();
|
---|
| 97 | evaluation = Double.Parse(scoreString);
|
---|
[9790] | 98 | }
|
---|
[9881] | 99 | catch (Exception ex) {
|
---|
| 100 | throw new Exception("Error parsing score string: " + ex.ToString());
|
---|
[9601] | 101 | }
|
---|
[9881] | 102 | finally {
|
---|
| 103 | DeleteRobotFiles(path, outputname);
|
---|
| 104 | }
|
---|
[9565] | 105 |
|
---|
[9881] | 106 | return evaluation;
|
---|
| 107 | }
|
---|
[9565] | 108 |
|
---|
[9881] | 109 | private static void DeleteRobotFiles(string path, string outputname) {
|
---|
[9601] | 110 | try {
|
---|
| 111 | File.Delete(path + "/robots/Evaluation/" + outputname + ".java");
|
---|
| 112 | File.Delete(path + "/robots/Evaluation/" + outputname + ".class");
|
---|
| 113 | }
|
---|
[9881] | 114 | catch { }
|
---|
[9601] | 115 | }
|
---|
[9565] | 116 |
|
---|
[9601] | 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 | }
|
---|
[9565] | 124 | } |
---|