#region License Information
/* HeuristicLab
* Copyright (C) 2002-2018 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.TestFunctions.MultiObjective {
[StorableClass]
[Item("HypervolumeAnalyzer", "This analyzer is functionally equivalent to the HypervolumeAnalyzer in HeuristicLab.Analysis, but is kept as not to break backwards compatibility")]
public class HypervolumeAnalyzer : MOTFAnalyzer {
public ILookupParameter ReferencePointParameter => (ILookupParameter)Parameters["ReferencePoint"];
public IResultParameter HypervolumeResultParameter => (IResultParameter)Parameters["Hypervolume"];
public IResultParameter BestKnownHypervolumeResultParameter => (IResultParameter)Parameters["Best known hypervolume"];
public IResultParameter HypervolumeDistanceResultParameter => (IResultParameter)Parameters["Absolute Distance to BestKnownHypervolume"];
[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() {
Parameters.Add(new LookupParameter("ReferencePoint", "The reference point for hypervolume calculation"));
Parameters.Add(new ResultParameter("Hypervolume", "The hypervolume of the current generation"));
Parameters.Add(new ResultParameter("Best known hypervolume", "The optimal hypervolume"));
Parameters.Add(new ResultParameter("Absolute Distance to BestKnownHypervolume", "The difference between the best known and the current hypervolume"));
HypervolumeResultParameter.DefaultValue = new DoubleValue(0);
BestKnownHypervolumeResultParameter.DefaultValue = new DoubleValue(0);
HypervolumeDistanceResultParameter.DefaultValue = new DoubleValue(0);
}
public override IOperation Apply() {
var qualities = QualitiesParameter.ActualValue;
var testFunction = TestFunctionParameter.ActualValue;
var objectives = qualities[0].Length;
var referencePoint = ReferencePointParameter.ActualValue;
var best = BestKnownHypervolumeResultParameter.ActualValue.Value;
if (referencePoint.SequenceEqual(testFunction.ReferencePoint(objectives))) {
best = Math.Max(best, testFunction.OptimalHypervolume(objectives));
}
var hv = HypervolumeCalculator.CalculateHypervolume(qualities.Select(x=>x.CloneAsArray()).ToArray(), referencePoint.ToArray(), testFunction.Maximization(objectives));
if (hv > best) {
best = hv;
}
HypervolumeResultParameter.ActualValue.Value = hv;
BestKnownHypervolumeResultParameter.ActualValue.Value = best;
HypervolumeDistanceResultParameter.ActualValue.Value = best - hv;
return base.Apply();
}
}
}