Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Analyzers/PMOBestSolutionHistoryAnalyzer.cs @ 13401

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

#1215 enabled analyzers for MetaOpt per default

File size: 4.2 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("PMOBestSolutionHistoryAnalyzer", "")]
15  [StorableClass]
16  public sealed class PMOBestSolutionHistoryAnalyzer : 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 ValueLookupParameter<ResultCollection> ResultsParameter {
29      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
30    }
31    public LookupParameter<ConstrainedItemList<IProblem>> ProblemsParameter {
32      get { return (LookupParameter<ConstrainedItemList<IProblem>>)Parameters[MetaOptimizationProblem.ProblemsParameterName]; }
33    }
34    public LookupParameter<IntValue> GenerationsParameter {
35      get { return (LookupParameter<IntValue>)Parameters["Generations"]; }
36    }
37    public LookupParameter<BoolValue> MaximizationParameter {
38      get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
39    }
40
41    public PMOBestSolutionHistoryAnalyzer()
42      : base() {
43      Parameters.Add(new ScopeTreeLookupParameter<ParameterConfigurationTree>("ParameterConfigurationTree", ""));
44      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", ""));
45      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", ""));
46      Parameters.Add(new LookupParameter<ConstrainedItemList<IProblem>>(MetaOptimizationProblem.ProblemsParameterName));
47      Parameters.Add(new LookupParameter<IntValue>("Generations", ""));
48      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized."));
49    }
50
51    [StorableConstructor]
52    private PMOBestSolutionHistoryAnalyzer(bool deserializing) : base(deserializing) { }
53    private PMOBestSolutionHistoryAnalyzer(PMOBestSolutionHistoryAnalyzer original, Cloner cloner) : base(original, cloner) { }
54    public override IDeepCloneable Clone(Cloner cloner) {
55      return new PMOBestSolutionHistoryAnalyzer(this, cloner);
56    }
57
58    public override IOperation Apply() {
59      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
60      ResultCollection results = ResultsParameter.ActualValue;
61      ItemArray<ParameterConfigurationTree> parameterTrees = ParameterConfigurationParameter.ActualValue;
62      int currentGeneration = GenerationsParameter.ActualValue.Value;
63      bool maximization = MaximizationParameter.ActualValue.Value;
64
65      int idxBest;
66      if (maximization)
67        idxBest = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).Last().index;
68      else
69        idxBest = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
70
71      ParameterConfigurationTree best = parameterTrees[idxBest];
72
73      string key = "BestSolutionHistory";
74      RunCollection bestSolutionHistory;
75      if (!results.ContainsKey(key)) {
76        bestSolutionHistory = new RunCollection();
77        results.Add(new Result(key, bestSolutionHistory));
78      } else {
79        bestSolutionHistory = results[key].Value as RunCollection;
80      }
81      if (bestSolutionHistory.Count == 0 || best.ParameterInfoString != ((StringValue)bestSolutionHistory.Last().Results["Meta-ParameterInfoString"]).Value) {
82        IRun run = best.ToRun(string.Format("{0}: {1}", currentGeneration, best.ParameterInfoString), false);
83        run.Results.Add("Meta-Generation", new IntValue(currentGeneration));
84        run.Results.Add("Meta-ParameterInfoString", new StringValue(best.ParameterInfoString));
85        bestSolutionHistory.Add(run);
86      }
87
88      return base.Apply();
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.