Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2679_HeuristicLab.GoalSeekingProblem/HeuristicLab.GoalSeekingProblem/3.4/Analyzers/BestSolutionAnalyzer.cs @ 16133

Last change on this file since 16133 was 14526, checked in by bburlacu, 8 years ago

#2679: Add analyzer for SingleObjectiveGoalSeekingProblem. Add result aggregation in the GoalSeekingOptimizer. Remove unused dataset parameter from the problems.

File size: 7.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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;
32using HeuristicLab.Problems.DataAnalysis;
33
34namespace HeuristicLab.GoalSeeking {
35  [StorableClass]
36  [Item("BestSolutionAnalyzer", "An analyzer which identifies the best solution from the SingleObjectiveProcessParameterOptimizationProblem")]
37  public class BestSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer {
38    private const string RowParameterName = "Row";
39    private const string ProblemDataParameterName = "ProblemData";
40    private const string QualityParameterName = "Quality";
41    private const string MaximizationParameterName = "Maximization";
42
43    public bool EnabledByDefault {
44      get { return true; }
45    }
46    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
47      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
48    }
49    public ILookupParameter<BoolValue> MaximizationParameter {
50      get { return (ILookupParameter<BoolValue>)Parameters[MaximizationParameterName]; }
51    }
52    public IFixedValueParameter<StringValue> BestSolutionResultNameParameter {
53      get { return (IFixedValueParameter<StringValue>)Parameters["BestSolution ResultName"]; }
54    }
55    public ILookupParameter<DoubleValue> BestKnownQualityParameter {
56      get { return (ILookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
57    }
58    public ILookupParameter<ResultCollection> ResultsParameter {
59      get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
60    }
61    public ILookupParameter<IntValue> RowParameter {
62      get { return (ILookupParameter<IntValue>)Parameters[RowParameterName]; }
63    }
64    public ILookupParameter<IRegressionProblemData> ProblemDataParameter {
65      get { return (ILookupParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; }
66    }
67
68    public BoolValue Maximization {
69      get { return MaximizationParameter.ActualValue; }
70    }
71
72    public ItemArray<DoubleValue> Quality {
73      get { return QualityParameter.ActualValue; }
74    }
75
76    public ResultCollection Results {
77      get { return ResultsParameter.ActualValue; }
78    }
79
80    public string BestSolutionResultName {
81      get { return BestSolutionResultNameParameter.Value.Value; }
82      set { BestSolutionResultNameParameter.Value.Value = value; }
83    }
84
85    public BestSolutionAnalyzer() {
86      Parameters.Add(new FixedValueParameter<StringValue>("BestSolution ResultName", "The name of the result for storing the best solution.", new StringValue("Best Solution")));
87      Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution."));
88      Parameters.Add(new LookupParameter<IntValue>(RowParameterName, "The current row"));
89      Parameters.Add(new LookupParameter<IRegressionProblemData>(ProblemDataParameterName, "The problem data"));
90      Parameters.Add(new LookupParameter<BoolValue>(MaximizationParameterName, "Specifies whether the problem is a minimization or a maximization problem."));
91      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(QualityParameterName, "The qualities of the individuals in the population"));
92      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The result collection"));
93    }
94
95    protected BestSolutionAnalyzer(BestSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
96
97    public override IDeepCloneable Clone(Cloner cloner) {
98      return new BestSolutionAnalyzer(this, cloner);
99    }
100
101    [StorableConstructor]
102    protected BestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
103
104
105    [StorableHook(HookType.AfterDeserialization)]
106    private void AfterDeserialization() {
107      if (!Parameters.ContainsKey(MaximizationParameterName))
108        Parameters.Add(new LookupParameter<BoolValue>(MaximizationParameterName, "Specifies whether the problem is a minimization or a maximization problem."));
109      if (!Parameters.ContainsKey(QualityParameterName))
110        Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(QualityParameterName, "The qualities of the individuals in the population"));
111      if (!Parameters.ContainsKey("Results"))
112        Parameters.Add(new LookupParameter<ResultCollection>("Results", "The result collection"));
113    }
114
115    public override IOperation Apply() {
116      IEnumerable<IScope> scopes = new[] { ExecutionContext.Scope };
117      var zipped = Quality.Select((x, index) => new { Index = index, x.Value });
118      var best = Maximization.Value ? zipped.OrderBy(x => x.Value).First() : zipped.OrderByDescending(x => x.Value).First();
119      for (int j = 0; j < QualityParameter.Depth; j++)
120        scopes = scopes.SelectMany(x => x.SubScopes);
121      IScope currentBestScope = scopes.ToList()[best.Index];
122      var bestSolution = (RealVector)currentBestScope.Variables["RealVector"].Value;
123
124      var bestSolutionMatrix = new DoubleMatrix(bestSolution.Length, 3);
125      var targetNames = bestSolution.ElementNames.ToList();
126      bestSolutionMatrix.RowNames = targetNames;
127      bestSolutionMatrix.ColumnNames = new[] { "Estimated value", "Target value", "Deviation" };
128
129      var problemData = ProblemDataParameter.ActualValue;
130      var row = RowParameter.ActualValue.Value;
131
132      for (int i = 0; i < bestSolution.Length; ++i) {
133        var estimatedValue = bestSolution[i];
134        var targetValue = problemData.Dataset.GetDoubleValue(targetNames[i], row);
135        bestSolutionMatrix[i, 0] = estimatedValue;
136        bestSolutionMatrix[i, 1] = targetValue;
137        bestSolutionMatrix[i, 2] = estimatedValue - targetValue;
138      }
139
140      if (!Results.ContainsKey(BestSolutionResultName)) {
141        Results.Add(new Result(BestSolutionResultName, bestSolutionMatrix));
142      } else {
143        var result = Results[BestSolutionResultName];
144        result.Value = bestSolutionMatrix;
145      }
146
147      return base.Apply();
148    }
149  }
150}
Note: See TracBrowser for help on using the repository browser.