Free cookie consent management tool by TermsFeed Policy Generator

Changeset 9881


Ignore:
Timestamp:
08/13/13 15:27:42 (11 years ago)
Author:
ascheibe
Message:

#2069 improved interpreter

Location:
branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3
Files:
2 edited

Legend:

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

    r9880 r9881  
    2424using System.IO;
    2525using System.Linq;
    26 using System.Text;
    2726using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2827
    2928namespace HeuristicLab.Problems.Robocode {
    3029  public class Interpreter {
    31 
    32     static string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    33 
    3430    public static double EvaluateTankProgram(ISymbolicExpressionTree tree, string path) {
    35       //TankStats tankStats = EvaluateTankProgram(tree.Root, solutionCandidateRoot, null);
    36       //Evaluator.tankStatLog += "\r\n" + tankStats.shots + " / " + tankStats.moves;
    3731      string interpretedProgram = InterpretProgramTree(tree.Root);
    38       Random random = new Random();
    3932      string formattedPath = path.Replace("/", "\\");
    4033      string outputname = "";
    41       string output = "";
    42       bool noError = true;
    43       do {
    44         try {
    45           outputname = new string(Enumerable.Repeat(chars, 8).Select(s => s[random.Next(s.Length)]).ToArray());
    46           output = /*solutionCandidateRoot.InnerText*/interpretedProgram.Replace("class output", "class " + outputname);
    47           File.WriteAllText(path + "/robots/Evaluation/" + outputname + ".java", output, System.Text.Encoding.Default);
    48           noError = true;
    49         }
    50         catch (Exception e) { noError = false; }
    51       } while (!noError);
    5234
    53       //string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
    54       //path.Replace("\\", "\\\\");
    55       //path.Replace(" ", "\\ ");
    56       #region Compile code file
     35      try {
     36        outputname = Guid.NewGuid().ToString();
     37        outputname = outputname.Replace('-', '_');
     38        outputname = "Robocode" + outputname;
     39        string output = interpretedProgram.Replace("class output", "class " + outputname);
     40        File.WriteAllText(path + "/robots/Evaluation/" + outputname + ".java", output, System.Text.Encoding.Default);
     41      }
     42      catch (Exception ex) {
     43        throw;
     44      }
    5745
    5846      ProcessStartInfo javaCompileInfo = new ProcessStartInfo();
     
    6856      javaCompile.Start();
    6957
    70       /*Evaluator.tankStatLog = "=====================================================\r\n";
    71       Evaluator.tankStatLog += "Tree Compilation Output: \r\n";
    72       Evaluator.tankStatLog += "=====================================================\r\n";*/
    73 
    7458      string cmdOutput = javaCompile.StandardOutput.ReadToEnd();
    7559      cmdOutput += javaCompile.StandardError.ReadToEnd();
    7660
    77       //Evaluator.tankStatLog += cmdOutput;
    78       //Console.WriteLine(cmdOutput);
    79       //Console.ReadLine();
    80 
    81       /*Evaluator.tankStatLog = "=====================================================\r\n";
    82       Evaluator.tankStatLog += "End of Tree Compilation Output: \r\n";
    83       Evaluator.tankStatLog += "=====================================================\r\n";*/
    84 
    8561      javaCompile.WaitForExit();
    8662      if (javaCompile.ExitCode != 0) {
    87         return -1000.0;
     63        DeleteRobotFiles(path, outputname);
     64        throw new Exception("Compile Error: " + cmdOutput);
    8865      }
    89       #endregion
    9066
    91 
     67      //parallel execution of multiple robocode instances can sometimes lead to a damaged robot.database
    9268      try { File.Delete(path + "/robots/robot.database"); }
    93       catch (Exception e) { }
    94 
    95       #region evaluate code
     69      catch { }
    9670
    9771      ProcessStartInfo evaluateCodeInfo = new ProcessStartInfo();
    9872      evaluateCodeInfo.FileName = "cmd.exe";
    99       //javaCompileInfo.Arguments = "/C javac -cp C:\\robocode\\libs\\robocode.jar \"" + path + "\\Spaced Up\\output.java\"";
    10073      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 + "* false";
    101       //Console.WriteLine(javaCompileInfo.Arguments);
    10274      evaluateCodeInfo.RedirectStandardOutput = true;
    10375      evaluateCodeInfo.RedirectStandardError = true;
     
    10880      evaluateCode.StartInfo = evaluateCodeInfo;
    10981      evaluateCode.Start();
    110 
    111       StringBuilder q = new StringBuilder();
    112       string scoreString = "";
    11382      evaluateCode.WaitForExit();
    114 
    115       try {
    116         scoreString = evaluateCode.StandardOutput.ReadToEnd().Split(
    117         new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Last();
    118         q.Append("Output: " + scoreString + "\r\n");
    119         q.Append("Error: " + evaluateCode.StandardError.ReadToEnd() + "\r\n");
    120         cmdOutput += q.ToString();
    121       }
    122       catch {
    123         return -1000.0;
     83      if (evaluateCode.ExitCode != 0) {
     84        DeleteRobotFiles(path, outputname);
     85        throw new Exception("Error running Robocode: " + evaluateCode.StandardError.ReadToEnd());
    12486      }
    12587
    126       #endregion
     88      string scoreString = "";
     89      double evaluation = -900;
     90      try {
     91        scoreString = evaluateCode.StandardOutput.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Last();
     92        evaluation = Double.Parse(scoreString);
     93      }
     94      catch (Exception ex) {
     95        throw new Exception("Error parsing score string: " + ex.ToString());
     96      }
     97      finally {
     98        DeleteRobotFiles(path, outputname);
     99      }
    127100
    128       double evaluation = -900;
     101      return evaluation;
     102    }
     103
     104    private static void DeleteRobotFiles(string path, string outputname) {
    129105      try {
    130106        File.Delete(path + "/robots/Evaluation/" + outputname + ".java");
    131107        File.Delete(path + "/robots/Evaluation/" + outputname + ".class");
    132         evaluation = Double.Parse(scoreString);
    133108      }
    134       catch (Exception e) { evaluation = -900; }
    135       Random rand = new Random();
    136       evaluation += rand.NextDouble() / 100;
    137 
    138       return evaluation;
     109      catch { }
    139110    }
    140111
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/RobocodeEvaluator.cs

    r9880 r9881  
    2020#endregion
    2121
    22 using System.Threading;
    2322using HeuristicLab.Common;
    2423using HeuristicLab.Core;
     
    3736    private const string TankProgramParameterName = "TankProgram";
    3837    private const string RobocodePathParamaterName = "Path";
    39 
    40     //TODO: this should be removed
    41     public static SemaphoreSlim semaphore = new SemaphoreSlim(10);
    4238
    4339    #region Parameters
     
    6561
    6662    public override IOperation Apply() {
    67       semaphore.Wait();
    68 
    6963      ISymbolicExpressionTree tree = TankProgramParameter.ActualValue;
    7064      string path = RobocodePathParameter.ActualValue.Value;
    7165      QualityParameter.ActualValue = new DoubleValue(Interpreter.EvaluateTankProgram(tree, path));
    72 
    73       semaphore.Release();
    7466
    7567      return base.Apply();
Note: See TracChangeset for help on using the changeset viewer.