Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 2.9 KB
Line 
1using HeuristicLab.Common;
2using HeuristicLab.Core;
3using HeuristicLab.Data;
4using HeuristicLab.Optimization;
5using HeuristicLab.Parameters;
6using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
7using System.Linq;
8using HeuristicLab.BioBoost.Representation;
9
10namespace HeuristicLab.BioBoost.Evaluators {
11  [StorableClass]
12  public class SinkEvaluator : BioBoostEvaluator {
13
14    #region Construction & Cloning
15    [StorableConstructor]
16    protected SinkEvaluator(bool isDeserializing) : base(isDeserializing) {}
17    protected SinkEvaluator(SinkEvaluator original, Cloner cloner) : base(original, cloner) { }
18    public SinkEvaluator() {
19      Parameters.Add(new ScopeParameter("Scope", "The scope to search products in."));
20    }
21    public override IDeepCloneable Clone(Cloner cloner) {
22      return new SinkEvaluator(this, cloner);
23    }
24    #endregion
25
26    private static LayerDescriptor emptyLayerDescriptor = new LayerDescriptor("", "", "");
27
28    public override IOperation Apply() {
29      foreach (var product in ProblemData.Products) {
30        InvoiceProduct(product.Label, product.Price, emptyLayerDescriptor, LayerDescriptor.DischargeCosts);
31        InvoiceProduct(product.Label, product.PriceAtSource, LayerDescriptor.AmountsAtSource, LayerDescriptor.TotalCostsAtSource);
32        InvoiceProduct(product.Label, product.PriceAtTarget, LayerDescriptor.AmountsAtTarget, LayerDescriptor.TotalCostsAtTarget);
33      }
34      return base.Apply();
35    }
36
37    private void InvoiceProduct(string productName, double price, LayerDescriptor amountLayerDescriptor, LayerDescriptor costLayerDescriptor) {
38      var variables = ExecutionContext.Scope.Variables;
39      IVariable amountVariable, costsVariable;
40      if (!variables.TryGetValue(amountLayerDescriptor.NameWithPrefix(productName), out amountVariable)) return;
41      var amounts = amountVariable.Value as DoubleArray;
42      if (amounts == null) return;
43      var relativeProductionCosts = new DoubleArray(amounts.Length);
44      DoubleArray productionCosts = null;
45      if (variables.TryGetValue(costLayerDescriptor.NameWithPrefix(productName), out costsVariable)) {
46        productionCosts = costsVariable.Value as DoubleArray;
47      }
48      var totalCost = amounts.Sum()*-price;
49      var length = amounts.Length;
50      var costs = new DoubleArray(length);
51      for (int i = 0; i < length; i++) {
52        costs[i] = amounts[i]*-price;
53        if (productionCosts != null && amounts[i] != 0)
54          relativeProductionCosts[i] = productionCosts[i]/amounts[i];
55      }
56      if (totalCost != 0) {
57        AddInScope(LayerDescriptor.DischargeCosts.NameWithPrefix(productName), costs, false);
58        Costs.Add(new Result(costLayerDescriptor.NameWithPrefix(productName), new DoubleValue(totalCost)));
59        if (totalCost > 0)
60          AddInScope(LayerDescriptor.LocalAddedValue.Name, costs, false);
61      }
62    }
63  }
64}
Note: See TracBrowser for help on using the repository browser.