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 | using HeuristicLab.Analysis;
|
---|
12 |
|
---|
13 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
14 | /// <summary>
|
---|
15 | /// TODO
|
---|
16 | /// </summary>
|
---|
17 | [Item("PMOBestSolutionHistoryAnalyzer", "")]
|
---|
18 | [StorableClass]
|
---|
19 | public sealed class PMOBestSolutionHistoryAnalyzer : SingleSuccessorOperator, IAnalyzer {
|
---|
20 |
|
---|
21 | public ScopeTreeLookupParameter<ParameterConfigurationTree> ParameterConfigurationParameter {
|
---|
22 | get { return (ScopeTreeLookupParameter<ParameterConfigurationTree>)Parameters["ParameterConfigurationTree"]; }
|
---|
23 | }
|
---|
24 | public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
25 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
26 | }
|
---|
27 | public ValueLookupParameter<ResultCollection> ResultsParameter {
|
---|
28 | get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
29 | }
|
---|
30 | public LookupParameter<ConstrainedItemList<IProblem>> ProblemsParameter {
|
---|
31 | get { return (LookupParameter<ConstrainedItemList<IProblem>>)Parameters[MetaOptimizationProblem.ProblemsParameterName]; }
|
---|
32 | }
|
---|
33 | public LookupParameter<IntValue> GenerationsParameter {
|
---|
34 | get { return (LookupParameter<IntValue>)Parameters["Generations"]; }
|
---|
35 | }
|
---|
36 |
|
---|
37 | public PMOBestSolutionHistoryAnalyzer()
|
---|
38 | : base() {
|
---|
39 | Parameters.Add(new ScopeTreeLookupParameter<ParameterConfigurationTree>("ParameterConfigurationTree", ""));
|
---|
40 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", ""));
|
---|
41 | Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", ""));
|
---|
42 | Parameters.Add(new LookupParameter<ConstrainedItemList<IProblem>>(MetaOptimizationProblem.ProblemsParameterName));
|
---|
43 | Parameters.Add(new LookupParameter<IntValue>("Generations", ""));
|
---|
44 | }
|
---|
45 |
|
---|
46 | [StorableConstructor]
|
---|
47 | private PMOBestSolutionHistoryAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
48 | private PMOBestSolutionHistoryAnalyzer(PMOBestSolutionHistoryAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
49 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
50 | return new PMOBestSolutionHistoryAnalyzer(this, cloner);
|
---|
51 | }
|
---|
52 |
|
---|
53 | public override IOperation Apply() {
|
---|
54 | ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
|
---|
55 | ResultCollection results = ResultsParameter.ActualValue;
|
---|
56 | ItemArray<ParameterConfigurationTree> parameterTrees = ParameterConfigurationParameter.ActualValue;
|
---|
57 | int currentGeneration = GenerationsParameter.ActualValue.Value;
|
---|
58 | int idxBest = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index; // todo: respect Min/Max
|
---|
59 | ParameterConfigurationTree best = parameterTrees[idxBest];
|
---|
60 |
|
---|
61 | string key = "BestSolutionHistory";
|
---|
62 | RunCollection bestSolutionHistory;
|
---|
63 | if (!results.ContainsKey(key)) {
|
---|
64 | bestSolutionHistory = new RunCollection();
|
---|
65 | results.Add(new Result(key, bestSolutionHistory));
|
---|
66 | } else {
|
---|
67 | bestSolutionHistory = results[key].Value as RunCollection;
|
---|
68 | }
|
---|
69 | if (bestSolutionHistory.Count == 0 || best.ParameterInfoString != ((StringValue)bestSolutionHistory.Last().Results["Meta.ParameterInfoString"]).Value) {
|
---|
70 | IRun run = best.ToRun(string.Format("{0}: {1}", currentGeneration, best.ParameterInfoString));
|
---|
71 | run.Results.Add("Meta.Generation", new IntValue(currentGeneration));
|
---|
72 | run.Results.Add("Meta.ParameterInfoString", new StringValue(best.ParameterInfoString));
|
---|
73 | bestSolutionHistory.Add(run);
|
---|
74 | }
|
---|
75 |
|
---|
76 | return base.Apply();
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|