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.Threading;
|
---|
23 | using HeuristicLab.BioBoost.ProblemDescription;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Operators;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 | using System.Collections.Generic;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.BioBoost.Evaluators {
|
---|
34 | [StorableClass]
|
---|
35 | public class AggregateEvaluator : AlgorithmOperator, IBioBoostSimulationEvaluator {
|
---|
36 |
|
---|
37 | #region ISingleObjectiveEvaluator Members
|
---|
38 | public ILookupParameter<DoubleValue> QualityParameter { get { return TotalCostParameter; } }
|
---|
39 | #endregion
|
---|
40 |
|
---|
41 | #region IMultiObjectEvaluator Members
|
---|
42 | public ILookupParameter<DoubleArray> QualitiesParameter { get { return (ILookupParameter<DoubleArray>) Parameters["QualityCriteria"]; } }
|
---|
43 | #endregion
|
---|
44 |
|
---|
45 | #region Parameters
|
---|
46 | public ILookupParameter<ResultCollection> CostsParameter {
|
---|
47 | get { return (ILookupParameter<ResultCollection>)Parameters["Costs"]; }
|
---|
48 | }
|
---|
49 | public ILookupParameter<ResultCollection> IntermediateResultsParameter {
|
---|
50 | get { return (ILookupParameter<ResultCollection>)Parameters["IntermediateResults"]; }
|
---|
51 | }
|
---|
52 | public LookupParameter<BioBoostProblemData> ProblemDataParameter {
|
---|
53 | get { return (LookupParameter<BioBoostProblemData>)Parameters["ProblemData"]; }
|
---|
54 | }
|
---|
55 | public LookupParameter<DoubleValue> TotalCostParameter {
|
---|
56 | get { return (LookupParameter<DoubleValue>)Parameters["TotalCost"]; }
|
---|
57 | }
|
---|
58 | public LookupParameter<BoolValue> RemoveIntermediateResultsParameter {
|
---|
59 | get { return (LookupParameter<BoolValue>)Parameters["RemoveIntermediateResults"]; }
|
---|
60 | }
|
---|
61 | #endregion
|
---|
62 |
|
---|
63 | #region Parameter Values
|
---|
64 | public ResultCollection Costs {
|
---|
65 | get { return CostsParameter.ActualValue; }
|
---|
66 | }
|
---|
67 | public ResultCollection IntermediateResults {
|
---|
68 | get { return IntermediateResultsParameter.ActualValue; }
|
---|
69 | }
|
---|
70 | public BioBoostProblemData ProblemData {
|
---|
71 | get { return ProblemDataParameter.ActualValue; }
|
---|
72 | set { ProblemDataParameter.ActualValue = value; }
|
---|
73 | }
|
---|
74 | public bool RemoveIntermediateResults {
|
---|
75 | get { return RemoveIntermediateResultsParameter.ActualValue.Value; }
|
---|
76 | set { RemoveIntermediateResultsParameter.ActualValue = new BoolValue(value); }
|
---|
77 | }
|
---|
78 | #endregion
|
---|
79 |
|
---|
80 | #region Construction & Cloning
|
---|
81 | [StorableConstructor]
|
---|
82 | protected AggregateEvaluator(bool isDeserializing) : base(isDeserializing) { }
|
---|
83 | protected AggregateEvaluator(AggregateEvaluator orig, Cloner cloner) : base(orig, cloner) { }
|
---|
84 | public AggregateEvaluator() {
|
---|
85 | Parameters.Add(new LookupParameter<ResultCollection>("Costs", "Collection of all costs"));
|
---|
86 | Parameters.Add(new LookupParameter<ResultCollection>("IntermediateResults", "Collection of all intermediate results."));
|
---|
87 | Parameters.Add(new LookupParameter<DoubleValue>("TotalCost", "Total cost of all aggregation steps."));
|
---|
88 | Parameters.Add(new LookupParameter<BioBoostProblemData>("ProblemData", "The encapsulated problem instance."));
|
---|
89 | Parameters.Add(new LookupParameter<BoolValue>("RemoveIntermediateResults", "Whether to remove intermediate results created during evaluation."));
|
---|
90 | Parameters.Add(new LookupParameter<DoubleArray>("QualityCriteria", "The list of quality criteria used for multi-objecjtive optimization."));
|
---|
91 | SetOperatorChain(new SingleSuccessorOperator[] {
|
---|
92 | new InitializationEvaluator(),
|
---|
93 | new FeedstockCostEvaluator(),
|
---|
94 | new LogisticCostEvaluator(),
|
---|
95 | new ConversionCostEvaluator(),
|
---|
96 | new LogisticCostEvaluator(),
|
---|
97 | new ConversionCostEvaluator(),
|
---|
98 | new SinkEvaluator(),
|
---|
99 | new ConcludingEvaluator()
|
---|
100 | });
|
---|
101 | }
|
---|
102 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
103 | return new AggregateEvaluator(this, cloner);
|
---|
104 | }
|
---|
105 |
|
---|
106 | [StorableHook(HookType.AfterDeserialization)]
|
---|
107 | void AfterDeserialization() {
|
---|
108 | if (!Parameters.ContainsKey("QualityCriteria"))
|
---|
109 | Parameters.Add(new LookupParameter<DoubleArray>("QualityCriteria", "The list of quality criteria used for multi-objecjtive optimization."));
|
---|
110 | }
|
---|
111 | #endregion
|
---|
112 |
|
---|
113 | protected void SetOperatorChain(IEnumerable<SingleSuccessorOperator> operators) {
|
---|
114 | OperatorGraph.InitialOperator = null;
|
---|
115 | OperatorGraph.Operators.Clear();
|
---|
116 | SingleSuccessorOperator lastOp = null;
|
---|
117 | foreach (var op in operators) {
|
---|
118 | if (lastOp == null) {
|
---|
119 | OperatorGraph.InitialOperator = op;
|
---|
120 | } else {
|
---|
121 | lastOp.Successor = op;
|
---|
122 | }
|
---|
123 | lastOp = op;
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | protected bool InitializeCostsAndIntermediateResults = true;
|
---|
128 | public override IOperation Apply() {
|
---|
129 | if (InitializeCostsAndIntermediateResults) {
|
---|
130 | CostsParameter.ActualValue = new ResultCollection();
|
---|
131 | IntermediateResultsParameter.ActualValue = new ResultCollection();
|
---|
132 | }
|
---|
133 | return base.Apply();
|
---|
134 | }
|
---|
135 |
|
---|
136 | // evaluates a solution for a bioboost problem specified by the utlizations and transport targets
|
---|
137 | // and returns the resulting scope after complete evaluation
|
---|
138 | // this is called by the BioBoostCompoundSolution to update all solution information after a change
|
---|
139 | public static Scope Evaluate(BioBoostProblemData problemData,
|
---|
140 | IEnumerable<KeyValuePair<string, DoubleArray>> utilizations,
|
---|
141 | IEnumerable<KeyValuePair<string, IntArray>> transportTargets) {
|
---|
142 | // prepare scope structure
|
---|
143 | // (necessary because InitializationEvaluator clears the solutionCandidateScope)
|
---|
144 | var problemScope = new Scope("Problem");
|
---|
145 | var solutionCandScope = new Scope("SolutionCandidate");
|
---|
146 | problemScope.SubScopes.Add(solutionCandScope);
|
---|
147 |
|
---|
148 | // create variables for parameter values
|
---|
149 | problemScope.Variables.Add(new Variable("ProblemData", problemData));
|
---|
150 | problemScope.Variables.Add(new Variable("RemoveIntermediateResults", new BoolValue(true)));
|
---|
151 | foreach (var kvp in utilizations) {
|
---|
152 | solutionCandScope.Variables.Add(new Variable(kvp.Key, kvp.Value));
|
---|
153 | }
|
---|
154 | foreach (var kvp in transportTargets) {
|
---|
155 | solutionCandScope.Variables.Add(new Variable(kvp.Key, kvp.Value));
|
---|
156 | }
|
---|
157 |
|
---|
158 | // effectively executes the operators like an engine
|
---|
159 | var eval = new AggregateEvaluator();
|
---|
160 | var context = new HeuristicLab.Core.ExecutionContext(null, eval, solutionCandScope);
|
---|
161 |
|
---|
162 | // start engine to execute the operatorgraph within this algorithmoperator and wait until it's done
|
---|
163 | using (var wh = new AutoResetEvent(false)) {
|
---|
164 | var engine = new HeuristicLab.SequentialEngine.SequentialEngine();
|
---|
165 | engine.Stopped += (sender, args) => {
|
---|
166 | wh.Set();
|
---|
167 | };
|
---|
168 | engine.ExceptionOccurred += (sender, args) => {
|
---|
169 | wh.Set();
|
---|
170 | };
|
---|
171 |
|
---|
172 | engine.Prepare(context);
|
---|
173 | engine.Start();
|
---|
174 | wh.WaitOne();
|
---|
175 | }
|
---|
176 | return solutionCandScope;
|
---|
177 | }
|
---|
178 |
|
---|
179 | }
|
---|
180 | }
|
---|