Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization (trunk integration)/HeuristicLab.Problems.MetaOptimization/3.3/Analyzers/PMOProblemQualitiesAnalyzer.cs @ 12547

Last change on this file since 12547 was 8576, checked in by jkarder, 12 years ago

#1853: created branch for MetaOptimization (trunk integration)

File size: 6.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Linq;
23using HeuristicLab.Analysis;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.ParameterConfigurationEncoding;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Problems.MetaOptimization {
34  /// <summary>
35  /// An analyzer for the qualities of each defined problem.
36  /// </summary>
37  [Item("PMOProblemQualitiesAnalyzer", "An analyzer for the qualities of each defined problem.")]
38  [StorableClass]
39  public sealed class PMOProblemQualitiesAnalyzer : SingleSuccessorOperator, IAnalyzer {
40    public bool EnabledByDefault {
41      get { return true; }
42    }
43
44    public ScopeTreeLookupParameter<ParameterConfigurationTree> ParameterConfigurationParameter {
45      get { return (ScopeTreeLookupParameter<ParameterConfigurationTree>)Parameters["ParameterConfigurationTree"]; }
46    }
47    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
48      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
49    }
50    public ValueLookupParameter<ResultCollection> ResultsParameter {
51      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
52    }
53    public LookupParameter<ConstrainedItemList<IProblem>> ProblemsParameter {
54      get { return (LookupParameter<ConstrainedItemList<IProblem>>)Parameters[MetaOptimizationProblem.ProblemsParameterName]; }
55    }
56    public LookupParameter<BoolValue> MaximizationParameter {
57      get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
58    }
59
60    #region Constructors and Cloning
61    public PMOProblemQualitiesAnalyzer()
62      : base() {
63      Parameters.Add(new ScopeTreeLookupParameter<ParameterConfigurationTree>("ParameterConfigurationTree", ""));
64      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", ""));
65      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", ""));
66      Parameters.Add(new LookupParameter<ConstrainedItemList<IProblem>>(MetaOptimizationProblem.ProblemsParameterName));
67      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized."));
68    }
69
70    [StorableConstructor]
71    private PMOProblemQualitiesAnalyzer(bool deserializing) : base(deserializing) { }
72    private PMOProblemQualitiesAnalyzer(PMOProblemQualitiesAnalyzer original, Cloner cloner) : base(original, cloner) { }
73    public override IDeepCloneable Clone(Cloner cloner) {
74      return new PMOProblemQualitiesAnalyzer(this, cloner);
75    }
76    #endregion
77
78    public override IOperation Apply() {
79      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
80      ResultCollection results = ResultsParameter.ActualValue;
81      ItemArray<ParameterConfigurationTree> parameterTrees = ParameterConfigurationParameter.ActualValue;
82      bool maximization = MaximizationParameter.ActualValue.Value;
83
84      int idxBest;
85      int idxWorst;
86      var sortedQualities = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value);
87      if (maximization) {
88        idxBest = sortedQualities.Last().index;
89        idxWorst = sortedQualities.First().index;
90      } else {
91        idxBest = sortedQualities.First().index;
92        idxWorst = sortedQualities.Last().index;
93      }
94
95      int problemCount = ProblemsParameter.ActualValue.Count;
96
97      double[][] problemQualities = GetProblemQualities(parameterTrees, problemCount);
98
99      for (int i = 0; i < problemCount; i++) {
100        DataTable problemQualitiesDataTable;
101        string resultKey = "Problem." + i;
102        if (!results.ContainsKey(resultKey)) {
103          problemQualitiesDataTable = new DataTable();
104          problemQualitiesDataTable.Name = resultKey + " Qualities";
105          results.Add(new Result(resultKey, problemQualitiesDataTable));
106        } else {
107          problemQualitiesDataTable = results[resultKey].Value as DataTable;
108        }
109
110        AddValue(problemQualitiesDataTable, parameterTrees[idxBest].AverageQualities[i], "BestQuality", "BestQuality");
111        AddValue(problemQualitiesDataTable, problemQualities[i].Average(), "AverageQuality", "BestQuality");
112        AddValue(problemQualitiesDataTable, parameterTrees[idxWorst].AverageQualities[i], "WorstQuality", "BestQuality");
113      }
114
115      return base.Apply();
116    }
117
118    private static double[][] GetProblemQualities(ItemArray<ParameterConfigurationTree> parameterTrees, int problemCount) {
119      double[][] problemQualities = new double[problemCount][];
120      for (int i = 0; i < problemCount; i++) {
121        problemQualities[i] = new double[parameterTrees.Length];
122        for (int j = 0; j < parameterTrees.Length; j++) {
123          problemQualities[i][j] = parameterTrees[j].AverageQualities[i];
124        }
125      }
126      return problemQualities;
127    }
128
129    private static void AddValue(DataTable table, double data, string name, string description) {
130      DataRow row;
131      table.Rows.TryGetValue(name, out row);
132      if (row == null) {
133        row = new DataRow(name, description);
134        row.Values.Add(data);
135        table.Rows.Add(row);
136      } else {
137        row.Values.Add(data);
138      }
139    }
140  }
141}
Note: See TracBrowser for help on using the repository browser.