Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.ParameterOptimization/3.3/BestSolutionAnalyzer.cs @ 12093

Last change on this file since 12093 was 12009, checked in by ascheibe, 10 years ago

#2212 updated copyright year

File size: 6.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 System.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.RealVectorEncoding;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.ParameterOptimization {
33  [Item("BestSolutionAnalyzer", "Tracks the best parameter vector solution of the current algorithm run.")]
34  [StorableClass]
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."));
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."));
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."));
88      Parameters.Add(new ValueLookupParameter<ResultCollection>(ResultsParameterName, "The result collection where the results should be stored."));
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
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;
100
101      var bestQuality = qualities[indexOfBest].Value;
102      var bestParameterVector = (RealVector)parameterVectors[indexOfBest].Clone();
103      ResultCollection results = ResultsParameter.ActualValue;
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
110      if (!results.ContainsKey(BestSolutionResultName)) {
111        results.Add(new Result(BestSolutionResultName, new DoubleArray(bestParameterVector.ToArray())));
112        var bestSolution = (DoubleArray)results[BestSolutionResultName].Value;
113        bestSolution.ElementNames = ParameterNamesParameter.ActualValue;
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;
122      }
123
124      //update best known quality
125      if (bestKnownQuality == null
126        || max && bestQuality > bestKnownQuality.Value
127        || !max && bestQuality < bestKnownQuality.Value) {
128        BestKnownQualityParameter.ActualValue = new DoubleValue(bestQuality);
129      }
130
131      return base.Apply();
132    }
133  }
134}
Note: See TracBrowser for help on using the repository browser.