#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System.Threading;
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;
using HEAL.Attic;
namespace HeuristicLab.BioBoost.Evaluators {
[StorableType("5882C8A6-F31E-4EF7-88A7-45D467038686")]
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(StorableConstructorFlag _) : base(_) { }
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;
}
}
}