Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13988 was 13988, checked in by bwerth, 8 years ago

#1087 moved project bugfixes

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;
23using System.Collections.Generic;
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("GenerationalDistanceAnalyzer", "Computes the enclosed Hypervolume between the current front and a given reference Point")]
34  public class HypervolumeAnalyzer : MOTFAnalyzer {
35    [StorableHook(HookType.AfterDeserialization)]
36    private void AfterDeserialization() {
37    }
38    [StorableConstructor]
39    protected HypervolumeAnalyzer(bool deserializing) : base(deserializing) { }
40    public HypervolumeAnalyzer(HypervolumeAnalyzer original, Cloner cloner) : base(original, cloner) { }
41    public override IDeepCloneable Clone(Cloner cloner) {
42      return new HypervolumeAnalyzer(this, cloner);
43    }
44
45    public IValueParameter<DoubleArray> ReferencePointParameter {
46      get {
47        return (IValueParameter<DoubleArray>)Parameters["ReferencePoint"];
48      }
49    }
50
51    public IValueParameter<DoubleValue> BestKnownHyperVolumeParameter {
52      get {
53        return (IValueParameter<DoubleValue>)Parameters["BestKnownHyperVolume"];
54      }
55      set {
56        Parameters["BestKnownHyperVolume"].ActualValue = value;
57      }
58    }
59
60    public HypervolumeAnalyzer() {
61      Parameters.Add(new ValueParameter<DoubleArray>("ReferencePoint", "The reference point for hypervolume calculation"));
62      Parameters.Add(new ValueParameter<DoubleValue>("BestKnownHyperVolume", "The currently best known hypervolume"));
63    }
64
65    private void RegisterEventHandlers() {
66      ReferencePointParameter.ValueChanged += ReferencePointParameterOnValueChanged;
67    }
68
69    private void ReferencePointParameterOnValueChanged(object sender, EventArgs e) {
70      BestKnownHyperVolumeParameter.Value = new DoubleValue(0);
71    }
72
73    public override void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results) {
74      if (qualities == null || qualities.Length < 1) return;
75      int objectives = qualities[0].Length;
76      double best = BestKnownHyperVolumeParameter.Value.Value;
77
78      double diff;
79
80      if (!results.ContainsKey("Hypervolume")) results.Add(new Result("Hypervolume", typeof(DoubleValue)));
81      IEnumerable<double[]> front = NonDominatedSelect.selectNonDominatedVectors(qualities, TestFunctionParameter.ActualValue.Maximization(objectives), true);
82      if (!results.ContainsKey("BestKnownHypervolume")) results.Add(new Result("BestKnownHypervolume", typeof(DoubleValue)));
83      else {
84        DoubleValue dv = (DoubleValue)(results["BestKnownHypervolume"].Value);
85        best = dv.Value;
86      }
87      if (!results.ContainsKey("Absolute Distance to BestKnownHypervolume")) results.Add(new Result("Absolute Distance to BestKnownHypervolume", typeof(DoubleValue)));
88
89      double hv = Double.NaN;
90
91      hv = Hypervolume.Calculate(front, TestFunctionParameter.ActualValue.ReferencePoint(objectives), TestFunctionParameter.ActualValue.Maximization(objectives));
92
93
94      if (best < 0) {
95        best = hv;
96      }
97      diff = best - hv;
98      if (!Double.IsNaN(hv) && (diff < 0 || best < 0)) {
99        best = hv;
100        diff = 0;
101        BestKnownFrontParameter.ActualValue = new DoubleMatrix(MultiObjectiveTestFunctionProblem.To2D(qualities));
102      }
103
104      results["Hypervolume"].Value = new DoubleValue(hv);
105      results["Absolute Distance to BestKnownHypervolume"].Value = new DoubleValue(diff);
106      results["BestKnownHypervolume"].Value = new DoubleValue(best);
107
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.