#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.Globalization; using System.IO; using System.Linq; using System.Reflection; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; namespace HeuristicLab.Problems.Robocode { public static class Interpreter { public static double EvaluateTankProgram(ISymbolicExpressionTree tree, string path, EnemyCollection enemies, string robotName = null, bool showUI = false, int nrOfRounds = 3) { if (robotName == null) robotName = GenerateRobotName(); string interpretedProgram = InterpretProgramTree(tree.Root, robotName); string battleRunnerPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string roboCodeLibPath = Path.Combine(path, "libs"); string robocodeJar = Path.Combine(roboCodeLibPath, "robocode.jar"); string robocodeCoreJar = GetFileName(roboCodeLibPath, "robocode.core*"); string picocontainerJar = GetFileName(roboCodeLibPath, "picocontainer*"); string robotsPath = Path.Combine(path, "robots", "Evaluation"); string srcRobotPath = Path.Combine(robotsPath, robotName + ".java"); File.WriteAllText(srcRobotPath, interpretedProgram, System.Text.Encoding.Default); ProcessStartInfo javaCompileInfo = new ProcessStartInfo(); javaCompileInfo.FileName = "cmd.exe"; javaCompileInfo.Arguments = "/C javac -cp " + robocodeJar + "; " + srcRobotPath; 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, robotName); 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 -cp " + battleRunnerPath + ";" + robocodeCoreJar + ";" + robocodeJar + ";" + picocontainerJar + ";" + " BattleRunner Evaluation." + robotName + " " + path; if (showUI) { evaluateCodeInfo.Arguments += " true " + nrOfRounds + " "; } else { evaluateCodeInfo.Arguments += " false " + nrOfRounds + " "; } foreach (var enemy in enemies.CheckedItems) evaluateCodeInfo.Arguments += enemy.Value + " "; 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, robotName); throw new Exception("Error running Robocode: " + evaluateCode.StandardError.ReadToEnd()); } string scoreString = ""; double evaluation = -1.0; try { scoreString = evaluateCode.StandardOutput.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Last(); evaluation = Double.Parse(scoreString, CultureInfo.InvariantCulture); } catch (Exception ex) { throw new Exception("Error parsing score string: " + ex.ToString()); } finally { DeleteRobotFiles(path, robotName); } 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 { } } private static string GetFileName(string path, string pattern) { string fileName = string.Empty; try { fileName = Directory.GetFiles(path, pattern).First(); } catch { throw new Exception("Error finding required RoboCode files."); } return fileName; } private static string GenerateRobotName() { // Robocode class names are 32 char max and // Java class names have to start with a letter string outputname = Guid.NewGuid().ToString(); outputname = outputname.Remove(8, 1); outputname = outputname.Remove(12, 1); outputname = outputname.Remove(16, 1); outputname = outputname.Remove(20, 1); outputname = outputname.Remove(0, 1); outputname = outputname.Insert(0, "R"); return outputname; } public static string InterpretProgramTree(ISymbolicExpressionTreeNode node, string robotName) { var tankNode = node; while (!(tankNode.Symbol is Tank)) tankNode = tankNode.GetSubtree(0); string result = ((CodeNode)tankNode.Symbol).Interpret(tankNode, tankNode.Subtrees); result = result.Replace("class output", "class " + robotName); return result; } } }