[13368] | 1 | #region License Information
|
---|
[9682] | 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[9682] | 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 System.Linq;
|
---|
| 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
| 27 | using HeuristicLab.Operators;
|
---|
| 28 | using HeuristicLab.Optimization;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Problems.ParameterOptimization {
|
---|
| 33 | [Item("BestSolutionAnalyzer", "Tracks the best parameter vector solution of the current algorithm run.")]
|
---|
[14711] | 34 | [StorableType("8BA2D1B7-D35C-445A-9ADE-2D2735F971EB")]
|
---|
[9682] | 35 | public class BestSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer {
|
---|
| 36 | private const string MaximizationParameterName = "Maximization";
|
---|
| 37 | private const string ParameterVectorParameterName = "RealVector";
|
---|
| 38 | private const string ParameterNamesParameterName = "ParameterNames";
|
---|
| 39 | private const string QualityParameterName = "Quality";
|
---|
| 40 | private const string BestQualityParameterName = "BestQuality";
|
---|
| 41 | private const string BestKnownQualityParameterName = "BestKnownQuality";
|
---|
| 42 |
|
---|
| 43 | private const string ResultsParameterName = "Results";
|
---|
| 44 | private const string BestSolutionResultName = "Best Solution";
|
---|
| 45 |
|
---|
| 46 | public virtual bool EnabledByDefault {
|
---|
| 47 | get { return true; }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | public ILookupParameter<BoolValue> MaximizationParameter {
|
---|
| 51 | get { return (ILookupParameter<BoolValue>)Parameters[MaximizationParameterName]; }
|
---|
| 52 | }
|
---|
| 53 | public IScopeTreeLookupParameter<RealVector> ParameterVectorParameter {
|
---|
| 54 | get { return (IScopeTreeLookupParameter<RealVector>)Parameters[ParameterVectorParameterName]; }
|
---|
| 55 | }
|
---|
| 56 | public ILookupParameter<StringArray> ParameterNamesParameter {
|
---|
| 57 | get { return (ILookupParameter<StringArray>)Parameters[ParameterNamesParameterName]; }
|
---|
| 58 | }
|
---|
| 59 | public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
| 60 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
|
---|
| 61 | }
|
---|
| 62 | public ILookupParameter<DoubleValue> BestQualityParameter {
|
---|
| 63 | get { return (ILookupParameter<DoubleValue>)Parameters[BestQualityParameterName]; }
|
---|
| 64 | }
|
---|
| 65 | public ILookupParameter<DoubleValue> BestKnownQualityParameter {
|
---|
| 66 | get { return (ILookupParameter<DoubleValue>)Parameters[BestKnownQualityParameterName]; }
|
---|
| 67 | }
|
---|
| 68 | public IValueLookupParameter<ResultCollection> ResultsParameter {
|
---|
| 69 | get { return (IValueLookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | [StorableConstructor]
|
---|
| 73 | protected BestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
| 74 | protected BestSolutionAnalyzer(BestSolutionAnalyzer original, Cloner cloner)
|
---|
| 75 | : base(original, cloner) { }
|
---|
| 76 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 77 | return new BestSolutionAnalyzer(this, cloner);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | public BestSolutionAnalyzer()
|
---|
| 81 | : base() {
|
---|
| 82 | Parameters.Add(new LookupParameter<BoolValue>(MaximizationParameterName, "True if the problem is a maximization problem."));
|
---|
| 83 | Parameters.Add(new ScopeTreeLookupParameter<RealVector>(ParameterVectorParameterName, "The parameter vector which should be evaluated."));
|
---|
[10190] | 84 | Parameters.Add(new LookupParameter<StringArray>(ParameterNamesParameterName, "The names of the elements in the parameter vector."));
|
---|
| 85 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(QualityParameterName, "The quality name for the parameter vectors."));
|
---|
[9682] | 86 | Parameters.Add(new LookupParameter<DoubleValue>(BestQualityParameterName, "The best quality found so far."));
|
---|
| 87 | Parameters.Add(new LookupParameter<DoubleValue>(BestKnownQualityParameterName, "The quality of the best known solution."));
|
---|
[10190] | 88 | Parameters.Add(new ValueLookupParameter<ResultCollection>(ResultsParameterName, "The result collection where the results should be stored."));
|
---|
[9682] | 89 | }
|
---|
| 90 |
|
---|
| 91 | public override IOperation Apply() {
|
---|
| 92 | ItemArray<RealVector> parameterVectors = ParameterVectorParameter.ActualValue;
|
---|
| 93 | ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
|
---|
| 94 | bool max = MaximizationParameter.ActualValue.Value;
|
---|
| 95 | DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
|
---|
| 96 |
|
---|
[11077] | 97 | int indexOfBest = -1;
|
---|
| 98 | if (!max) indexOfBest = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
|
---|
| 99 | else indexOfBest = qualities.Select((x, index) => new { index, x.Value }).OrderByDescending(x => x.Value).First().index;
|
---|
[9682] | 100 |
|
---|
[11077] | 101 | var bestQuality = qualities[indexOfBest].Value;
|
---|
| 102 | var bestParameterVector = (RealVector)parameterVectors[indexOfBest].Clone();
|
---|
[10937] | 103 | ResultCollection results = ResultsParameter.ActualValue;
|
---|
[9682] | 104 |
|
---|
| 105 | if (BestQualityParameter.ActualValue == null) {
|
---|
| 106 | if (max) BestQualityParameter.ActualValue = new DoubleValue(double.MinValue);
|
---|
| 107 | else BestQualityParameter.ActualValue = new DoubleValue(double.MaxValue);
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[11019] | 110 | if (!results.ContainsKey(BestSolutionResultName)) {
|
---|
[10937] | 111 | results.Add(new Result(BestSolutionResultName, new DoubleArray(bestParameterVector.ToArray())));
|
---|
| 112 | var bestSolution = (DoubleArray)results[BestSolutionResultName].Value;
|
---|
| 113 | bestSolution.ElementNames = ParameterNamesParameter.ActualValue;
|
---|
[11019] | 114 | BestQualityParameter.ActualValue.Value = bestQuality;
|
---|
| 115 | } else if (max && bestQuality > BestQualityParameter.ActualValue.Value
|
---|
| 116 | || !max && bestQuality < BestQualityParameter.ActualValue.Value) {
|
---|
| 117 | var bestSolution = (DoubleArray)results[BestSolutionResultName].Value;
|
---|
| 118 | bestSolution.ElementNames = ParameterNamesParameter.ActualValue;
|
---|
| 119 | for (int i = 0; i < bestParameterVector.Length; i++)
|
---|
| 120 | bestSolution[i] = bestParameterVector[i];
|
---|
| 121 | BestQualityParameter.ActualValue.Value = bestQuality;
|
---|
[9682] | 122 | }
|
---|
[11019] | 123 |
|
---|
[9682] | 124 | //update best known quality
|
---|
[11077] | 125 | if (bestKnownQuality == null
|
---|
| 126 | || max && bestQuality > bestKnownQuality.Value
|
---|
[9682] | 127 | || !max && bestQuality < bestKnownQuality.Value) {
|
---|
| 128 | BestKnownQualityParameter.ActualValue = new DoubleValue(bestQuality);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | return base.Apply();
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 | }
|
---|