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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Analysis;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
29 | using HeuristicLab.Operators;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 | using HeuristicLab.Parameters;
|
---|
32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
33 | using HeuristicLab.Problems.DataAnalysis;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.Algorithms.EGO {
|
---|
36 | [Item("DiscreteCorrelationAnalyzer", "Analyzes the correlation between perdictions and actual fitness values")]
|
---|
37 | [StorableClass]
|
---|
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(bool 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 | }
|
---|