Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7173 was 7173, checked in by ascheibe, 12 years ago

#1215 enabled analyzers for MetaOpt per default

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