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

Last change on this file since 9926 was 9926, checked in by ascheibe, 7 years ago

#2069

  • renamed path parameter to robocode path
  • made number of rounds configurable
File size: 5.5 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
43    public bool EnabledByDefault {
44      get { return true; }
45    }
46
47    #region Parameters
48    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
49      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
50    }
51    public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
52      get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
53    }
54    public ILookupParameter<Solution> BestSolutionParameter {
55      get { return (ILookupParameter<Solution>)Parameters[BestSolutionParameterName]; }
56    }
57    public ILookupParameter<ResultCollection> ResultParameter {
58      get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
59    }
60    public ILookupParameter<DirectoryValue> RobocodePathParameter {
61      get { return (ILookupParameter<DirectoryValue>)Parameters[RobocodePathParamaterName]; }
62    }
63    public ILookupParameter<IntValue> NrOfRoundsParameter {
64      get { return (ILookupParameter<IntValue>)Parameters[NrOfRoundsParameterName]; }
65    }
66    #endregion
67
68    [StorableConstructor]
69    protected BestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
70    protected BestSolutionAnalyzer(BestSolutionAnalyzer original, Cloner cloner)
71      : base(original, cloner) { }
72
73    public BestSolutionAnalyzer() {
74      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(QualityParameterName, "The solution quality of the tank program."));
75      Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The tank program to evaluate represented as symbolic expression tree."));
76      Parameters.Add(new LookupParameter<Solution>(BestSolutionParameterName, "The best tank program."));
77      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The result collection of the algorithm."));
78      Parameters.Add(new LookupParameter<DirectoryValue>(RobocodePathParamaterName, "Path of the Robocode installation."));
79      Parameters.Add(new LookupParameter<IntValue>(NrOfRoundsParameterName, "Nr. of Rounds a Robot has to fight against each opponent."));
80    }
81
82    public override IOperation Apply() {
83      // get an array of all trees
84      // and an array of all qualities
85      var trees = SymbolicExpressionTreeParameter.ActualValue;
86      var qualities = QualityParameter.ActualValue;
87
88      // find the tree with the best quality
89      double maxQuality = double.NegativeInfinity;
90      ISymbolicExpressionTree bestTree = null;
91      for (int i = 0; i < qualities.Length; i++) {
92        if (qualities[i].Value > maxQuality) {
93          maxQuality = qualities[i].Value;
94          bestTree = trees[i];
95        }
96      }
97
98      // create a solution instance
99      var bestSolution = new Solution(bestTree, RobocodePathParameter.ActualValue.Value, NrOfRoundsParameter.ActualValue.Value);
100      // store the new solution in the best solution parameter
101      BestSolutionParameter.ActualValue = bestSolution;
102
103      // also add the best solution as a result to the result collection
104      // or alternatively update the existing result
105      var resultCollection = ResultParameter.ActualValue;
106      if (!resultCollection.ContainsKey(BestSolutionParameterName)) {
107        resultCollection.Add(new Result(BestSolutionParameterName, "The best tank program", bestSolution));
108      } else {
109        resultCollection[BestSolutionParameterName].Value = bestSolution;
110      }
111
112      return base.Apply();
113    }
114
115    public override IDeepCloneable Clone(Cloner cloner) {
116      return new BestSolutionAnalyzer(this, cloner);
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.