Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost/3.3/ProblemDescription/SolutionSpaceDiscoverer.cs @ 15711

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

#2499: added license headers and removed unused usings

File size: 6.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 System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.BioBoost.Data;
26using HeuristicLab.Data;
27
28namespace HeuristicLab.BioBoost.ProblemDescription {
29  public class SolutionSpaceDiscoverer {
30
31    private enum ProductCheck { None, NonFeasible, InProgress, Feasible };
32
33    // caches
34    private readonly List<Product> products;
35    private readonly HashSet<string> transportableProductNames;
36    private readonly List<Conversion> conversions;
37    private readonly Dictionary<string, double> prices;
38    private readonly Dictionary<Product, List<Conversion>> choices; 
39
40    // status
41    private readonly Dictionary<Product, ProductCheck> currentChecks;
42
43    // results
44    public List<string> FeasibleFeedstocks { get; private set; }
45    public List<string> FeasibleTransports { get; private set; }
46    public Dictionary<string, List<string>> FeasibleChoices { get; private set; }
47    public Dictionary<string, string> FeasibilityAnalysis { get; private set; }
48    public int MaxDepth { get; private set; }
49
50    public SolutionSpaceDiscoverer(IEnumerable<string> feedstockNames, List<Product> products, IEnumerable<Logistic> logistics, IEnumerable<Conversion> conversions) {
51      this.products = products.ToList();
52      transportableProductNames = new HashSet<string>();
53      foreach (var l in logistics) {
54        transportableProductNames.Add(l.Product);
55      }
56      this.conversions = conversions.ToList();
57      this.prices = products.ToDictionary(p => p.Label, p => p.Price);
58      this.choices = new Dictionary<Product, List<Conversion>>();
59
60      this.currentChecks = products.ToDictionary(p => p, p => ProductCheck.None);
61
62      this.FeasibilityAnalysis = products.ToDictionary(p => p.Label, p => string.Empty);
63      this.FeasibleFeedstocks = new List<string>();
64      this.FeasibleTransports = new List<string>();
65      this.MaxDepth = 0;
66      var feedstocks = new HashSet<string>(feedstockNames);
67      var feedstockProducts = products.Where(p => feedstocks.Contains(p.Label)).ToList();
68      foreach (var input in feedstockProducts) {
69        if (CheckProduct(input)) {
70          FeasibleFeedstocks.Add(input.Label);
71        }
72      }
73      foreach (var pair in currentChecks) {
74        if (pair.Value == ProductCheck.Feasible) {
75          FeasibleTransports.Add(pair.Key.Label);
76          List<Conversion> newChoices = null;
77          if (choices.TryGetValue(pair.Key, out newChoices)) {
78            FeasibleChoices.Add(pair.Key.Label, newChoices.Select(c => c.Label).ToList());
79          }
80        }
81      }
82    }
83
84    public SolutionSpaceDiscoverer(IEnumerable<string> feedstockNames, List<DataItem> dataItems)
85      : this(feedstockNames, dataItems.OfType<Product>().ToList(), dataItems.OfType<Logistic>(), dataItems.OfType<Conversion>()) {}
86
87
88    private bool CheckProduct(Product inputProduct, int currentDepth = 1) {
89
90      switch (currentChecks[inputProduct]) {
91        case ProductCheck.Feasible:
92          return true;
93        case ProductCheck.NonFeasible:
94          return false;
95        case ProductCheck.InProgress:
96          FeasibilityAnalysis[inputProduct.Label] += "not checked recursively: circular dependency";
97          return false;
98        case ProductCheck.None:
99          break;
100      }
101      currentChecks[inputProduct] = ProductCheck.InProgress;
102
103      // transportable?
104      if (!transportableProductNames.Contains(inputProduct.Label)) {
105        currentChecks[inputProduct] = ProductCheck.NonFeasible;
106        FeasibilityAnalysis[inputProduct.Label] += "NOT transportable";
107        return false; // not transportable
108      }
109
110      // convertable?
111      var otherOutputProducts = new HashSet<Product>();
112      var profitableOutputProducts = new HashSet<Product>();
113      var profitableConversions = new List<Conversion>();
114      foreach (var c in conversions.Where(c => c.Feedstock == inputProduct.Label)) {
115
116        // outputs?
117        foreach (var outputProductParameter in c.Products) {
118          var outputProductName = outputProductParameter.Name;
119          var outputAmount = outputProductParameter.Value as DoubleValue;
120          if (string.IsNullOrEmpty(outputProductName))
121            continue; // no product
122          var product = products.FirstOrDefault(p2 => p2.Label == outputProductName);
123          if (product == null || outputAmount == null)
124            continue; // not a real product
125          double amount = outputAmount.Value;
126          double price;
127          if (prices.TryGetValue(product.Label, out price) && price*amount > 0) {
128            profitableOutputProducts.Add(product);
129          } else {
130            otherOutputProducts.Add(product);
131          }
132        }
133        // recurse
134        foreach (var p in profitableOutputProducts)
135          CheckProduct(p, currentDepth + 1);
136        var otherProfits = otherOutputProducts.Any(p => CheckProduct(p, currentDepth + 1));
137
138        // analyze
139        if (profitableOutputProducts.Count == 0 && !otherProfits) {
140          currentChecks[inputProduct] = ProductCheck.NonFeasible;
141          FeasibilityAnalysis[inputProduct.Label] += "NO profitable direct or indirect output products";
142        } else {
143          currentChecks[inputProduct] = ProductCheck.Feasible;
144          MaxDepth = Math.Max(MaxDepth, currentDepth);
145          profitableConversions.Add(c);
146          if (profitableOutputProducts.Count > 0) FeasibilityAnalysis[inputProduct.Label] += "profitable direct output products ";
147          if (otherProfits) FeasibilityAnalysis[inputProduct.Label] += "profitable indirect output products";
148        }
149      }
150
151      // conclusion
152      if (profitableConversions.Count > 1)
153        choices.Add(inputProduct, profitableConversions);
154      return profitableConversions.Count > 0;
155    }
156  }
157}
Note: See TracBrowser for help on using the repository browser.