[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 |
|
---|
| 22 | using HeuristicLab.Common;
|
---|
[9565] | 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
| 25 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 26 | using HeuristicLab.Operators;
|
---|
| 27 | using HeuristicLab.Optimization;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 |
|
---|
[9790] | 31 | namespace 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";
|
---|
[9947] | 42 | private const string EnemiesParameterName = "Enemies";
|
---|
[9565] | 43 |
|
---|
[9880] | 44 | public bool EnabledByDefault {
|
---|
| 45 | get { return true; }
|
---|
| 46 | }
|
---|
[9565] | 47 |
|
---|
[9880] | 48 | #region Parameters
|
---|
[9790] | 49 | public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
[9880] | 50 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
|
---|
[9790] | 51 | }
|
---|
| 52 | public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
[9880] | 53 | get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
|
---|
[9790] | 54 | }
|
---|
| 55 | public ILookupParameter<Solution> BestSolutionParameter {
|
---|
[9880] | 56 | get { return (ILookupParameter<Solution>)Parameters[BestSolutionParameterName]; }
|
---|
[9790] | 57 | }
|
---|
| 58 | public ILookupParameter<ResultCollection> ResultParameter {
|
---|
[9880] | 59 | get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
|
---|
[9790] | 60 | }
|
---|
[9892] | 61 | public ILookupParameter<DirectoryValue> RobocodePathParameter {
|
---|
| 62 | get { return (ILookupParameter<DirectoryValue>)Parameters[RobocodePathParamaterName]; }
|
---|
[9790] | 63 | }
|
---|
[9926] | 64 | public ILookupParameter<IntValue> NrOfRoundsParameter {
|
---|
| 65 | get { return (ILookupParameter<IntValue>)Parameters[NrOfRoundsParameterName]; }
|
---|
| 66 | }
|
---|
[9971] | 67 | public ILookupParameter<EnemyCollection> EnemiesParameter {
|
---|
| 68 | get { return (ILookupParameter<EnemyCollection>)Parameters[EnemiesParameterName]; }
|
---|
[9947] | 69 | }
|
---|
[9790] | 70 | #endregion
|
---|
[9565] | 71 |
|
---|
[9790] | 72 | [StorableConstructor]
|
---|
| 73 | protected BestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
[9880] | 74 | protected BestSolutionAnalyzer(BestSolutionAnalyzer original, Cloner cloner)
|
---|
| 75 | : base(original, cloner) { }
|
---|
[9565] | 76 |
|
---|
[9790] | 77 | public BestSolutionAnalyzer() {
|
---|
[9880] | 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."));
|
---|
[9892] | 82 | Parameters.Add(new LookupParameter<DirectoryValue>(RobocodePathParamaterName, "Path of the Robocode installation."));
|
---|
[9926] | 83 | Parameters.Add(new LookupParameter<IntValue>(NrOfRoundsParameterName, "Nr. of Rounds a Robot has to fight against each opponent."));
|
---|
[9971] | 84 | Parameters.Add(new LookupParameter<EnemyCollection>(EnemiesParameterName, "The enemies that should be battled."));
|
---|
[9790] | 85 | }
|
---|
[9565] | 86 |
|
---|
[9790] | 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;
|
---|
[9565] | 92 |
|
---|
[9790] | 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 | }
|
---|
[9565] | 102 |
|
---|
[9790] | 103 | // create a solution instance
|
---|
[9947] | 104 | var bestSolution = new Solution(bestTree, RobocodePathParameter.ActualValue.Value, NrOfRoundsParameter.ActualValue.Value, EnemiesParameter.ActualValue);
|
---|
[9790] | 105 | // store the new solution in the best solution parameter
|
---|
| 106 | BestSolutionParameter.ActualValue = bestSolution;
|
---|
[9565] | 107 |
|
---|
[9790] | 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)) {
|
---|
[9880] | 112 | resultCollection.Add(new Result(BestSolutionParameterName, "The best tank program", bestSolution));
|
---|
[9790] | 113 | } else {
|
---|
| 114 | resultCollection[BestSolutionParameterName].Value = bestSolution;
|
---|
| 115 | }
|
---|
[9565] | 116 |
|
---|
[9790] | 117 | return base.Apply();
|
---|
| 118 | }
|
---|
[9565] | 119 |
|
---|
[9790] | 120 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 121 | return new BestSolutionAnalyzer(this, cloner);
|
---|
[9565] | 122 | }
|
---|
[9790] | 123 | }
|
---|
[9565] | 124 | } |
---|