Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1215 enabled analyzers for MetaOpt per default

File size: 5.6 KB
RevLine 
[4839]1using System.Linq;
2using HeuristicLab.Common;
3using HeuristicLab.Core;
4using HeuristicLab.Data;
[4832]5using HeuristicLab.Operators;
6using HeuristicLab.Optimization;
[4839]7using HeuristicLab.Parameters;
[4832]8using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
9
10namespace HeuristicLab.Problems.MetaOptimization {
11  /// <summary>
[5359]12  /// TODO
[4832]13  /// </summary>
[5281]14  [Item("BestParameterConfigurationAnalyzer", "")]
[4832]15  [StorableClass]
16  public sealed class BestParameterConfigurationAnalyzer : SingleSuccessorOperator, IAnalyzer {
17
[7173]18    public bool EnabledByDefault {
19      get { return true; }
20    }
21
[5009]22    public ScopeTreeLookupParameter<ParameterConfigurationTree> ParameterConfigurationParameter {
23      get { return (ScopeTreeLookupParameter<ParameterConfigurationTree>)Parameters["ParameterConfigurationTree"]; }
[4832]24    }
25    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
26      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
27    }
[5009]28    public LookupParameter<IRun> BestSolutionParameter {
29      get { return (LookupParameter<IRun>)Parameters["BestSolution"]; }
[4832]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    }
[5009]37    public LookupParameter<IRun> BestKnownSolutionParameter {
38      get { return (LookupParameter<IRun>)Parameters["BestKnownSolution"]; }
[4832]39    }
[5009]40    public LookupParameter<RunCollection> PopulationParameter {
41      get { return (LookupParameter<RunCollection>)Parameters["Population"]; }
42    }
[5337]43    public LookupParameter<ConstrainedItemList<IProblem>> ProblemsParameter {
44      get { return (LookupParameter<ConstrainedItemList<IProblem>>)Parameters[MetaOptimizationProblem.ProblemsParameterName]; }
[5212]45    }
[6018]46    public LookupParameter<BoolValue> MaximizationParameter {
47      get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
48    }
[5212]49
[5087]50    public BestParameterConfigurationAnalyzer()
51      : base() {
[5281]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", ""));
[5337]59      Parameters.Add(new LookupParameter<ConstrainedItemList<IProblem>>(MetaOptimizationProblem.ProblemsParameterName));
[6018]60      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized."));
[4832]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;
[5023]74      ItemArray<ParameterConfigurationTree> parameterTrees = ParameterConfigurationParameter.ActualValue;
[6018]75      bool maximization = MaximizationParameter.ActualValue.Value;
[4832]76
[6018]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;
[4832]82
[5087]83      ParameterConfigurationTree best = (ParameterConfigurationTree)parameterTrees[idxBest];
84      IRun bestRun = new Run();
85      best.CollectResultValues(bestRun.Results);
86      best.CollectParameterValues(bestRun.Parameters);
87
[7173]88      if (bestKnownQuality == null ||
[6018]89        (!maximization && (qualities[idxBest].Value < bestKnownQuality.Value) ||
90        (maximization && (qualities[idxBest].Value > bestKnownQuality.Value)))) {
[5087]91        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[idxBest].Value);
92
[5009]93        BestKnownSolutionParameter.ActualValue = bestRun;
[4832]94      }
95
[5087]96      if (BestSolutionParameter.ActualValue == null) {
[5009]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      }
[4832]103
[6090]104      // population (TODO: extract into PopulationAnalyzer)
[5087]105      int i = 0;
[5009]106      RunCollection rc = new RunCollection();
[6018]107      foreach (ParameterConfigurationTree pt in (maximization ? parameterTrees.OrderByDescending(x => x.Quality) : parameterTrees.OrderBy(x => x.Quality))) {
[6197]108        rc.Add(pt.ToRun(string.Format("Individual {0} ({1})", i, pt.ParameterInfoString), true));
[5087]109        i++;
[5009]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
[4832]119      return base.Apply();
120    }
121  }
122}
Note: See TracBrowser for help on using the repository browser.