#region License Information /* HeuristicLab * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Diagnostics; using System.IO; using System.Linq; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; namespace HeuristicLab.Problems.Robocode { public class Interpreter { public static double EvaluateTankProgram(ISymbolicExpressionTree tree, string path) { string interpretedProgram = InterpretProgramTree(tree.Root); string formattedPath = path.Replace("/", "\\"); string outputname = ""; try { outputname = Guid.NewGuid().ToString(); outputname = outputname.Replace('-', '_'); outputname = "Robocode" + outputname; string output = interpretedProgram.Replace("class output", "class " + outputname); File.WriteAllText(path + "/robots/Evaluation/" + outputname + ".java", output, System.Text.Encoding.Default); } catch (Exception ex) { throw; } ProcessStartInfo javaCompileInfo = new ProcessStartInfo(); javaCompileInfo.FileName = "cmd.exe"; javaCompileInfo.Arguments = "/C javac -cp " + formattedPath + "\\libs\\robocode.jar " + formattedPath + "\\robots\\Evaluation\\" + outputname + ".java"; javaCompileInfo.RedirectStandardOutput = true; javaCompileInfo.RedirectStandardError = true; javaCompileInfo.UseShellExecute = false; javaCompileInfo.CreateNoWindow = true; Process javaCompile = new Process(); javaCompile.StartInfo = javaCompileInfo; javaCompile.Start(); string cmdOutput = javaCompile.StandardOutput.ReadToEnd(); cmdOutput += javaCompile.StandardError.ReadToEnd(); javaCompile.WaitForExit(); if (javaCompile.ExitCode != 0) { DeleteRobotFiles(path, outputname); throw new Exception("Compile Error: " + cmdOutput); } //parallel execution of multiple robocode instances can sometimes lead to a damaged robot.database try { File.Delete(path + "/robots/robot.database"); } catch { } ProcessStartInfo evaluateCodeInfo = new ProcessStartInfo(); evaluateCodeInfo.FileName = "cmd.exe"; 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"; evaluateCodeInfo.RedirectStandardOutput = true; evaluateCodeInfo.RedirectStandardError = true; evaluateCodeInfo.UseShellExecute = false; evaluateCodeInfo.CreateNoWindow = true; Process evaluateCode = new Process(); evaluateCode.StartInfo = evaluateCodeInfo; evaluateCode.Start(); evaluateCode.WaitForExit(); if (evaluateCode.ExitCode != 0) { DeleteRobotFiles(path, outputname); throw new Exception("Error running Robocode: " + evaluateCode.StandardError.ReadToEnd()); } string scoreString = ""; double evaluation = -900; try { scoreString = evaluateCode.StandardOutput.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Last(); evaluation = Double.Parse(scoreString); } catch (Exception ex) { throw new Exception("Error parsing score string: " + ex.ToString()); } finally { DeleteRobotFiles(path, outputname); } return evaluation; } private static void DeleteRobotFiles(string path, string outputname) { try { File.Delete(path + "/robots/Evaluation/" + outputname + ".java"); File.Delete(path + "/robots/Evaluation/" + outputname + ".class"); } catch { } } public static string InterpretProgramTree(ISymbolicExpressionTreeNode node) { var tankNode = node; while (!(tankNode.Symbol is Tank)) tankNode = tankNode.GetSubtree(0); return ((CodeNode)tankNode.Symbol).Interpret(tankNode, tankNode.Subtrees); } } }