1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System.Linq;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Encodings.ParameterConfigurationEncoding;
|
---|
27 | using HeuristicLab.Operators;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
33 | /// <summary>
|
---|
34 | /// An analyzer that records the best parameter configuration.
|
---|
35 | /// </summary>
|
---|
36 | [Item("BestParameterConfigurationAnalyzer", "An analyzer that records the best parameter configuration.")]
|
---|
37 | [StorableClass]
|
---|
38 | public sealed class BestParameterConfigurationAnalyzer : SingleSuccessorOperator, IAnalyzer {
|
---|
39 | public bool EnabledByDefault {
|
---|
40 | get { return true; }
|
---|
41 | }
|
---|
42 |
|
---|
43 | public ScopeTreeLookupParameter<ParameterConfigurationTree> ParameterConfigurationParameter {
|
---|
44 | get { return (ScopeTreeLookupParameter<ParameterConfigurationTree>)Parameters["ParameterConfigurationTree"]; }
|
---|
45 | }
|
---|
46 | public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
47 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
48 | }
|
---|
49 | public LookupParameter<IRun> BestSolutionParameter {
|
---|
50 | get { return (LookupParameter<IRun>)Parameters["BestSolution"]; }
|
---|
51 | }
|
---|
52 | public ValueLookupParameter<ResultCollection> ResultsParameter {
|
---|
53 | get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
54 | }
|
---|
55 | public LookupParameter<DoubleValue> BestKnownQualityParameter {
|
---|
56 | get { return (LookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
57 | }
|
---|
58 | public LookupParameter<IRun> BestKnownSolutionParameter {
|
---|
59 | get { return (LookupParameter<IRun>)Parameters["BestKnownSolution"]; }
|
---|
60 | }
|
---|
61 | //public LookupParameter<RunCollection> PopulationParameter {
|
---|
62 | // get { return (LookupParameter<RunCollection>)Parameters["Population"]; }
|
---|
63 | //}
|
---|
64 | public LookupParameter<BoolValue> MaximizationParameter {
|
---|
65 | get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
66 | }
|
---|
67 |
|
---|
68 | #region Constructors and Cloning
|
---|
69 | public BestParameterConfigurationAnalyzer()
|
---|
70 | : base() {
|
---|
71 | Parameters.Add(new ScopeTreeLookupParameter<ParameterConfigurationTree>("ParameterConfigurationTree", ""));
|
---|
72 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", ""));
|
---|
73 | Parameters.Add(new LookupParameter<IRun>("BestSolution", ""));
|
---|
74 | Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", ""));
|
---|
75 | Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", ""));
|
---|
76 | Parameters.Add(new LookupParameter<IRun>("BestKnownSolution", ""));
|
---|
77 | //Parameters.Add(new LookupParameter<RunCollection>("Population", ""));
|
---|
78 | Parameters.Add(new LookupParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized."));
|
---|
79 | }
|
---|
80 |
|
---|
81 | [StorableConstructor]
|
---|
82 | private BestParameterConfigurationAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
83 | private BestParameterConfigurationAnalyzer(BestParameterConfigurationAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
84 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
85 | return new BestParameterConfigurationAnalyzer(this, cloner);
|
---|
86 | }
|
---|
87 | #endregion
|
---|
88 |
|
---|
89 | public override IOperation Apply() {
|
---|
90 | ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
|
---|
91 | ResultCollection results = ResultsParameter.ActualValue;
|
---|
92 | DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
|
---|
93 | ItemArray<ParameterConfigurationTree> parameterTrees = ParameterConfigurationParameter.ActualValue;
|
---|
94 | bool maximization = MaximizationParameter.ActualValue.Value;
|
---|
95 |
|
---|
96 | int idxBest;
|
---|
97 | if (maximization)
|
---|
98 | idxBest = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).Last().index;
|
---|
99 | else
|
---|
100 | idxBest = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
|
---|
101 |
|
---|
102 | ParameterConfigurationTree best = parameterTrees[idxBest];
|
---|
103 | IRun bestRun = new Run();
|
---|
104 | best.CollectResultValues(bestRun.Results);
|
---|
105 | best.CollectParameterValues(bestRun.Parameters);
|
---|
106 |
|
---|
107 | if (bestKnownQuality == null ||
|
---|
108 | (!maximization && (qualities[idxBest].Value < bestKnownQuality.Value) ||
|
---|
109 | (maximization && (qualities[idxBest].Value > bestKnownQuality.Value)))) {
|
---|
110 | BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[idxBest].Value);
|
---|
111 |
|
---|
112 | BestKnownSolutionParameter.ActualValue = bestRun;
|
---|
113 | }
|
---|
114 |
|
---|
115 | if (BestSolutionParameter.ActualValue == null) {
|
---|
116 | BestSolutionParameter.ActualValue = bestRun;
|
---|
117 | results.Add(new Result("Best Parameter Settings", bestRun));
|
---|
118 | } else {
|
---|
119 | BestSolutionParameter.ActualValue = bestRun;
|
---|
120 | results["Best Parameter Settings"].Value = bestRun;
|
---|
121 | }
|
---|
122 |
|
---|
123 | // population (TODO: extract into PopulationAnalyzer)
|
---|
124 | //int i = 0;
|
---|
125 | //RunCollection rc = new RunCollection();
|
---|
126 | //foreach (ParameterConfigurationTree pt in (maximization ? parameterTrees.OrderByDescending(x => x.Quality) : parameterTrees.OrderBy(x => x.Quality))) {
|
---|
127 | // rc.Add(pt.ToRun(string.Format("Individual {0} ({1})", i, pt.ParameterInfoString), true));
|
---|
128 | // i++;
|
---|
129 | //}
|
---|
130 | //if (PopulationParameter.ActualValue == null) {
|
---|
131 | // PopulationParameter.ActualValue = rc;
|
---|
132 | // results.Add(new Result("Population", rc));
|
---|
133 | //} else {
|
---|
134 | // PopulationParameter.ActualValue = rc;
|
---|
135 | // results["Population"].Value = rc;
|
---|
136 | //}
|
---|
137 |
|
---|
138 | return base.Apply();
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|