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