1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Analysis;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
29 | using HeuristicLab.Operators;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 | using HeuristicLab.Parameters;
|
---|
32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Algorithms.EGO {
|
---|
35 | [Item("DiscreteEvaluatedSolutionsAnalyzer", "Displays the evaluated Solutions for one or two dimensional problems")]
|
---|
36 | [StorableClass]
|
---|
37 | public class DiscreteEvaluatedSolutionsAnalyzer : SingleSuccessorOperator, IAnalyzer, IResultsOperator {
|
---|
38 | public override bool CanChangeName => true;
|
---|
39 | public bool EnabledByDefault => false;
|
---|
40 |
|
---|
41 | public IScopeTreeLookupParameter<IntegerVector> IntegerVectorParameter => (IScopeTreeLookupParameter<IntegerVector>)Parameters["IntegerVector"];
|
---|
42 | public IScopeTreeLookupParameter<DoubleValue> QualityParameter => (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"];
|
---|
43 | public ILookupParameter<ResultCollection> ResultsParameter => (ILookupParameter<ResultCollection>)Parameters["Results"];
|
---|
44 | public ILookupParameter<BoolValue> PhaseParameter => (ILookupParameter<BoolValue>)Parameters["InitialPhase"];
|
---|
45 |
|
---|
46 | private const string PlotName = "Evaluated Solutions Plot";
|
---|
47 | private const string InitialRowName = "Initial Samples";
|
---|
48 | private const string AdaptiveRowName = "Adaptive Samples";
|
---|
49 |
|
---|
50 |
|
---|
51 | [StorableConstructor]
|
---|
52 | protected DiscreteEvaluatedSolutionsAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
53 | protected DiscreteEvaluatedSolutionsAnalyzer(DiscreteEvaluatedSolutionsAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
54 | public DiscreteEvaluatedSolutionsAnalyzer() {
|
---|
55 | Parameters.Add(new ScopeTreeLookupParameter<IntegerVector>("IntegerVector", "The vector which should be collected."));
|
---|
56 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The quality associated which this vector"));
|
---|
57 | Parameters.Add(new LookupParameter<BoolValue>("InitialPhase", "Whether the initial phase has finished"));
|
---|
58 | Parameters.Add(new LookupParameter<ResultCollection>("Results", "The collection to store the results in."));
|
---|
59 | }
|
---|
60 |
|
---|
61 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
62 | return new DiscreteEvaluatedSolutionsAnalyzer(this, cloner);
|
---|
63 | }
|
---|
64 |
|
---|
65 | public sealed override IOperation Apply() {
|
---|
66 | var p = IntegerVectorParameter.ActualValue.ToArray();
|
---|
67 | if (p.Length == 0 || p[0].Length > 2 || p[0].Length == 0) return base.Apply();
|
---|
68 | var results = ResultsParameter.ActualValue;
|
---|
69 | var q = QualityParameter.ActualValue.Select(x => x.Value).ToArray();
|
---|
70 |
|
---|
71 | var plot = CreateScatterPlotResult(results, p[0].Length);
|
---|
72 | var points = plot.Rows[PhaseParameter.ActualValue.Value ? AdaptiveRowName : InitialRowName].Points;
|
---|
73 | if (p[0].Length == 2)
|
---|
74 | for (var i = 0; i < q.Length; i++) points.Add(new Point2D<double>(p[i][0], p[i][1], q[i]));
|
---|
75 | else
|
---|
76 | for (var i = 0; i < q.Length; i++) points.Add(new Point2D<double>(p[i][0], q[i]));
|
---|
77 | return base.Apply();
|
---|
78 |
|
---|
79 | }
|
---|
80 |
|
---|
81 | private static ScatterPlot CreateScatterPlotResult(ResultCollection results, int dim) {
|
---|
82 | ScatterPlot plot;
|
---|
83 | if (!results.ContainsKey(PlotName)) {
|
---|
84 | plot = new ScatterPlot("Evaluated Solutions", "The solution evaluated so far") {
|
---|
85 | VisualProperties = {
|
---|
86 | XAxisTitle = "First Dimension",
|
---|
87 | YAxisTitle = dim==2?"Second Dimension":"Quality"
|
---|
88 | }
|
---|
89 | };
|
---|
90 | results.Add(new Result(PlotName, plot));
|
---|
91 | } else { plot = (ScatterPlot)results[PlotName].Value; }
|
---|
92 | if (!plot.Rows.ContainsKey(InitialRowName)) {
|
---|
93 | plot.Rows.Add(new ScatterPlotDataRow(InitialRowName, InitialRowName, new List<Point2D<double>>()));
|
---|
94 | plot.Rows[InitialRowName].VisualProperties.PointSize = 5;
|
---|
95 | }
|
---|
96 | if (!plot.Rows.ContainsKey(AdaptiveRowName)) {
|
---|
97 | plot.Rows.Add(new ScatterPlotDataRow(AdaptiveRowName, AdaptiveRowName, new List<Point2D<double>>()));
|
---|
98 | plot.Rows[AdaptiveRowName].VisualProperties.PointSize = 5;
|
---|
99 | }
|
---|
100 | return plot;
|
---|
101 | }
|
---|
102 |
|
---|
103 | }
|
---|
104 | }
|
---|