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("BestParameterConfigurationAnalyzer", "")]
[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> ProblemsParameter {
get { return (LookupParameter>)Parameters[MetaOptimizationProblem.ProblemsParameterName]; }
}
public LookupParameter MaximizationParameter {
get { return (LookupParameter)Parameters["Maximization"]; }
}
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>(MetaOptimizationProblem.ProblemsParameterName));
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);
}
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 = (ParameterConfigurationTree)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();
}
}
}