Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Analyzers/SolutionCacheAnalyzer.cs @ 9584

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

#1215 enabled analyzers for MetaOpt per default

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