Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2929_PrioritizedGrammarEnumeration/HeuristicLab.Problems.ParameterOptimization/3.3/BestSolutionsAnalyzer.cs @ 16147

Last change on this file since 16147 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 8.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.RealVectorEncoding;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Problems.ParameterOptimization {
34  [Item("BestSolutionsAnalyzer", "Tracks the best parameter vector solutions of the current algorithm run.")]
35  [StorableClass]
36  public class BestSolutionsAnalyzer : SingleSuccessorOperator, IAnalyzer {
37    private const string MaximizationParameterName = "Maximization";
38    private const string ParameterVectorParameterName = "RealVector";
39    private const string ParameterNamesParameterName = "ParameterNames";
40    private const string QualityParameterName = "Quality";
41    private const string PreviousBestQualityParameterName = "PreviousBestQuality";
42    private const string BestQualityParameterName = "BestQuality";
43    private const string BestKnownQualityParameterName = "BestKnownQuality";
44
45    private const string ResultsParameterName = "Results";
46    private const string BestSolutionsResultName = "Best Solutions Store";
47
48    public virtual bool EnabledByDefault {
49      get { return false; }
50    }
51
52    public ILookupParameter<BoolValue> MaximizationParameter {
53      get { return (ILookupParameter<BoolValue>)Parameters[MaximizationParameterName]; }
54    }
55    public IScopeTreeLookupParameter<RealVector> ParameterVectorParameter {
56      get { return (IScopeTreeLookupParameter<RealVector>)Parameters[ParameterVectorParameterName]; }
57    }
58    public ILookupParameter<StringArray> ParameterNamesParameter {
59      get { return (ILookupParameter<StringArray>)Parameters[ParameterNamesParameterName]; }
60    }
61    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
62      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
63    }
64    public ILookupParameter<DoubleValue> PreviousBestQualityParameter {
65      get { return (ILookupParameter<DoubleValue>)Parameters[PreviousBestQualityParameterName]; }
66    }
67    public ILookupParameter<DoubleValue> BestQualityParameter {
68      get { return (ILookupParameter<DoubleValue>)Parameters[BestQualityParameterName]; }
69    }
70    public ILookupParameter<DoubleValue> BestKnownQualityParameter {
71      get { return (ILookupParameter<DoubleValue>)Parameters[BestKnownQualityParameterName]; }
72    }
73    public IValueLookupParameter<ResultCollection> ResultsParameter {
74      get { return (IValueLookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
75    }
76
77    [StorableConstructor]
78    protected BestSolutionsAnalyzer(bool deserializing) : base(deserializing) { }
79    protected BestSolutionsAnalyzer(BestSolutionsAnalyzer original, Cloner cloner)
80      : base(original, cloner) { }
81    public override IDeepCloneable Clone(Cloner cloner) {
82      return new BestSolutionsAnalyzer(this, cloner);
83    }
84
85    public BestSolutionsAnalyzer()
86      : base() {
87      Parameters.Add(new LookupParameter<BoolValue>(MaximizationParameterName, "True if the problem is a maximization problem."));
88      Parameters.Add(new ScopeTreeLookupParameter<RealVector>(ParameterVectorParameterName, "The parameter vector which should be evaluated."));
89      Parameters.Add(new LookupParameter<StringArray>(ParameterNamesParameterName, "The names of the elements in the parameter vector."));
90      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(QualityParameterName, "The quality name for the parameter vectors."));
91      Parameters.Add(new LookupParameter<DoubleValue>(PreviousBestQualityParameterName, "The best quality of the previous iteration."));
92      Parameters.Add(new LookupParameter<DoubleValue>(BestQualityParameterName, "The best quality found so far."));
93      Parameters.Add(new LookupParameter<DoubleValue>(BestKnownQualityParameterName, "The quality of the best known solution."));
94      Parameters.Add(new ValueLookupParameter<ResultCollection>(ResultsParameterName, "The result collection where the results should be stored."));
95    }
96
97    public override IOperation Apply() {
98      ItemArray<RealVector> parameterVectors = ParameterVectorParameter.ActualValue;
99      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
100      bool max = MaximizationParameter.ActualValue.Value;
101      DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
102
103      var solutions = parameterVectors.Zip(qualities, (ParameterVector, Quality) => new { ParameterVector, Quality });
104      if (max) solutions = solutions.MaxItems(s => s.Quality.Value);
105      else solutions = solutions.MinItems(s => s.Quality.Value);
106
107      if (BestQualityParameter.ActualValue == null) {
108        if (max) BestQualityParameter.ActualValue = new DoubleValue(double.MinValue);
109        else BestQualityParameter.ActualValue = new DoubleValue(double.MaxValue);
110      }
111      if (PreviousBestQualityParameter.ActualValue == null)
112        PreviousBestQualityParameter.ActualValue = (DoubleValue)BestQualityParameter.ActualValue.Clone();
113
114      //add result for best solutions
115      ResultCollection results = ResultsParameter.ActualValue;
116      if (!results.ContainsKey(BestSolutionsResultName))
117        results.Add(new Result(BestSolutionsResultName, new ItemSet<DoubleArray>(new DoubleArrayEqualityComparer())));
118
119      var previousBestQuality = PreviousBestQualityParameter.ActualValue.Value;
120      var bestQuality = solutions.First().Quality.Value;
121      var bestSolutions = (ItemSet<DoubleArray>)results[BestSolutionsResultName].Value;
122      //clear best solutions if new found quality is better than the existing one
123      if (max && bestQuality > previousBestQuality || !max && bestQuality < previousBestQuality)
124        bestSolutions.Clear();
125
126      //add new found solutions
127      if (max && bestQuality >= BestQualityParameter.ActualValue.Value
128          || !max && bestQuality <= BestQualityParameter.ActualValue.Value) {
129        foreach (var solution in solutions) {
130          var newSolution = (DoubleArray)solution.ParameterVector.Clone();
131          newSolution.ElementNames = ParameterNamesParameter.ActualValue;
132          bestSolutions.Add(newSolution);
133        }
134      }
135
136      //update best quality
137      if (max && bestQuality >= BestQualityParameter.ActualValue.Value
138          || !max && bestQuality <= BestQualityParameter.ActualValue.Value) {
139        BestQualityParameter.ActualValue.Value = bestQuality;
140      }
141      //update best known quality
142      if (bestKnownQuality == null || max && bestQuality > bestKnownQuality.Value
143        || !max && bestQuality < bestKnownQuality.Value) {
144        BestKnownQualityParameter.ActualValue = new DoubleValue(bestQuality);
145      }
146      PreviousBestQualityParameter.ActualValue = (DoubleValue)BestQualityParameter.ActualValue.Clone();
147
148      return base.Apply();
149    }
150  }
151
152  public class DoubleArrayEqualityComparer : IEqualityComparer<DoubleArray> {
153    public bool Equals(DoubleArray x, DoubleArray y) {
154      if (x == null && y == null) return true;
155      if (x == null) return false;
156      if (y == null) return false;
157      return x.SequenceEqual(y);
158    }
159
160    public int GetHashCode(DoubleArray obj) {
161      if (obj == null) return 0;
162      return (int)obj.Aggregate(23L, (current, item) => current ^ System.BitConverter.DoubleToInt64Bits(item));
163    }
164  }
165}
Note: See TracBrowser for help on using the repository browser.