Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost/3.3/Evaluators/SinkEvaluator.cs @ 17777

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

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

File size: 3.8 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 HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Optimization;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using System.Linq;
29using HeuristicLab.BioBoost.Representation;
30using HEAL.Attic;
31
32namespace HeuristicLab.BioBoost.Evaluators {
33  [StorableType("EB23EA04-FF16-43C0-902D-9DC974F5A246")]
34  public class SinkEvaluator : BioBoostEvaluator {
35
36    #region Construction & Cloning
37    [StorableConstructor]
38    protected SinkEvaluator(StorableConstructorFlag _) : base(_) { }
39    protected SinkEvaluator(SinkEvaluator original, Cloner cloner) : base(original, cloner) { }
40    public SinkEvaluator() {
41      Parameters.Add(new ScopeParameter("Scope", "The scope to search products in."));
42    }
43    public override IDeepCloneable Clone(Cloner cloner) {
44      return new SinkEvaluator(this, cloner);
45    }
46    #endregion
47
48    private static LayerDescriptor emptyLayerDescriptor = new LayerDescriptor("", "", "");
49
50    public override IOperation Apply() {
51      foreach (var product in ProblemData.Products) {
52        InvoiceProduct(product.Label, product.Price, emptyLayerDescriptor, LayerDescriptor.DischargeCosts);
53        InvoiceProduct(product.Label, product.PriceAtSource, LayerDescriptor.AmountsAtSource, LayerDescriptor.TotalCostsAtSource);
54        InvoiceProduct(product.Label, product.PriceAtTarget, LayerDescriptor.AmountsAtTarget, LayerDescriptor.TotalCostsAtTarget);
55      }
56      return base.Apply();
57    }
58
59    private void InvoiceProduct(string productName, double price, LayerDescriptor amountLayerDescriptor, LayerDescriptor costLayerDescriptor) {
60      var variables = ExecutionContext.Scope.Variables;
61      IVariable amountVariable, costsVariable;
62      if (!variables.TryGetValue(amountLayerDescriptor.NameWithPrefix(productName), out amountVariable)) return;
63      var amounts = amountVariable.Value as DoubleArray;
64      if (amounts == null) return;
65      var relativeProductionCosts = new DoubleArray(amounts.Length);
66      DoubleArray productionCosts = null;
67      if (variables.TryGetValue(costLayerDescriptor.NameWithPrefix(productName), out costsVariable)) {
68        productionCosts = costsVariable.Value as DoubleArray;
69      }
70      var totalCost = amounts.Sum()*-price;
71      var length = amounts.Length;
72      var costs = new DoubleArray(length);
73      for (int i = 0; i < length; i++) {
74        costs[i] = amounts[i]*-price;
75        if (productionCosts != null && amounts[i] != 0)
76          relativeProductionCosts[i] = productionCosts[i]/amounts[i];
77      }
78      if (totalCost != 0) {
79        AddInScope(LayerDescriptor.DischargeCosts.NameWithPrefix(productName), costs, false);
80        Costs.Add(new Result(costLayerDescriptor.NameWithPrefix(productName), new DoubleValue(totalCost)));
81        if (totalCost > 0)
82          AddInScope(LayerDescriptor.LocalAddedValue.Name, costs, false);
83      }
84    }
85  }
86}
Note: See TracBrowser for help on using the repository browser.