Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2745_EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/Operators/EvaluatedSolutionsAnalyzer.cs @ 16147

Last change on this file since 16147 was 15343, checked in by bwerth, 7 years ago

#2745 added discretized EGO-version for use with IntegerVectors

File size: 4.9 KB
Line 
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
22using System.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Analysis;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.RealVectorEncoding;
29using HeuristicLab.Operators;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33
34namespace HeuristicLab.Algorithms.EGO {
35  [Item("EvaluatedSolutionsAnalyzer", "Displays the evaluated Solutions for one or two dimensional problems")]
36  [StorableClass]
37  public class EvaluatedSolutionsAnalyzer : SingleSuccessorOperator, IAnalyzer, IResultsOperator {
38    public override bool CanChangeName => true;
39    public bool EnabledByDefault => false;
40
41    public IScopeTreeLookupParameter<RealVector> RealVectorParameter => (IScopeTreeLookupParameter<RealVector>)Parameters["RealVector"];
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 EvaluatedSolutionsAnalyzer(bool deserializing) : base(deserializing) { }
53    protected EvaluatedSolutionsAnalyzer(EvaluatedSolutionsAnalyzer original, Cloner cloner) : base(original, cloner) { }
54    public EvaluatedSolutionsAnalyzer() {
55      Parameters.Add(new ScopeTreeLookupParameter<RealVector>("RealVector", "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 EvaluatedSolutionsAnalyzer(this, cloner);
63    }
64
65    public sealed override IOperation Apply() {
66      var p = RealVectorParameter.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}
Note: See TracBrowser for help on using the repository browser.