Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1853: created branch for MetaOptimization (trunk integration)

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