Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5212 was 5212, checked in by cneumuel, 14 years ago

#1215

  • added normalization for quality values of individuals
File size: 6.9 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    // stores the medians of the qualities of each problem
43    public LookupParameter<DoubleArray> ProblemQualityMediansParameter {
44      get { return (LookupParameter<DoubleArray>)Parameters["ProblemQualityMedians"]; }
45    }
46    public LookupParameter<ConstrainedItemList<ISingleObjectiveProblem>> ProblemsParameter {
47      get { return (LookupParameter<ConstrainedItemList<ISingleObjectiveProblem>>)Parameters[MetaOptimizationProblem.ProblemsParameterName]; }
48    }
49
50    public BestParameterConfigurationAnalyzer()
51      : base() {
52      Parameters.Add(new ScopeTreeLookupParameter<ParameterConfigurationTree>("ParameterConfigurationTree", "TODO The TSP solutions given in path representation from which the best solution should be analyzed."));
53      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "TODO The qualities of the TSP solutions which should be analyzed."));
54      Parameters.Add(new LookupParameter<IRun>("BestSolution", "TODO The best TSP solution."));
55      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "TODO The result collection where the best TSP solution should be stored."));
56      Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "TODO The quality of the best known solution of this TSP instance."));
57      Parameters.Add(new LookupParameter<IRun>("BestKnownSolution", "TODO The best known solution of this TSP instance."));
58      Parameters.Add(new LookupParameter<RunCollection>("Population", "TODO The best known solution of this TSP instance."));
59      Parameters.Add(new LookupParameter<DoubleArray>("ProblemQualityMedians", ""));
60      Parameters.Add(new LookupParameter<ConstrainedItemList<ISingleObjectiveProblem>>(MetaOptimizationProblem.ProblemsParameterName));
61    }
62
63    [StorableConstructor]
64    private BestParameterConfigurationAnalyzer(bool deserializing) : base(deserializing) { }
65    private BestParameterConfigurationAnalyzer(BestParameterConfigurationAnalyzer original, Cloner cloner) : base(original, cloner) { }
66    public override IDeepCloneable Clone(Cloner cloner) {
67      return new BestParameterConfigurationAnalyzer(this, cloner);
68    }
69
70    public override IOperation Apply() {
71      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
72      ResultCollection results = ResultsParameter.ActualValue;
73      DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
74      ItemArray<ParameterConfigurationTree> parameterTrees = ParameterConfigurationParameter.ActualValue;
75
76      int idxBest = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
77
78      ParameterConfigurationTree best = (ParameterConfigurationTree)parameterTrees[idxBest];
79      IRun bestRun = new Run();
80      best.CollectResultValues(bestRun.Results);
81      best.CollectParameterValues(bestRun.Parameters);
82
83      if (bestKnownQuality == null || qualities[idxBest].Value < bestKnownQuality.Value) { // todo: respect Maximization:true/false
84        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[idxBest].Value);
85
86        BestKnownSolutionParameter.ActualValue = bestRun;
87      }
88
89      if (BestSolutionParameter.ActualValue == null) {
90        BestSolutionParameter.ActualValue = bestRun;
91        results.Add(new Result("Best Parameter Settings", bestRun));
92      } else {
93        BestSolutionParameter.ActualValue = bestRun;
94        results["Best Parameter Settings"].Value = bestRun;
95      }
96
97      // population
98      int i = 0;
99      RunCollection rc = new RunCollection();
100      foreach (ParameterConfigurationTree pt in parameterTrees.OrderBy(x => x.AverageQualityNormalized)) { // todo: respect Maximization:true/false
101        IRun run = new Run();
102        run.Name = string.Format("Individuum ({0})", i);
103        pt.CollectResultValues(run.Results);
104        pt.CollectParameterValues(run.Parameters);
105        rc.Add(run);
106        i++;
107      }
108      if (PopulationParameter.ActualValue == null) {
109        PopulationParameter.ActualValue = rc;
110        results.Add(new Result("Population", rc));
111      } else {
112        PopulationParameter.ActualValue = rc;
113        results["Population"].Value = rc;
114      }
115
116      // medians for the problems
117      DoubleArray medians = new DoubleArray(ProblemsParameter.ActualValue.Count);
118      for (int pi = 0; pi < ProblemsParameter.ActualValue.Count; pi++) {
119        double[] values = new double[rc.Count];
120        for (int ri = 0; ri < rc.Count; ri++) {
121          values[ri] = ((DoubleArray)rc.ElementAt(ri).Results["RunsAverageQualities"])[pi];
122        }
123        medians[pi] = values.Median();
124      }
125      ProblemQualityMediansParameter.ActualValue = medians;
126
127      if (results.ContainsKey("ProblemsAverageQualityMedians")) {
128        results["ProblemsAverageQualityMedians"].Value = medians;
129      } else {
130        results.Add(new Result("ProblemsAverageQualityMedians", medians));
131      }
132
133      return base.Apply();
134    }
135  }
136}
Note: See TracBrowser for help on using the repository browser.