Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost/3.3/Evaluators/AggregateEvaluator.cs @ 16575

Last change on this file since 16575 was 16575, checked in by gkronber, 5 years ago

#2520: changed HeuristicLab.BioBoost addon to compile with new HL.Persistence

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