using System.Threading; using GeoAPI.CoordinateSystems; using HeuristicLab.BioBoost.ProblemDescription; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Operators; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using System.Collections.Generic; namespace HeuristicLab.BioBoost.Evaluators { [StorableClass] public class AggregateEvaluator : AlgorithmOperator, IBioBoostSimulationEvaluator { #region ISingleObjectiveEvaluator Members public ILookupParameter QualityParameter { get { return TotalCostParameter; } } #endregion #region IMultiObjectEvaluator Members public ILookupParameter QualitiesParameter { get { return (ILookupParameter) Parameters["QualityCriteria"]; } } #endregion #region Parameters public ILookupParameter CostsParameter { get { return (ILookupParameter)Parameters["Costs"]; } } public ILookupParameter IntermediateResultsParameter { get { return (ILookupParameter)Parameters["IntermediateResults"]; } } public LookupParameter ProblemDataParameter { get { return (LookupParameter)Parameters["ProblemData"]; } } public LookupParameter TotalCostParameter { get { return (LookupParameter)Parameters["TotalCost"]; } } public LookupParameter RemoveIntermediateResultsParameter { get { return (LookupParameter)Parameters["RemoveIntermediateResults"]; } } #endregion #region Parameter Values public ResultCollection Costs { get { return CostsParameter.ActualValue; } } public ResultCollection IntermediateResults { get { return IntermediateResultsParameter.ActualValue; } } public BioBoostProblemData ProblemData { get { return ProblemDataParameter.ActualValue; } set { ProblemDataParameter.ActualValue = value; } } public bool RemoveIntermediateResults { get { return RemoveIntermediateResultsParameter.ActualValue.Value; } set { RemoveIntermediateResultsParameter.ActualValue = new BoolValue(value); } } #endregion #region Construction & Cloning [StorableConstructor] protected AggregateEvaluator(bool isDeserializing) : base(isDeserializing) { } protected AggregateEvaluator(AggregateEvaluator orig, Cloner cloner) : base(orig, cloner) { } public AggregateEvaluator() { Parameters.Add(new LookupParameter("Costs", "Collection of all costs")); Parameters.Add(new LookupParameter("IntermediateResults", "Collection of all intermediate results.")); Parameters.Add(new LookupParameter("TotalCost", "Total cost of all aggregation steps.")); Parameters.Add(new LookupParameter("ProblemData", "The encapsulated problem instance.")); Parameters.Add(new LookupParameter("RemoveIntermediateResults", "Whether to remove intermediate results created during evaluation.")); Parameters.Add(new LookupParameter("QualityCriteria", "The list of quality criteria used for multi-objecjtive optimization.")); SetOperatorChain(new SingleSuccessorOperator[] { new InitializationEvaluator(), new FeedstockCostEvaluator(), new LogisticCostEvaluator(), new ConversionCostEvaluator(), new LogisticCostEvaluator(), new ConversionCostEvaluator(), new SinkEvaluator(), new ConcludingEvaluator() }); } public override IDeepCloneable Clone(Cloner cloner) { return new AggregateEvaluator(this, cloner); } [StorableHook(HookType.AfterDeserialization)] void AfterDeserialization() { if (!Parameters.ContainsKey("QualityCriteria")) Parameters.Add(new LookupParameter("QualityCriteria", "The list of quality criteria used for multi-objecjtive optimization.")); } #endregion protected void SetOperatorChain(IEnumerable operators) { OperatorGraph.InitialOperator = null; OperatorGraph.Operators.Clear(); SingleSuccessorOperator lastOp = null; foreach (var op in operators) { if (lastOp == null) { OperatorGraph.InitialOperator = op; } else { lastOp.Successor = op; } lastOp = op; } } protected bool InitializeCostsAndIntermediateResults = true; public override IOperation Apply() { if (InitializeCostsAndIntermediateResults) { CostsParameter.ActualValue = new ResultCollection(); IntermediateResultsParameter.ActualValue = new ResultCollection(); } return base.Apply(); } // evaluates a solution for a bioboost problem specified by the utlizations and transport targets // and returns the resulting scope after complete evaluation // this is called by the BioBoostCompoundSolution to update all solution information after a change public static Scope Evaluate(BioBoostProblemData problemData, IEnumerable> utilizations, IEnumerable> transportTargets) { // prepare scope structure // (necessary because InitializationEvaluator clears the solutionCandidateScope) var problemScope = new Scope("Problem"); var solutionCandScope = new Scope("SolutionCandidate"); problemScope.SubScopes.Add(solutionCandScope); // create variables for parameter values problemScope.Variables.Add(new Variable("ProblemData", problemData)); problemScope.Variables.Add(new Variable("RemoveIntermediateResults", new BoolValue(true))); foreach (var kvp in utilizations) { solutionCandScope.Variables.Add(new Variable(kvp.Key, kvp.Value)); } foreach (var kvp in transportTargets) { solutionCandScope.Variables.Add(new Variable(kvp.Key, kvp.Value)); } // effectively executes the operators like an engine var eval = new AggregateEvaluator(); var context = new HeuristicLab.Core.ExecutionContext(null, eval, solutionCandScope); // start engine to execute the operatorgraph within this algorithmoperator and wait until it's done using (var wh = new AutoResetEvent(false)) { var engine = new HeuristicLab.SequentialEngine.SequentialEngine(); engine.Stopped += (sender, args) => { wh.Set(); }; engine.ExceptionOccurred += (sender, args) => { wh.Set(); }; engine.Prepare(context); engine.Start(); wh.WaitOne(); } return solutionCandScope; } } }