Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 4.4 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("SolutionCacheAnalyzer", "")]
16  [StorableType("0D06B2C3-DA06-47B3-AA1C-6BF491173504")]
17  public sealed class SolutionCacheAnalyzer : SingleSuccessorOperator, IAnalyzer {
18
19    public bool EnabledByDefault {
20      get { return true; }
21    }
22
23    public ValueLookupParameter<ResultCollection> ResultsParameter {
24      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
25    }
26    public ScopeTreeLookupParameter<ParameterConfigurationTree> ParameterConfigurationParameter {
27      get { return (ScopeTreeLookupParameter<ParameterConfigurationTree>)Parameters["ParameterConfigurationTree"]; }
28    }
29    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
30      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
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 ValueParameter<BoolValue> StoreAllRunsParameter {
39      get { return (ValueParameter<BoolValue>)Parameters["StoreAllRuns"]; }
40    }
41
42    public SolutionCacheAnalyzer()
43      : base() {
44      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", ""));
45      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", ""));
46      Parameters.Add(new ScopeTreeLookupParameter<ParameterConfigurationTree>("ParameterConfigurationTree", ""));
47      Parameters.Add(new LookupParameter<ConstrainedItemList<IProblem>>(MetaOptimizationProblem.ProblemsParameterName, ""));
48      Parameters.Add(new LookupParameter<IntValue>("Generations", ""));
49      Parameters.Add(new ValueParameter<BoolValue>("StoreAllRuns", "If true all runs ever executed are stored. Otherwise only the runs from the latest generateion are stored for caching purposes.", new BoolValue(false)));
50    }
51
52    [StorableConstructor]
53    private SolutionCacheAnalyzer(StorableConstructorFlag _) : base(_) { }
54    private SolutionCacheAnalyzer(SolutionCacheAnalyzer original, Cloner cloner) : base(original, cloner) { }
55    public override IDeepCloneable Clone(Cloner cloner) {
56      return new SolutionCacheAnalyzer(this, cloner);
57    }
58
59    public override IOperation Apply() {
60      ResultCollection results = ResultsParameter.ActualValue;
61      ItemArray<ParameterConfigurationTree> solutions = ParameterConfigurationParameter.ActualValue;
62      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
63      ItemDictionary<StringValue, RunCollection> allRuns = ResultsParameter.ActualValue.ContainsKey("SolutionCache") ? (ItemDictionary<StringValue, RunCollection>)ResultsParameter.ActualValue["SolutionCache"].Value : new ItemDictionary<StringValue, RunCollection>();
64      bool storeAllRuns = ((BoolValue)StoreAllRunsParameter.ActualValue).Value;
65
66      if (!storeAllRuns) {
67        allRuns.Clear();
68      }
69
70      foreach (var solution in solutions) {
71        string key = solution.ParameterInfoString;
72        bool first = false;
73        if (allRuns.Count(x => x.Key.Value == key) == 0) {
74          allRuns.Add(new StringValue(key), new RunCollection());
75          first = true; // no other runs yet
76        }
77
78        if (solution.Runs != null) { // Runs is null when a base-level algorithm exception happened due to invalid parameters
79          var runCollection = allRuns.Single(x => x.Key.Value == key).Value;
80          foreach (var run in solution.Runs) {
81            if (!((BoolValue)run.Results["Meta-FromCache"]).Value || first) {
82              run.Results["Meta-FromCache"] = new BoolValue(true);
83              runCollection.Add(run);
84            }
85          }
86        }
87      }
88
89      if (!results.ContainsKey("SolutionCache")) {
90        results.Add(new Result("SolutionCache", allRuns));
91      }
92
93      return base.Apply();
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.