source: branches/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost/3.3/Evaluators/AggregateEvaluator.cs @ 13069

Last change on this file since 13069 was 13069, checked in by gkronber, 7 years ago

#2499: imported source code for HeuristicLab.BioBoost from private repository with some changes

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