Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1853: created branch for MetaOptimization (trunk integration)

File size: 5.0 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  /// An analyzer that tracks the history of best parameter configurations.
35  /// </summary>
36  [Item("PMOBestSolutionHistoryAnalyzer", "An analyzer that tracks the history of best solutions.")]
37  [StorableClass]
38  public sealed class PMOBestSolutionHistoryAnalyzer : SingleSuccessorOperator, IAnalyzer {
39    public bool EnabledByDefault {
40      get { return true; }
41    }
42
43    public ScopeTreeLookupParameter<ParameterConfigurationTree> ParameterConfigurationParameter {
44      get { return (ScopeTreeLookupParameter<ParameterConfigurationTree>)Parameters["ParameterConfigurationTree"]; }
45    }
46    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
47      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
48    }
49    public ValueLookupParameter<ResultCollection> ResultsParameter {
50      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
51    }
52    public LookupParameter<IntValue> GenerationsParameter {
53      get { return (LookupParameter<IntValue>)Parameters["Generations"]; }
54    }
55    public LookupParameter<BoolValue> MaximizationParameter {
56      get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
57    }
58
59    #region Constructors and Cloning
60    public PMOBestSolutionHistoryAnalyzer()
61      : base() {
62      Parameters.Add(new ScopeTreeLookupParameter<ParameterConfigurationTree>("ParameterConfigurationTree", ""));
63      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", ""));
64      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", ""));
65      Parameters.Add(new LookupParameter<IntValue>("Generations", ""));
66      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized."));
67    }
68
69    [StorableConstructor]
70    private PMOBestSolutionHistoryAnalyzer(bool deserializing) : base(deserializing) { }
71    private PMOBestSolutionHistoryAnalyzer(PMOBestSolutionHistoryAnalyzer original, Cloner cloner) : base(original, cloner) { }
72    public override IDeepCloneable Clone(Cloner cloner) {
73      return new PMOBestSolutionHistoryAnalyzer(this, cloner);
74    }
75    #endregion
76
77    public override IOperation Apply() {
78      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
79      ResultCollection results = ResultsParameter.ActualValue;
80      ItemArray<ParameterConfigurationTree> parameterTrees = ParameterConfigurationParameter.ActualValue;
81      int currentGeneration = GenerationsParameter.ActualValue.Value;
82      bool maximization = MaximizationParameter.ActualValue.Value;
83
84      int idxBest;
85      if (maximization)
86        idxBest = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).Last().index;
87      else
88        idxBest = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
89
90      ParameterConfigurationTree best = parameterTrees[idxBest];
91
92      string key = "BestSolutionHistory";
93      RunCollection bestSolutionHistory;
94      if (!results.ContainsKey(key)) {
95        bestSolutionHistory = new RunCollection();
96        results.Add(new Result(key, bestSolutionHistory));
97      } else {
98        bestSolutionHistory = results[key].Value as RunCollection;
99      }
100      if (bestSolutionHistory.Count == 0 || best.ParameterInfoString != ((StringValue)bestSolutionHistory.Last().Results["Meta-ParameterInfoString"]).Value) {
101        IRun run = best.ToRun(string.Format("{0}: {1}", currentGeneration, best.ParameterInfoString), false);
102        run.Results.Add("Meta-Generation", new IntValue(currentGeneration));
103        run.Results.Add("Meta-ParameterInfoString", new StringValue(best.ParameterInfoString));
104        bestSolutionHistory.Add(run);
105      }
106
107      return base.Apply();
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.