Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14085 was 14085, checked in by mkommend, 8 years ago

#1087: Further refactoring of testfunction problem and analyzers.

File size: 4.2 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 ILookupParameter<DoubleArray> ReferencePointParameter {
37      get { return (ILookupParameter<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
52    protected HypervolumeAnalyzer(HypervolumeAnalyzer original, Cloner cloner)
53      : base(original, cloner) {
54    }
55    public override IDeepCloneable Clone(Cloner cloner) {
56      return new HypervolumeAnalyzer(this, cloner);
57    }
58
59    public HypervolumeAnalyzer() {
60      Parameters.Add(new LookupParameter<DoubleArray>("ReferencePoint", "The reference point for hypervolume calculation"));
61      Parameters.Add(new FixedValueParameter<DoubleValue>("BestKnownHyperVolume", "The currently best known hypervolume", new DoubleValue(0)));
62    }
63
64    public override IOperation Apply() {
65      var results = ResultsParameter.ActualValue;
66      var qualities = QualitiesParameter.ActualValue;
67      var testFunction = TestFunctionParameter.ActualValue;
68      var referencePoint = ReferencePointParameter.ActualValue;
69      int objectives = qualities[0].Length;
70
71
72
73      if (!results.ContainsKey("Hypervolume")) results.Add(new Result("Hypervolume", typeof(DoubleValue)));
74      if (!results.ContainsKey("Absolute Distance to BestKnownHypervolume")) results.Add(new Result("Absolute Distance to BestKnownHypervolume", typeof(DoubleValue)));
75
76      double best;
77      if (!results.ContainsKey("BestKnownHypervolume")) {
78        results.Add(new Result("BestKnownHypervolume", typeof(DoubleValue)));
79        best = BestKnownHyperVolumeParameter.Value.Value;
80      } else {
81        best = ((DoubleValue)(results["BestKnownHypervolume"].Value)).Value;
82      }
83
84
85      IEnumerable<double[]> front = NonDominatedSelect.SelectNonDominatedVectors(qualities.Select(q => q.ToArray()), testFunction.Maximization(objectives), true);
86
87      double hv = front.Any() ? Hypervolume.Calculate(front, referencePoint.CloneAsArray(), testFunction.Maximization(objectives)) : 0;
88
89
90      if (double.IsNaN(best) || best < hv) {
91        best = hv;
92        BestKnownFrontParameter.ActualValue = new DoubleMatrix(MultiObjectiveTestFunctionProblem.To2D(qualities.Select(q => q.ToArray()).ToArray()));
93      }
94
95      results["Hypervolume"].Value = new DoubleValue(hv);
96      results["BestKnownHypervolume"].Value = new DoubleValue(best);
97      results["Absolute Distance to BestKnownHypervolume"].Value = new DoubleValue(best - hv);
98
99      return base.Apply();
100    }
101
102  }
103}
Note: See TracBrowser for help on using the repository browser.