#region License Information /* HeuristicLab * Copyright (C) 2002-2015 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.Encodings.SymbolicExpressionTreeEncoding; namespace HeuristicLab.Problems.GeneticProgramming.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); // compile java source to class file 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; using (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 var robotsDbFileName = Path.Combine(path, "robots", "robot.database"); if (File.Exists(robotsDbFileName)) File.Delete(robotsDbFileName); ProcessStartInfo evaluateCodeInfo = new ProcessStartInfo(); // execute a battle with numberOfRounds against a number of enemies // TODO: seems there is a bug when selecting multiple enemies evaluateCodeInfo.FileName = "cmd.exe"; var classpath = string.Join(";", new[] { battleRunnerPath, robocodeCoreJar, robocodeJar, picocontainerJar }); var enemyRobotNames = string.Join(" ", enemies.CheckedItems.Select(i => i.Value)); evaluateCodeInfo.Arguments = string.Format("/C java -cp {0} BattleRunner Evaluation.{1} {2} {3} {4} {5}", classpath, robotName, path, showUI, nrOfRounds, enemyRobotNames); evaluateCodeInfo.RedirectStandardOutput = true; evaluateCodeInfo.RedirectStandardError = true; evaluateCodeInfo.UseShellExecute = false; evaluateCodeInfo.CreateNoWindow = true; double evaluation; using (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() + Environment.NewLine + evaluateCode.StandardOutput.ReadToEnd()); } try { string 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); } finally { DeleteRobotFiles(path, robotName); } } return evaluation; } private static void DeleteRobotFiles(string path, string outputname) { File.Delete(path + @"\robots\Evaluation\" + outputname + ".java"); File.Delete(path + @"\robots\Evaluation\" + outputname + ".class"); } 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; } } }