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