using System.Linq; 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.Problems.MetaOptimization { /// /// TODO /// [Item("PMOBestSolutionHistoryAnalyzer", "")] [StorableClass] public sealed class PMOBestSolutionHistoryAnalyzer : 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 ValueLookupParameter ResultsParameter { get { return (ValueLookupParameter)Parameters["Results"]; } } public LookupParameter> ProblemsParameter { get { return (LookupParameter>)Parameters[MetaOptimizationProblem.ProblemsParameterName]; } } public LookupParameter GenerationsParameter { get { return (LookupParameter)Parameters["Generations"]; } } public LookupParameter MaximizationParameter { get { return (LookupParameter)Parameters["Maximization"]; } } public PMOBestSolutionHistoryAnalyzer() : base() { Parameters.Add(new ScopeTreeLookupParameter("ParameterConfigurationTree", "")); Parameters.Add(new ScopeTreeLookupParameter("Quality", "")); Parameters.Add(new ValueLookupParameter("Results", "")); Parameters.Add(new LookupParameter>(MetaOptimizationProblem.ProblemsParameterName)); Parameters.Add(new LookupParameter("Generations", "")); Parameters.Add(new LookupParameter("Maximization", "Set to false if the problem should be minimized.")); } [StorableConstructor] private PMOBestSolutionHistoryAnalyzer(bool deserializing) : base(deserializing) { } private PMOBestSolutionHistoryAnalyzer(PMOBestSolutionHistoryAnalyzer original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new PMOBestSolutionHistoryAnalyzer(this, cloner); } public override IOperation Apply() { ItemArray qualities = QualityParameter.ActualValue; ResultCollection results = ResultsParameter.ActualValue; ItemArray parameterTrees = ParameterConfigurationParameter.ActualValue; int currentGeneration = GenerationsParameter.ActualValue.Value; 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]; string key = "BestSolutionHistory"; RunCollection bestSolutionHistory; if (!results.ContainsKey(key)) { bestSolutionHistory = new RunCollection(); results.Add(new Result(key, bestSolutionHistory)); } else { bestSolutionHistory = results[key].Value as RunCollection; } if (bestSolutionHistory.Count == 0 || best.ParameterInfoString != ((StringValue)bestSolutionHistory.Last().Results["Meta-ParameterInfoString"]).Value) { IRun run = best.ToRun(string.Format("{0}: {1}", currentGeneration, best.ParameterInfoString), false); run.Results.Add("Meta-Generation", new IntValue(currentGeneration)); run.Results.Add("Meta-ParameterInfoString", new StringValue(best.ParameterInfoString)); bestSolutionHistory.Add(run); } return base.Apply(); } } }