Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11255 was 7173, checked in by ascheibe, 13 years ago

#1215 enabled analyzers for MetaOpt per default

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