source: branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/BestSolutionAnalyzer.cs @ 9947

Last change on this file since 9947 was 9947, checked in by jkarder, 7 years ago

#2069:

  • modified the RobocodeProblem to extract all robots in robocode directory
  • modified the battle runner to take a number of enemies that should be battled
File size: 5.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.Robocode {
32  [StorableClass]
33  [Item("Best Tank Program Analyzer",
34        "Analyzer that stores the best tank program.")]
35  public class BestSolutionAnalyzer : SingleSuccessorOperator, ISymbolicExpressionTreeAnalyzer {
36    private const string QualityParameterName = "Quality";
37    private const string SymbolicExpressionTreeParameterName = "TankProgram";
38    private const string BestSolutionParameterName = "Best solution";
39    private const string ResultsParameterName = "Results";
40    private const string RobocodePathParamaterName = "RobocodePath";
41    private const string NrOfRoundsParameterName = "NrOfRounds";
42    private const string EnemiesParameterName = "Enemies";
43
44    public bool EnabledByDefault {
45      get { return true; }
46    }
47
48    #region Parameters
49    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
50      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
51    }
52    public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
53      get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
54    }
55    public ILookupParameter<Solution> BestSolutionParameter {
56      get { return (ILookupParameter<Solution>)Parameters[BestSolutionParameterName]; }
57    }
58    public ILookupParameter<ResultCollection> ResultParameter {
59      get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
60    }
61    public ILookupParameter<DirectoryValue> RobocodePathParameter {
62      get { return (ILookupParameter<DirectoryValue>)Parameters[RobocodePathParamaterName]; }
63    }
64    public ILookupParameter<IntValue> NrOfRoundsParameter {
65      get { return (ILookupParameter<IntValue>)Parameters[NrOfRoundsParameterName]; }
66    }
67    public ILookupParameter<ICheckedItemList<StringValue>> EnemiesParameter {
68      get { return (ILookupParameter<ICheckedItemList<StringValue>>)Parameters[EnemiesParameterName]; }
69    }
70    #endregion
71
72    [StorableConstructor]
73    protected BestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
74    protected BestSolutionAnalyzer(BestSolutionAnalyzer original, Cloner cloner)
75      : base(original, cloner) { }
76
77    public BestSolutionAnalyzer() {
78      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(QualityParameterName, "The solution quality of the tank program."));
79      Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The tank program to evaluate represented as symbolic expression tree."));
80      Parameters.Add(new LookupParameter<Solution>(BestSolutionParameterName, "The best tank program."));
81      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The result collection of the algorithm."));
82      Parameters.Add(new LookupParameter<DirectoryValue>(RobocodePathParamaterName, "Path of the Robocode installation."));
83      Parameters.Add(new LookupParameter<IntValue>(NrOfRoundsParameterName, "Nr. of Rounds a Robot has to fight against each opponent."));
84      Parameters.Add(new LookupParameter<ICheckedItemList<StringValue>>(EnemiesParameterName, "The enemies that should be battled."));
85    }
86
87    public override IOperation Apply() {
88      // get an array of all trees
89      // and an array of all qualities
90      var trees = SymbolicExpressionTreeParameter.ActualValue;
91      var qualities = QualityParameter.ActualValue;
92
93      // find the tree with the best quality
94      double maxQuality = double.NegativeInfinity;
95      ISymbolicExpressionTree bestTree = null;
96      for (int i = 0; i < qualities.Length; i++) {
97        if (qualities[i].Value > maxQuality) {
98          maxQuality = qualities[i].Value;
99          bestTree = trees[i];
100        }
101      }
102
103      // create a solution instance
104      var bestSolution = new Solution(bestTree, RobocodePathParameter.ActualValue.Value, NrOfRoundsParameter.ActualValue.Value, EnemiesParameter.ActualValue);
105      // store the new solution in the best solution parameter
106      BestSolutionParameter.ActualValue = bestSolution;
107
108      // also add the best solution as a result to the result collection
109      // or alternatively update the existing result
110      var resultCollection = ResultParameter.ActualValue;
111      if (!resultCollection.ContainsKey(BestSolutionParameterName)) {
112        resultCollection.Add(new Result(BestSolutionParameterName, "The best tank program", bestSolution));
113      } else {
114        resultCollection[BestSolutionParameterName].Value = bestSolution;
115      }
116
117      return base.Apply();
118    }
119
120    public override IDeepCloneable Clone(Cloner cloner) {
121      return new BestSolutionAnalyzer(this, cloner);
122    }
123  }
124}
Note: See TracBrowser for help on using the repository browser.