#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.IntegerVectorEncoding; using HeuristicLab.Operators; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Problems.DataAnalysis; namespace HeuristicLab.Algorithms.EGO { [Item("DiscreteCorrelationAnalyzer", "Analyzes the correlation between perdictions and actual fitness values")] [StorableType("1657262e-1ada-4672-a073-bc8b144ecf42")] public class DiscreteCorrelationAnalyzer : SingleSuccessorOperator, IAnalyzer, IResultsOperator { public override bool CanChangeName => true; public bool EnabledByDefault => false; public IScopeTreeLookupParameter IntegerVectorParameter => (IScopeTreeLookupParameter)Parameters["IntegerVector"]; 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 DiscreteCorrelationAnalyzer(StorableConstructorFlag deserializing) : base(deserializing) { } protected DiscreteCorrelationAnalyzer(DiscreteCorrelationAnalyzer original, Cloner cloner) : base(original, cloner) { } public DiscreteCorrelationAnalyzer() { Parameters.Add(new ScopeTreeLookupParameter("IntegerVector", "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 DiscreteCorrelationAnalyzer(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 = IntegerVectorParameter.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; } } }