#region License Information
/* HeuristicLab
* Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System.Collections.Generic;
using System.Linq;
using HEAL.Attic;
using HeuristicLab.Analysis;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.RealVectorEncoding;
using HeuristicLab.Operators;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Problems.DataAnalysis;
namespace HeuristicLab.Algorithms.EGO {
[Item("CorrelationAnalyzer", "Analyzes the correlation between perdictions and actual fitness values")]
[StorableType("f0dec41c-e5c6-4785-a8f8-af1f40c598b9")]
public class CorrelationAnalyzer : SingleSuccessorOperator, IAnalyzer, IResultsOperator {
public override bool CanChangeName => true;
public bool EnabledByDefault => false;
public IScopeTreeLookupParameter RealVectorParameter => (IScopeTreeLookupParameter)Parameters["RealVector"];
public IScopeTreeLookupParameter QualityParameter => (IScopeTreeLookupParameter)Parameters["Quality"];
public ILookupParameter ModelParameter => (ILookupParameter)Parameters["Model"];
public ILookupParameter ResultsParameter => (ILookupParameter)Parameters["Results"];
private const string PlotName = "Prediction";
private const string RowName = "Samples";
[StorableConstructor]
protected CorrelationAnalyzer(StorableConstructorFlag deserializing) : base(deserializing) { }
protected CorrelationAnalyzer(CorrelationAnalyzer original, Cloner cloner) : base(original, cloner) { }
public CorrelationAnalyzer() {
Parameters.Add(new ScopeTreeLookupParameter("RealVector", "The vector which should be collected."));
Parameters.Add(new ScopeTreeLookupParameter("Quality", "The quality associated which this vector"));
Parameters.Add(new LookupParameter("Model", "The model of this iteration"));
Parameters.Add(new LookupParameter("Results", "The collection to store the results in."));
}
public override IDeepCloneable Clone(Cloner cloner) {
return new CorrelationAnalyzer(this, cloner);
}
public sealed override IOperation Apply() {
var model = ModelParameter.ActualValue;
var results = ResultsParameter.ActualValue;
var q = QualityParameter.ActualValue.Select(x => x.Value).ToArray();
var p = RealVectorParameter.ActualValue.ToArray();
if (model == null) return base.Apply();
var plot = CreateScatterPlotResult(results);
for (var i = 0; i < q.Length; i++) plot.Rows[RowName].Points.Add(new Point2D(model.Model.GetEstimation(p[i]), q[i], p[i]));
return base.Apply();
}
private static ScatterPlot CreateScatterPlotResult(ResultCollection results) {
ScatterPlot plot;
if (!results.ContainsKey(PlotName)) {
plot = new ScatterPlot("Fitness-Prediction-Correlation", "The correlation between the predicted and actual fitness values") {
VisualProperties = {
XAxisTitle = "Predicted Objective Value",
YAxisTitle = "True Objective Value"
}
};
results.Add(new Result(PlotName, plot));
} else { plot = (ScatterPlot)results[PlotName].Value; }
if (!plot.Rows.ContainsKey(RowName)) {
plot.Rows.Add(new ScatterPlotDataRow(RowName, RowName, new List>()));
plot.Rows[RowName].VisualProperties.PointSize = 5;
}
return plot;
}
}
}