#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; namespace HeuristicLab.Algorithms.EGO { [Item("DiscreteEvaluatedSolutionsAnalyzer", "Displays the evaluated Solutions for one or two dimensional problems")] [StorableType("7a785e19-b120-4037-8acb-9223f19a2401")] public class DiscreteEvaluatedSolutionsAnalyzer : 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 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 DiscreteEvaluatedSolutionsAnalyzer(StorableConstructorFlag deserializing) : base(deserializing) { } protected DiscreteEvaluatedSolutionsAnalyzer(DiscreteEvaluatedSolutionsAnalyzer original, Cloner cloner) : base(original, cloner) { } public DiscreteEvaluatedSolutionsAnalyzer() { 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("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 DiscreteEvaluatedSolutionsAnalyzer(this, cloner); } public sealed override IOperation Apply() { var p = IntegerVectorParameter.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; } } }