#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 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] public class BestSingleObjectiveTestFunctionSolutionAnalyzer : SingleSuccessorOperator, IBestSingleObjectiveTestFunctionSolutionAnalyzer { public virtual bool EnabledByDefault { get { return true; } } public LookupParameter MaximizationParameter { get { return (LookupParameter)Parameters["Maximization"]; } } public IScopeTreeLookupParameter RealVectorsParameter { get { return (IScopeTreeLookupParameter)Parameters["RealVectors"]; } } public IScopeTreeLookupParameter QualityParameter { get { return (IScopeTreeLookupParameter)Parameters["Quality"]; } } 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 TestFunctionParameter { get { return (IValueLookupParameter)Parameters["TestFunction"]; } } public IValueLookupParameter BoundsParameter { get { return (IValueLookupParameter)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("RealVectors", "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("TestFunction", "The evaluator with which the solution is evaluated.")); Parameters.Add(new ValueLookupParameter("Bounds", "The bounds of the function.")); MaximizationParameter.Hidden = true; RealVectorsParameter.Hidden = true; QualityParameter.Hidden = true; BestSolutionParameter.Hidden = true; BestKnownSolutionParameter.Hidden = true; BestKnownQualityParameter.Hidden = true; ResultsParameter.Hidden = true; TestFunctionParameter.Hidden = true; BoundsParameter.Hidden = true; } public override IDeepCloneable Clone(Cloner cloner) { return new BestSingleObjectiveTestFunctionSolutionAnalyzer(this, cloner); } public override IOperation Apply() { ItemArray realVectors = RealVectorsParameter.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((RealVector)realVectors[i].Clone(), (DoubleValue)qualities[i].Clone(), TestFunctionParameter.ActualValue); solution.Population = realVectors[i].Length == 2 ? new ItemArray(realVectors.Select(x => x.Clone()).Cast()) : 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 = (RealVector)realVectors[i].Clone(); solution.BestQuality = (DoubleValue)qualities[i].Clone(); } solution.Population = realVectors[i].Length == 2 ? new ItemArray(realVectors.Select(x => x.Clone()).Cast()) : null; } return base.Apply(); } } }