Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost/3.3/Evaluators/FeedstockCostEvaluator.cs @ 13071

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

#2499: added license headers and removed unused usings

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