Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1215

  • support for maximization problems
  • made base level algorithms stoppable
  • optimization for multiple goals possible (AverageQuality, AverageDeviation, AverageEvaluatedSolutions)
  • lots of fixes
File size: 4.1 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 ValueLookupParameter<ResultCollection> ResultsParameter {
19      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
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 LookupParameter<ConstrainedItemList<IProblem>> ProblemsParameter {
28      get { return (LookupParameter<ConstrainedItemList<IProblem>>)Parameters[MetaOptimizationProblem.ProblemsParameterName]; }
29    }
30    public LookupParameter<IntValue> GenerationsParameter {
31      get { return (LookupParameter<IntValue>)Parameters["Generations"]; }
32    }
33    public ValueParameter<BoolValue> StoreAllRunsParameter {
34      get { return (ValueParameter<BoolValue>)Parameters["StoreAllRuns"]; }
35    }
36
37    public SolutionCacheAnalyzer()
38      : base() {
39      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", ""));
40      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", ""));
41      Parameters.Add(new ScopeTreeLookupParameter<ParameterConfigurationTree>("ParameterConfigurationTree", ""));
42      Parameters.Add(new LookupParameter<ConstrainedItemList<IProblem>>(MetaOptimizationProblem.ProblemsParameterName, ""));
43      Parameters.Add(new LookupParameter<IntValue>("Generations", ""));
44      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)));
45    }
46
47    [StorableConstructor]
48    private SolutionCacheAnalyzer(bool deserializing) : base(deserializing) { }
49    private SolutionCacheAnalyzer(SolutionCacheAnalyzer original, Cloner cloner) : base(original, cloner) { }
50    public override IDeepCloneable Clone(Cloner cloner) {
51      return new SolutionCacheAnalyzer(this, cloner);
52    }
53
54    public override IOperation Apply() {
55      ResultCollection results = ResultsParameter.ActualValue;
56      ItemArray<ParameterConfigurationTree> solutions = ParameterConfigurationParameter.ActualValue;
57      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
58      ItemDictionary<StringValue, RunCollection> allRuns = ResultsParameter.ActualValue.ContainsKey("SolutionCache") ? (ItemDictionary<StringValue, RunCollection>)ResultsParameter.ActualValue["SolutionCache"].Value : new ItemDictionary<StringValue, RunCollection>();
59      bool storeAllRuns = ((BoolValue)StoreAllRunsParameter.ActualValue).Value;
60
61      if (!storeAllRuns) {
62        allRuns.Clear();
63      }
64
65      foreach (var solution in solutions) {
66        string key = solution.ParameterInfoString;
67        bool first = false;
68        if (allRuns.Count(x => x.Key.Value == key) == 0) {
69          allRuns.Add(new StringValue(key), new RunCollection());
70          first = true; // no other runs yet
71        }
72
73        var runCollection = allRuns.Single(x => x.Key.Value == key).Value;
74        foreach (var run in solution.Runs) {
75          if (!((BoolValue)run.Results["Meta.FromCache"]).Value || first) {
76            run.Results["Meta.FromCache"] = new BoolValue(true);
77            runCollection.Add(run);
78          }
79        }
80      }
81
82      if (!results.ContainsKey("SolutionCache")) {
83        results.Add(new Result("SolutionCache", allRuns));
84      }
85
86      return base.Apply();
87    }
88  }
89}
Note: See TracBrowser for help on using the repository browser.