source: branches/2989_MovingPeaksBenchmark/HeuristicLab.Problems.MovingPeaksBenchmark/3.3/Analyzers/BestMovingPeaksBenchmarkSolutionAnalyzer.cs @ 16611

Last change on this file since 16611 was 16611, checked in by swagner, 3 years ago

#2989: Moving Peaks Benchmark

  • added calculation of offline error
File size: 7.5 KB
Line 
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
22using System;
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.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      if (bestKnownQuality == null ||
105          max && qualities[i].Value > bestKnownQuality.Value
106          || !max && qualities[i].Value < bestKnownQuality.Value) {
107        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
108        BestKnownSolutionParameter.ActualValue = (RealVector)realVectors[i].Clone();
109      }
110
111      RealVector best = (RealVector)realVectors[i].Clone();
112      RealVector bestKnown = (RealVector)BestKnownSolutionParameter.ActualValue.Clone();
113      ResultCollection results = ResultsParameter.ActualValue;
114      IResult bestSolution, bestKnownSolution;
115      if (!results.TryGetValue("Best Solution", out bestSolution)) {
116        bestSolution = new Result("Best Solution", best);
117        results.Add(bestSolution);
118      } else {
119        bestSolution.Value = best;
120      }
121      if (!results.TryGetValue("Best Known Solution", out bestKnownSolution)) {
122        bestKnownSolution = new Result("Best Known Solution", bestKnown);
123        results.Add(bestKnownSolution);
124      } else {
125        bestKnownSolution.Value = bestKnown;
126      }
127
128      double distanceToOptimum = 0;
129      for (int j = 0; j < best.Length; j++) {
130        distanceToOptimum += (best[j] - bestKnown[j]) * (best[j] - bestKnown[j]);
131      }
132      distanceToOptimum = Math.Sqrt(distanceToOptimum);
133
134      IResult distanceTable;
135      if (!results.TryGetValue("Distance to Optimum", out distanceTable)) {
136        DataTable table = new DataTable("Distance to Optimum");
137        table.Rows.Add(new DataRow("Distance to Optimum"));
138        table.Rows["Distance to Optimum"].VisualProperties.StartIndexZero = true;
139        distanceTable = new Result("Distance to Optimum", table);
140        results.Add(distanceTable);
141      }
142      (distanceTable.Value as DataTable).Rows["Distance to Optimum"].Values.Add(distanceToOptimum);
143
144      IResult offlineErrorTable;
145      if (!results.TryGetValue("Offline Error Chart", out offlineErrorTable)) {
146        DataTable table = new DataTable("Offline Error");
147        table.Rows.Add(new DataRow("Offline Error"));
148        table.Rows["Offline Error"].VisualProperties.StartIndexZero = true;
149        offlineErrorTable = new Result("Offline Error Chart", table);
150        results.Add(offlineErrorTable);
151      }
152      (offlineErrorTable.Value as DataTable).Rows["Offline Error"].Values.Add((results["Offline Error"].Value as DoubleValue).Value);
153
154      return base.Apply();
155    }
156  }
157}
Note: See TracBrowser for help on using the repository browser.