1 | using System.Linq;
|
---|
2 | using HeuristicLab.Common;
|
---|
3 | using HeuristicLab.Core;
|
---|
4 | using HeuristicLab.Data;
|
---|
5 | using HeuristicLab.Operators;
|
---|
6 | using HeuristicLab.Optimization;
|
---|
7 | using HeuristicLab.Parameters;
|
---|
8 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
9 | using System.Collections.Generic;
|
---|
10 | using System;
|
---|
11 |
|
---|
12 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
13 | /// <summary>
|
---|
14 | /// TODO
|
---|
15 | /// </summary>
|
---|
16 | [Item("ReferenceQualityAnalyzer", "")]
|
---|
17 | [StorableClass]
|
---|
18 | public sealed class ReferenceQualityAnalyzer : SingleSuccessorOperator, IAnalyzer {
|
---|
19 |
|
---|
20 | public ValueLookupParameter<ResultCollection> ResultsParameter {
|
---|
21 | get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
22 | }
|
---|
23 | public ScopeTreeLookupParameter<ParameterConfigurationTree> ParameterConfigurationParameter {
|
---|
24 | get { return (ScopeTreeLookupParameter<ParameterConfigurationTree>)Parameters["ParameterConfigurationTree"]; }
|
---|
25 | }
|
---|
26 | public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
27 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
28 | }
|
---|
29 | public LookupParameter<DoubleArray> ProblemQualityReferenceParameter {
|
---|
30 | get { return (LookupParameter<DoubleArray>)Parameters["ProblemQualityReferences"]; }
|
---|
31 | }
|
---|
32 | public LookupParameter<ConstrainedItemList<IProblem>> ProblemsParameter {
|
---|
33 | get { return (LookupParameter<ConstrainedItemList<IProblem>>)Parameters[MetaOptimizationProblem.ProblemsParameterName]; }
|
---|
34 | }
|
---|
35 |
|
---|
36 | public ReferenceQualityAnalyzer()
|
---|
37 | : base() {
|
---|
38 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", ""));
|
---|
39 | Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", ""));
|
---|
40 | Parameters.Add(new ScopeTreeLookupParameter<ParameterConfigurationTree>("ParameterConfigurationTree", ""));
|
---|
41 | Parameters.Add(new LookupParameter<DoubleArray>("ProblemQualityReferences", ""));
|
---|
42 | Parameters.Add(new LookupParameter<ConstrainedItemList<IProblem>>(MetaOptimizationProblem.ProblemsParameterName, ""));
|
---|
43 | }
|
---|
44 |
|
---|
45 | [StorableConstructor]
|
---|
46 | private ReferenceQualityAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
47 | private ReferenceQualityAnalyzer(ReferenceQualityAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
48 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
49 | return new ReferenceQualityAnalyzer(this, cloner);
|
---|
50 | }
|
---|
51 |
|
---|
52 | public override IOperation Apply() {
|
---|
53 | ResultCollection results = ResultsParameter.ActualValue;
|
---|
54 | ItemArray<ParameterConfigurationTree> solutions = ParameterConfigurationParameter.ActualValue;
|
---|
55 | ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
|
---|
56 |
|
---|
57 | if (ProblemQualityReferenceParameter.ActualValue == null) {
|
---|
58 | DoubleArray referenceQualities = CalculateReferenceQualities(solutions);
|
---|
59 | ProblemQualityReferenceParameter.ActualValue = referenceQualities;
|
---|
60 | NormalizePopulation(solutions, qualities, referenceQualities);
|
---|
61 | results.Add(new Result("QualityReferences", referenceQualities));
|
---|
62 | }
|
---|
63 |
|
---|
64 | return base.Apply();
|
---|
65 | }
|
---|
66 |
|
---|
67 | private DoubleArray CalculateReferenceQualities(ItemArray<ParameterConfigurationTree> solutions) {
|
---|
68 | DoubleArray referenceQualities = new DoubleArray(ProblemsParameter.ActualValue.Count);
|
---|
69 | for (int pi = 0; pi < ProblemsParameter.ActualValue.Count; pi++) {
|
---|
70 | referenceQualities[pi] = solutions.Where(x => x.AverageQualities != null).Select(x => x.AverageQualities[pi]).Min(); // todo: respect minimization / maximization
|
---|
71 | }
|
---|
72 | return referenceQualities;
|
---|
73 | }
|
---|
74 |
|
---|
75 | private void NormalizePopulation(ItemArray<ParameterConfigurationTree> solutions, ItemArray<DoubleValue> qualities, DoubleArray referenceQualities) {
|
---|
76 | for (int i = 0; i < solutions.Length; i++) {
|
---|
77 | qualities[i].Value = MetaOptimizationUtil.NormalizeQualities(solutions[i], referenceQualities.ToArray());
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|
81 | }
|
---|