Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2745_EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/DiscreteEGO/DiscreteCorrelationAnalyzer.cs @ 17717

Last change on this file since 17717 was 17332, checked in by bwerth, 5 years ago

#2745 updated persistence to HEAL.Attic

File size: 4.5 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 HEAL.Attic;
25using HeuristicLab.Analysis;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.IntegerVectorEncoding;
30using HeuristicLab.Operators;
31using HeuristicLab.Optimization;
32using HeuristicLab.Parameters;
33using HeuristicLab.Problems.DataAnalysis;
34
35namespace HeuristicLab.Algorithms.EGO {
36  [Item("DiscreteCorrelationAnalyzer", "Analyzes the correlation between perdictions and actual fitness values")]
37    [StorableType("1657262e-1ada-4672-a073-bc8b144ecf42")]
38    public class DiscreteCorrelationAnalyzer : SingleSuccessorOperator, IAnalyzer, IResultsOperator {
39    public override bool CanChangeName => true;
40    public bool EnabledByDefault => false;
41
42    public IScopeTreeLookupParameter<IntegerVector> IntegerVectorParameter => (IScopeTreeLookupParameter<IntegerVector>)Parameters["IntegerVector"];
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 DiscreteCorrelationAnalyzer(StorableConstructorFlag deserializing) : base(deserializing) { }
52    protected DiscreteCorrelationAnalyzer(DiscreteCorrelationAnalyzer original, Cloner cloner) : base(original, cloner) { }
53    public DiscreteCorrelationAnalyzer() {
54      Parameters.Add(new ScopeTreeLookupParameter<IntegerVector>("IntegerVector", "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 DiscreteCorrelationAnalyzer(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 = IntegerVectorParameter.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.