Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Analyzers/PMOProblemQualitiesAnalyzer.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: 5.1 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 ScopeTreeLookupParameter<ParameterConfigurationTree> ParameterConfigurationParameter {
20      get { return (ScopeTreeLookupParameter<ParameterConfigurationTree>)Parameters["ParameterConfigurationTree"]; }
21    }
22    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
23      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
24    }
25    public ValueLookupParameter<ResultCollection> ResultsParameter {
26      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
27    }
28    public LookupParameter<ConstrainedItemList<IProblem>> ProblemsParameter {
29      get { return (LookupParameter<ConstrainedItemList<IProblem>>)Parameters[MetaOptimizationProblem.ProblemsParameterName]; }
30    }
31    public LookupParameter<BoolValue> MaximizationParameter {
32      get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
33    }
34
35    public PMOProblemQualitiesAnalyzer()
36      : base() {
37      Parameters.Add(new ScopeTreeLookupParameter<ParameterConfigurationTree>("ParameterConfigurationTree", ""));
38      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", ""));
39      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", ""));
40      Parameters.Add(new LookupParameter<ConstrainedItemList<IProblem>>(MetaOptimizationProblem.ProblemsParameterName));
41      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized."));
42    }
43
44    [StorableConstructor]
45    private PMOProblemQualitiesAnalyzer(bool deserializing) : base(deserializing) { }
46    private PMOProblemQualitiesAnalyzer(PMOProblemQualitiesAnalyzer original, Cloner cloner) : base(original, cloner) { }
47    public override IDeepCloneable Clone(Cloner cloner) {
48      return new PMOProblemQualitiesAnalyzer(this, cloner);
49    }
50
51    public override IOperation Apply() {
52      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
53      ResultCollection results = ResultsParameter.ActualValue;
54      ItemArray<ParameterConfigurationTree> parameterTrees = ParameterConfigurationParameter.ActualValue;
55      bool maximization = MaximizationParameter.ActualValue.Value;
56
57      int idxBest ;
58      int idxWorst;
59      if (maximization) {
60        idxBest = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).Last().index;
61        idxWorst = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
62      } else {
63        idxBest = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
64        idxWorst = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).Last().index;
65      }
66
67      int problemCount = ProblemsParameter.ActualValue.Count;
68
69      double[][] problemQualities = GetProblemQualities(parameterTrees, problemCount);
70
71      for (int i = 0; i < problemCount; i++) {
72        DataTable problemQualitiesDataTable;
73        string resultKey = "Problem." + i;
74        if (!results.ContainsKey(resultKey)) {
75          problemQualitiesDataTable = new DataTable();
76          problemQualitiesDataTable.Name = resultKey + " Qualities";
77          results.Add(new Result(resultKey, problemQualitiesDataTable));
78        } else {
79          problemQualitiesDataTable = results[resultKey].Value as DataTable;
80        }
81
82        AddValue(problemQualitiesDataTable, parameterTrees[idxBest].AverageQualities[i], "BestQuality", "BestQuality");
83        AddValue(problemQualitiesDataTable, problemQualities[i].Average(), "AverageQuality", "BestQuality");
84        AddValue(problemQualitiesDataTable, parameterTrees[idxWorst].AverageQualities[i], "WorstQuality", "BestQuality");
85      }
86
87      return base.Apply();
88    }
89
90    private static double[][] GetProblemQualities(ItemArray<ParameterConfigurationTree> parameterTrees, int problemCount) {
91      double[][] problemQualities = new double[problemCount][];
92      for (int i = 0; i < problemCount; i++) {
93        problemQualities[i] = new double[parameterTrees.Length];
94        for (int j = 0; j < parameterTrees.Length; j++) {
95          problemQualities[i][j] = parameterTrees[j].AverageQualities[i];
96        }
97      }
98      return problemQualities;
99    }
100
101    private static void AddValue(DataTable table, double data, string name, string description) {
102      DataRow row;
103      table.Rows.TryGetValue(name, out row);
104      if (row == null) {
105        row = new DataRow(name, description);
106        row.Values.Add(data);
107        table.Rows.Add(row);
108      } else {
109        row.Values.Add(data);
110      }
111    }
112  }
113}
Note: See TracBrowser for help on using the repository browser.