1 | using System.Linq;
|
---|
2 | using HeuristicLab.Common;
|
---|
3 | using HeuristicLab.Core;
|
---|
4 | using HeuristicLab.Data;
|
---|
5 | using HeuristicLab.Operators;
|
---|
6 | using HeuristicLab.Optimization;
|
---|
7 | using HeuristicLab.Parameters;
|
---|
8 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
9 | using HEAL.Attic;
|
---|
10 |
|
---|
11 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
12 | /// <summary>
|
---|
13 | /// TODO
|
---|
14 | /// </summary>
|
---|
15 | [Item("BestParameterConfigurationAnalyzer", "")]
|
---|
16 | [StorableType("9A11F9C1-B076-45BF-9CCB-661989FC4273")]
|
---|
17 | public sealed class BestParameterConfigurationAnalyzer : SingleSuccessorOperator, IAnalyzer {
|
---|
18 |
|
---|
19 | public bool EnabledByDefault {
|
---|
20 | get { return true; }
|
---|
21 | }
|
---|
22 |
|
---|
23 | public ScopeTreeLookupParameter<ParameterConfigurationTree> ParameterConfigurationParameter {
|
---|
24 | get { return (ScopeTreeLookupParameter<ParameterConfigurationTree>)Parameters["ParameterConfigurationTree"]; }
|
---|
25 | }
|
---|
26 | public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
27 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
28 | }
|
---|
29 | public LookupParameter<IRun> BestSolutionParameter {
|
---|
30 | get { return (LookupParameter<IRun>)Parameters["BestSolution"]; }
|
---|
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 | }
|
---|
38 | public LookupParameter<IRun> BestKnownSolutionParameter {
|
---|
39 | get { return (LookupParameter<IRun>)Parameters["BestKnownSolution"]; }
|
---|
40 | }
|
---|
41 | public LookupParameter<RunCollection> PopulationParameter {
|
---|
42 | get { return (LookupParameter<RunCollection>)Parameters["Population"]; }
|
---|
43 | }
|
---|
44 | public LookupParameter<ConstrainedItemList<IProblem>> ProblemsParameter {
|
---|
45 | get { return (LookupParameter<ConstrainedItemList<IProblem>>)Parameters[MetaOptimizationProblem.ProblemsParameterName]; }
|
---|
46 | }
|
---|
47 | public LookupParameter<BoolValue> MaximizationParameter {
|
---|
48 | get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
49 | }
|
---|
50 |
|
---|
51 | public BestParameterConfigurationAnalyzer()
|
---|
52 | : base() {
|
---|
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", ""));
|
---|
60 | Parameters.Add(new LookupParameter<ConstrainedItemList<IProblem>>(MetaOptimizationProblem.ProblemsParameterName));
|
---|
61 | Parameters.Add(new LookupParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized."));
|
---|
62 | }
|
---|
63 |
|
---|
64 | [StorableConstructor]
|
---|
65 | private BestParameterConfigurationAnalyzer(StorableConstructorFlag _) : base(_) { }
|
---|
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;
|
---|
75 | ItemArray<ParameterConfigurationTree> parameterTrees = ParameterConfigurationParameter.ActualValue;
|
---|
76 | bool maximization = MaximizationParameter.ActualValue.Value;
|
---|
77 |
|
---|
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;
|
---|
83 |
|
---|
84 | ParameterConfigurationTree best = (ParameterConfigurationTree)parameterTrees[idxBest];
|
---|
85 | IRun bestRun = new Run();
|
---|
86 | best.CollectResultValues(bestRun.Results);
|
---|
87 | best.CollectParameterValues(bestRun.Parameters);
|
---|
88 |
|
---|
89 | if (bestKnownQuality == null ||
|
---|
90 | (!maximization && (qualities[idxBest].Value < bestKnownQuality.Value) ||
|
---|
91 | (maximization && (qualities[idxBest].Value > bestKnownQuality.Value)))) {
|
---|
92 | BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[idxBest].Value);
|
---|
93 |
|
---|
94 | BestKnownSolutionParameter.ActualValue = bestRun;
|
---|
95 | }
|
---|
96 |
|
---|
97 | if (BestSolutionParameter.ActualValue == null) {
|
---|
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 | }
|
---|
104 |
|
---|
105 | // population (TODO: extract into PopulationAnalyzer)
|
---|
106 | int i = 0;
|
---|
107 | RunCollection rc = new RunCollection();
|
---|
108 | foreach (ParameterConfigurationTree pt in (maximization ? parameterTrees.OrderByDescending(x => x.Quality) : parameterTrees.OrderBy(x => x.Quality))) {
|
---|
109 | rc.Add(pt.ToRun(string.Format("Individual {0} ({1})", i, pt.ParameterInfoString), true));
|
---|
110 | i++;
|
---|
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 |
|
---|
120 | return base.Apply();
|
---|
121 | }
|
---|
122 | }
|
---|
123 | }
|
---|