#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("PopulationAnalyzer", "An analyzer that records the best parameter configuration.")] [StorableClass] public sealed class PopulationAnalyzer : SingleSuccessorOperator, IAnalyzer { public bool EnabledByDefault { get { return true; } } public ScopeTreeLookupParameter ParameterConfigurationParameter { get { return (ScopeTreeLookupParameter)Parameters["ParameterConfigurationTree"]; } } public ValueLookupParameter ResultsParameter { get { return (ValueLookupParameter)Parameters["Results"]; } } public LookupParameter PopulationParameter { get { return (LookupParameter)Parameters["Population"]; } } public LookupParameter MaximizationParameter { get { return (LookupParameter)Parameters["Maximization"]; } } #region Constructors and Cloning public PopulationAnalyzer() : base() { Parameters.Add(new ScopeTreeLookupParameter("ParameterConfigurationTree", "")); Parameters.Add(new ValueLookupParameter("Results", "")); Parameters.Add(new LookupParameter("Population", "")); Parameters.Add(new LookupParameter("Maximization", "Set to false if the problem should be minimized.")); } [StorableConstructor] private PopulationAnalyzer(bool deserializing) : base(deserializing) { } private PopulationAnalyzer(PopulationAnalyzer original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new PopulationAnalyzer(this, cloner); } #endregion public override IOperation Apply() { ResultCollection results = ResultsParameter.ActualValue; ItemArray parameterTrees = ParameterConfigurationParameter.ActualValue; bool maximization = MaximizationParameter.ActualValue.Value; 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(); } } }