Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Analyzers/ReferenceQualityAnalyzer.cs @ 6090

Last change on this file since 6090 was 6090, checked in by cneumuel, 13 years ago

#1215

  • added weight parameters for quality, stddev and evaluated solutions
  • lots of fixes
File size: 9.3 KB
Line 
1using System.Linq;
2using HeuristicLab.Common;
3using HeuristicLab.Core;
4using HeuristicLab.Data;
5using HeuristicLab.Operators;
6using HeuristicLab.Optimization;
7using HeuristicLab.Parameters;
8using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
9
10namespace HeuristicLab.Problems.MetaOptimization {
11  /// <summary>
12  /// TODO
13  /// </summary>
14  [Item("ReferenceQualityAnalyzer", "")]
15  [StorableClass]
16  public sealed class ReferenceQualityAnalyzer : SingleSuccessorOperator, IAnalyzer {
17
18    public ValueLookupParameter<ResultCollection> ResultsParameter {
19      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
20    }
21    public ScopeTreeLookupParameter<ParameterConfigurationTree> ParameterConfigurationParameter {
22      get { return (ScopeTreeLookupParameter<ParameterConfigurationTree>)Parameters["ParameterConfigurationTree"]; }
23    }
24    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
25      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
26    }
27    public LookupParameter<DoubleArray> ReferenceQualityAveragesParameter {
28      get { return (LookupParameter<DoubleArray>)Parameters["ReferenceQualityAverages"]; }
29    }
30    public LookupParameter<DoubleArray> ReferenceQualityDeviationsParameter {
31      get { return (LookupParameter<DoubleArray>)Parameters["ReferenceQualityDeviations"]; }
32    }
33    public LookupParameter<DoubleArray> ReferenceEvaluatedSolutionAveragesParameter {
34      get { return (LookupParameter<DoubleArray>)Parameters["ReferenceEvaluatedSolutionAverages"]; }
35    }
36    public LookupParameter<ConstrainedItemList<IProblem>> ProblemsParameter {
37      get { return (LookupParameter<ConstrainedItemList<IProblem>>)Parameters[MetaOptimizationProblem.ProblemsParameterName]; }
38    }
39    public LookupParameter<BoolValue> MaximizationParameter {
40      get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
41    }
42
43    public LookupParameter<DoubleValue> QualityWeightParameter {
44      get { return (LookupParameter<DoubleValue>)Parameters[MetaOptimizationProblem.QualityWeightParameterName]; }
45    }
46    public LookupParameter<DoubleValue> StandardDeviationWeightParameter {
47      get { return (LookupParameter<DoubleValue>)Parameters[MetaOptimizationProblem.StandardDeviationWeightParameterName]; }
48    }
49    public LookupParameter<DoubleValue> EvaluatedSolutionsWeightParameter {
50      get { return (LookupParameter<DoubleValue>)Parameters[MetaOptimizationProblem.EvaluatedSolutionsWeightParameterName]; }
51    }
52
53    public ReferenceQualityAnalyzer()
54      : base() {
55      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", ""));
56      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", ""));
57      Parameters.Add(new ScopeTreeLookupParameter<ParameterConfigurationTree>("ParameterConfigurationTree", ""));
58      Parameters.Add(new LookupParameter<DoubleArray>("ReferenceQualityAverages", ""));
59      Parameters.Add(new LookupParameter<DoubleArray>("ReferenceQualityDeviations", ""));
60      Parameters.Add(new LookupParameter<DoubleArray>("ReferenceEvaluatedSolutionAverages", ""));
61      Parameters.Add(new LookupParameter<ConstrainedItemList<IProblem>>(MetaOptimizationProblem.ProblemsParameterName));
62      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized."));
63      Parameters.Add(new LookupParameter<DoubleValue>(MetaOptimizationProblem.QualityWeightParameterName));
64      Parameters.Add(new LookupParameter<DoubleValue>(MetaOptimizationProblem.StandardDeviationWeightParameterName));
65      Parameters.Add(new LookupParameter<DoubleValue>(MetaOptimizationProblem.EvaluatedSolutionsWeightParameterName));
66    }
67
68    [StorableConstructor]
69    private ReferenceQualityAnalyzer(bool deserializing) : base(deserializing) { }
70    private ReferenceQualityAnalyzer(ReferenceQualityAnalyzer original, Cloner cloner) : base(original, cloner) { }
71    public override IDeepCloneable Clone(Cloner cloner) {
72      return new ReferenceQualityAnalyzer(this, cloner);
73    }
74
75    public override IOperation Apply() {
76      ResultCollection results = ResultsParameter.ActualValue;
77      ItemArray<ParameterConfigurationTree> solutions = ParameterConfigurationParameter.ActualValue;
78      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
79      bool maximization = MaximizationParameter.ActualValue.Value;
80      double qualityWeight = QualityWeightParameter.ActualValue.Value;
81      double standardDeviationWeight = StandardDeviationWeightParameter.ActualValue.Value;
82      double evaluatedSolutionsWeight = EvaluatedSolutionsWeightParameter.ActualValue.Value;
83
84      if (ReferenceQualityAveragesParameter.ActualValue == null) {
85        // this is generation zero. calculate the reference values and apply them on population. in future generations `AlgorithmRunsAnalyzer` will do the nomalization
86        DoubleArray referenceQualityAverages = CalculateReferenceQualityAverages(solutions, maximization);
87        DoubleArray referenceQualityDeviations = CalculateReferenceQualityDeviations(solutions, maximization);
88        DoubleArray referenceEvaluatedSolutionAverages = CalculateReferenceEvaluatedSolutionAverages(solutions, maximization);
89
90        ReferenceQualityAveragesParameter.ActualValue = referenceQualityAverages;
91        ReferenceQualityDeviationsParameter.ActualValue = referenceQualityDeviations;
92        ReferenceEvaluatedSolutionAveragesParameter.ActualValue = referenceEvaluatedSolutionAverages;
93
94        NormalizePopulation(solutions, qualities, referenceQualityAverages, referenceQualityDeviations, referenceEvaluatedSolutionAverages, qualityWeight, standardDeviationWeight, evaluatedSolutionsWeight, maximization);
95
96        results.Add(new Result("ReferenceQualities", referenceQualityAverages));
97        results.Add(new Result("ReferenceQualityDeviations", referenceQualityDeviations));
98        results.Add(new Result("ReferenceEvaluatedSolutionAverages", referenceEvaluatedSolutionAverages));
99      }
100
101      return base.Apply();
102    }
103
104    private DoubleArray CalculateReferenceQualityAverages(ItemArray<ParameterConfigurationTree> solutions, bool maximization) {
105      DoubleArray references = new DoubleArray(ProblemsParameter.ActualValue.Count);
106      for (int pi = 0; pi < ProblemsParameter.ActualValue.Count; pi++) {
107        if (maximization)
108          references[pi] = solutions.Where(x => x.AverageQualities != null).Select(x => x.AverageQualities[pi]).Max();
109        else
110          references[pi] = solutions.Where(x => x.AverageQualities != null).Select(x => x.AverageQualities[pi]).Min();
111      }
112      return references;
113    }
114
115    private DoubleArray CalculateReferenceQualityDeviations(ItemArray<ParameterConfigurationTree> solutions, bool maximization) {
116      DoubleArray references = new DoubleArray(ProblemsParameter.ActualValue.Count);
117      for (int pi = 0; pi < ProblemsParameter.ActualValue.Count; pi++) {
118        if (maximization)
119          references[pi] = solutions.Where(x => x.QualityStandardDeviations != null).Select(x => x.QualityStandardDeviations[pi]).Max();
120        else
121          references[pi] = solutions.Where(x => x.QualityStandardDeviations != null).Select(x => x.QualityStandardDeviations[pi]).Min();
122      }
123      return references;
124    }
125
126    private DoubleArray CalculateReferenceEvaluatedSolutionAverages(ItemArray<ParameterConfigurationTree> solutions, bool maximization) {
127      DoubleArray references = new DoubleArray(ProblemsParameter.ActualValue.Count);
128      for (int pi = 0; pi < ProblemsParameter.ActualValue.Count; pi++) {
129        if (maximization)
130          references[pi] = solutions.Where(x => x.AverageEvaluatedSolutions != null).Select(x => x.AverageEvaluatedSolutions[pi]).Max();
131        else
132          references[pi] = solutions.Where(x => x.AverageEvaluatedSolutions != null).Select(x => x.AverageEvaluatedSolutions[pi]).Min();
133      }
134      return references;
135    }
136
137    private void NormalizePopulation(ItemArray<ParameterConfigurationTree> solutions, ItemArray<DoubleValue> qualities,
138          DoubleArray referenceQualityAverages,
139          DoubleArray referenceQualityDeviations,
140          DoubleArray referenceEvaluatedSolutionAverages,
141          double qualityAveragesWeight,
142          double qualityDeviationsWeight,
143          double evaluatedSolutionsWeight,
144          bool maximization) {
145      for (int i = 0; i < solutions.Length; i++) {
146        if (solutions[i].AverageQualities == null || solutions[i].QualityStandardDeviations == null || solutions[i].AverageEvaluatedSolutions == null) {
147          // this parameterConfigurationTree has not been evaluated correctly (due to a faulty configuration, which led to an exception)
148          // since we are in generation zero, there is no WorstQuality available for a penalty value
149          double penaltyValue = maximization ? double.MinValue : double.MaxValue;
150          qualities[i].Value = penaltyValue;
151        } else {
152          qualities[i].Value = MetaOptimizationUtil.Normalize(solutions[i],
153            referenceQualityAverages.ToArray(),
154            referenceQualityDeviations.ToArray(),
155            referenceEvaluatedSolutionAverages.ToArray(),
156            qualityAveragesWeight,
157            qualityDeviationsWeight,
158            evaluatedSolutionsWeight,
159            maximization);
160        }
161      }
162    }
163  }
164}
Note: See TracBrowser for help on using the repository browser.