#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 tracks the history of best parameter configurations.
///
[Item("PMOBestSolutionHistoryAnalyzer", "An analyzer that tracks the history of best solutions.")]
[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 GenerationsParameter {
get { return (LookupParameter)Parameters["Generations"]; }
}
public LookupParameter MaximizationParameter {
get { return (LookupParameter)Parameters["Maximization"]; }
}
#region Constructors and Cloning
public PMOBestSolutionHistoryAnalyzer()
: base() {
Parameters.Add(new ScopeTreeLookupParameter("ParameterConfigurationTree", ""));
Parameters.Add(new ScopeTreeLookupParameter("Quality", ""));
Parameters.Add(new ValueLookupParameter("Results", ""));
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);
}
#endregion
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();
}
}
}