#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.ParameterConfigurationEncoding; using HeuristicLab.Operators; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.MetaOptimization { /// /// An analyzer that records the best parameter configuration. /// [Item("BestParameterConfigurationAnalyzer", "An analyzer that records the best parameter configuration.")] [StorableClass] public sealed class BestParameterConfigurationAnalyzer : SingleSuccessorOperator, IAnalyzer { public bool EnabledByDefault { get { return true; } } public ScopeTreeLookupParameter ParameterConfigurationParameter { get { return (ScopeTreeLookupParameter)Parameters["ParameterConfigurationTree"]; } } public ScopeTreeLookupParameter QualityParameter { get { return (ScopeTreeLookupParameter)Parameters["Quality"]; } } public LookupParameter BestSolutionParameter { get { return (LookupParameter)Parameters["BestSolution"]; } } public ValueLookupParameter ResultsParameter { get { return (ValueLookupParameter)Parameters["Results"]; } } public LookupParameter BestKnownQualityParameter { get { return (LookupParameter)Parameters["BestKnownQuality"]; } } public LookupParameter BestKnownSolutionParameter { get { return (LookupParameter)Parameters["BestKnownSolution"]; } } //public LookupParameter PopulationParameter { // get { return (LookupParameter)Parameters["Population"]; } //} public LookupParameter MaximizationParameter { get { return (LookupParameter)Parameters["Maximization"]; } } #region Constructors and Cloning public BestParameterConfigurationAnalyzer() : base() { Parameters.Add(new ScopeTreeLookupParameter("ParameterConfigurationTree", "")); Parameters.Add(new ScopeTreeLookupParameter("Quality", "")); Parameters.Add(new LookupParameter("BestSolution", "")); Parameters.Add(new ValueLookupParameter("Results", "")); Parameters.Add(new LookupParameter("BestKnownQuality", "")); Parameters.Add(new LookupParameter("BestKnownSolution", "")); //Parameters.Add(new LookupParameter("Population", "")); Parameters.Add(new LookupParameter("Maximization", "Set to false if the problem should be minimized.")); } [StorableConstructor] private BestParameterConfigurationAnalyzer(bool deserializing) : base(deserializing) { } private BestParameterConfigurationAnalyzer(BestParameterConfigurationAnalyzer original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new BestParameterConfigurationAnalyzer(this, cloner); } #endregion public override IOperation Apply() { ItemArray qualities = QualityParameter.ActualValue; ResultCollection results = ResultsParameter.ActualValue; DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue; ItemArray parameterTrees = ParameterConfigurationParameter.ActualValue; bool maximization = MaximizationParameter.ActualValue.Value; int idxBest; if (maximization) idxBest = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).Last().index; else idxBest = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index; ParameterConfigurationTree best = parameterTrees[idxBest]; IRun bestRun = new Run(); best.CollectResultValues(bestRun.Results); best.CollectParameterValues(bestRun.Parameters); if (bestKnownQuality == null || (!maximization && (qualities[idxBest].Value < bestKnownQuality.Value) || (maximization && (qualities[idxBest].Value > bestKnownQuality.Value)))) { BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[idxBest].Value); BestKnownSolutionParameter.ActualValue = bestRun; } if (BestSolutionParameter.ActualValue == null) { BestSolutionParameter.ActualValue = bestRun; results.Add(new Result("Best Parameter Settings", bestRun)); } else { BestSolutionParameter.ActualValue = bestRun; results["Best Parameter Settings"].Value = bestRun; } // population (TODO: extract into PopulationAnalyzer) //int i = 0; //RunCollection rc = new RunCollection(); //foreach (ParameterConfigurationTree pt in (maximization ? parameterTrees.OrderByDescending(x => x.Quality) : parameterTrees.OrderBy(x => x.Quality))) { // rc.Add(pt.ToRun(string.Format("Individual {0} ({1})", i, pt.ParameterInfoString), true)); // i++; //} //if (PopulationParameter.ActualValue == null) { // PopulationParameter.ActualValue = rc; // results.Add(new Result("Population", rc)); //} else { // PopulationParameter.ActualValue = rc; // results["Population"].Value = rc; //} return base.Apply(); } } }