Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.ParameterOptimization/3.3/BestSolutionsAnalyzer.cs @ 10635

Last change on this file since 10635 was 10594, checked in by mkommend, 11 years ago

#2082: Updated copyright and used Environment.NewLine instead of \r\n.

File size: 8.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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
124          || !max && bestQuality < previousBestQuality)
125        bestSolutions.Clear();
126
127      //add new found solutions
128      if (max && bestQuality >= BestQualityParameter.ActualValue.Value
129          || !max && bestQuality <= BestQualityParameter.ActualValue.Value) {
130        foreach (var solution in solutions) {
131          var newSolution = (DoubleArray)solution.ParameterVector.Clone();
132          newSolution.ElementNames = ParameterNamesParameter.ActualValue;
133          bestSolutions.Add(newSolution);
134        }
135      }
136      PreviousBestQualityParameter.ActualValue = (DoubleValue)BestQualityParameter.ActualValue.Clone();
137
138      //update best known quality
139      if (bestKnownQuality == null || max && bestQuality > bestKnownQuality.Value
140        || !max && bestQuality < bestKnownQuality.Value) {
141        BestKnownQualityParameter.ActualValue = new DoubleValue(bestQuality);
142      }
143
144      return base.Apply();
145    }
146  }
147
148  public class DoubleArrayEqualityComparer : IEqualityComparer<DoubleArray> {
149    public bool Equals(DoubleArray x, DoubleArray y) {
150      if (x == null && y == null) return true;
151      if (x == null) return false;
152      if (y == null) return false;
153      return x.SequenceEqual(y);
154    }
155
156    public int GetHashCode(DoubleArray obj) {
157      if (obj == null) return 0;
158      return obj.Aggregate(23, (current, item) => current ^ (int)item);
159    }
160  }
161}
Note: See TracBrowser for help on using the repository browser.