[5576] | 1 | using System.Linq;
|
---|
[6018] | 2 | using HeuristicLab.Analysis;
|
---|
[5576] | 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 |
|
---|
| 11 | namespace 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 | }
|
---|
[6018] | 31 | public LookupParameter<BoolValue> MaximizationParameter {
|
---|
| 32 | get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
| 33 | }
|
---|
[5576] | 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));
|
---|
[6018] | 41 | Parameters.Add(new LookupParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized."));
|
---|
[5576] | 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;
|
---|
[6018] | 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 |
|
---|
[5576] | 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 | }
|
---|