Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5359 was 5359, checked in by cneumuel, 13 years ago

#1215

  • renamed RunsAnalyzer to SolutionCacheAnalyzer
  • only most recent results will stay in cache to avoid memory problems
  • some minor tweaks and bugfixes
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 System.Collections.Generic;
10using System;
11
12namespace HeuristicLab.Problems.MetaOptimization {
13  /// <summary>
14  /// TODO
15  /// </summary>
16  [Item("SolutionCacheAnalyzer", "")]
17  [StorableClass]
18  public sealed class SolutionCacheAnalyzer : SingleSuccessorOperator, IAnalyzer {
19
20    public ValueLookupParameter<ResultCollection> ResultsParameter {
21      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
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 LookupParameter<DoubleArray> ProblemQualityReferenceParameter {
30      get { return (LookupParameter<DoubleArray>)Parameters["ProblemQualityReferences"]; }
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<DoubleArray>("ProblemQualityReferences", ""));
48      Parameters.Add(new LookupParameter<ConstrainedItemList<IProblem>>(MetaOptimizationProblem.ProblemsParameterName, ""));
49      Parameters.Add(new LookupParameter<IntValue>("Generations", ""));
50      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)));
51    }
52
53    [StorableConstructor]
54    private SolutionCacheAnalyzer(bool deserializing) : base(deserializing) { }
55    private SolutionCacheAnalyzer(SolutionCacheAnalyzer original, Cloner cloner) : base(original, cloner) { }
56    public override IDeepCloneable Clone(Cloner cloner) {
57      return new SolutionCacheAnalyzer(this, cloner);
58    }
59
60    public override IOperation Apply() {
61      ResultCollection results = ResultsParameter.ActualValue;
62      ItemArray<ParameterConfigurationTree> solutions = ParameterConfigurationParameter.ActualValue;
63      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
64      ItemDictionary<StringValue, RunCollection> allRuns = ResultsParameter.ActualValue.ContainsKey("SolutionCache") ? (ItemDictionary<StringValue, RunCollection>)ResultsParameter.ActualValue["SolutionCache"].Value : new ItemDictionary<StringValue, RunCollection>();
65      bool storeAllRuns = ((BoolValue)StoreAllRunsParameter.ActualValue).Value;
66
67      if (!storeAllRuns) {
68        allRuns.Clear();
69      }
70
71      foreach (var solution in solutions) {
72        string key = solution.ParameterInfoString;
73        bool first = false;
74        if (allRuns.Count(x => x.Key.Value == key) == 0) {
75          allRuns.Add(new StringValue(key), new RunCollection());
76          first = true; // no other runs yet
77        }
78
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      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.