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