Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5576 was 5576, checked in by cneumuel, 12 years ago

#1215

  • implemented best solution history analyzer
  • implemented quality analyzer for each base-level problem
File size: 3.8 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;
9using System.Collections.Generic;
10using System;
11using HeuristicLab.Analysis;
12
13namespace HeuristicLab.Problems.MetaOptimization {
14  /// <summary>
15  /// TODO
16  /// </summary>
17  [Item("PMOBestSolutionHistoryAnalyzer", "")]
18  [StorableClass]
19  public sealed class PMOBestSolutionHistoryAnalyzer : SingleSuccessorOperator, IAnalyzer {
20
21    public ScopeTreeLookupParameter<ParameterConfigurationTree> ParameterConfigurationParameter {
22      get { return (ScopeTreeLookupParameter<ParameterConfigurationTree>)Parameters["ParameterConfigurationTree"]; }
23    }
24    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
25      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
26    }
27    public ValueLookupParameter<ResultCollection> ResultsParameter {
28      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
29    }
30    public LookupParameter<ConstrainedItemList<IProblem>> ProblemsParameter {
31      get { return (LookupParameter<ConstrainedItemList<IProblem>>)Parameters[MetaOptimizationProblem.ProblemsParameterName]; }
32    }
33    public LookupParameter<IntValue> GenerationsParameter {
34      get { return (LookupParameter<IntValue>)Parameters["Generations"]; }
35    }
36
37    public PMOBestSolutionHistoryAnalyzer()
38      : base() {
39      Parameters.Add(new ScopeTreeLookupParameter<ParameterConfigurationTree>("ParameterConfigurationTree", ""));
40      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", ""));
41      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", ""));
42      Parameters.Add(new LookupParameter<ConstrainedItemList<IProblem>>(MetaOptimizationProblem.ProblemsParameterName));
43      Parameters.Add(new LookupParameter<IntValue>("Generations", ""));
44    }
45
46    [StorableConstructor]
47    private PMOBestSolutionHistoryAnalyzer(bool deserializing) : base(deserializing) { }
48    private PMOBestSolutionHistoryAnalyzer(PMOBestSolutionHistoryAnalyzer original, Cloner cloner) : base(original, cloner) { }
49    public override IDeepCloneable Clone(Cloner cloner) {
50      return new PMOBestSolutionHistoryAnalyzer(this, cloner);
51    }
52
53    public override IOperation Apply() {
54      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
55      ResultCollection results = ResultsParameter.ActualValue;
56      ItemArray<ParameterConfigurationTree> parameterTrees = ParameterConfigurationParameter.ActualValue;
57      int currentGeneration = GenerationsParameter.ActualValue.Value;
58      int idxBest = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index; // todo: respect Min/Max
59      ParameterConfigurationTree best = parameterTrees[idxBest];
60
61      string key = "BestSolutionHistory";
62      RunCollection bestSolutionHistory;
63      if (!results.ContainsKey(key)) {
64        bestSolutionHistory = new RunCollection();
65        results.Add(new Result(key, bestSolutionHistory));
66      } else {
67        bestSolutionHistory = results[key].Value as RunCollection;
68      }
69      if (bestSolutionHistory.Count == 0 || best.ParameterInfoString != ((StringValue)bestSolutionHistory.Last().Results["Meta.ParameterInfoString"]).Value) {
70        IRun run = best.ToRun(string.Format("{0}: {1}", currentGeneration, best.ParameterInfoString));
71        run.Results.Add("Meta.Generation", new IntValue(currentGeneration));
72        run.Results.Add("Meta.ParameterInfoString", new StringValue(best.ParameterInfoString));
73        bestSolutionHistory.Add(run);
74      }
75
76      return base.Apply();
77    }
78  }
79}
Note: See TracBrowser for help on using the repository browser.