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", "TODO An operator for analyzing the best solution of Traveling Salesman Problems given in path representation using city coordinates.")]
|
---|
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 |
|
---|
42 | public BestParameterConfigurationAnalyzer() : base() {
|
---|
43 | Parameters.Add(new ScopeTreeLookupParameter<ParameterConfigurationTree>("ParameterConfigurationTree", "TODO The TSP solutions given in path representation from which the best solution should be analyzed."));
|
---|
44 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "TODO The qualities of the TSP solutions which should be analyzed."));
|
---|
45 | Parameters.Add(new LookupParameter<IRun>("BestSolution", "TODO The best TSP solution."));
|
---|
46 | Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "TODO The result collection where the best TSP solution should be stored."));
|
---|
47 | Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "TODO The quality of the best known solution of this TSP instance."));
|
---|
48 | Parameters.Add(new LookupParameter<IRun>("BestKnownSolution", "TODO The best known solution of this TSP instance."));
|
---|
49 | Parameters.Add(new LookupParameter<RunCollection>("Population", "TODO The best known solution of this TSP instance."));
|
---|
50 | }
|
---|
51 |
|
---|
52 | [StorableConstructor]
|
---|
53 | private BestParameterConfigurationAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
54 | private BestParameterConfigurationAnalyzer(BestParameterConfigurationAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
55 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
56 | return new BestParameterConfigurationAnalyzer(this, cloner);
|
---|
57 | }
|
---|
58 |
|
---|
59 | public override IOperation Apply() {
|
---|
60 | ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
|
---|
61 | ResultCollection results = ResultsParameter.ActualValue;
|
---|
62 | DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
|
---|
63 | ItemArray<ParameterConfigurationTree> parameterTrees = ParameterConfigurationParameter.ActualValue;
|
---|
64 |
|
---|
65 | int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
|
---|
66 |
|
---|
67 | EngineAlgorithm bestAlg = ((EngineAlgorithm)((ParameterConfigurationTree)parameterTrees[i]).ActualValue.Value);
|
---|
68 | Run bestRun = new Run(bestAlg);
|
---|
69 | ((ParameterConfigurationTree)parameterTrees[i]).CollectResultValues(bestRun.Results);
|
---|
70 |
|
---|
71 | if (bestKnownQuality == null || qualities[i].Value < bestKnownQuality.Value) { // todo: respect Maximization:true/false
|
---|
72 | BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
|
---|
73 | BestKnownSolutionParameter.ActualValue = bestRun;
|
---|
74 | }
|
---|
75 |
|
---|
76 | IRun best = BestSolutionParameter.ActualValue;
|
---|
77 | if (best == null) {
|
---|
78 | BestSolutionParameter.ActualValue = bestRun;
|
---|
79 | results.Add(new Result("Best Parameter Settings", bestRun));
|
---|
80 | } else {
|
---|
81 | BestSolutionParameter.ActualValue = bestRun;
|
---|
82 | results["Best Parameter Settings"].Value = bestRun;
|
---|
83 | }
|
---|
84 |
|
---|
85 | // population
|
---|
86 | RunCollection rc = new RunCollection();
|
---|
87 | foreach (ParameterConfigurationTree pt in parameterTrees.OrderByDescending(x => x.BestQuality.Value)) { // todo: respect Maximization:true/false
|
---|
88 | IAlgorithm alg = (IAlgorithm)pt.ActualValue.Value;
|
---|
89 | alg.StoreAlgorithmInEachRun = false;
|
---|
90 | IRun run = new Run(alg);
|
---|
91 | pt.CollectResultValues(run.Results);
|
---|
92 | rc.Add(run);
|
---|
93 | }
|
---|
94 | if (PopulationParameter.ActualValue == null) {
|
---|
95 | PopulationParameter.ActualValue = rc;
|
---|
96 | results.Add(new Result("Population", rc));
|
---|
97 | } else {
|
---|
98 | PopulationParameter.ActualValue = rc;
|
---|
99 | results["Population"].Value = rc;
|
---|
100 | }
|
---|
101 |
|
---|
102 | return base.Apply();
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|