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 An operator for analyzing the best solution of Traveling Salesman Problems given in path representation using city coordinates.
|
---|
15 | /// </summary>
|
---|
16 | [Item("BestParameterConfigurationAnalyzer", "")]
|
---|
17 | [StorableClass]
|
---|
18 | public sealed class BestParameterConfigurationAnalyzer : SingleSuccessorOperator, IAnalyzer {
|
---|
19 |
|
---|
20 | public ScopeTreeLookupParameter<ParameterConfigurationTree> ParameterConfigurationParameter {
|
---|
21 | get { return (ScopeTreeLookupParameter<ParameterConfigurationTree>)Parameters["ParameterConfigurationTree"]; }
|
---|
22 | }
|
---|
23 | public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
24 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
25 | }
|
---|
26 | public LookupParameter<IRun> BestSolutionParameter {
|
---|
27 | get { return (LookupParameter<IRun>)Parameters["BestSolution"]; }
|
---|
28 | }
|
---|
29 | public ValueLookupParameter<ResultCollection> ResultsParameter {
|
---|
30 | get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
31 | }
|
---|
32 | public LookupParameter<DoubleValue> BestKnownQualityParameter {
|
---|
33 | get { return (LookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
34 | }
|
---|
35 | public LookupParameter<IRun> BestKnownSolutionParameter {
|
---|
36 | get { return (LookupParameter<IRun>)Parameters["BestKnownSolution"]; }
|
---|
37 | }
|
---|
38 | public LookupParameter<RunCollection> PopulationParameter {
|
---|
39 | get { return (LookupParameter<RunCollection>)Parameters["Population"]; }
|
---|
40 | }
|
---|
41 | public LookupParameter<DoubleArray> ProblemQualityReferencesParameter {
|
---|
42 | get { return (LookupParameter<DoubleArray>)Parameters["ProblemQualityReferences"]; }
|
---|
43 | }
|
---|
44 | public LookupParameter<ConstrainedItemList<ISingleObjectiveProblem>> ProblemsParameter {
|
---|
45 | get { return (LookupParameter<ConstrainedItemList<ISingleObjectiveProblem>>)Parameters[MetaOptimizationProblem.ProblemsParameterName]; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | public BestParameterConfigurationAnalyzer()
|
---|
49 | : base() {
|
---|
50 | Parameters.Add(new ScopeTreeLookupParameter<ParameterConfigurationTree>("ParameterConfigurationTree", ""));
|
---|
51 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", ""));
|
---|
52 | Parameters.Add(new LookupParameter<IRun>("BestSolution", ""));
|
---|
53 | Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", ""));
|
---|
54 | Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", ""));
|
---|
55 | Parameters.Add(new LookupParameter<IRun>("BestKnownSolution", ""));
|
---|
56 | Parameters.Add(new LookupParameter<RunCollection>("Population", ""));
|
---|
57 | Parameters.Add(new LookupParameter<DoubleArray>("ProblemQualityReferences", ""));
|
---|
58 | Parameters.Add(new LookupParameter<ConstrainedItemList<ISingleObjectiveProblem>>(MetaOptimizationProblem.ProblemsParameterName));
|
---|
59 | }
|
---|
60 |
|
---|
61 | [StorableConstructor]
|
---|
62 | private BestParameterConfigurationAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
63 | private BestParameterConfigurationAnalyzer(BestParameterConfigurationAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
64 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
65 | return new BestParameterConfigurationAnalyzer(this, cloner);
|
---|
66 | }
|
---|
67 |
|
---|
68 | public override IOperation Apply() {
|
---|
69 | ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
|
---|
70 | ResultCollection results = ResultsParameter.ActualValue;
|
---|
71 | DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
|
---|
72 | ItemArray<ParameterConfigurationTree> parameterTrees = ParameterConfigurationParameter.ActualValue;
|
---|
73 |
|
---|
74 | int idxBest = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
|
---|
75 |
|
---|
76 | ParameterConfigurationTree best = (ParameterConfigurationTree)parameterTrees[idxBest];
|
---|
77 | IRun bestRun = new Run();
|
---|
78 | best.CollectResultValues(bestRun.Results);
|
---|
79 | best.CollectParameterValues(bestRun.Parameters);
|
---|
80 |
|
---|
81 | if (bestKnownQuality == null || qualities[idxBest].Value < bestKnownQuality.Value) { // todo: respect Maximization:true/false
|
---|
82 | BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[idxBest].Value);
|
---|
83 |
|
---|
84 | BestKnownSolutionParameter.ActualValue = bestRun;
|
---|
85 | }
|
---|
86 |
|
---|
87 | if (BestSolutionParameter.ActualValue == null) {
|
---|
88 | BestSolutionParameter.ActualValue = bestRun;
|
---|
89 | results.Add(new Result("Best Parameter Settings", bestRun));
|
---|
90 | } else {
|
---|
91 | BestSolutionParameter.ActualValue = bestRun;
|
---|
92 | results["Best Parameter Settings"].Value = bestRun;
|
---|
93 | }
|
---|
94 |
|
---|
95 | // population
|
---|
96 | int i = 0;
|
---|
97 | RunCollection rc = new RunCollection();
|
---|
98 | foreach (ParameterConfigurationTree pt in parameterTrees.OrderBy(x => x.AverageQualityNormalized)) { // todo: respect Maximization:true/false
|
---|
99 | IRun run = new Run();
|
---|
100 | run.Name = string.Format("Individuum ({0})", i);
|
---|
101 | pt.CollectResultValues(run.Results);
|
---|
102 | pt.CollectParameterValues(run.Parameters);
|
---|
103 | rc.Add(run);
|
---|
104 | i++;
|
---|
105 | }
|
---|
106 | if (PopulationParameter.ActualValue == null) {
|
---|
107 | PopulationParameter.ActualValue = rc;
|
---|
108 | results.Add(new Result("Population", rc));
|
---|
109 | } else {
|
---|
110 | PopulationParameter.ActualValue = rc;
|
---|
111 | results["Population"].Value = rc;
|
---|
112 | }
|
---|
113 |
|
---|
114 | return base.Apply();
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|