Free cookie consent management tool by TermsFeed Policy Generator

source: branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/Operators/EvaluatedSolutionsAnalyzer.cs @ 15064

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

#2745 implemented EGO as EngineAlgorithm + some simplifications in the IInfillCriterion interface

File size: 5.0 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  /// <summary>
36  /// A base class for operators that manipulate real-valued vectors.
37  /// </summary>
38  [Item("EvaluatedSolutionsAnalyzer", "Displays the evaluated Solutions for one or two dimensional problems")]
39  [StorableClass]
40  public class EvaluatedSolutionsAnalyzer : SingleSuccessorOperator, IAnalyzer {
41    public override bool CanChangeName => true;
42    public bool EnabledByDefault => false;
43
44    public IScopeTreeLookupParameter<RealVector> RealVectorParameter => (IScopeTreeLookupParameter<RealVector>)Parameters["RealVector"];
45    public IScopeTreeLookupParameter<DoubleValue> QualityParameter => (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"];
46    public ILookupParameter<ResultCollection> ResultsParameter => (ILookupParameter<ResultCollection>)Parameters["Results"];
47    public ILookupParameter<BoolValue> PhaseParameter => (ILookupParameter<BoolValue>)Parameters["InitialPhase"];
48
49    private const string PlotName = "Evaluated Solutions Plot";
50    private const string InitialRowName = "Initial Samples";
51    private const string AdaptiveRowName = "Adaptive Samples";
52
53
54    [StorableConstructor]
55    protected EvaluatedSolutionsAnalyzer(bool deserializing) : base(deserializing) { }
56    protected EvaluatedSolutionsAnalyzer(EvaluatedSolutionsAnalyzer original, Cloner cloner) : base(original, cloner) { }
57    public EvaluatedSolutionsAnalyzer() {
58      Parameters.Add(new ScopeTreeLookupParameter<RealVector>("RealVector", "The vector which should be collected."));
59      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The quality associated which this vector"));
60      Parameters.Add(new LookupParameter<BoolValue>("InitialPhase", "Whether the initial phase has finished"));
61      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The collection to store the results in."));
62    }
63
64    public override IDeepCloneable Clone(Cloner cloner) {
65      return new EvaluatedSolutionsAnalyzer(this, cloner);
66    }
67
68    public sealed override IOperation Apply() {
69      var p = RealVectorParameter.ActualValue.ToArray();
70      if (p.Length == 0 || p[0].Length > 2 || p[0].Length == 0) return base.Apply();
71      var results = ResultsParameter.ActualValue;
72      var q = QualityParameter.ActualValue.Select(x => x.Value).ToArray();
73
74      var plot = CreateScatterPlotResult(results, p[0].Length);
75      var points = plot.Rows[PhaseParameter.ActualValue.Value ? AdaptiveRowName : InitialRowName].Points;
76      if (p[0].Length == 2)
77        for (var i = 0; i < q.Length; i++) points.Add(new Point2D<double>(p[i][0], p[i][1], q[i]));
78      else
79        for (var i = 0; i < q.Length; i++) points.Add(new Point2D<double>(p[i][0], q[i]));
80      return base.Apply();
81
82    }
83
84    private static ScatterPlot CreateScatterPlotResult(ResultCollection results, int dim) {
85      ScatterPlot plot;
86      if (!results.ContainsKey(PlotName)) {
87        plot = new ScatterPlot("Evaluated Solutions", "The solution evaluated so far") {
88          VisualProperties = {
89            XAxisTitle = "First Dimension",
90            YAxisTitle = dim==2?"Second Dimension":"Quality"
91          }
92        };
93        results.Add(new Result(PlotName, plot));
94      } else { plot = (ScatterPlot)results[PlotName].Value; }
95      if (!plot.Rows.ContainsKey(InitialRowName)) {
96        plot.Rows.Add(new ScatterPlotDataRow(InitialRowName, InitialRowName, new List<Point2D<double>>()));
97        plot.Rows[InitialRowName].VisualProperties.PointSize = 5;
98      }
99      if (!plot.Rows.ContainsKey(AdaptiveRowName)) {
100        plot.Rows.Add(new ScatterPlotDataRow(AdaptiveRowName, AdaptiveRowName, new List<Point2D<double>>()));
101        plot.Rows[AdaptiveRowName].VisualProperties.PointSize = 5;
102      }
103      return plot;
104    }
105
106  }
107}
Note: See TracBrowser for help on using the repository browser.