Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Analyzers/ReferenceQualityAnalyzer.cs @ 16574

Last change on this file since 16574 was 16574, checked in by gkronber, 6 years ago

#2520: changed HeuristicLab.MetaOptimization addon to compile with new HL.Persistence

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