Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2499: added code from HeuristicLab.BioBoost.Views (from private repository) nothing much has been changed

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