Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.ExternalEvaluation Scientific/HeuristicLab.Problems.ParameterOptimization/3.3/BestSolutionAnalyzer.cs @ 9844

Last change on this file since 9844 was 9682, checked in by mkommend, 11 years ago

#2082: Added new plugins for parameter optimization and external evaluation in Scilab.

File size: 6.7 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 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
47    public virtual bool EnabledByDefault {
48      get { return true; }
49    }
50
51    public ILookupParameter<BoolValue> MaximizationParameter {
52      get { return (ILookupParameter<BoolValue>)Parameters[MaximizationParameterName]; }
53    }
54    public IScopeTreeLookupParameter<RealVector> ParameterVectorParameter {
55      get { return (IScopeTreeLookupParameter<RealVector>)Parameters[ParameterVectorParameterName]; }
56    }
57    public ILookupParameter<StringArray> ParameterNamesParameter {
58      get { return (ILookupParameter<StringArray>)Parameters[ParameterNamesParameterName]; }
59    }
60    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
61      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
62    }
63    public ILookupParameter<DoubleValue> BestQualityParameter {
64      get { return (ILookupParameter<DoubleValue>)Parameters[BestQualityParameterName]; }
65    }
66    public ILookupParameter<DoubleValue> BestKnownQualityParameter {
67      get { return (ILookupParameter<DoubleValue>)Parameters[BestKnownQualityParameterName]; }
68    }
69    public IValueLookupParameter<ResultCollection> ResultsParameter {
70      get { return (IValueLookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
71    }
72
73    [StorableConstructor]
74    protected BestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
75    protected BestSolutionAnalyzer(BestSolutionAnalyzer original, Cloner cloner)
76      : base(original, cloner) { }
77    public override IDeepCloneable Clone(Cloner cloner) {
78      return new BestSolutionAnalyzer(this, cloner);
79    }
80
81    public BestSolutionAnalyzer()
82      : base() {
83      Parameters.Add(new LookupParameter<BoolValue>(MaximizationParameterName, "True if the problem is a maximization problem."));
84      Parameters.Add(new ScopeTreeLookupParameter<RealVector>(ParameterVectorParameterName, "The parameter vector which should be evaluated."));
85      Parameters.Add(new LookupParameter<StringArray>(ParameterNamesParameterName, "The names which are used within the SciLab script to calculate the quality of a parameter vector."));
86      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(QualityParameterName, "The quality name for the SciLab parameter vectors."));
87      Parameters.Add(new LookupParameter<DoubleValue>(BestQualityParameterName, "The best quality found so far."));
88      Parameters.Add(new LookupParameter<DoubleValue>(BestKnownQualityParameterName, "The quality of the best known solution."));
89      Parameters.Add(new ValueLookupParameter<ResultCollection>(ResultsParameterName, "The result collection where the SciLab parameter should be stored."));
90    }
91
92    public override IOperation Apply() {
93      ItemArray<RealVector> parameterVectors = ParameterVectorParameter.ActualValue;
94      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
95      bool max = MaximizationParameter.ActualValue.Value;
96      DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
97
98      int ind = -1;
99      if (!max) ind = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
100      else ind = qualities.Select((x, index) => new { index, x.Value }).OrderByDescending(x => x.Value).First().index;
101
102      var bestQuality = qualities[ind].Value;
103      var bestParameterVector = (RealVector)parameterVectors[ind].Clone();
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 (max && bestQuality > BestQualityParameter.ActualValue.Value
111          || !max && bestQuality < BestQualityParameter.ActualValue.Value) {
112        BestQualityParameter.ActualValue.Value = bestQuality;
113        ResultCollection results = ResultsParameter.ActualValue;
114        if (results.ContainsKey(BestSolutionResultName)) {
115          var bestSolution = (DoubleArray)results[BestSolutionResultName].Value;
116          bestSolution.ElementNames = ParameterNamesParameter.ActualValue;
117          for (int i = 0; i < bestParameterVector.Length; i++)
118            bestSolution[i] = bestParameterVector[i];
119        } else {
120          results.Add(new Result(BestSolutionResultName, new DoubleArray(bestParameterVector.ToArray())));
121          var bestSolution = (DoubleArray)results[BestSolutionResultName].Value;
122          bestSolution.ElementNames = ParameterNamesParameter.ActualValue;
123        }
124      }
125
126      //update best known quality
127      if (bestKnownQuality == null || max && bestQuality > bestKnownQuality.Value
128        || !max && bestQuality < bestKnownQuality.Value) {
129        BestKnownQualityParameter.ActualValue = new DoubleValue(bestQuality);
130      }
131
132      return base.Apply();
133    }
134  }
135}
Note: See TracBrowser for help on using the repository browser.