Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Analyzers/PMOBestSolutionHistoryAnalyzer.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.2 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("PMOBestSolutionHistoryAnalyzer", "")]
15  [StorableClass]
16  public sealed class PMOBestSolutionHistoryAnalyzer : SingleSuccessorOperator, IAnalyzer {
17
18    public ScopeTreeLookupParameter<ParameterConfigurationTree> ParameterConfigurationParameter {
19      get { return (ScopeTreeLookupParameter<ParameterConfigurationTree>)Parameters["ParameterConfigurationTree"]; }
20    }
21    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
22      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
23    }
24    public ValueLookupParameter<ResultCollection> ResultsParameter {
25      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
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 LookupParameter<BoolValue> MaximizationParameter {
34      get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
35    }
36
37    public PMOBestSolutionHistoryAnalyzer()
38      : base() {
39      Parameters.Add(new ScopeTreeLookupParameter<ParameterConfigurationTree>("ParameterConfigurationTree", ""));
40      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", ""));
41      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", ""));
42      Parameters.Add(new LookupParameter<ConstrainedItemList<IProblem>>(MetaOptimizationProblem.ProblemsParameterName));
43      Parameters.Add(new LookupParameter<IntValue>("Generations", ""));
44      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized."));
45    }
46
47    [StorableConstructor]
48    private PMOBestSolutionHistoryAnalyzer(bool deserializing) : base(deserializing) { }
49    private PMOBestSolutionHistoryAnalyzer(PMOBestSolutionHistoryAnalyzer original, Cloner cloner) : base(original, cloner) { }
50    public override IDeepCloneable Clone(Cloner cloner) {
51      return new PMOBestSolutionHistoryAnalyzer(this, cloner);
52    }
53
54    public override IOperation Apply() {
55      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
56      ResultCollection results = ResultsParameter.ActualValue;
57      ItemArray<ParameterConfigurationTree> parameterTrees = ParameterConfigurationParameter.ActualValue;
58      int currentGeneration = GenerationsParameter.ActualValue.Value;
59      bool maximization = MaximizationParameter.ActualValue.Value;
60
61      int idxBest;
62      if (maximization)
63        idxBest = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).Last().index;
64      else
65        idxBest = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
66
67      ParameterConfigurationTree best = parameterTrees[idxBest];
68
69      string key = "BestSolutionHistory";
70      RunCollection bestSolutionHistory;
71      if (!results.ContainsKey(key)) {
72        bestSolutionHistory = new RunCollection();
73        results.Add(new Result(key, bestSolutionHistory));
74      } else {
75        bestSolutionHistory = results[key].Value as RunCollection;
76      }
77      if (bestSolutionHistory.Count == 0 || best.ParameterInfoString != ((StringValue)bestSolutionHistory.Last().Results["Meta.ParameterInfoString"]).Value) {
78        IRun run = best.ToRun(string.Format("{0}: {1}", currentGeneration, best.ParameterInfoString));
79        run.Results.Add("Meta.Generation", new IntValue(currentGeneration));
80        run.Results.Add("Meta.ParameterInfoString", new StringValue(best.ParameterInfoString));
81        bestSolutionHistory.Add(run);
82      }
83
84      return base.Apply();
85    }
86  }
87}
Note: See TracBrowser for help on using the repository browser.