1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using HeuristicLab.BioBoost.ProblemDescription;
|
---|
24 | using HeuristicLab.BioBoost.Representation;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Operators;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 | using System.Linq;
|
---|
33 | using HEAL.Attic;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.BioBoost.Analysis {
|
---|
36 |
|
---|
37 | [Item("BestBioBoostSolutionAnalyzer", "An operator for analyzing the best bioboost solution.")]
|
---|
38 | [StorableType("B6E57285-A990-4675-9BD2-C3D61DF46E96")]
|
---|
39 | public class BestBioBoostSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer {
|
---|
40 |
|
---|
41 | public bool EnabledByDefault { get { return true; } }
|
---|
42 |
|
---|
43 | #region Parameters
|
---|
44 | public LookupParameter<BoolValue> MaximizationParameter {
|
---|
45 | get { return (LookupParameter<BoolValue>) Parameters["Maximization"]; }
|
---|
46 | }
|
---|
47 | public ScopeTreeLookupParameter<DoubleValue> TotalCostParameter {
|
---|
48 | get { return (ScopeTreeLookupParameter<DoubleValue>) Parameters["TotalCost"]; }
|
---|
49 | }
|
---|
50 | public LookupParameter<BioBoostProblemData> ProblemDataParameter {
|
---|
51 | get { return (LookupParameter<BioBoostProblemData>) Parameters["ProblemData"]; }
|
---|
52 | }
|
---|
53 | public LookupParameter<BioBoostCompoundSolution> BestSolutionParameter {
|
---|
54 | get { return (LookupParameter<BioBoostCompoundSolution>) Parameters["BestSolution"]; }
|
---|
55 | }
|
---|
56 | public ValueLookupParameter<ResultCollection> ResultsParameter {
|
---|
57 | get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
58 | }
|
---|
59 | public LookupParameter<DoubleValue> BestKnownQualityParameter {
|
---|
60 | get { return (LookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
61 | }
|
---|
62 | public LookupParameter<DoubleValue> BestQualityParameter {
|
---|
63 | get { return (LookupParameter<DoubleValue>)Parameters["BestQuality"]; }
|
---|
64 | }
|
---|
65 | public LookupParameter<BioBoostCompoundSolution> BestKnownSolutionParameter {
|
---|
66 | get { return (LookupParameter<BioBoostCompoundSolution>)Parameters["BestKnownSolution"]; }
|
---|
67 | }
|
---|
68 | public LookupParameter<ItemCollection<BioBoostCompoundSolution>> SolutionHistoryParameter {
|
---|
69 | get { return (LookupParameter<ItemCollection<BioBoostCompoundSolution>>)Parameters["SolutionHistory"]; }
|
---|
70 | }
|
---|
71 | public ValueLookupParameter<BoolValue> TrackHistoryParameter {
|
---|
72 | get { return (ValueLookupParameter<BoolValue>) Parameters["TrackHistory"]; }
|
---|
73 | }
|
---|
74 | #endregion
|
---|
75 |
|
---|
76 | #region Parameter Values
|
---|
77 | public bool Maximization {
|
---|
78 | get { return MaximizationParameter.ActualValue.Value; }
|
---|
79 | }
|
---|
80 | public BioBoostProblemData ProblemData {
|
---|
81 | get { return ProblemDataParameter.ActualValue; }
|
---|
82 | }
|
---|
83 | public BioBoostCompoundSolution BestSolution {
|
---|
84 | get { return BestSolutionParameter.ActualValue; }
|
---|
85 | set { BestSolutionParameter.ActualValue = value; }
|
---|
86 | }
|
---|
87 | private bool TrackHistory { get { return TrackHistoryParameter.ActualValue.Value; } }
|
---|
88 | #endregion
|
---|
89 |
|
---|
90 | #region Construction & Cloning
|
---|
91 | [StorableConstructor]
|
---|
92 | protected BestBioBoostSolutionAnalyzer(StorableConstructorFlag _) : base(_) { }
|
---|
93 | protected BestBioBoostSolutionAnalyzer(BestBioBoostSolutionAnalyzer orig, Cloner cloner) : base(orig, cloner) {}
|
---|
94 | public BestBioBoostSolutionAnalyzer() {
|
---|
95 | Parameters.Add(new LookupParameter<BoolValue>("Maximization", "Whether this is a maximization problem."));
|
---|
96 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("TotalCost", "The total cost of the whole scenario."));
|
---|
97 | Parameters.Add(new LookupParameter<BioBoostProblemData>("ProblemData", "The collection of problem data."));
|
---|
98 | Parameters.Add(new LookupParameter<BioBoostCompoundSolution>("BestSolution", "Aggregation of solution components."));
|
---|
99 | Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best solution should be stored."));
|
---|
100 | Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this instance."));
|
---|
101 | Parameters.Add(new LookupParameter<DoubleValue>("BestQuality", "The quality of the best solution of this run."));
|
---|
102 | Parameters.Add(new LookupParameter<BioBoostCompoundSolution>("BestKnownSolution", "The best known solution of this instance."));
|
---|
103 | Parameters.Add(new LookupParameter<ItemCollection<BioBoostCompoundSolution>>("SolutionHistory", "The history of best solutions over time."));
|
---|
104 | Parameters.Add(new ValueLookupParameter<BoolValue>("TrackHistory", "True if all best solutions (over time) should be recorded in SolutionHistory", new BoolValue(false)));
|
---|
105 | }
|
---|
106 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
107 | return new BestBioBoostSolutionAnalyzer(this, cloner);
|
---|
108 | }
|
---|
109 |
|
---|
110 | [StorableHook(HookType.AfterDeserialization)]
|
---|
111 | public void AfterDeserialization() {
|
---|
112 | if (!Parameters.ContainsKey("Maximization"))
|
---|
113 | Parameters.Add(new LookupParameter<BoolValue>("Maximization", "Whether this is a maximization problem."));
|
---|
114 | if (!Parameters.ContainsKey("SolutionHistory"))
|
---|
115 | Parameters.Add(new LookupParameter<ItemCollection<BioBoostCompoundSolution>>("SolutionHistory", "The history of best solutions over time."));
|
---|
116 | if (!Parameters.ContainsKey("TrackHistory"))
|
---|
117 | Parameters.Add(new ValueLookupParameter<BoolValue>("TrackHistory", "True if all best solutions (over time) should be recorded in SolutionHistory", new BoolValue(false)));
|
---|
118 | if (!Parameters.ContainsKey("BestQuality"))
|
---|
119 | Parameters.Add(new LookupParameter<DoubleValue>("BestQuality", "The quality of the best solution of this run."));
|
---|
120 | }
|
---|
121 | #endregion
|
---|
122 |
|
---|
123 | IEnumerable<IScope> GetSubScopes(IScope scope, int depth) {
|
---|
124 | if (depth == 1)
|
---|
125 | return scope.SubScopes;
|
---|
126 | return scope.SubScopes.Select(s => GetSubScopes(s, depth - 1)).Aggregate((l1, l2) => l1.Concat(l2));
|
---|
127 | }
|
---|
128 |
|
---|
129 | public override IOperation Apply() {
|
---|
130 | var costs = TotalCostParameter.ActualValue.Select((cost, idx) => new {cost.Value, idx}).OrderBy(p => p.Value);
|
---|
131 | var best = Maximization ? costs.Last() : costs.First();
|
---|
132 | var bestScope = TotalCostParameter.Depth == 0 ? ExecutionContext.Scope : GetSubScopes(ExecutionContext.Scope, TotalCostParameter.Depth).ToList()[best.idx];
|
---|
133 | var newBestSolution = new BioBoostCompoundSolution(bestScope, ProblemData);
|
---|
134 | if (BestSolution == null) {
|
---|
135 | BestSolution = newBestSolution;
|
---|
136 | ResultsParameter.ActualValue.Add(new Result("BestSolution", BestSolution));
|
---|
137 | } else {
|
---|
138 | BestSolution.UpdateTo(newBestSolution);
|
---|
139 | }
|
---|
140 | if (BestKnownQualityParameter.ActualValue == null ||
|
---|
141 | Maximization && best.Value > BestKnownQualityParameter.ActualValue.Value ||
|
---|
142 | !Maximization && best.Value < BestKnownQualityParameter.ActualValue.Value) {
|
---|
143 | BestKnownQualityParameter.ActualValue = new DoubleValue(best.Value);
|
---|
144 | BestKnownSolutionParameter.ActualValue = BestSolution;
|
---|
145 | }
|
---|
146 | if (TrackHistory &&
|
---|
147 | (BestQualityParameter.ActualValue == null ||
|
---|
148 | Maximization && best.Value > BestQualityParameter.ActualValue.Value ||
|
---|
149 | !Maximization && best.Value < BestQualityParameter.ActualValue.Value)) {
|
---|
150 | IResult solutionHistoryResult = null;
|
---|
151 | if (!ResultsParameter.ActualValue.TryGetValue("SolutionHistory", out solutionHistoryResult)) {
|
---|
152 | solutionHistoryResult = new Result("SolutionHistory", new ItemCollection<BioBoostCompoundSolution>());
|
---|
153 | ResultsParameter.ActualValue.Add(solutionHistoryResult);
|
---|
154 | }
|
---|
155 | ((ItemCollection<BioBoostCompoundSolution>) solutionHistoryResult.Value).Add(
|
---|
156 | (BioBoostCompoundSolution) newBestSolution.SlimClone());
|
---|
157 | }
|
---|
158 | return base.Apply();
|
---|
159 | }
|
---|
160 |
|
---|
161 | }
|
---|
162 | }
|
---|