#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; using System.Linq; using HEAL.Attic; using HeuristicLab.Analysis; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Operators; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Problems.DataAnalysis; namespace HeuristicLab.Algorithms.EGO { [Item("VariableVariabilityAnalyzer", "Analyzes the correlation between perdictions and actual fitness values")] [StorableType("3bc82bbb-e9dd-4a50-8241-cfcf230be8c9")] public class VariableVariabilityAnalyzer : SingleSuccessorOperator, IAnalyzer, IResultsOperator { public override bool CanChangeName => true; public bool EnabledByDefault => false; public ILookupParameter DatasetParameter => (ILookupParameter)Parameters["Dataset"]; public ILookupParameter ResultsParameter => (ILookupParameter)Parameters["Results"]; public ILookupParameter InitialEvaluationsParameter => (ILookupParameter)Parameters["Initial Evaluations"]; public IFixedValueParameter LookBackSizeParameter => (IFixedValueParameter)Parameters["LookBackSize"]; private const string NormalizedPlotName = "Normalized Variable Variance"; private const string PlotName = "Variable Variance"; [StorableConstructor] protected VariableVariabilityAnalyzer(StorableConstructorFlag deserializing) : base(deserializing) { } protected VariableVariabilityAnalyzer(VariableVariabilityAnalyzer original, Cloner cloner) : base(original, cloner) { } public VariableVariabilityAnalyzer() { Parameters.Add(new FixedValueParameter("LookBackSize", new IntValue(10))); Parameters.Add(new LookupParameter("Initial Evaluations")); Parameters.Add(new LookupParameter("Dataset")); Parameters.Add(new LookupParameter("Results", "The collection to store the results in.")); } public override IDeepCloneable Clone(Cloner cloner) { return new VariableVariabilityAnalyzer(this, cloner); } public sealed override IOperation Apply() { var dataset = DatasetParameter.ActualValue; var results = ResultsParameter.ActualValue; var initialEvals = InitialEvaluationsParameter.ActualValue.Value; var lbsize = LookBackSizeParameter.Value.Value; var normPlot = CreateScatterPlotResult(results, NormalizedPlotName); var plot = CreateScatterPlotResult(results, PlotName); foreach (var s in dataset.VariableNames) { if (!normPlot.Rows.ContainsKey(s)) normPlot.Rows.Add(new DataRow(s)); if (!plot.Rows.ContainsKey(s)) plot.Rows.Add(new DataRow(s)); if (dataset.Rows < lbsize) continue; var wd = dataset.GetDoubleValues(s, Enumerable.Range(dataset.Rows - lbsize, lbsize)).StandardDeviation(); plot.Rows[s].Values.Add(wd); if (dataset.Rows < Math.Max(initialEvals, lbsize)) continue; var sd = dataset.GetDoubleValues(s, Enumerable.Range(0, initialEvals)).StandardDeviation(); normPlot.Rows[s].Values.Add(wd / sd); } return base.Apply(); } private static DataTable CreateScatterPlotResult(ResultCollection results, string plotname) { DataTable plot; if (!results.ContainsKey(plotname)) { plot = new DataTable(plotname) { VisualProperties = { XAxisTitle = "Iteration", YAxisTitle = plotname.Equals(NormalizedPlotName)? "Normalized Variance (Variance of last Samples)/(Variance of Initial Samples)" : "Standard deviation of last samples" } }; results.Add(new Result(plotname, plot)); } plot = (DataTable)results[plotname].Value; return plot; } } }