#region License Information
/* HeuristicLab
* Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Problems.MultiObjectiveTestFunctions {
[StorableClass]
[Item("HypervolumeAnalyzer", "Computes the enclosed Hypervolume between the current front and a given reference Point")]
public class HypervolumeAnalyzer : MOTFAnalyzer {
[StorableConstructor]
protected HypervolumeAnalyzer(bool deserializing) : base(deserializing) { }
protected HypervolumeAnalyzer(HypervolumeAnalyzer original, Cloner cloner)
: base(original, cloner) {
}
public override IDeepCloneable Clone(Cloner cloner) {
return new HypervolumeAnalyzer(this, cloner);
}
public HypervolumeAnalyzer() { }
public override IOperation Apply() {
var results = ResultsParameter.ActualValue;
var qualities = QualitiesParameter.ActualValue;
var testFunction = TestFunctionParameter.ActualValue;
int objectives = qualities[0].Length;
var referencePoint = testFunction.ReferencePoint(objectives);
if (!results.ContainsKey("Hypervolume")) results.Add(new Result("Hypervolume", typeof(DoubleValue)));
if (!results.ContainsKey("Absolute Distance to BestKnownHypervolume")) results.Add(new Result("Absolute Distance to BestKnownHypervolume", typeof(DoubleValue)));
double best = testFunction.OptimalHypervolume(objectives);
if (!results.ContainsKey("BestKnownHypervolume")) {
results.Add(new Result("BestKnownHypervolume", typeof(DoubleValue)));
}
best = Math.Max(best, ((DoubleValue)(results["BestKnownHypervolume"].Value)).Value);
IEnumerable front = NonDominatedSelect.SelectNonDominatedVectors(qualities.Select(q => q.ToArray()), testFunction.Maximization(objectives), true);
double hv = Hypervolume.Calculate(front, (double[])referencePoint.Clone(), testFunction.Maximization(objectives));
if (double.IsNaN(best) || best < hv) {
best = hv;
BestKnownFrontParameter.ActualValue = new DoubleMatrix(MultiObjectiveTestFunctionProblem.To2D(qualities.Select(q => q.ToArray()).ToArray()));
}
((DoubleValue)(results["Hypervolume"].Value)).Value = hv;
((DoubleValue)(results["BestKnownHypervolume"].Value)).Value = best;
((DoubleValue)(results["Absolute Distance to BestKnownHypervolume"].Value)).Value = best - hv;
return base.Apply();
}
}
}