Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12448 was 7173, checked in by ascheibe, 13 years ago

#1215 enabled analyzers for MetaOpt per default

File size: 5.2 KB
Line 
1using System.Linq;
2using HeuristicLab.Analysis;
3using HeuristicLab.Common;
4using HeuristicLab.Core;
5using HeuristicLab.Data;
6using HeuristicLab.Operators;
7using HeuristicLab.Optimization;
8using HeuristicLab.Parameters;
9using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
10
11namespace HeuristicLab.Problems.MetaOptimization {
12  /// <summary>
13  /// TODO
14  /// </summary>
15  [Item("PMOProblemQualitiesAnalyzer", "")]
16  [StorableClass]
17  public sealed class PMOProblemQualitiesAnalyzer : SingleSuccessorOperator, IAnalyzer {
18
19    public bool EnabledByDefault {
20      get { return true; }
21    }
22
23    public ScopeTreeLookupParameter<ParameterConfigurationTree> ParameterConfigurationParameter {
24      get { return (ScopeTreeLookupParameter<ParameterConfigurationTree>)Parameters["ParameterConfigurationTree"]; }
25    }
26    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
27      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
28    }
29    public ValueLookupParameter<ResultCollection> ResultsParameter {
30      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
31    }
32    public LookupParameter<ConstrainedItemList<IProblem>> ProblemsParameter {
33      get { return (LookupParameter<ConstrainedItemList<IProblem>>)Parameters[MetaOptimizationProblem.ProblemsParameterName]; }
34    }
35    public LookupParameter<BoolValue> MaximizationParameter {
36      get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
37    }
38
39    public PMOProblemQualitiesAnalyzer()
40      : base() {
41      Parameters.Add(new ScopeTreeLookupParameter<ParameterConfigurationTree>("ParameterConfigurationTree", ""));
42      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", ""));
43      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", ""));
44      Parameters.Add(new LookupParameter<ConstrainedItemList<IProblem>>(MetaOptimizationProblem.ProblemsParameterName));
45      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized."));
46    }
47
48    [StorableConstructor]
49    private PMOProblemQualitiesAnalyzer(bool deserializing) : base(deserializing) { }
50    private PMOProblemQualitiesAnalyzer(PMOProblemQualitiesAnalyzer original, Cloner cloner) : base(original, cloner) { }
51    public override IDeepCloneable Clone(Cloner cloner) {
52      return new PMOProblemQualitiesAnalyzer(this, cloner);
53    }
54
55    public override IOperation Apply() {
56      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
57      ResultCollection results = ResultsParameter.ActualValue;
58      ItemArray<ParameterConfigurationTree> parameterTrees = ParameterConfigurationParameter.ActualValue;
59      bool maximization = MaximizationParameter.ActualValue.Value;
60
61      int idxBest;
62      int idxWorst;
63      if (maximization) {
64        idxBest = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).Last().index;
65        idxWorst = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
66      } else {
67        idxBest = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
68        idxWorst = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).Last().index;
69      }
70
71      int problemCount = ProblemsParameter.ActualValue.Count;
72
73      double[][] problemQualities = GetProblemQualities(parameterTrees, problemCount);
74
75      for (int i = 0; i < problemCount; i++) {
76        DataTable problemQualitiesDataTable;
77        string resultKey = "Problem." + i;
78        if (!results.ContainsKey(resultKey)) {
79          problemQualitiesDataTable = new DataTable();
80          problemQualitiesDataTable.Name = resultKey + " Qualities";
81          results.Add(new Result(resultKey, problemQualitiesDataTable));
82        } else {
83          problemQualitiesDataTable = results[resultKey].Value as DataTable;
84        }
85
86        AddValue(problemQualitiesDataTable, parameterTrees[idxBest].AverageQualities[i], "BestQuality", "BestQuality");
87        AddValue(problemQualitiesDataTable, problemQualities[i].Average(), "AverageQuality", "BestQuality");
88        AddValue(problemQualitiesDataTable, parameterTrees[idxWorst].AverageQualities[i], "WorstQuality", "BestQuality");
89      }
90
91      return base.Apply();
92    }
93
94    private static double[][] GetProblemQualities(ItemArray<ParameterConfigurationTree> parameterTrees, int problemCount) {
95      double[][] problemQualities = new double[problemCount][];
96      for (int i = 0; i < problemCount; i++) {
97        problemQualities[i] = new double[parameterTrees.Length];
98        for (int j = 0; j < parameterTrees.Length; j++) {
99          problemQualities[i][j] = parameterTrees[j].AverageQualities[i];
100        }
101      }
102      return problemQualities;
103    }
104
105    private static void AddValue(DataTable table, double data, string name, string description) {
106      DataRow row;
107      table.Rows.TryGetValue(name, out row);
108      if (row == null) {
109        row = new DataRow(name, description);
110        row.Values.Add(data);
111        table.Rows.Add(row);
112      } else {
113        row.Values.Add(data);
114      }
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.