Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/15/15 16:39:12 (9 years ago)
Author:
gkronber
Message:

#2069: reviewing and minor changes

File:
1 edited

Legend:

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

    r13011 r13013  
    2626using System.Linq;
    2727using System.Reflection;
    28 using HeuristicLab.Core;
    29 using HeuristicLab.Data;
    3028using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    3129
    32 namespace HeuristicLab.Problems.GeneticProgramming.RoboCode {
     30namespace HeuristicLab.Problems.GeneticProgramming.Robocode {
    3331  public static class Interpreter {
    3432    public static double EvaluateTankProgram(ISymbolicExpressionTree tree, string path, EnemyCollection enemies, string robotName = null, bool showUI = false, int nrOfRounds = 3) {
     
    4745      File.WriteAllText(srcRobotPath, interpretedProgram, System.Text.Encoding.Default);
    4846
     47      // compile java source to class file
    4948      ProcessStartInfo javaCompileInfo = new ProcessStartInfo();
    5049      javaCompileInfo.FileName = "cmd.exe";
     
    5554      javaCompileInfo.CreateNoWindow = true;
    5655
    57       Process javaCompile = new Process();
    58       javaCompile.StartInfo = javaCompileInfo;
    59       javaCompile.Start();
     56      using (Process javaCompile = new Process()) {
     57        javaCompile.StartInfo = javaCompileInfo;
     58        javaCompile.Start();
    6059
    61       string cmdOutput = javaCompile.StandardOutput.ReadToEnd();
    62       cmdOutput += javaCompile.StandardError.ReadToEnd();
     60        string cmdOutput = javaCompile.StandardOutput.ReadToEnd();
     61        cmdOutput += javaCompile.StandardError.ReadToEnd();
    6362
    64       javaCompile.WaitForExit();
    65       if (javaCompile.ExitCode != 0) {
    66         DeleteRobotFiles(path, robotName);
    67         throw new Exception("Compile Error: " + cmdOutput);
     63        javaCompile.WaitForExit();
     64        if (javaCompile.ExitCode != 0) {
     65          DeleteRobotFiles(path, robotName);
     66          throw new Exception("Compile Error: " + cmdOutput);
     67        }
    6868      }
    6969
    7070      //parallel execution of multiple robocode instances can sometimes lead to a damaged robot.database
    71       try { File.Delete(path + @"\robots\robot.database"); }
    72       catch { }
     71      var robotsDbFileName = Path.Combine(path, "robots", "robot.database");
     72      if (File.Exists(robotsDbFileName))
     73        File.Delete(robotsDbFileName);
    7374
    7475      ProcessStartInfo evaluateCodeInfo = new ProcessStartInfo();
    7576
     77      // execute a battle with numberOfRounds against a number of enemies
     78      // TODO: seems there is a bug when selecting multiple enemies
    7679      evaluateCodeInfo.FileName = "cmd.exe";
    77       evaluateCodeInfo.Arguments = "/C java -cp " + battleRunnerPath + ";" + robocodeCoreJar + ";" + robocodeJar +
    78                                   ";" + picocontainerJar + ";" + " BattleRunner Evaluation." + robotName + " " + path;
    79 
    80       if (showUI) {
    81         evaluateCodeInfo.Arguments += " true " + nrOfRounds + " ";
    82       } else {
    83         evaluateCodeInfo.Arguments += " false " + nrOfRounds + " ";
    84       }
    85 
    86       foreach (var enemy in enemies.CheckedItems)
    87         evaluateCodeInfo.Arguments += enemy.Value + " ";
     80      var classpath = string.Join(";", new[] { battleRunnerPath, robocodeCoreJar, robocodeJar, picocontainerJar });
     81      var enemyRobotNames = string.Join(" ", enemies.CheckedItems.Select(i => i.Value));
     82      evaluateCodeInfo.Arguments = string.Format("/C java -cp {0} BattleRunner Evaluation.{1} {2} {3} {4} {5}", classpath, robotName, path, showUI, nrOfRounds, enemyRobotNames);
    8883
    8984      evaluateCodeInfo.RedirectStandardOutput = true;
     
    9287      evaluateCodeInfo.CreateNoWindow = true;
    9388
    94       Process evaluateCode = new Process();
    95       evaluateCode.StartInfo = evaluateCodeInfo;
    96       evaluateCode.Start();
    97       evaluateCode.WaitForExit();
     89      double evaluation;
     90      using (Process evaluateCode = new Process()) {
     91        evaluateCode.StartInfo = evaluateCodeInfo;
     92        evaluateCode.Start();
     93        evaluateCode.WaitForExit();
    9894
    99       if (evaluateCode.ExitCode != 0) {
    100         DeleteRobotFiles(path, robotName);
    101         throw new Exception("Error running Robocode: " + evaluateCode.StandardError.ReadToEnd());
    102       }
     95        if (evaluateCode.ExitCode != 0) {
     96          DeleteRobotFiles(path, robotName);
     97          throw new Exception("Error running Robocode: " + evaluateCode.StandardError.ReadToEnd() + Environment.NewLine +
     98                              evaluateCode.StandardOutput.ReadToEnd());
     99        }
    103100
    104       string scoreString = "";
    105       double evaluation = -1.0;
    106       try {
    107         scoreString = evaluateCode.StandardOutput.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Last();
    108         evaluation = Double.Parse(scoreString, CultureInfo.InvariantCulture);
    109       }
    110       catch (Exception ex) {
    111         throw new Exception("Error parsing score string: " + ex.ToString());
    112       }
    113       finally {
    114         DeleteRobotFiles(path, robotName);
     101        try {
     102          string scoreString =
     103            evaluateCode.StandardOutput.ReadToEnd()
     104              .Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
     105              .Last();
     106          evaluation = Double.Parse(scoreString, CultureInfo.InvariantCulture);
     107        }
     108        catch (Exception ex) {
     109          throw new Exception("Error parsing score string: " + ex);
     110        }
     111        finally {
     112          DeleteRobotFiles(path, robotName);
     113        }
    115114      }
    116115
     
    119118
    120119    private static void DeleteRobotFiles(string path, string outputname) {
    121       try {
    122         File.Delete(path + @"\robots\Evaluation\" + outputname + ".java");
    123         File.Delete(path + @"\robots\Evaluation\" + outputname + ".class");
    124       }
    125       catch { }
     120      File.Delete(path + @"\robots\Evaluation\" + outputname + ".java");
     121      File.Delete(path + @"\robots\Evaluation\" + outputname + ".class");
    126122    }
    127123
     
    132128      }
    133129      catch {
    134         throw new Exception("Error finding required RoboCode files.");
     130        throw new Exception("Error finding required Robocode files.");
    135131      }
    136132      return fileName;
Note: See TracChangeset for help on using the changeset viewer.