Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost/3.3/Evaluators/FeedstockCostEvaluator.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: 4.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.Persistence.Default.CompositeSerializers.Storable;
26using System;
27using System.Linq;
28using HeuristicLab.BioBoost.Representation;
29using HeuristicLab.BioBoost.Utils;
30using HEAL.Attic;
31
32namespace HeuristicLab.BioBoost.Evaluators {
33
34  [StorableType("A2DAA8F7-71AB-48F8-B019-CF14FB34A979")]
35  [Item("FeedstockCostEvaluator", @"Calculates all feedstock costs for known products,
36for which both <Product.Label>Potentials and <Product.Label>Utilizations are available.
37The intermedate results are vectors of amounts and costs per product and region and a
38final output named <Product.Label>Cost for each product.")]
39  public class FeedstockCostEvaluator : BioBoostEvaluator {
40
41    #region Construction & Cloning
42    [StorableConstructor]
43    protected FeedstockCostEvaluator(StorableConstructorFlag _) : base(_) { }
44    protected FeedstockCostEvaluator(FeedstockCostEvaluator original, Cloner cloner) : base(original, cloner) { }
45    public FeedstockCostEvaluator() { }
46    public override IDeepCloneable Clone(Cloner cloner) {
47      return new FeedstockCostEvaluator(this, cloner);
48    }
49    #endregion
50
51    /// <summary>
52    /// foreach product with both potential and utilization calculate
53    /// amounts = potential*utilization
54    /// costs = amounts*price*penalty(utilization)
55    /// cost = sum(costs)
56    /// </summary>
57    public override IOperation Apply() {
58      foreach (var p in ProblemData.Products) {
59        var totalProductCost = 0d;
60        var potentials = GetFromProblemData<DoubleArray>(LayerDescriptor.PotentialsFromProblemData.NameWithPrefix(p.Label));
61        var utilizations = GetFromScope<DoubleArray>(LayerDescriptor.Utilizations.NameWithPrefix(p.Label));
62        if (potentials == null || utilizations == null) continue;
63        var utilizationsEffective = (DoubleArray)utilizations.Clone();
64        var length = Math.Min(potentials.Length, utilizations.Length);
65        var amountsAtSource = new DoubleArray(length);
66        var feedstockCosts = new DoubleArray(length);
67       
68        // price/supply curve quadr. for utilization > 0.5
69        for (var i = 0; i < length; i++) {
70          var regionName = ProblemData.LocationNames[i];
71          var amount = potentials[i] * utilizations[i];
72          if (amount < 100) { // TODO: make this configurable
73            utilizationsEffective[i] = 0;
74            amount = 0;
75          }
76          amountsAtSource[i] = amount;
77          var cost = GetScaledCost(
78            p.GetRegionalBasePrice(regionName),
79            p.GetRegionalMaxPrice(regionName),
80            amount, utilizations[i]);
81          feedstockCosts[i] = cost;
82          totalProductCost += cost;
83        }
84
85        PutInScope(LayerDescriptor.UtilizationsEffective.NameWithPrefix(p.Label), utilizationsEffective, false);
86        PutInScope(LayerDescriptor.AmountsAtSource.NameWithPrefix(p.Label), amountsAtSource, true);
87        PutInScope(LayerDescriptor.TotalCostsAtSource.NameWithPrefix(p.Label) , feedstockCosts, false);
88        PutInScope(LayerDescriptor.RelativeCostAtSource.NameWithPrefix(p.Label), feedstockCosts.Zip(amountsAtSource, (c,a) => a == 0 ? 0 : c/a).ToDoubleArray(), false);
89        AddCost(LayerDescriptor.AcquisitionCosts.NameWithPrefix(p.Label), totalProductCost);     
90        AddInScope(LayerDescriptor.LocalAddedValue.Name, (DoubleArray)feedstockCosts.Clone(), false);
91      }
92         
93      return base.Apply();
94    }
95
96    public static double GetScaledCost(double basePrice, double maxPrice, double amount, double utilization) {
97      if (utilization > 0.5) {
98        var priceDifference = maxPrice - basePrice;
99         return amount*(priceDifference*Math.Pow((utilization - 0.5)*2, 2) + basePrice);
100      } else {
101       return amount*basePrice;
102      }
103    }
104  }
105}
Note: See TracBrowser for help on using the repository browser.