source: branches/HeuristicLab.Problems.MultiObjectiveTestFunctions/HeuristicLab.Problems.MultiObjectiveTestFunctions/3.3/Analyzers/HypervolumeAnalyzer.cs @ 14081

Last change on this file since 14081 was 14081, checked in by mkommend, 6 years ago

#1087: Refactored utility class NonDominatedSelect.

File size: 4.5 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.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.MultiObjectiveTestFunctions {
32  [StorableClass]
33  [Item("HypervolumeAnalyzer", "Computes the enclosed Hypervolume between the current front and a given reference Point")]
34  public class HypervolumeAnalyzer : MOTFAnalyzer {
35
36    public IValueParameter<DoubleArray> ReferencePointParameter {
37      get { return (IValueParameter<DoubleArray>)Parameters["ReferencePoint"]; }
38    }
39
40    public IFixedValueParameter<DoubleValue> BestKnownHyperVolumeParameter {
41      get { return (IFixedValueParameter<DoubleValue>)Parameters["BestKnownHyperVolume"]; }
42    }
43
44    public double BestKnownHyperVolume {
45      get { return BestKnownHyperVolumeParameter.Value.Value; }
46      set { BestKnownHyperVolumeParameter.Value.Value = value; }
47    }
48
49    [StorableConstructor]
50    protected HypervolumeAnalyzer(bool deserializing) : base(deserializing) { }
51    [StorableHook(HookType.AfterDeserialization)]
52    private void AfterDeserialization() {
53      RegisterEventHandlers();
54    }
55
56    protected HypervolumeAnalyzer(HypervolumeAnalyzer original, Cloner cloner)
57      : base(original, cloner) {
58      RegisterEventHandlers();
59    }
60    public override IDeepCloneable Clone(Cloner cloner) {
61      return new HypervolumeAnalyzer(this, cloner);
62    }
63
64    public HypervolumeAnalyzer() {
65      Parameters.Add(new ValueParameter<DoubleArray>("ReferencePoint", "The reference point for hypervolume calculation"));
66      Parameters.Add(new FixedValueParameter<DoubleValue>("BestKnownHyperVolume", "The currently best known hypervolume", new DoubleValue(0)));
67
68      RegisterEventHandlers();
69    }
70
71    private void RegisterEventHandlers() {
72      ReferencePointParameter.ValueChanged += (o, e) => BestKnownHyperVolume = 0;
73    }
74
75    public override IOperation Apply() {
76      var results = ResultsParameter.ActualValue;
77      var qualities = QualitiesParameter.ActualValue;
78      var testFunction = TestFunctionParameter.ActualValue;
79      int objectives = qualities[0].Length;
80
81      if (!results.ContainsKey("Hypervolume")) results.Add(new Result("Hypervolume", typeof(DoubleValue)));
82      if (!results.ContainsKey("Absolute Distance to BestKnownHypervolume")) results.Add(new Result("Absolute Distance to BestKnownHypervolume", typeof(DoubleValue)));
83
84      double best;
85      if (!results.ContainsKey("BestKnownHypervolume")) {
86        results.Add(new Result("BestKnownHypervolume", typeof(DoubleValue)));
87        best = BestKnownHyperVolumeParameter.Value.Value;
88      } else {
89        best = ((DoubleValue)(results["BestKnownHypervolume"].Value)).Value;
90      }
91
92
93      IEnumerable<double[]> front = NonDominatedSelect.SelectNonDominatedVectors(qualities.Select(q => q.ToArray()), testFunction.Maximization(objectives), true);
94
95      double hv = front.Any() ? Hypervolume.Calculate(front, testFunction.ReferencePoint(objectives), testFunction.Maximization(objectives)) : 0;
96
97
98      if (double.IsNaN(best) || best < hv) {
99        best = hv;
100        BestKnownFrontParameter.ActualValue = new DoubleMatrix(MultiObjectiveTestFunctionProblem.To2D(qualities.Select(q => q.ToArray()).ToArray()));
101      }
102
103      results["Hypervolume"].Value = new DoubleValue(hv);
104      results["BestKnownHypervolume"].Value = new DoubleValue(best);
105      results["Absolute Distance to BestKnownHypervolume"].Value = new DoubleValue(best - hv);
106
107      return base.Apply();
108    }
109
110  }
111}
Note: See TracBrowser for help on using the repository browser.