Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Analyzers/PMOBestSolutionHistoryAnalyzer.cs @ 16574

Last change on this file since 16574 was 16574, checked in by gkronber, 5 years ago

#2520: changed HeuristicLab.MetaOptimization addon to compile with new HL.Persistence

File size: 4.3 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 HEAL.Attic;
10
11namespace HeuristicLab.Problems.MetaOptimization {
12  /// <summary>
13  /// TODO
14  /// </summary>
15  [Item("PMOBestSolutionHistoryAnalyzer", "")]
16  [StorableType("7B257F08-ACCC-4FDF-961A-412344994AD1")]
17  public sealed class PMOBestSolutionHistoryAnalyzer : SingleSuccessorOperator, IAnalyzer {
18
19    public bool EnabledByDefault {
20      get { return true; }
21    }
22
23    public ScopeTreeLookupParameter<ParameterConfigurationTree> ParameterConfigurationParameter {
24      get { return (ScopeTreeLookupParameter<ParameterConfigurationTree>)Parameters["ParameterConfigurationTree"]; }
25    }
26    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
27      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
28    }
29    public ValueLookupParameter<ResultCollection> ResultsParameter {
30      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
31    }
32    public LookupParameter<ConstrainedItemList<IProblem>> ProblemsParameter {
33      get { return (LookupParameter<ConstrainedItemList<IProblem>>)Parameters[MetaOptimizationProblem.ProblemsParameterName]; }
34    }
35    public LookupParameter<IntValue> GenerationsParameter {
36      get { return (LookupParameter<IntValue>)Parameters["Generations"]; }
37    }
38    public LookupParameter<BoolValue> MaximizationParameter {
39      get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
40    }
41
42    public PMOBestSolutionHistoryAnalyzer()
43      : base() {
44      Parameters.Add(new ScopeTreeLookupParameter<ParameterConfigurationTree>("ParameterConfigurationTree", ""));
45      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", ""));
46      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", ""));
47      Parameters.Add(new LookupParameter<ConstrainedItemList<IProblem>>(MetaOptimizationProblem.ProblemsParameterName));
48      Parameters.Add(new LookupParameter<IntValue>("Generations", ""));
49      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized."));
50    }
51
52    [StorableConstructor]
53    private PMOBestSolutionHistoryAnalyzer(StorableConstructorFlag _) : base(_) { }
54    private PMOBestSolutionHistoryAnalyzer(PMOBestSolutionHistoryAnalyzer original, Cloner cloner) : base(original, cloner) { }
55    public override IDeepCloneable Clone(Cloner cloner) {
56      return new PMOBestSolutionHistoryAnalyzer(this, cloner);
57    }
58
59    public override IOperation Apply() {
60      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
61      ResultCollection results = ResultsParameter.ActualValue;
62      ItemArray<ParameterConfigurationTree> parameterTrees = ParameterConfigurationParameter.ActualValue;
63      int currentGeneration = GenerationsParameter.ActualValue.Value;
64      bool maximization = MaximizationParameter.ActualValue.Value;
65
66      int idxBest;
67      if (maximization)
68        idxBest = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).Last().index;
69      else
70        idxBest = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
71
72      ParameterConfigurationTree best = parameterTrees[idxBest];
73
74      string key = "BestSolutionHistory";
75      RunCollection bestSolutionHistory;
76      if (!results.ContainsKey(key)) {
77        bestSolutionHistory = new RunCollection();
78        results.Add(new Result(key, bestSolutionHistory));
79      } else {
80        bestSolutionHistory = results[key].Value as RunCollection;
81      }
82      if (bestSolutionHistory.Count == 0 || best.ParameterInfoString != ((StringValue)bestSolutionHistory.Last().Results["Meta-ParameterInfoString"]).Value) {
83        IRun run = best.ToRun(string.Format("{0}: {1}", currentGeneration, best.ParameterInfoString), false);
84        run.Results.Add("Meta-Generation", new IntValue(currentGeneration));
85        run.Results.Add("Meta-ParameterInfoString", new StringValue(best.ParameterInfoString));
86        bestSolutionHistory.Add(run);
87      }
88
89      return base.Apply();
90    }
91  }
92}
Note: See TracBrowser for help on using the repository browser.