Changeset 9880


Ignore:
Timestamp:
08/13/13 12:11:18 (9 years ago)
Author:
ascheibe
Message:

#2069 removed dead code

Location:
branches/Robocode.TrunkInt
Files:
5 edited

Legend:

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

    r9790 r9880  
    4242      : base() {
    4343      InitializeComponent();
    44       this.Path = "F:/robocode/";
     44      this.Path = "c:/robocode/";
    4545      this.programCode.Text = "";
    4646    }
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/BestSolutionAnalyzer.cs

    r9790 r9880  
    2020#endregion
    2121
    22 
    2322using HeuristicLab.Common;
    2423using HeuristicLab.Core;
     
    3231namespace HeuristicLab.Problems.Robocode {
    3332  [StorableClass]
    34   [Item("Best Tank program Analyzer",
     33  [Item("Best Tank Program Analyzer",
    3534        "Analyzer that stores the best tank program.")]
    36   public class BestSolutionAnalyzer : SingleSuccessorOperator,
    37     ISymbolicExpressionTreeAnalyzer {
    38     #region parameter names
     35  public class BestSolutionAnalyzer : SingleSuccessorOperator, ISymbolicExpressionTreeAnalyzer {
    3936    private const string QualityParameterName = "Quality";
    40     private const string SymbolicExpressionTreeParameterName =
    41       "TankProgram";
    42     private const string MovesParameterName = "Moves";
    43     private const string ShotsParameterName = "Shots";
     37    private const string SymbolicExpressionTreeParameterName = "TankProgram";
    4438    private const string BestSolutionParameterName = "Best solution";
    4539    private const string ResultsParameterName = "Results";
    4640    private const string RobocodePathParamaterName = "Path";
    47     private const string CoevolutionParameterName = "Coevolution";
    48     #endregion
    4941
     42    public bool EnabledByDefault {
     43      get { return true; }
     44    }
    5045
    51     #region parameters
     46    #region Parameters
    5247    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
    53       get {
    54         return (IScopeTreeLookupParameter<DoubleValue>)
    55                   Parameters[QualityParameterName];
    56       }
     48      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
    5749    }
    5850    public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
    59       get {
    60         return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)
    61                   Parameters[SymbolicExpressionTreeParameterName];
    62       }
     51      get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
    6352    }
    64     //public ILookupParameter<IntValue> MovesParameter
    65     //{
    66     //    get
    67     //    {
    68     //        return (ILookupParameter<IntValue>)
    69     //                  Parameters[MovesParameterName];
    70     //    }
    71     //}
    72     //public ILookupParameter<IntValue> ShotsParameter
    73     //{
    74     //    get
    75     //    {
    76     //        return (ILookupParameter<IntValue>)
    77     //                  Parameters[ShotsParameterName];
    78     //    }
    79     //}
    8053    public ILookupParameter<Solution> BestSolutionParameter {
    81       get {
    82         return (ILookupParameter<Solution>)
    83                   Parameters[BestSolutionParameterName];
    84       }
     54      get { return (ILookupParameter<Solution>)Parameters[BestSolutionParameterName]; }
    8555    }
    8656    public ILookupParameter<ResultCollection> ResultParameter {
    87       get {
    88         return (ILookupParameter<ResultCollection>)
    89                   Parameters[ResultsParameterName];
    90       }
     57      get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
    9158    }
    9259    public ILookupParameter<StringValue> RobocodePathParameter {
    93       get {
    94         return (ILookupParameter<StringValue>)
    95             Parameters[RobocodePathParamaterName];
    96       }
    97     }
    98     public ILookupParameter<BoolValue> CoevolutionParameter {
    99       get {
    100         return (ILookupParameter<BoolValue>)
    101             Parameters[CoevolutionParameterName];
    102       }
     60      get { return (ILookupParameter<StringValue>)Parameters[RobocodePathParamaterName]; }
    10361    }
    10462    #endregion
     
    10664    [StorableConstructor]
    10765    protected BestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
    108     protected BestSolutionAnalyzer(BestSolutionAnalyzer original,
    109                                    Cloner cloner)
    110       : base(original, cloner) {
    111     }
     66    protected BestSolutionAnalyzer(BestSolutionAnalyzer original, Cloner cloner)
     67      : base(original, cloner) { }
    11268
    11369    public BestSolutionAnalyzer() {
    114       Parameters.Add(
    115         new ScopeTreeLookupParameter<DoubleValue>(
    116           QualityParameterName,
    117           "The solution quality of the tank program."));
    118       Parameters.Add(
    119         new ScopeTreeLookupParameter<ISymbolicExpressionTree>(
    120           SymbolicExpressionTreeParameterName,
    121           "The tank program to evaluate represented " +
    122           "as symbolic expression tree."));
    123       Parameters.Add(
    124         new LookupParameter<Solution>(
    125           BestSolutionParameterName, "The best tank program."));
    126       //Parameters.Add(
    127       //  new LookupParameter<IntValue>(
    128       //    MovesParameterName, "The number of moves made."));
    129       //Parameters.Add(
    130       //  new LookupParameter<IntValue>(
    131       //    ShotsParameterName, "The shots made."));
    132       Parameters.Add(
    133         new LookupParameter<ResultCollection>(
    134           ResultsParameterName, "The result collection of the algorithm."));
    135       Parameters.Add(
    136         new LookupParameter<StringValue>(
    137           RobocodePathParamaterName,
    138           "Path of the Robocode installation."));
    139       Parameters.Add(
    140           new LookupParameter<BoolValue>(
    141               CoevolutionParameterName,
    142               "Use Coevolution"));
     70      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(QualityParameterName, "The solution quality of the tank program."));
     71      Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The tank program to evaluate represented as symbolic expression tree."));
     72      Parameters.Add(new LookupParameter<Solution>(BestSolutionParameterName, "The best tank program."));
     73      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The result collection of the algorithm."));
     74      Parameters.Add(new LookupParameter<StringValue>(RobocodePathParamaterName, "Path of the Robocode installation."));
    14375    }
    14476
     
    15991      }
    16092
    161       var coevolution = CoevolutionParameter.ActualValue.Value;
    162       double actualQuality = 0;
    163 
    164       if (coevolution)
    165         actualQuality = Interpreter.EvaluateTankProgram(bestTree, null, RobocodePathParameter.ActualValue.Value);
    166 
    16793      // create a solution instance
    168       //int shots = ShotsParameter.ActualValue.Value;
    169       //int moves = MovesParameter.ActualValue.Value;
    170       var bestSolution = new Solution(bestTree, RobocodePathParameter.ActualValue.Value);//, moves, shots);
     94      var bestSolution = new Solution(bestTree, RobocodePathParameter.ActualValue.Value);
    17195      // store the new solution in the best solution parameter
    17296      BestSolutionParameter.ActualValue = bestSolution;
     
    176100      var resultCollection = ResultParameter.ActualValue;
    177101      if (!resultCollection.ContainsKey(BestSolutionParameterName)) {
    178         resultCollection.Add(
    179           new Result(BestSolutionParameterName,
    180                      "The best tank program", bestSolution));
    181         //if(coevolution)
    182         //    resultCollection.Add(
    183         //        new Result("Actual Quality",
    184         //            "The actual quality of the best program", new DoubleValue(actualQuality)));
     102        resultCollection.Add(new Result(BestSolutionParameterName, "The best tank program", bestSolution));
    185103      } else {
    186104        resultCollection[BestSolutionParameterName].Value = bestSolution;
    187105      }
    188106
    189       if (coevolution) {
    190         if (!resultCollection.ContainsKey("Actual Quality")) {
    191           resultCollection.Add(
    192               new Result("Actual Quality",
    193                   "The actual quality of the best program", new DoubleValue(actualQuality)));
    194         } else {
    195           resultCollection["Actual Quality"].Value = new DoubleValue(actualQuality);
    196         }
    197       }
    198 
    199       // important return base.Apply() to make sure the
    200       // next operator is queued for execution
    201107      return base.Apply();
    202108    }
     
    205111      return new BestSolutionAnalyzer(this, cloner);
    206112    }
    207 
    208     // override this property to indicate that this analyzer
    209     // should be enabled by default in the algorithm
    210     public bool EnabledByDefault {
    211       get { return true; }
    212     }
    213113  }
    214114}
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/Interpreter.cs

    r9879 r9880  
    2525using System.Linq;
    2626using System.Text;
    27 using System.Xml;
    28 using HeuristicLab.Core;
    2927using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    3028
     
    3230  public class Interpreter {
    3331
    34     private struct TankStats {
    35       public int moves, shots;
    36       public TankStats(int moves, int shots) {
    37         this.moves = moves;
    38         this.shots = shots;
    39       }
    40     }
    4132    static string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    4233
    43     public static string BuildTankProgram(ISymbolicExpressionTree tree) {
    44       XmlDocument solutionCandidateRoot = new XmlDocument();
    45       solutionCandidateRoot.Load("../tank.xml");
    46 
    47       TankStats tankStats = EvaluateTankProgram(tree.Root, solutionCandidateRoot.FirstChild, null);
    48       return solutionCandidateRoot.InnerText;
    49     }
    50 
    51     public static double EvaluateTankProgram(ISymbolicExpressionTree tree, XmlNode solutionCandidateRoot, string path) {
     34    public static double EvaluateTankProgram(ISymbolicExpressionTree tree, string path) {
    5235      //TankStats tankStats = EvaluateTankProgram(tree.Root, solutionCandidateRoot, null);
    5336      //Evaluator.tankStatLog += "\r\n" + tankStats.shots + " / " + tankStats.moves;
     
    153136      evaluation += rand.NextDouble() / 100;
    154137
    155       /*Evaluator.tankStatLog = "=====================================================\r\n";
    156       Evaluator.tankStatLog += "START OF TREE - Score: " + evaluation + " (" + tankStats.shots + "/" + tankStats.moves + ")" + "\r\n" + " Compilation Output: " + cmdOutput + "\r\n";
    157       Evaluator.tankStatLog += "=====================================================\r\n";
    158 
    159       Evaluator.tankStatLog += solutionCandidateRoot.InnerText + "\r\n";
    160 
    161       Evaluator.tankStatLog += "=====================================================\r\n";
    162       Evaluator.tankStatLog += "END OF TREE\r\n";
    163       Evaluator.tankStatLog += "=====================================================\r\n";
    164 
    165       File.AppendAllText("../TankLog.txt", Evaluator.tankStatLog);*/
    166138      return evaluation;
    167     }
    168 
    169     public static double EvaluateTankProgram(ISymbolicExpressionTree tree, ScopeList coevolutionIndividuals, XmlNode solutionCandidateRoot, string path) {
    170       Random random = new Random();
    171       string formattedPath = path.Replace("/", "\\");
    172 
    173       // Interpret and compile main tree
    174       //XmlNode mainTreeNode = solutionCandidateRoot.Clone();
    175       //EvaluateTankProgram(tree.Root, mainTreeNode, null);
    176       string interpretedMainTree = InterpretProgramTree(tree.Root);
    177       string mainTreeName = writeTreeToFile(interpretedMainTree, path, random);
    178       compileCodefile(/*mainTreeName,*/ mainTreeName, formattedPath);
    179 
    180       // Interpret and compile coevolutionIndividuals
    181       String[] coevolutionIndividualsNames = new String[coevolutionIndividuals.Count];
    182       for (int i = 0; i < coevolutionIndividuals.Count; i++) {
    183         //XmlNode individualTreeNode = solutionCandidateRoot.Clone();
    184         //EvaluateTankProgram(((ISymbolicExpressionTree)coevolutionIndividuals[i].Variables.ToArray()[0].Value).Root, individualTreeNode, null);
    185         string interpretedIndividual = InterpretProgramTree(((ISymbolicExpressionTree)coevolutionIndividuals[i].Variables.ToArray()[0].Value).Root);
    186         coevolutionIndividualsNames[i] = writeTreeToFile(/*individualTreeNode,*/ interpretedIndividual, path, random);
    187         compileCodefile(coevolutionIndividualsNames[i], formattedPath);
    188       }
    189 
    190       // Force Robocode to update its robot database by deleting the existing one
    191       try { File.Delete(path + "/robots/robot.database"); }
    192       catch (Exception e) { }
    193 
    194       // Run evaluator
    195       double evaluation = runEvaluator(mainTreeName, coevolutionIndividualsNames, formattedPath);
    196       try {
    197         File.Delete(path + "/robots/Evaluation/" + mainTreeName + ".java");
    198         File.Delete(path + "/robots/Evaluation/" + mainTreeName + ".class");
    199         foreach (string s in coevolutionIndividualsNames) {
    200           File.Delete(path + "/robots/Evaluation/" + s + ".java");
    201           File.Delete(path + "/robots/Evaluation/" + s + ".class");
    202         }
    203       }
    204       catch (Exception e) { }
    205       Random rand = new Random();
    206       evaluation += rand.NextDouble() / 100;
    207 
    208       return evaluation;
    209     }
    210 
    211     private static string writeTreeToFile(/*XmlNode node,*/ string interpretedTree, string path, Random random) {
    212       string outputName = "";
    213       string outputString = "";
    214       bool noError = true;
    215       do {
    216         try {
    217           outputName = new string(Enumerable.Repeat(chars, 8).Select(s => s[random.Next(s.Length)]).ToArray());
    218           outputString = /*node.InnerText*/interpretedTree.Replace("class output", "class " + outputName);
    219           File.WriteAllText(path + "/robots/Evaluation/" + outputName + ".java", outputString, System.Text.Encoding.Default);
    220           noError = true;
    221         }
    222         catch (Exception e) { noError = false; }
    223       } while (!noError);
    224 
    225       return outputName;
    226     }
    227 
    228     private static bool compileCodefile(string filename, string formattedPath) {
    229       ProcessStartInfo javaCompileInfo = new ProcessStartInfo();
    230       javaCompileInfo.FileName = "cmd.exe";
    231       javaCompileInfo.Arguments = "/C javac -cp " + formattedPath + "\\libs\\robocode.jar " + formattedPath + "\\robots\\Evaluation\\" + filename + ".java";
    232       javaCompileInfo.RedirectStandardOutput = true;
    233       javaCompileInfo.RedirectStandardError = true;
    234       javaCompileInfo.UseShellExecute = false;
    235       javaCompileInfo.CreateNoWindow = true;
    236 
    237       Process javaCompile = new Process();
    238       javaCompile.StartInfo = javaCompileInfo;
    239       javaCompile.Start();
    240 
    241       string cmdOutput = javaCompile.StandardOutput.ReadToEnd();
    242       cmdOutput += javaCompile.StandardError.ReadToEnd();
    243 
    244       javaCompile.WaitForExit();
    245 
    246       if (cmdOutput.Equals(""))
    247         return true;
    248       return false;
    249     }
    250 
    251     private static double runEvaluator(string mainTreeFilename, string[] coevolutionIndividualsNames, string formattedPath) {
    252       ProcessStartInfo evaluateCodeInfo = new ProcessStartInfo();
    253       evaluateCodeInfo.FileName = "cmd.exe";
    254       //javaCompileInfo.Arguments = "/C javac -cp C:\\robocode\\libs\\robocode.jar \"" + path + "\\Spaced Up\\output.java\"";
    255       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." + mainTreeFilename + "* ";
    256       if (coevolutionIndividualsNames != null) {
    257         foreach (string s in coevolutionIndividualsNames)
    258           evaluateCodeInfo.Arguments += "Evaluation." + s + "* ";
    259       } else
    260         evaluateCodeInfo.Arguments += "false";
    261       //Console.WriteLine(javaCompileInfo.Arguments);
    262       evaluateCodeInfo.RedirectStandardOutput = true;
    263       evaluateCodeInfo.RedirectStandardError = true;
    264       evaluateCodeInfo.UseShellExecute = false;
    265       evaluateCodeInfo.CreateNoWindow = true;
    266 
    267       Process evaluateCode = new Process();
    268       evaluateCode.StartInfo = evaluateCodeInfo;
    269       evaluateCode.Start();
    270 
    271       StringBuilder q = new StringBuilder();
    272       string scoreString = "";
    273       while (!evaluateCode.HasExited) {
    274         scoreString = evaluateCode.StandardOutput.ReadToEnd().Split(
    275         new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Last();
    276         q.Append("Output: " + scoreString + "\r\n");
    277         q.Append("Error: " + evaluateCode.StandardError.ReadToEnd() + "\r\n");
    278       }
    279       string cmdOutput = q.ToString();
    280       try {
    281         double evaluation = Double.Parse(scoreString);
    282         return evaluation;
    283       }
    284       catch (Exception e) { }
    285 
    286       return -900;
    287     }
    288 
    289     private static TankStats EvaluateTankProgram(ISymbolicExpressionTreeNode node, XmlNode docNode, string method) {
    290       TankStats tankStats = new TankStats(0, 0);
    291 
    292       //string log = "Doing node: \r\n";
    293       /*if (node.Symbol is ProgramRootSymbol ||
    294           node.Symbol is StartSymbol)
    295           tankStats = EvaluateTankProgram(node.GetSubtree(0), ref docNode, null);
    296       else*/
    297       if (node.Symbol is ShotPower)
    298         docNode[method]["code"].InnerText += " " + ((ShotPowerTreeNode)node).Value + " ";
    299       else if (node.Symbol is LogicalValue)
    300         docNode[method]["code"].InnerText += " " + ((BooleanTreeNode)node).Value.ToString().ToLower() + " ";
    301       else if (node.Symbol is Block) {
    302         docNode[method]["code"].InnerText += "{\r\n";
    303         foreach (ISymbolicExpressionTreeNode n in node.Subtrees)
    304           EvaluateTankProgram(n, docNode, method);
    305         docNode[method]["code"].InnerText += "}\r\n";
    306       } else if (node.Symbol is IfStatement) {
    307         docNode[method]["code"].InnerText += "if(";
    308         EvaluateTankProgram(node.GetSubtree(0), docNode, method);
    309         docNode[method]["code"].InnerText += ")\r\n";
    310         EvaluateTankProgram(node.GetSubtree(1), docNode, method);
    311         if (node.SubtreeCount == 3)
    312           EvaluateTankProgram(node.GetSubtree(2), docNode, method);
    313       } else if (node.Symbol is ElseStatement) {
    314         docNode[method]["code"].InnerText += "else\r\n";
    315         if (node.SubtreeCount == 1)
    316           EvaluateTankProgram(node.GetSubtree(0), docNode, method);
    317       } else if (node.Symbol is WhileLoop) {
    318         docNode[method]["code"].InnerText += "While(";
    319         EvaluateTankProgram(node.GetSubtree(0), docNode, method);
    320         docNode[method]["code"].InnerText += ")\r\n";
    321         EvaluateTankProgram(node.GetSubtree(1), docNode, method);
    322       } else if (node.Symbol is Ahead) {
    323         docNode[method]["code"].InnerText += "setAhead(";
    324         EvaluateTankProgram(node.GetSubtree(0), docNode, method);
    325         docNode[method]["code"].InnerText += ");\r\n";
    326         tankStats.moves = 1;
    327       } else if (node.Symbol is Back) {
    328         docNode[method]["code"].InnerText += "setBack(";
    329         EvaluateTankProgram(node.GetSubtree(0), docNode, method);
    330         docNode[method]["code"].InnerText += ");\r\n";
    331         tankStats.moves = 1;
    332       } else if (node.Symbol is TurnGunLeft) {
    333         docNode[method]["code"].InnerText += "setTurnGunLeft(";
    334         EvaluateTankProgram(node.GetSubtree(0), docNode, method);
    335         docNode[method]["code"].InnerText += ");\r\n";
    336         tankStats.moves = 1;
    337       } else if (node.Symbol is TurnGunRight) {
    338         docNode[method]["code"].InnerText += "setTurnGunRight(";
    339         EvaluateTankProgram(node.GetSubtree(0), docNode, method);
    340         docNode[method]["code"].InnerText += ");\r\n";
    341         tankStats.moves = 1;
    342       } else if (node.Symbol is TurnLeft) {
    343         docNode[method]["code"].InnerText += "setTurnLeft(";
    344         EvaluateTankProgram(node.GetSubtree(0), docNode, method);
    345         docNode[method]["code"].InnerText += ");\r\n";
    346         tankStats.moves = 1;
    347       } else if (node.Symbol is TurnRadarLeft) {
    348         docNode[method]["code"].InnerText += "setTurnRadarLeft(";
    349         EvaluateTankProgram(node.GetSubtree(0), docNode, method);
    350         docNode[method]["code"].InnerText += ");\r\n";
    351         tankStats.moves = 1;
    352       } else if (node.Symbol is TurnRadarRight) {
    353         docNode[method]["code"].InnerText += "setTurnRadarRight(";
    354         EvaluateTankProgram(node.GetSubtree(0), docNode, method);
    355         docNode[method]["code"].InnerText += ");\r\n";
    356         tankStats.moves = 1;
    357       } else if (node.Symbol is TurnRight) {
    358         docNode[method]["code"].InnerText += "setTurnRight(";
    359         EvaluateTankProgram(node.GetSubtree(0), docNode, method);
    360         docNode[method]["code"].InnerText += ");\r\n";
    361         tankStats.moves = 1;
    362       } else if (node.Symbol is Fire) {
    363         docNode[method]["code"].InnerText += "setFire(";
    364         EvaluateTankProgram(node.GetSubtree(0), docNode, method);
    365         docNode[method]["code"].InnerText += ");\r\n";
    366         tankStats.shots = 1;
    367       } else if (node.Symbol is Run ||
    368             node.Symbol is OnBulletHit ||
    369             node.Symbol is OnBulletMissed ||
    370             node.Symbol is OnHitByBullet ||
    371             node.Symbol is OnHitRobot ||
    372             node.Symbol is OnHitWall ||
    373             node.Symbol is OnScannedRobot) {
    374         tankStats = new TankStats();
    375         foreach (SymbolicExpressionTreeNode n in node.Subtrees) {
    376           string methodName = node.Symbol.GetType().ToString().Split('.').Last();
    377           try {
    378             var tempStats = EvaluateTankProgram(n, docNode, methodName);
    379             tankStats.moves += tempStats.moves;
    380             tankStats.shots += tempStats.shots;
    381           }
    382           catch (Exception e) {
    383             //log += "NULL EXCEPTION ON:::::::: -" + methodName + "-\r\n";
    384             //foreach (XmlNode f in docNode[methodName])
    385             //    log += f.Name + "\r\n";
    386           }
    387         }
    388       } else if (node.Symbol is Tank) {
    389         foreach (SymbolicExpressionTreeNode n in node.Subtrees) {
    390           try {
    391             var tempStats = EvaluateTankProgram(n, docNode, method);
    392             tankStats.moves += tempStats.moves;
    393             tankStats.shots += tempStats.shots;
    394           }
    395           catch (Exception e) { }
    396         }
    397       } else
    398         tankStats = EvaluateTankProgram(node.GetSubtree(0), docNode, null);
    399 
    400       //log += tankStats.shots + " / " + tankStats.moves + "\r\n";
    401       //File.AppendAllText("../TankLog.txt", log);
    402 
    403       return tankStats;
    404139    }
    405140
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/RobocodeEvaluator.cs

    r9879 r9880  
    2020#endregion
    2121
    22 
    23 using System;
    2422using System.Threading;
    25 using System.Xml;
    2623using HeuristicLab.Common;
    2724using HeuristicLab.Core;
     
    3633  [StorableClass]
    3734  [Item("Robocode Evaluator", "Evaluator for the Robocode GP problem.")]
    38   public class RobocodeEvaluator : SingleSuccessorOperator,
    39     ISingleObjectiveEvaluator {
    40     #region parameter names
     35  public class RobocodeEvaluator : SingleSuccessorOperator, ISingleObjectiveEvaluator {
    4136    private const string QualityParameterName = "Quality";
    4237    private const string TankProgramParameterName = "TankProgram";
    4338    private const string RobocodePathParamaterName = "Path";
    44     private const string CoevolutionParameterName = "Coevolution";
    4539
     40    //TODO: this should be removed
     41    public static SemaphoreSlim semaphore = new SemaphoreSlim(10);
    4642
    47     private static string solutionTemplatePath = "";
    48     public static string SolutionTemplate {
    49       get { return solutionTemplatePath; }
    50       set {
    51         solutionTemplatePath = value;
    52       }
    53     }
    54 
    55     public static SemaphoreSlim semaphore = new SemaphoreSlim(10);
    56     #endregion
    57 
    58 
    59     #region parameters
     43    #region Parameters
    6044    public ILookupParameter<DoubleValue> QualityParameter {
    61       get {
    62         return (ILookupParameter<DoubleValue>)
    63                  Parameters[QualityParameterName];
    64       }
     45      get { return (ILookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
    6546    }
    6647    public ILookupParameter<ISymbolicExpressionTree> TankProgramParameter {
    67       get {
    68         return (ILookupParameter<ISymbolicExpressionTree>)
    69                  Parameters[TankProgramParameterName];
    70       }
     48      get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[TankProgramParameterName]; }
    7149    }
    7250    public ILookupParameter<StringValue> RobocodePathParameter {
    73       get {
    74         return (ILookupParameter<StringValue>)
    75             Parameters[RobocodePathParamaterName];
    76       }
    77     }
    78     public ILookupParameter<BoolValue> CoevolutionParameter {
    79       get {
    80         return (ILookupParameter<BoolValue>)
    81             Parameters[CoevolutionParameterName];
    82       }
     51      get { return (ILookupParameter<StringValue>)Parameters[RobocodePathParamaterName]; }
    8352    }
    8453    #endregion
    85 
    8654
    8755    [StorableConstructor]
    8856    protected RobocodeEvaluator(bool deserializing) : base(deserializing) { }
    8957    protected RobocodeEvaluator(RobocodeEvaluator original, Cloner cloner)
    90       : base(original, cloner) {
     58      : base(original, cloner) { }
     59
     60    public RobocodeEvaluator() {
     61      Parameters.Add(new LookupParameter<DoubleValue>(QualityParameterName, "The solution quality of the Robocode tank program."));
     62      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(TankProgramParameterName, "The Robocode tank program to evaluate represented as a symbolic expression tree."));
     63      Parameters.Add(new LookupParameter<StringValue>(RobocodePathParamaterName, "Path of the Robocode installation."));
    9164    }
    9265
    93     // default constructor for the evaluator
    94     public RobocodeEvaluator() {
    95       Parameters.Add(
    96         new LookupParameter<DoubleValue>(
    97           QualityParameterName,
    98           "The solution quality of the Robocode tank program."));
    99       Parameters.Add(
    100         new LookupParameter<ISymbolicExpressionTree>(
    101           TankProgramParameterName,
    102           "The Robocode tank program to evaluate represented as a " +
    103           "symbolic expression tree."));
    104       Parameters.Add(
    105         new LookupParameter<StringValue>(
    106           RobocodePathParamaterName,
    107           "Path of the Robocode installation."));
    108       Parameters.Add(
    109           new LookupParameter<BoolValue>(
    110               CoevolutionParameterName,
    111               "Use Coevolution"));
    112 
    113       SolutionTemplate = "../tank.xml";
    114     }
    115 
    116     // override the apply method to change the behaviour of the operator
    117     // here we take trees as inputs and calculate qualities
    118     // (fitness evaluation)
    11966    public override IOperation Apply() {
    12067      semaphore.Wait();
    12168
    12269      ISymbolicExpressionTree tree = TankProgramParameter.ActualValue;
    123       //XmlDocument doc = new XmlDocument();
    124       //doc.Load("../tank.xml");
    125       XmlNode node = null;
    12670      string path = RobocodePathParameter.ActualValue.Value;
    127 
    128       if (CoevolutionParameter.ActualValue.Value) {
    129         var trees = ExecutionContext.Parent.Scope.SubScopes;//SymbolicExpressionTreeParameter.ActualValue;
    130         if (trees.Count == 2) {
    131           trees = trees[0].SubScopes;
    132           //treeCount = trees[0].SubScopes.Count;
    133           //int selTreeCount = trees[1].SubScopes.Count;
    134         }
    135 
    136         Random random = new Random();
    137         ScopeList chosenScopes = new ScopeList(5);
    138         for (int i = 0; i < 5; i++) {
    139           var newScope = trees.ToArray()[random.Next(0, trees.Count - 1)];
    140           chosenScopes.Add(newScope);
    141         }
    142 
    143         //if (evaluations % 99 == 0)
    144         //    evaluations = evaluations;
    145 
    146         QualityParameter.ActualValue = new DoubleValue(Interpreter.EvaluateTankProgram(tree, chosenScopes, node, path));
    147       } else
    148         QualityParameter.ActualValue = new DoubleValue(Interpreter.EvaluateTankProgram(tree, node, path));
     71      QualityParameter.ActualValue = new DoubleValue(Interpreter.EvaluateTankProgram(tree, path));
    14972
    15073      semaphore.Release();
    15174
    152       // important: return base.Apply() to make sure that the
    153       // next operator is queued for execution
    15475      return base.Apply();
    15576    }
     
    15879      return new RobocodeEvaluator(this, cloner);
    15980    }
    160 
    161     public bool EnabledByDefault {
    162       get { return true; }
    163     }
    16481  }
    16582}
  • branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/RobocodeProblem.cs

    r9879 r9880  
    4343    private const string TankGrammarParameterName = "Grammar";
    4444    private const string RobocodePathParamaterName = "Path";
    45     private const string CoevolutionParameterName = "Coevolution";
    4645    #endregion
    4746
    4847    #region Parameters
    4948    public IFixedValueParameter<IntValue> MaxTankProgramLengthParameter {
    50       get {
    51         return (IFixedValueParameter<IntValue>)
    52           Parameters[MaxTankProgramLengthParameterName];
    53       }
     49      get { return (IFixedValueParameter<IntValue>)Parameters[MaxTankProgramLengthParameterName]; }
    5450    }
    5551    public IFixedValueParameter<IntValue> MaxTankProgramDepthParameter {
    56       get {
    57         return (IFixedValueParameter<IntValue>)
    58           Parameters[MaxTankProgramDepthParameterName];
    59       }
     52      get { return (IFixedValueParameter<IntValue>)Parameters[MaxTankProgramDepthParameterName]; }
    6053    }
    6154    public IValueParameter<Grammar> GrammarParameter {
    62       get {
    63         return (IValueParameter<Grammar>)
    64           Parameters[TankGrammarParameterName];
    65       }
     55      get { return (IValueParameter<Grammar>)Parameters[TankGrammarParameterName]; }
    6656    }
    6757    public IFixedValueParameter<StringValue> RobocodePathParameter {
    68       get {
    69         return (IFixedValueParameter<StringValue>)
    70             Parameters[RobocodePathParamaterName];
    71       }
    72     }
    73     public IFixedValueParameter<BoolValue> CoevolutionParameter {
    74       get {
    75         return (IFixedValueParameter<BoolValue>)
    76             Parameters[CoevolutionParameterName];
    77       }
     58      get { return (IFixedValueParameter<StringValue>)Parameters[RobocodePathParamaterName]; }
    7859    }
    7960    #endregion
     
    8768    }
    8869
    89     // default constructor for the problem
    90     // also creates the fitness evaluation operator (Evaluator),
    91     // and the tree creation operator (RampedHalfAndHalfTreeCreator)
    9270    public RobocodeProblem()
    9371      : base(new RobocodeEvaluator(), new RampedHalfAndHalfTreeCreator()) {
    94       Parameters.Add(
    95          new FixedValueParameter<IntValue>(MaxTankProgramDepthParameterName,
    96            "Maximal depth of the Robocode tank program.",
    97            new IntValue(6)));
    98       Parameters.Add(new FixedValueParameter<IntValue>(MaxTankProgramLengthParameterName,
    99           "Maximal length of the tank program.", new IntValue(1000)));
    100       Parameters.Add(
    101         new ValueParameter<Grammar>(TankGrammarParameterName,
    102           "Grammar for the tank program.",
    103           new Grammar()));
    104       Parameters.Add(new FixedValueParameter<StringValue>(RobocodePathParamaterName,
    105           "Path of the Robocode installation.",
    106           new StringValue("F:/robocode2")));
    107       Parameters.Add(new FixedValueParameter<BoolValue>(CoevolutionParameterName,
    108           "Use Coevolution", new BoolValue(false)));
     72      Parameters.Add(new FixedValueParameter<IntValue>(MaxTankProgramDepthParameterName, "Maximal depth of the Robocode tank program.", new IntValue(6)));
     73      Parameters.Add(new FixedValueParameter<IntValue>(MaxTankProgramLengthParameterName, "Maximal length of the tank program.", new IntValue(1000)));
     74      Parameters.Add(new ValueParameter<Grammar>(TankGrammarParameterName, "Grammar for the tank program.", new Grammar()));
     75      Parameters.Add(new FixedValueParameter<StringValue>(RobocodePathParamaterName, "Path of the Robocode installation.", new StringValue("C:/robocode")));
    10976
    11077      Maximization.Value = true;
Note: See TracChangeset for help on using the changeset viewer.