Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15711 was 13071, checked in by gkronber, 9 years ago

#2499: added license headers and removed unused usings

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