#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 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.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Algorithms.EGO { [Item("EvaluatedSolutionsAnalyzer", "Displays the evaluated Solutions for one or two dimensional problems")] [StorableClass] public class EvaluatedSolutionsAnalyzer : 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 ResultsParameter => (ILookupParameter)Parameters["Results"]; public ILookupParameter PhaseParameter => (ILookupParameter)Parameters["InitialPhase"]; private const string PlotName = "Evaluated Solutions Plot"; private const string InitialRowName = "Initial Samples"; private const string AdaptiveRowName = "Adaptive Samples"; [StorableConstructor] protected EvaluatedSolutionsAnalyzer(bool deserializing) : base(deserializing) { } protected EvaluatedSolutionsAnalyzer(EvaluatedSolutionsAnalyzer original, Cloner cloner) : base(original, cloner) { } public EvaluatedSolutionsAnalyzer() { 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("InitialPhase", "Whether the initial phase has finished")); Parameters.Add(new LookupParameter("Results", "The collection to store the results in.")); } public override IDeepCloneable Clone(Cloner cloner) { return new EvaluatedSolutionsAnalyzer(this, cloner); } public sealed override IOperation Apply() { var p = RealVectorParameter.ActualValue.ToArray(); if (p.Length == 0 || p[0].Length > 2 || p[0].Length == 0) return base.Apply(); var results = ResultsParameter.ActualValue; var q = QualityParameter.ActualValue.Select(x => x.Value).ToArray(); var plot = CreateScatterPlotResult(results, p[0].Length); var points = plot.Rows[PhaseParameter.ActualValue.Value ? AdaptiveRowName : InitialRowName].Points; if (p[0].Length == 2) for (var i = 0; i < q.Length; i++) points.Add(new Point2D(p[i][0], p[i][1], q[i])); else for (var i = 0; i < q.Length; i++) points.Add(new Point2D(p[i][0], q[i])); return base.Apply(); } private static ScatterPlot CreateScatterPlotResult(ResultCollection results, int dim) { ScatterPlot plot; if (!results.ContainsKey(PlotName)) { plot = new ScatterPlot("Evaluated Solutions", "The solution evaluated so far") { VisualProperties = { XAxisTitle = "First Dimension", YAxisTitle = dim==2?"Second Dimension":"Quality" } }; results.Add(new Result(PlotName, plot)); } else { plot = (ScatterPlot)results[PlotName].Value; } if (!plot.Rows.ContainsKey(InitialRowName)) { plot.Rows.Add(new ScatterPlotDataRow(InitialRowName, InitialRowName, new List>())); plot.Rows[InitialRowName].VisualProperties.PointSize = 5; } if (!plot.Rows.ContainsKey(AdaptiveRowName)) { plot.Rows.Add(new ScatterPlotDataRow(AdaptiveRowName, AdaptiveRowName, new List>())); plot.Rows[AdaptiveRowName].VisualProperties.PointSize = 5; } return plot; } } }