Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/21/13 14:38:09 (11 years ago)
Author:
ascheibe
Message:

#2069 cleaned up views

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Interpreter.cs

    r9888 r9889  
    2929namespace HeuristicLab.Problems.Robocode {
    3030  public class Interpreter {
    31     public static double EvaluateTankProgram(ISymbolicExpressionTree tree, string path) {
    32       string interpretedProgram = InterpretProgramTree(tree.Root);
     31    public static double EvaluateTankProgram(ISymbolicExpressionTree tree, string path, string robotName = null, bool showUI = false) {
     32      if (robotName == null)
     33        robotName = GenerateRobotName();
     34
     35      string interpretedProgram = InterpretProgramTree(tree.Root, robotName);
    3336      string formattedPath = path.Replace("/", "\\");
    34       string outputname = "";
    3537
    3638      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);
     39        File.WriteAllText(path + "/robots/Evaluation/" + robotName + ".java", interpretedProgram, System.Text.Encoding.Default);
    4740      }
    4841      catch (Exception ex) {
     
    5245      ProcessStartInfo javaCompileInfo = new ProcessStartInfo();
    5346      javaCompileInfo.FileName = "cmd.exe";
    54       javaCompileInfo.Arguments = "/C javac -cp " + formattedPath + "\\libs\\robocode.jar " + formattedPath + "\\robots\\Evaluation\\" + outputname + ".java";
     47      javaCompileInfo.Arguments = "/C javac -cp " + formattedPath + "\\libs\\robocode.jar " +
     48                                  formattedPath + "\\robots\\Evaluation\\" + robotName + ".java";
    5549      javaCompileInfo.RedirectStandardOutput = true;
    5650      javaCompileInfo.RedirectStandardError = true;
     
    6761      javaCompile.WaitForExit();
    6862      if (javaCompile.ExitCode != 0) {
    69         DeleteRobotFiles(path, outputname);
     63        DeleteRobotFiles(path, robotName);
    7064        throw new Exception("Compile Error: " + cmdOutput);
    7165      }
     
    7771      ProcessStartInfo evaluateCodeInfo = new ProcessStartInfo();
    7872      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";
     73      evaluateCodeInfo.Arguments = "/C java -classpath " + formattedPath + "\\libs;" + formattedPath +
     74                                   "\\libs\\robocode.core-1.8.1.0.jar;" + formattedPath + "\\libs\\robocode.jar;" +
     75                                   formattedPath + "\\libs\\picocontainer-2.14.2.jar BattleRunner Evaluation." +
     76                                   robotName + "* " + formattedPath;
     77      if (showUI)
     78        evaluateCodeInfo.Arguments += " true";
     79
    8080      evaluateCodeInfo.RedirectStandardOutput = true;
    8181      evaluateCodeInfo.RedirectStandardError = true;
     
    8787      evaluateCode.Start();
    8888      evaluateCode.WaitForExit();
     89
    8990      if (evaluateCode.ExitCode != 0) {
    90         DeleteRobotFiles(path, outputname);
     91        DeleteRobotFiles(path, robotName);
    9192        throw new Exception("Error running Robocode: " + evaluateCode.StandardError.ReadToEnd());
    9293      }
     
    102103      }
    103104      finally {
    104         DeleteRobotFiles(path, outputname);
     105        DeleteRobotFiles(path, robotName);
    105106      }
    106107
     
    116117    }
    117118
    118     public static string InterpretProgramTree(ISymbolicExpressionTreeNode node) {
     119    private static string GenerateRobotName() {
     120      // Robocode class names are 32 char max and
     121      // Java class names have to start with a letter
     122      string outputname = Guid.NewGuid().ToString();
     123      outputname = outputname.Remove(8, 1);
     124      outputname = outputname.Remove(12, 1);
     125      outputname = outputname.Remove(16, 1);
     126      outputname = outputname.Remove(20, 1);
     127      outputname = outputname.Remove(0, 1);
     128      outputname = outputname.Insert(0, "R");
     129      return outputname;
     130    }
     131
     132    public static string InterpretProgramTree(ISymbolicExpressionTreeNode node, string robotName) {
    119133      var tankNode = node;
    120134      while (!(tankNode.Symbol is Tank))
    121135        tankNode = tankNode.GetSubtree(0);
    122       return ((CodeNode)tankNode.Symbol).Interpret(tankNode, tankNode.Subtrees);
     136
     137      string result = ((CodeNode)tankNode.Symbol).Interpret(tankNode, tankNode.Subtrees);
     138      result = result.Replace("class output", "class " + robotName);
     139      return result;
    123140    }
    124141  }
Note: See TracChangeset for help on using the changeset viewer.