#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 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.Linq;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.RealVectorEncoding;
using HeuristicLab.Operators;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Problems.TestFunctions {
///
/// An operator for analyzing the best solution for a SingleObjectiveTestFunction problem.
///
[Item("BestSingleObjectiveTestFunctionSolutionAnalyzer", "An operator for analyzing the best solution for a SingleObjectiveTestFunction problem.")]
[StorableClass]
class BestSingleObjectiveTestFunctionSolutionAnalyzer : SingleSuccessorOperator, IBestSingleObjectiveTestFunctionSolutionAnalyzer, IAnalyzer {
public virtual bool EnabledByDefault {
get { return true; }
}
public LookupParameter MaximizationParameter {
get { return (LookupParameter)Parameters["Maximization"]; }
}
public ScopeTreeLookupParameter RealVectorParameter {
get { return (ScopeTreeLookupParameter)Parameters["RealVector"]; }
}
ILookupParameter IBestSingleObjectiveTestFunctionSolutionAnalyzer.RealVectorParameter {
get { return RealVectorParameter; }
}
public ScopeTreeLookupParameter QualityParameter {
get { return (ScopeTreeLookupParameter)Parameters["Quality"]; }
}
ILookupParameter IBestSingleObjectiveTestFunctionSolutionAnalyzer.QualityParameter {
get { return QualityParameter; }
}
public ILookupParameter BestSolutionParameter {
get { return (ILookupParameter)Parameters["BestSolution"]; }
}
public ILookupParameter BestKnownSolutionParameter {
get { return (ILookupParameter)Parameters["BestKnownSolution"]; }
}
public ILookupParameter BestKnownQualityParameter {
get { return (ILookupParameter)Parameters["BestKnownQuality"]; }
}
public IValueLookupParameter ResultsParameter {
get { return (IValueLookupParameter)Parameters["Results"]; }
}
public IValueLookupParameter EvaluatorParameter {
get { return (IValueLookupParameter)Parameters["Evaluator"]; }
}
public ILookupParameter BoundsParameter {
get { return (ILookupParameter)Parameters["Bounds"]; }
}
[StorableConstructor]
protected BestSingleObjectiveTestFunctionSolutionAnalyzer(bool deserializing) : base(deserializing) { }
protected BestSingleObjectiveTestFunctionSolutionAnalyzer(BestSingleObjectiveTestFunctionSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
public BestSingleObjectiveTestFunctionSolutionAnalyzer()
: base() {
Parameters.Add(new LookupParameter("Maximization", "True if the problem is a maximization problem."));
Parameters.Add(new ScopeTreeLookupParameter("RealVector", "The SingleObjectiveTestFunction solutions from which the best solution should be visualized."));
Parameters.Add(new ScopeTreeLookupParameter("Quality", "The qualities of the SingleObjectiveTestFunction solutions which should be visualized."));
Parameters.Add(new LookupParameter("BestSolution", "The best SingleObjectiveTestFunction solution."));
Parameters.Add(new LookupParameter("BestKnownSolution", "The best known solution."));
Parameters.Add(new LookupParameter("BestKnownQuality", "The quality of the best known solution."));
Parameters.Add(new ValueLookupParameter("Results", "The result collection where the SingleObjectiveTestFunction solution should be stored."));
Parameters.Add(new ValueLookupParameter("Evaluator", "The evaluator with which the solution is evaluated."));
Parameters.Add(new LookupParameter("Bounds", "The bounds of the function."));
MaximizationParameter.Hidden = true;
RealVectorParameter.Hidden = true;
QualityParameter.Hidden = true;
BestSolutionParameter.Hidden = true;
BestKnownSolutionParameter.Hidden = true;
BestKnownQualityParameter.Hidden = true;
ResultsParameter.Hidden = true;
EvaluatorParameter.Hidden = true;
BoundsParameter.Hidden = true;
}
///
/// This method can simply be removed when the plugin version is > 3.3
///
[StorableHook(HookType.AfterDeserialization)]
private void AfterDeserialization() {
// BackwardsCompatibility3.3
// Bounds are introduced in 3.3.0.3894
if (!Parameters.ContainsKey("Bounds"))
Parameters.Add(new LookupParameter("Bounds", "The bounds of the function."));
}
public override IDeepCloneable Clone(Cloner cloner) {
return new BestSingleObjectiveTestFunctionSolutionAnalyzer(this, cloner);
}
public override IOperation Apply() {
ItemArray realVectors = RealVectorParameter.ActualValue;
ItemArray qualities = QualityParameter.ActualValue;
bool max = MaximizationParameter.ActualValue.Value;
DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
SingleObjectiveTestFunctionSolution solution = BestSolutionParameter.ActualValue;
int i = -1;
if (!max) i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
else i = qualities.Select((x, index) => new { index, x.Value }).OrderByDescending(x => x.Value).First().index;
if (bestKnownQuality == null ||
max && qualities[i].Value > bestKnownQuality.Value
|| !max && qualities[i].Value < bestKnownQuality.Value) {
BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
BestKnownSolutionParameter.ActualValue = (RealVector)realVectors[i].Clone();
if (solution != null)
solution.BestKnownRealVector = BestKnownSolutionParameter.ActualValue;
}
if (solution == null) {
ResultCollection results = ResultsParameter.ActualValue;
solution = new SingleObjectiveTestFunctionSolution(realVectors[i], qualities[i], EvaluatorParameter.ActualValue);
solution.Population = realVectors[i].Length == 2 ? realVectors : null;
solution.BestKnownRealVector = BestKnownSolutionParameter.ActualValue;
solution.Bounds = BoundsParameter.ActualValue;
BestSolutionParameter.ActualValue = solution;
results.Add(new Result("Best Solution", solution));
} else {
if (max && qualities[i].Value > solution.BestQuality.Value
|| !max && qualities[i].Value < solution.BestQuality.Value) {
solution.BestRealVector = realVectors[i];
solution.BestQuality = qualities[i];
}
solution.Population = realVectors[i].Length == 2 ? realVectors : null;
}
return base.Apply();
}
}
}