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 |
|
---|
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.")]
|
---|
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 ind = -1;
|
---|
98 | if (!max) ind = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
|
---|
99 | else ind = qualities.Select((x, index) => new { index, x.Value }).OrderByDescending(x => x.Value).First().index;
|
---|
100 |
|
---|
101 | var bestQuality = qualities[ind].Value;
|
---|
102 | var bestParameterVector = (RealVector)parameterVectors[ind].Clone();
|
---|
103 |
|
---|
104 | if (BestQualityParameter.ActualValue == null) {
|
---|
105 | if (max) BestQualityParameter.ActualValue = new DoubleValue(double.MinValue);
|
---|
106 | else BestQualityParameter.ActualValue = new DoubleValue(double.MaxValue);
|
---|
107 | }
|
---|
108 |
|
---|
109 | if (max && bestQuality > BestQualityParameter.ActualValue.Value
|
---|
110 | || !max && bestQuality < BestQualityParameter.ActualValue.Value) {
|
---|
111 | BestQualityParameter.ActualValue.Value = bestQuality;
|
---|
112 | ResultCollection results = ResultsParameter.ActualValue;
|
---|
113 | if (results.ContainsKey(BestSolutionResultName)) {
|
---|
114 | var bestSolution = (DoubleArray)results[BestSolutionResultName].Value;
|
---|
115 | bestSolution.ElementNames = ParameterNamesParameter.ActualValue;
|
---|
116 | for (int i = 0; i < bestParameterVector.Length; i++)
|
---|
117 | bestSolution[i] = bestParameterVector[i];
|
---|
118 | } else {
|
---|
119 | results.Add(new Result(BestSolutionResultName, new DoubleArray(bestParameterVector.ToArray())));
|
---|
120 | var bestSolution = (DoubleArray)results[BestSolutionResultName].Value;
|
---|
121 | bestSolution.ElementNames = ParameterNamesParameter.ActualValue;
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | //update best known quality
|
---|
126 | if (bestKnownQuality == null || max && bestQuality > bestKnownQuality.Value
|
---|
127 | || !max && bestQuality < bestKnownQuality.Value) {
|
---|
128 | BestKnownQualityParameter.ActualValue = new DoubleValue(bestQuality);
|
---|
129 | }
|
---|
130 |
|
---|
131 | return base.Apply();
|
---|
132 | }
|
---|
133 | }
|
---|
134 | }
|
---|