1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2019 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;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Analysis;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
29 | using HeuristicLab.Operators;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 | using HeuristicLab.Parameters;
|
---|
32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Problems.MovingPeaksBenchmark {
|
---|
35 | /// <summary>
|
---|
36 | /// An operator for analyzing the best solution for a SingleObjectiveTestFunction problem.
|
---|
37 | /// </summary>
|
---|
38 | [Item("BestMovingPeaksBenchmarkSolutionAnalyzer", "An operator for analyzing the best solution for a Moving Peaks Benchmark problem.")]
|
---|
39 | [StorableClass]
|
---|
40 | public class BestMovingPeaksBenchmarkSolutionAnalyzer : SingleSuccessorOperator, IBestMovingPeaksBenchmarkSolutionAnalyzer {
|
---|
41 | public virtual bool EnabledByDefault {
|
---|
42 | get { return true; }
|
---|
43 | }
|
---|
44 |
|
---|
45 | public LookupParameter<BoolValue> MaximizationParameter {
|
---|
46 | get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
47 | }
|
---|
48 | public ScopeTreeLookupParameter<RealVector> RealVectorParameter {
|
---|
49 | get { return (ScopeTreeLookupParameter<RealVector>)Parameters["RealVector"]; }
|
---|
50 | }
|
---|
51 | ILookupParameter IBestMovingPeaksBenchmarkSolutionAnalyzer.RealVectorParameter {
|
---|
52 | get { return RealVectorParameter; }
|
---|
53 | }
|
---|
54 | public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
55 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
56 | }
|
---|
57 | ILookupParameter IBestMovingPeaksBenchmarkSolutionAnalyzer.QualityParameter {
|
---|
58 | get { return QualityParameter; }
|
---|
59 | }
|
---|
60 | public ILookupParameter<RealVector> BestKnownSolutionParameter {
|
---|
61 | get { return (ILookupParameter<RealVector>)Parameters["BestKnownSolution"]; }
|
---|
62 | }
|
---|
63 | public ILookupParameter<DoubleValue> BestKnownQualityParameter {
|
---|
64 | get { return (ILookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
65 | }
|
---|
66 | public IValueLookupParameter<ResultCollection> ResultsParameter {
|
---|
67 | get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
68 | }
|
---|
69 |
|
---|
70 | [StorableConstructor]
|
---|
71 | protected BestMovingPeaksBenchmarkSolutionAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
72 | protected BestMovingPeaksBenchmarkSolutionAnalyzer(BestMovingPeaksBenchmarkSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
73 | public BestMovingPeaksBenchmarkSolutionAnalyzer()
|
---|
74 | : base() {
|
---|
75 | Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem."));
|
---|
76 | Parameters.Add(new ScopeTreeLookupParameter<RealVector>("RealVector", "The SingleObjectiveTestFunction solutions from which the best solution should be visualized."));
|
---|
77 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the SingleObjectiveTestFunction solutions which should be visualized."));
|
---|
78 | Parameters.Add(new LookupParameter<RealVector>("BestKnownSolution", "The best known solution."));
|
---|
79 | Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution."));
|
---|
80 | Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the SingleObjectiveTestFunction solution should be stored."));
|
---|
81 |
|
---|
82 | MaximizationParameter.Hidden = true;
|
---|
83 | RealVectorParameter.Hidden = true;
|
---|
84 | QualityParameter.Hidden = true;
|
---|
85 | BestKnownSolutionParameter.Hidden = true;
|
---|
86 | BestKnownQualityParameter.Hidden = true;
|
---|
87 | ResultsParameter.Hidden = true;
|
---|
88 | }
|
---|
89 |
|
---|
90 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
91 | return new BestMovingPeaksBenchmarkSolutionAnalyzer(this, cloner);
|
---|
92 | }
|
---|
93 |
|
---|
94 | public override IOperation Apply() {
|
---|
95 | ItemArray<RealVector> realVectors = RealVectorParameter.ActualValue;
|
---|
96 | ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
|
---|
97 | bool max = MaximizationParameter.ActualValue.Value;
|
---|
98 | DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
|
---|
99 |
|
---|
100 | int i = -1;
|
---|
101 | if (!max) i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
|
---|
102 | else i = qualities.Select((x, index) => new { index, x.Value }).OrderByDescending(x => x.Value).First().index;
|
---|
103 |
|
---|
104 | RealVector best = (RealVector)realVectors[i].Clone();
|
---|
105 | RealVector bestKnown = (RealVector)BestKnownSolutionParameter.ActualValue.Clone();
|
---|
106 | ResultCollection results = ResultsParameter.ActualValue;
|
---|
107 | IResult bestSolution, bestKnownSolution;
|
---|
108 | if (!results.TryGetValue("Best Solution", out bestSolution)) {
|
---|
109 | bestSolution = new Result("Best Solution", best);
|
---|
110 | results.Add(bestSolution);
|
---|
111 | } else {
|
---|
112 | bestSolution.Value = best;
|
---|
113 | }
|
---|
114 | if (!results.TryGetValue("Best Known Solution", out bestKnownSolution)) {
|
---|
115 | bestKnownSolution = new Result("Best Known Solution", bestKnown);
|
---|
116 | results.Add(bestKnownSolution);
|
---|
117 | } else {
|
---|
118 | bestKnownSolution.Value = bestKnown;
|
---|
119 | }
|
---|
120 |
|
---|
121 | double distanceToOptimum = 0;
|
---|
122 | for (int j = 0; j < best.Length; j++) {
|
---|
123 | distanceToOptimum += (best[j] - bestKnown[j]) * (best[j] - bestKnown[j]);
|
---|
124 | }
|
---|
125 | distanceToOptimum = Math.Sqrt(distanceToOptimum);
|
---|
126 |
|
---|
127 | IResult distanceTable;
|
---|
128 | if (!results.TryGetValue("Distance to Optimum", out distanceTable)) {
|
---|
129 | DataTable table = new DataTable("Distance to Optimum");
|
---|
130 | table.Rows.Add(new DataRow("Distance to Optimum"));
|
---|
131 | table.Rows["Distance to Optimum"].VisualProperties.StartIndexZero = true;
|
---|
132 | distanceTable = new Result("Distance to Optimum", table);
|
---|
133 | results.Add(distanceTable);
|
---|
134 | }
|
---|
135 | (distanceTable.Value as DataTable).Rows["Distance to Optimum"].Values.Add(distanceToOptimum);
|
---|
136 |
|
---|
137 | IResult offlineErrorTable;
|
---|
138 | if (!results.TryGetValue("Offline Error Chart", out offlineErrorTable)) {
|
---|
139 | DataTable table = new DataTable("Offline Error");
|
---|
140 | table.Rows.Add(new DataRow("Offline Error"));
|
---|
141 | table.Rows["Offline Error"].VisualProperties.StartIndexZero = true;
|
---|
142 | offlineErrorTable = new Result("Offline Error Chart", table);
|
---|
143 | results.Add(offlineErrorTable);
|
---|
144 | }
|
---|
145 | (offlineErrorTable.Value as DataTable).Rows["Offline Error"].Values.Add((results["Offline Error"].Value as DoubleValue).Value);
|
---|
146 |
|
---|
147 | return base.Apply();
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|