Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization (trunk integration)/HeuristicLab.Problems.MetaOptimization/3.3/Analyzers/SolutionCacheAnalyzer.cs @ 13042

Last change on this file since 13042 was 8576, checked in by jkarder, 12 years ago

#1853: created branch for MetaOptimization (trunk integration)

File size: 4.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.ParameterConfigurationEncoding;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.MetaOptimization {
33  /// <summary>
34  /// TODO
35  /// </summary>
36  [Item("SolutionCacheAnalyzer", "TODO")]
37  [StorableClass]
38  public sealed class SolutionCacheAnalyzer : SingleSuccessorOperator, IAnalyzer {
39    public bool EnabledByDefault {
40      get { return true; }
41    }
42
43    public ValueLookupParameter<ResultCollection> ResultsParameter {
44      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
45    }
46    public ScopeTreeLookupParameter<ParameterConfigurationTree> ParameterConfigurationParameter {
47      get { return (ScopeTreeLookupParameter<ParameterConfigurationTree>)Parameters["ParameterConfigurationTree"]; }
48    }
49    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
50      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
51    }
52    public ValueParameter<BoolValue> StoreAllRunsParameter {
53      get { return (ValueParameter<BoolValue>)Parameters["StoreAllRuns"]; }
54    }
55
56    #region Constructors and Cloning
57    public SolutionCacheAnalyzer()
58      : base() {
59      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", ""));
60      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", ""));
61      Parameters.Add(new ScopeTreeLookupParameter<ParameterConfigurationTree>("ParameterConfigurationTree", ""));
62      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)));
63    }
64
65    [StorableConstructor]
66    private SolutionCacheAnalyzer(bool deserializing) : base(deserializing) { }
67    private SolutionCacheAnalyzer(SolutionCacheAnalyzer original, Cloner cloner) : base(original, cloner) { }
68    public override IDeepCloneable Clone(Cloner cloner) {
69      return new SolutionCacheAnalyzer(this, cloner);
70    }
71    #endregion
72
73    public override IOperation Apply() {
74      ResultCollection results = ResultsParameter.ActualValue;
75      ItemArray<ParameterConfigurationTree> solutions = ParameterConfigurationParameter.ActualValue;
76      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
77      ItemDictionary<StringValue, RunCollection> allRuns = ResultsParameter.ActualValue.ContainsKey("SolutionCache") ? (ItemDictionary<StringValue, RunCollection>)ResultsParameter.ActualValue["SolutionCache"].Value : new ItemDictionary<StringValue, RunCollection>();
78      bool storeAllRuns = ((BoolValue)StoreAllRunsParameter.ActualValue).Value;
79
80      if (!storeAllRuns) {
81        allRuns.Clear();
82      }
83
84      foreach (var solution in solutions) {
85        string key = solution.ParameterInfoString;
86        bool first = false;
87        if (allRuns.Count(x => x.Key.Value == key) == 0) {
88          allRuns.Add(new StringValue(key), new RunCollection());
89          first = true; // no other runs yet
90        }
91
92        if (solution.Runs != null) { // Runs is null when a base-level algorithm exception happened due to invalid parameters
93          var runCollection = allRuns.Single(x => x.Key.Value == key).Value;
94          foreach (var run in solution.Runs) {
95            if (!((BoolValue)run.Results["Meta-FromCache"]).Value || first) {
96              run.Results["Meta-FromCache"] = new BoolValue(true);
97              runCollection.Add(run);
98            }
99          }
100        }
101      }
102
103      if (!results.ContainsKey("SolutionCache")) {
104        results.Add(new Result("SolutionCache", allRuns));
105      }
106
107      return base.Apply();
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.