Free cookie consent management tool by TermsFeed Policy Generator

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, 11 years ago

#2069

  • renamed path parameter to robocode path
  • made number of rounds configurable
File size: 5.5 KB
RevLine 
[9790]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;
[9565]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
[9790]31namespace HeuristicLab.Problems.Robocode {
32  [StorableClass]
[9880]33  [Item("Best Tank Program Analyzer",
[9790]34        "Analyzer that stores the best tank program.")]
[9880]35  public class BestSolutionAnalyzer : SingleSuccessorOperator, ISymbolicExpressionTreeAnalyzer {
[9790]36    private const string QualityParameterName = "Quality";
[9880]37    private const string SymbolicExpressionTreeParameterName = "TankProgram";
[9790]38    private const string BestSolutionParameterName = "Best solution";
39    private const string ResultsParameterName = "Results";
[9926]40    private const string RobocodePathParamaterName = "RobocodePath";
41    private const string NrOfRoundsParameterName = "NrOfRounds";
[9565]42
[9880]43    public bool EnabledByDefault {
44      get { return true; }
45    }
[9565]46
[9880]47    #region Parameters
[9790]48    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
[9880]49      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
[9790]50    }
51    public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
[9880]52      get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
[9790]53    }
54    public ILookupParameter<Solution> BestSolutionParameter {
[9880]55      get { return (ILookupParameter<Solution>)Parameters[BestSolutionParameterName]; }
[9790]56    }
57    public ILookupParameter<ResultCollection> ResultParameter {
[9880]58      get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
[9790]59    }
[9892]60    public ILookupParameter<DirectoryValue> RobocodePathParameter {
61      get { return (ILookupParameter<DirectoryValue>)Parameters[RobocodePathParamaterName]; }
[9790]62    }
[9926]63    public ILookupParameter<IntValue> NrOfRoundsParameter {
64      get { return (ILookupParameter<IntValue>)Parameters[NrOfRoundsParameterName]; }
65    }
[9790]66    #endregion
[9565]67
[9790]68    [StorableConstructor]
69    protected BestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
[9880]70    protected BestSolutionAnalyzer(BestSolutionAnalyzer original, Cloner cloner)
71      : base(original, cloner) { }
[9565]72
[9790]73    public BestSolutionAnalyzer() {
[9880]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."));
[9892]78      Parameters.Add(new LookupParameter<DirectoryValue>(RobocodePathParamaterName, "Path of the Robocode installation."));
[9926]79      Parameters.Add(new LookupParameter<IntValue>(NrOfRoundsParameterName, "Nr. of Rounds a Robot has to fight against each opponent."));
[9790]80    }
[9565]81
[9790]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;
[9565]87
[9790]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      }
[9565]97
[9790]98      // create a solution instance
[9926]99      var bestSolution = new Solution(bestTree, RobocodePathParameter.ActualValue.Value, NrOfRoundsParameter.ActualValue.Value);
[9790]100      // store the new solution in the best solution parameter
101      BestSolutionParameter.ActualValue = bestSolution;
[9565]102
[9790]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)) {
[9880]107        resultCollection.Add(new Result(BestSolutionParameterName, "The best tank program", bestSolution));
[9790]108      } else {
109        resultCollection[BestSolutionParameterName].Value = bestSolution;
110      }
[9565]111
[9790]112      return base.Apply();
113    }
[9565]114
[9790]115    public override IDeepCloneable Clone(Cloner cloner) {
116      return new BestSolutionAnalyzer(this, cloner);
[9565]117    }
[9790]118  }
[9565]119}
Note: See TracBrowser for help on using the repository browser.