Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1087 bugfix + additional relative HV calculation added

File size: 4.8 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      try {
91        if (objectives == 2) { //Hypervolume analysis only with 2 objectives for now
92          hv = Hypervolume.Calculate(front, ReferencePointParameter.Value, TestFunctionParameter.ActualValue.Maximization(objectives));
93        } else if (Array.TrueForAll(TestFunctionParameter.ActualValue.Maximization(objectives), x => !x)) {
94          hv = MultiDimensionalHypervolume.Calculate(front, ReferencePointParameter.Value);
95        }
96      }
97      catch (ArgumentException) {
98
99      }
100
101      if (best < 0) {
102        best = hv;
103      }
104      diff = best - hv;
105      if (!Double.IsNaN(hv) && (diff < 0 || best < 0)) {
106        best = hv;
107        diff = 0;
108        BestKnownFrontParameter.ActualValue = new DoubleMatrix(MultiObjectiveTestFunctionProblem.To2D(qualities));
109      }
110
111      results["Hypervolume"].Value = new DoubleValue(hv);
112      results["Absolute Distance to BestKnownHypervolume"].Value = new DoubleValue(diff);
113      results["BestKnownHypervolume"].Value = new DoubleValue(best);
114
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.