Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2069

  • added license headers
  • corrected version information
  • fixed formatting
File size: 7.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
22
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.Robocode {
33  [StorableClass]
34  [Item("Best Tank program Analyzer",
35        "Analyzer that stores the best tank program.")]
36  public class BestSolutionAnalyzer : SingleSuccessorOperator,
37    ISymbolicExpressionTreeAnalyzer {
38    #region parameter names
39    private const string QualityParameterName = "Quality";
40    private const string SymbolicExpressionTreeParameterName =
41      "TankProgram";
42    private const string MovesParameterName = "Moves";
43    private const string ShotsParameterName = "Shots";
44    private const string BestSolutionParameterName = "Best solution";
45    private const string ResultsParameterName = "Results";
46    private const string RobocodePathParamaterName = "Path";
47    private const string CoevolutionParameterName = "Coevolution";
48    #endregion
49
50
51    #region parameters
52    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
53      get {
54        return (IScopeTreeLookupParameter<DoubleValue>)
55                  Parameters[QualityParameterName];
56      }
57    }
58    public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
59      get {
60        return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)
61                  Parameters[SymbolicExpressionTreeParameterName];
62      }
63    }
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    //}
80    public ILookupParameter<Solution> BestSolutionParameter {
81      get {
82        return (ILookupParameter<Solution>)
83                  Parameters[BestSolutionParameterName];
84      }
85    }
86    public ILookupParameter<ResultCollection> ResultParameter {
87      get {
88        return (ILookupParameter<ResultCollection>)
89                  Parameters[ResultsParameterName];
90      }
91    }
92    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      }
103    }
104    #endregion
105
106    [StorableConstructor]
107    protected BestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
108    protected BestSolutionAnalyzer(BestSolutionAnalyzer original,
109                                   Cloner cloner)
110      : base(original, cloner) {
111    }
112
113    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"));
143    }
144
145    public override IOperation Apply() {
146      // get an array of all trees
147      // and an array of all qualities
148      var trees = SymbolicExpressionTreeParameter.ActualValue;
149      var qualities = QualityParameter.ActualValue;
150
151      // find the tree with the best quality
152      double maxQuality = double.NegativeInfinity;
153      ISymbolicExpressionTree bestTree = null;
154      for (int i = 0; i < qualities.Length; i++) {
155        if (qualities[i].Value > maxQuality) {
156          maxQuality = qualities[i].Value;
157          bestTree = trees[i];
158        }
159      }
160
161      var coevolution = CoevolutionParameter.ActualValue.Value;
162      double actualQuality = 0;
163
164      if (coevolution)
165        actualQuality = Interpreter.EvaluateTankProgram(bestTree, null, RobocodePathParameter.ActualValue.Value);
166
167      // 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);
171      // store the new solution in the best solution parameter
172      BestSolutionParameter.ActualValue = bestSolution;
173
174      // also add the best solution as a result to the result collection
175      // or alternatively update the existing result
176      var resultCollection = ResultParameter.ActualValue;
177      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)));
185      } else {
186        resultCollection[BestSolutionParameterName].Value = bestSolution;
187      }
188
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
201      return base.Apply();
202    }
203
204    public override IDeepCloneable Clone(Cloner cloner) {
205      return new BestSolutionAnalyzer(this, cloner);
206    }
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    }
213  }
214}
Note: See TracBrowser for help on using the repository browser.