Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Analyzers/PMOProblemQualitiesAnalyzer.cs @ 5576

Last change on this file since 5576 was 5576, checked in by cneumuel, 12 years ago

#1215

  • implemented best solution history analyzer
  • implemented quality analyzer for each base-level problem
File size: 4.6 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;
11using HeuristicLab.Analysis;
12
13namespace HeuristicLab.Problems.MetaOptimization {
14  /// <summary>
15  /// TODO
16  /// </summary>
17  [Item("PMOProblemQualitiesAnalyzer", "")]
18  [StorableClass]
19  public sealed class PMOProblemQualitiesAnalyzer : SingleSuccessorOperator, IAnalyzer {
20
21    public ScopeTreeLookupParameter<ParameterConfigurationTree> ParameterConfigurationParameter {
22      get { return (ScopeTreeLookupParameter<ParameterConfigurationTree>)Parameters["ParameterConfigurationTree"]; }
23    }
24    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
25      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
26    }
27    public ValueLookupParameter<ResultCollection> ResultsParameter {
28      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
29    }
30    public LookupParameter<ConstrainedItemList<IProblem>> ProblemsParameter {
31      get { return (LookupParameter<ConstrainedItemList<IProblem>>)Parameters[MetaOptimizationProblem.ProblemsParameterName]; }
32    }
33
34    public PMOProblemQualitiesAnalyzer()
35      : base() {
36      Parameters.Add(new ScopeTreeLookupParameter<ParameterConfigurationTree>("ParameterConfigurationTree", ""));
37      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", ""));
38      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", ""));
39      Parameters.Add(new LookupParameter<ConstrainedItemList<IProblem>>(MetaOptimizationProblem.ProblemsParameterName));
40    }
41
42    [StorableConstructor]
43    private PMOProblemQualitiesAnalyzer(bool deserializing) : base(deserializing) { }
44    private PMOProblemQualitiesAnalyzer(PMOProblemQualitiesAnalyzer original, Cloner cloner) : base(original, cloner) { }
45    public override IDeepCloneable Clone(Cloner cloner) {
46      return new PMOProblemQualitiesAnalyzer(this, cloner);
47    }
48
49    public override IOperation Apply() {
50      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
51      ResultCollection results = ResultsParameter.ActualValue;
52      ItemArray<ParameterConfigurationTree> parameterTrees = ParameterConfigurationParameter.ActualValue;
53      int idxBest = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index; // todo: respect Min/Max
54      int idxWorst = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).Last().index;
55     
56      int problemCount = ProblemsParameter.ActualValue.Count;
57
58      double[][] problemQualities = GetProblemQualities(parameterTrees, problemCount);
59
60      for (int i = 0; i < problemCount; i++) {
61        DataTable problemQualitiesDataTable;
62        string resultKey = "Problem." + i;
63        if (!results.ContainsKey(resultKey)) {
64          problemQualitiesDataTable = new DataTable();
65          problemQualitiesDataTable.Name = resultKey + " Qualities";
66          results.Add(new Result(resultKey, problemQualitiesDataTable));
67        } else {
68          problemQualitiesDataTable = results[resultKey].Value as DataTable;
69        }
70
71        AddValue(problemQualitiesDataTable, parameterTrees[idxBest].AverageQualities[i], "BestQuality", "BestQuality");
72        AddValue(problemQualitiesDataTable, problemQualities[i].Average(), "AverageQuality", "BestQuality");
73        AddValue(problemQualitiesDataTable, parameterTrees[idxWorst].AverageQualities[i], "WorstQuality", "BestQuality");
74      }
75
76      return base.Apply();
77    }
78
79    private static double[][] GetProblemQualities(ItemArray<ParameterConfigurationTree> parameterTrees, int problemCount) {
80      double[][] problemQualities = new double[problemCount][];
81      for (int i = 0; i < problemCount; i++) {
82        problemQualities[i] = new double[parameterTrees.Length];
83        for (int j = 0; j < parameterTrees.Length; j++) {
84          problemQualities[i][j] = parameterTrees[j].AverageQualities[i];
85        }
86      }
87      return problemQualities;
88    }
89
90    private static void AddValue(DataTable table, double data, string name, string description) {
91      DataRow row;
92      table.Rows.TryGetValue(name, out row);
93      if (row == null) {
94        row = new DataRow(name, description);
95        row.Values.Add(data);
96        table.Rows.Add(row);
97      } else {
98        row.Values.Add(data);
99      }
100    }
101  }
102}
Note: See TracBrowser for help on using the repository browser.