Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Analyzers/BestParameterConfigurationAnalyzer.cs @ 5087

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

#1215

  • enabled multiple problems
  • enabled n repetitions
  • improved results output
  • reduced memory footprint significantly
  • removed viewhost icons for less screen space waste
File size: 5.5 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 An operator for analyzing the best solution of Traveling Salesman Problems given in path representation using city coordinates.
15  /// </summary>
16  [Item("BestParameterConfigurationAnalyzer", "TODO An operator for analyzing the best solution of Traveling Salesman Problems given in path representation using city coordinates.")]
17  [StorableClass]
18  public sealed class BestParameterConfigurationAnalyzer : SingleSuccessorOperator, IAnalyzer {
19
20    public ScopeTreeLookupParameter<ParameterConfigurationTree> ParameterConfigurationParameter {
21      get { return (ScopeTreeLookupParameter<ParameterConfigurationTree>)Parameters["ParameterConfigurationTree"]; }
22    }
23    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
24      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
25    }
26    public LookupParameter<IRun> BestSolutionParameter {
27      get { return (LookupParameter<IRun>)Parameters["BestSolution"]; }
28    }
29    public ValueLookupParameter<ResultCollection> ResultsParameter {
30      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
31    }
32    public LookupParameter<DoubleValue> BestKnownQualityParameter {
33      get { return (LookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
34    }
35    public LookupParameter<IRun> BestKnownSolutionParameter {
36      get { return (LookupParameter<IRun>)Parameters["BestKnownSolution"]; }
37    }
38    public LookupParameter<RunCollection> PopulationParameter {
39      get { return (LookupParameter<RunCollection>)Parameters["Population"]; }
40    }
41
42    public BestParameterConfigurationAnalyzer()
43      : base() {
44      Parameters.Add(new ScopeTreeLookupParameter<ParameterConfigurationTree>("ParameterConfigurationTree", "TODO The TSP solutions given in path representation from which the best solution should be analyzed."));
45      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "TODO The qualities of the TSP solutions which should be analyzed."));
46      Parameters.Add(new LookupParameter<IRun>("BestSolution", "TODO The best TSP solution."));
47      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "TODO The result collection where the best TSP solution should be stored."));
48      Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "TODO The quality of the best known solution of this TSP instance."));
49      Parameters.Add(new LookupParameter<IRun>("BestKnownSolution", "TODO The best known solution of this TSP instance."));
50      Parameters.Add(new LookupParameter<RunCollection>("Population", "TODO The best known solution of this TSP instance."));
51    }
52
53    [StorableConstructor]
54    private BestParameterConfigurationAnalyzer(bool deserializing) : base(deserializing) { }
55    private BestParameterConfigurationAnalyzer(BestParameterConfigurationAnalyzer original, Cloner cloner) : base(original, cloner) { }
56    public override IDeepCloneable Clone(Cloner cloner) {
57      return new BestParameterConfigurationAnalyzer(this, cloner);
58    }
59
60    public override IOperation Apply() {
61      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
62      ResultCollection results = ResultsParameter.ActualValue;
63      DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
64      ItemArray<ParameterConfigurationTree> parameterTrees = ParameterConfigurationParameter.ActualValue;
65
66      int idxBest = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
67
68
69      ParameterConfigurationTree best = (ParameterConfigurationTree)parameterTrees[idxBest];
70      IRun bestRun = new Run();
71      best.CollectResultValues(bestRun.Results);
72      best.CollectParameterValues(bestRun.Parameters);
73
74      if (bestKnownQuality == null || qualities[idxBest].Value < bestKnownQuality.Value) { // todo: respect Maximization:true/false
75        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[idxBest].Value);
76
77        BestKnownSolutionParameter.ActualValue = bestRun;
78      }
79
80      if (BestSolutionParameter.ActualValue == null) {
81        BestSolutionParameter.ActualValue = bestRun;
82        results.Add(new Result("Best Parameter Settings", bestRun));
83      } else {
84        BestSolutionParameter.ActualValue = bestRun;
85        results["Best Parameter Settings"].Value = bestRun;
86      }
87
88      // population
89
90      int i = 0;
91      RunCollection rc = new RunCollection();
92      foreach (ParameterConfigurationTree pt in parameterTrees.OrderByDescending(x => x.BestQuality.Value)) { // todo: respect Maximization:true/false
93        IRun run = new Run();
94        run.Name = string.Format("Individuum ({0})", i);
95        pt.CollectResultValues(run.Results);
96        pt.CollectParameterValues(run.Parameters);
97        rc.Add(run);
98        i++;
99      }
100      if (PopulationParameter.ActualValue == null) {
101        PopulationParameter.ActualValue = rc;
102        results.Add(new Result("Population", rc));
103      } else {
104        PopulationParameter.ActualValue = rc;
105        results["Population"].Value = rc;
106      }
107
108      return base.Apply();
109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.