Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9880 was 9880, checked in by ascheibe, 11 years ago

#2069 removed dead code

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