#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 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 HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Operators;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Analysis.QualityAnalysis {
[Item("ScaledQualityDifferenceAnalyzer", @"Calculates the quality value relative to a certain range given with a minimum and a maximum value.
The difference lies in the interval [0;1] if the range [min;max] is as large as the observed quality values, otherwise the difference will become < 0 or > 1.
A value towards 0 always means that it's closer to the better fitness value, while a value towards 1 means that it's closer to the worse fitness value.")]
[StorableClass("DFE2E8CE-6274-485F-A418-F9BA258D58A0")]
public class ScaledQualityDifferenceAnalyzer : SingleSuccessorOperator, IAnalyzer, ISingleObjectiveOperator {
public virtual bool EnabledByDefault {
get { return true; }
}
public ILookupParameter QualityParameter {
get { return (ILookupParameter)Parameters["Quality"]; }
}
public ILookupParameter MaximizationParameter {
get { return (ILookupParameter)Parameters["Maximization"]; }
}
public ILookupParameter ScaledDifferenceParameter {
get { return (ILookupParameter)Parameters["ScaledDifference"]; }
}
public ILookupParameter ResultsParameter {
get { return (ILookupParameter)Parameters["Results"]; }
}
public IValueLookupParameter MinimumQualityParameter {
get { return (IValueLookupParameter)Parameters["MinimumQuality"]; }
}
public IValueLookupParameter MaximumQualityParameter {
get { return (IValueLookupParameter)Parameters["MaximumQuality"]; }
}
[StorableConstructor]
protected ScaledQualityDifferenceAnalyzer(bool deserializing) : base(deserializing) { }
protected ScaledQualityDifferenceAnalyzer(ScaledQualityDifferenceAnalyzer original, Cloner cloner) : base(original, cloner) { }
public ScaledQualityDifferenceAnalyzer()
: base() {
Parameters.Add(new LookupParameter("Quality", "The quality value that should be compared."));
Parameters.Add(new LookupParameter("Maximization", "True if the problem is a maximization problem, false otherwise."));
Parameters.Add(new LookupParameter("ScaledDifference", "The value that describes whether the quality value lies towards the better quality in the range (<=0.5) or more towards the worse quality (> 0.5)."));
Parameters.Add(new LookupParameter("Results", "The result collection where the difference will be stored (in addition to the scope)."));
Parameters.Add(new ValueLookupParameter("MinimumQuality", "The lower bound of the quality range to which the quality is compared."));
Parameters.Add(new ValueLookupParameter("MaximumQuality", "The upper bound of the quality range to which the quality is compared."));
}
public override IDeepCloneable Clone(Cloner cloner) {
return new ScaledQualityDifferenceAnalyzer(this, cloner);
}
public override IOperation Apply() {
bool maximization = MaximizationParameter.ActualValue.Value;
double quality = QualityParameter.ActualValue.Value, max = MaximumQualityParameter.ActualValue.Value, min = MinimumQualityParameter.ActualValue.Value;
double difference;
difference = ((quality - min) / (max - min));
if (maximization) difference = 1.0 - difference;
DoubleValue differenceValue = ScaledDifferenceParameter.ActualValue;
if (differenceValue == null) {
differenceValue = new DoubleValue(difference);
ScaledDifferenceParameter.ActualValue = differenceValue;
} else differenceValue.Value = difference;
ResultCollection results = ResultsParameter.ActualValue;
if (results != null) {
IResult r;
if (!results.TryGetValue(ScaledDifferenceParameter.TranslatedName, out r)) {
r = new Result(ScaledDifferenceParameter.TranslatedName, differenceValue);
results.Add(r);
}
}
return base.Apply();
}
}
}