[4839] | 1 | using System.Linq;
|
---|
| 2 | using HeuristicLab.Common;
|
---|
| 3 | using HeuristicLab.Core;
|
---|
| 4 | using HeuristicLab.Data;
|
---|
[4832] | 5 | using HeuristicLab.Operators;
|
---|
| 6 | using HeuristicLab.Optimization;
|
---|
[4839] | 7 | using HeuristicLab.Parameters;
|
---|
[4832] | 8 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[16574] | 9 | using HEAL.Attic;
|
---|
[4832] | 10 |
|
---|
| 11 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
| 12 | /// <summary>
|
---|
[5359] | 13 | /// TODO
|
---|
[4832] | 14 | /// </summary>
|
---|
[5281] | 15 | [Item("BestParameterConfigurationAnalyzer", "")]
|
---|
[16574] | 16 | [StorableType("9A11F9C1-B076-45BF-9CCB-661989FC4273")]
|
---|
[4832] | 17 | public sealed class BestParameterConfigurationAnalyzer : SingleSuccessorOperator, IAnalyzer {
|
---|
| 18 |
|
---|
[7173] | 19 | public bool EnabledByDefault {
|
---|
| 20 | get { return true; }
|
---|
| 21 | }
|
---|
| 22 |
|
---|
[5009] | 23 | public ScopeTreeLookupParameter<ParameterConfigurationTree> ParameterConfigurationParameter {
|
---|
| 24 | get { return (ScopeTreeLookupParameter<ParameterConfigurationTree>)Parameters["ParameterConfigurationTree"]; }
|
---|
[4832] | 25 | }
|
---|
| 26 | public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
| 27 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
| 28 | }
|
---|
[5009] | 29 | public LookupParameter<IRun> BestSolutionParameter {
|
---|
| 30 | get { return (LookupParameter<IRun>)Parameters["BestSolution"]; }
|
---|
[4832] | 31 | }
|
---|
| 32 | public ValueLookupParameter<ResultCollection> ResultsParameter {
|
---|
| 33 | get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
| 34 | }
|
---|
| 35 | public LookupParameter<DoubleValue> BestKnownQualityParameter {
|
---|
| 36 | get { return (LookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
| 37 | }
|
---|
[5009] | 38 | public LookupParameter<IRun> BestKnownSolutionParameter {
|
---|
| 39 | get { return (LookupParameter<IRun>)Parameters["BestKnownSolution"]; }
|
---|
[4832] | 40 | }
|
---|
[5009] | 41 | public LookupParameter<RunCollection> PopulationParameter {
|
---|
| 42 | get { return (LookupParameter<RunCollection>)Parameters["Population"]; }
|
---|
| 43 | }
|
---|
[5337] | 44 | public LookupParameter<ConstrainedItemList<IProblem>> ProblemsParameter {
|
---|
| 45 | get { return (LookupParameter<ConstrainedItemList<IProblem>>)Parameters[MetaOptimizationProblem.ProblemsParameterName]; }
|
---|
[5212] | 46 | }
|
---|
[6018] | 47 | public LookupParameter<BoolValue> MaximizationParameter {
|
---|
| 48 | get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
| 49 | }
|
---|
[5212] | 50 |
|
---|
[5087] | 51 | public BestParameterConfigurationAnalyzer()
|
---|
| 52 | : base() {
|
---|
[5281] | 53 | Parameters.Add(new ScopeTreeLookupParameter<ParameterConfigurationTree>("ParameterConfigurationTree", ""));
|
---|
| 54 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", ""));
|
---|
| 55 | Parameters.Add(new LookupParameter<IRun>("BestSolution", ""));
|
---|
| 56 | Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", ""));
|
---|
| 57 | Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", ""));
|
---|
| 58 | Parameters.Add(new LookupParameter<IRun>("BestKnownSolution", ""));
|
---|
| 59 | Parameters.Add(new LookupParameter<RunCollection>("Population", ""));
|
---|
[5337] | 60 | Parameters.Add(new LookupParameter<ConstrainedItemList<IProblem>>(MetaOptimizationProblem.ProblemsParameterName));
|
---|
[6018] | 61 | Parameters.Add(new LookupParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized."));
|
---|
[4832] | 62 | }
|
---|
| 63 |
|
---|
| 64 | [StorableConstructor]
|
---|
[16574] | 65 | private BestParameterConfigurationAnalyzer(StorableConstructorFlag _) : base(_) { }
|
---|
[4832] | 66 | private BestParameterConfigurationAnalyzer(BestParameterConfigurationAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
| 67 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 68 | return new BestParameterConfigurationAnalyzer(this, cloner);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | public override IOperation Apply() {
|
---|
| 72 | ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
|
---|
| 73 | ResultCollection results = ResultsParameter.ActualValue;
|
---|
| 74 | DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
|
---|
[5023] | 75 | ItemArray<ParameterConfigurationTree> parameterTrees = ParameterConfigurationParameter.ActualValue;
|
---|
[6018] | 76 | bool maximization = MaximizationParameter.ActualValue.Value;
|
---|
[4832] | 77 |
|
---|
[6018] | 78 | int idxBest;
|
---|
| 79 | if (maximization)
|
---|
| 80 | idxBest = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).Last().index;
|
---|
| 81 | else
|
---|
| 82 | idxBest = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
|
---|
[4832] | 83 |
|
---|
[5087] | 84 | ParameterConfigurationTree best = (ParameterConfigurationTree)parameterTrees[idxBest];
|
---|
| 85 | IRun bestRun = new Run();
|
---|
| 86 | best.CollectResultValues(bestRun.Results);
|
---|
| 87 | best.CollectParameterValues(bestRun.Parameters);
|
---|
| 88 |
|
---|
[7173] | 89 | if (bestKnownQuality == null ||
|
---|
[6018] | 90 | (!maximization && (qualities[idxBest].Value < bestKnownQuality.Value) ||
|
---|
| 91 | (maximization && (qualities[idxBest].Value > bestKnownQuality.Value)))) {
|
---|
[5087] | 92 | BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[idxBest].Value);
|
---|
| 93 |
|
---|
[5009] | 94 | BestKnownSolutionParameter.ActualValue = bestRun;
|
---|
[4832] | 95 | }
|
---|
| 96 |
|
---|
[5087] | 97 | if (BestSolutionParameter.ActualValue == null) {
|
---|
[5009] | 98 | BestSolutionParameter.ActualValue = bestRun;
|
---|
| 99 | results.Add(new Result("Best Parameter Settings", bestRun));
|
---|
| 100 | } else {
|
---|
| 101 | BestSolutionParameter.ActualValue = bestRun;
|
---|
| 102 | results["Best Parameter Settings"].Value = bestRun;
|
---|
| 103 | }
|
---|
[4832] | 104 |
|
---|
[6090] | 105 | // population (TODO: extract into PopulationAnalyzer)
|
---|
[5087] | 106 | int i = 0;
|
---|
[5009] | 107 | RunCollection rc = new RunCollection();
|
---|
[6018] | 108 | foreach (ParameterConfigurationTree pt in (maximization ? parameterTrees.OrderByDescending(x => x.Quality) : parameterTrees.OrderBy(x => x.Quality))) {
|
---|
[6197] | 109 | rc.Add(pt.ToRun(string.Format("Individual {0} ({1})", i, pt.ParameterInfoString), true));
|
---|
[5087] | 110 | i++;
|
---|
[5009] | 111 | }
|
---|
| 112 | if (PopulationParameter.ActualValue == null) {
|
---|
| 113 | PopulationParameter.ActualValue = rc;
|
---|
| 114 | results.Add(new Result("Population", rc));
|
---|
| 115 | } else {
|
---|
| 116 | PopulationParameter.ActualValue = rc;
|
---|
| 117 | results["Population"].Value = rc;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
[4832] | 120 | return base.Apply();
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 | }
|
---|