Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost.Views/3.3/SupplierCollector.cs @ 15428

Last change on this file since 15428 was 13074, checked in by gkronber, 9 years ago

#2499: added license header and removed unused usings

File size: 8.2 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 System;
23using System.Collections.Generic;
24using System.Diagnostics;
25using System.Text.RegularExpressions;
26using HeuristicLab.BioBoost.Representation;
27using HeuristicLab.Data;
28
29namespace HeuristicLab.BioBoost.Views {
30
31
32  public class SupplierCollector {
33
34    public class ProductInfo {
35      public string Name;
36      public double Potential;
37      public double Utilization;
38      public double ConverterCapacity;
39      public double StorageCapacity;
40      public double ConversionCost;
41      public double StorageCost;
42      public double ConversionPlantConstructionCost;
43      public double ConversionPlantOperationCost;
44      public string TargetName;
45      public IntArray TransportTargts;
46      public DoubleArray TransportedAmounts;
47      public DoubleArray Utilizations;
48      public DoubleArray Potentials;
49      public DoubleArray AcquisitionCosts;
50      public DoubleArray DischargeCosts;
51      public DoubleArray TransportCosts;
52      public DoubleArray HandlingCosts;
53      public DoubleArray RelativeProductionCosts;
54      public DoubleArray TransportDistances;
55      public DoubleArray Tkm;
56      public StringArray TransportModes;
57      public Dictionary<string, double> Suppliers;
58      public List<int> SuppliersIndices;
59    }
60
61    public List<ProductInfo> Feedstocks { get; private set; }
62    public List<ProductInfo> Products { get; private set; }
63
64    public SupplierCollector(BioBoostCompoundSolution solution, string regionName) {
65      Feedstocks = new List<ProductInfo>();
66      Products = new List<ProductInfo>();
67      var locidx = solution.LocationNames.IndexOfFirst(s => s == regionName);
68      if (locidx < 0) return;
69      var products = new Dictionary<string, ProductInfo>();
70      foreach (var kvp in solution.DoubleValues) {
71        AddProductInfo(
72          products, kvp.Key, LayerDescriptor.PotentialsFromProblemData.FullName,
73          pi => {
74            pi.Potential = kvp.Value[locidx];
75            pi.Potentials = kvp.Value;
76          });
77        AddProductInfo(
78          products, kvp.Key, LayerDescriptor.Utilizations.FullName,
79          pi => {
80            pi.Utilization = kvp.Value[locidx];
81            pi.Utilizations = kvp.Value;
82          });
83        AddProductInfo(products, kvp.Key, LayerDescriptor.AmountsTransportedFromSource.FullName, pi => { pi.TransportedAmounts = kvp.Value; });
84        AddProductInfo(products, kvp.Key, LayerDescriptor.TotalCostsAtSource.FullName, pi => { pi.AcquisitionCosts = kvp.Value; });
85        AddProductInfo(products, kvp.Key, LayerDescriptor.DischargeCosts.FullName, pi => { pi.DischargeCosts = kvp.Value; });
86        //AddProductInfo(products, kvp.Key, LayerDescriptor.TotalCostsAtSource.FullName, pi => { pi.Costs = kvp.Value; });
87        //AddProductInfo(products, kvp.Key, LayerDescriptor.TotalCostsAtTarget.FullName, pi => { pi.Costs = kvp.Value; });
88        //AddProductInfo(products, kvp.Key, LayerDescriptor.Costs.FullName, pi => { pi.Costs = kvp.Value; });
89        //AddProductInfo(products, kvp.Key, LayerDescriptor.AmountsAtSource.FullName + LayerDescriptor.Costs.FullName, pi => { pi.Costs = Add(pi.Costs, kvp.Value); });
90        //AddProductInfo(products, kvp.Key, LayerDescriptor.AmountsAtTarget.FullName + LayerDescriptor.Costs.FullName, pi => { pi.Costs = Add(pi.Costs, kvp.Value); });
91        AddProductInfo(products, kvp.Key, LayerDescriptor.TransportCosts.FullName, pi => { pi.TransportCosts = kvp.Value; });
92        AddProductInfo(products, kvp.Key, LayerDescriptor.HandlingCosts.FullName, pi => { pi.HandlingCosts = kvp.Value; });
93        AddProductInfo(products, kvp.Key, LayerDescriptor.RelativeCostAtSource.FullName, pi => { pi.RelativeProductionCosts = kvp.Value; });
94        AddProductInfo(products, kvp.Key, LayerDescriptor.TransportDistance.FullName, pi => { pi.TransportDistances = kvp.Value; });
95        AddProductInfo(products, kvp.Key, LayerDescriptor.Tkm.FullName, pi => { pi.Tkm = kvp.Value; });
96        AddProductInfo(products, kvp.Key, LayerDescriptor.ConverterCapacities.FullName, pi => { pi.ConverterCapacity = kvp.Value[locidx]; });
97        AddProductInfo(products, kvp.Key, LayerDescriptor.StorageCapacities.FullName, pi => { pi.StorageCapacity = kvp.Value[locidx]; });
98        AddProductInfo(products, kvp.Key, LayerDescriptor.ConversionCosts.FullName, pi => { pi.ConversionCost = kvp.Value[locidx]; });
99        AddProductInfo(products, kvp.Key, LayerDescriptor.StorageCost.FullName, pi => { pi.StorageCost = kvp.Value[locidx]; });
100        AddProductInfo(products, kvp.Key, LayerDescriptor.ConversionPlantConstructionCost.FullName, pi => { pi.ConversionPlantConstructionCost = kvp.Value[locidx]; });
101        AddProductInfo(products, kvp.Key, LayerDescriptor.ConversionPlantOperationCost.FullName, pi => { pi.ConversionPlantOperationCost = kvp.Value[locidx]; });
102      }
103      foreach (var kvp in solution.IntValues) {
104        AddProductInfo(
105          products, kvp.Key, LayerDescriptor.TransportTargets.FullName,
106          pi => {
107            var target = kvp.Value[locidx];
108            pi.TargetName = target >= 0 ? solution.LocationNames[target] : "<none>";
109            pi.TransportTargts = kvp.Value;
110          });
111      }
112      if (solution.StringValues == null) {
113        foreach (var kvp in solution.StringValues) {
114          AddProductInfo(
115            products, kvp.Key, LayerDescriptor.TransportModes.FullName, pi => { pi.TransportModes = kvp.Value; });
116        }
117      }
118      foreach (var product in products.Values) {
119        product.Suppliers = new Dictionary<string, double>();
120        product.SuppliersIndices = new List<int>();
121        Products.Add(product);
122        if (product.Potentials != null) Feedstocks.Add(product);
123        if (product.TransportTargts == null) {
124          product.Suppliers.Add(solution.LocationNames[locidx], 0);
125          product.SuppliersIndices.Add(locidx);
126        } else {
127          for (int i = 0; i < product.TransportTargts.Length; i++) {
128            if (product.TransportTargts[i] != locidx) continue;
129            var amount = 1d;
130            if (product.TransportedAmounts != null) {
131              amount = product.TransportedAmounts[i];
132            } else if (product.Potentials != null && product.Utilizations != null) {
133              amount = product.Potentials[i]*product.Utilizations[i];
134            }
135            if (amount > 0) {
136              product.SuppliersIndices.Add(i);
137              product.Suppliers.Add(solution.LocationNames[i], amount);
138            }
139          }
140        }
141      }
142    }
143
144    private static readonly Regex nameSplitter = new Regex(@"([^ ]+)( .*)");
145
146    private void AddProductInfo(Dictionary<string, ProductInfo> dict, string name, string suffix, Action<ProductInfo> setter) {
147      var match = nameSplitter.Match(name);
148      if (!match.Success) return;
149      if (!match.Groups[2].Success || match.Groups[2].Value != suffix) return;
150      var productName = match.Groups[1].Value;
151      ProductInfo info;
152      if (!dict.TryGetValue(productName, out info)) {
153        info = new ProductInfo { Name = productName };
154        dict.Add(productName, info);
155      }
156      setter(info);
157    }
158
159    private static DoubleArray Add(DoubleArray a, DoubleArray b) {
160      if (a == null) return b;
161      if (b == null) return a;
162      Debug.Assert(a.Length == b.Length);
163      var result = new DoubleArray(a.Length);
164      for (int i = 0; i < a.Length; i++) {
165        result[i] = a[i] + b[i];
166      }
167      return result;
168    }
169
170  }
171}
Note: See TracBrowser for help on using the repository browser.