Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Analyzers/BestParameterConfigurationAnalyzer.cs @ 6486

Last change on this file since 6486 was 6197, checked in by cneumuel, 14 years ago

#1215

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