Free cookie consent management tool by TermsFeed Policy Generator

source: branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/Operators/CorrelationAnalyzer.cs @ 15870

Last change on this file since 15870 was 15338, checked in by bwerth, 7 years ago

#2745 fixed bug concerning new Start and StartAsync methods; passed CancellationToken to sub algorithms

File size: 4.4 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.Analysis;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.RealVectorEncoding;
29using HeuristicLab.Operators;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.Problems.DataAnalysis;
34
35namespace HeuristicLab.Algorithms.EGO {
36  [Item("CorrelationAnalyzer", "Analyzes the correlation between perdictions and actual fitness values")]
37  [StorableClass]
38  public class CorrelationAnalyzer : SingleSuccessorOperator, IAnalyzer, IResultsOperator {
39    public override bool CanChangeName => true;
40    public bool EnabledByDefault => false;
41
42    public IScopeTreeLookupParameter<RealVector> RealVectorParameter => (IScopeTreeLookupParameter<RealVector>)Parameters["RealVector"];
43    public IScopeTreeLookupParameter<DoubleValue> QualityParameter => (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"];
44    public ILookupParameter<IRegressionSolution> ModelParameter => (ILookupParameter<IRegressionSolution>)Parameters["Model"];
45    public ILookupParameter<ResultCollection> ResultsParameter => (ILookupParameter<ResultCollection>)Parameters["Results"];
46
47    private const string PlotName = "Prediction";
48    private const string RowName = "Samples";
49
50    [StorableConstructor]
51    protected CorrelationAnalyzer(bool deserializing) : base(deserializing) { }
52    protected CorrelationAnalyzer(CorrelationAnalyzer original, Cloner cloner) : base(original, cloner) { }
53    public CorrelationAnalyzer() {
54      Parameters.Add(new ScopeTreeLookupParameter<RealVector>("RealVector", "The vector which should be collected."));
55      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The quality associated which this vector"));
56      Parameters.Add(new LookupParameter<IRegressionSolution>("Model", "The model of this iteration"));
57      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The collection to store the results in."));
58    }
59
60    public override IDeepCloneable Clone(Cloner cloner) {
61      return new CorrelationAnalyzer(this, cloner);
62    }
63
64    public sealed override IOperation Apply() {
65      var model = ModelParameter.ActualValue;
66      var results = ResultsParameter.ActualValue;
67      var q = QualityParameter.ActualValue.Select(x => x.Value).ToArray();
68      var p = RealVectorParameter.ActualValue.ToArray();
69      if (model == null) return base.Apply();
70      var plot = CreateScatterPlotResult(results);
71      for (var i = 0; i < q.Length; i++) plot.Rows[RowName].Points.Add(new Point2D<double>(model.Model.GetEstimation(p[i]), q[i], p[i]));
72      return base.Apply();
73    }
74
75    private static ScatterPlot CreateScatterPlotResult(ResultCollection results) {
76      ScatterPlot plot;
77      if (!results.ContainsKey(PlotName)) {
78        plot = new ScatterPlot("Fitness-Prediction-Correlation", "The correlation between the predicted and actual fitness values") {
79          VisualProperties = {
80            XAxisTitle = "Predicted Objective Value",
81            YAxisTitle = "True Objective Value"
82          }
83        };
84        results.Add(new Result(PlotName, plot));
85      } else { plot = (ScatterPlot)results[PlotName].Value; }
86      if (!plot.Rows.ContainsKey(RowName)) {
87        plot.Rows.Add(new ScatterPlotDataRow(RowName, RowName, new List<Point2D<double>>()));
88        plot.Rows[RowName].VisualProperties.PointSize = 5;
89      }
90      return plot;
91    }
92
93  }
94}
Note: See TracBrowser for help on using the repository browser.