Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost.Views/3.3/BioBoostSolutionCostsView.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.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Globalization;
4using HeuristicLab.BioBoost.Representation;
5using HeuristicLab.Core.Views;
6using HeuristicLab.Data;
7using HeuristicLab.MainForm;
8using System.Drawing;
9using System.Windows.Forms;
10
11namespace HeuristicLab.BioBoost.Views {
12
13  [Content(typeof(RegionDetailData), IsDefaultView = false)]
14  public partial class BioBoostSolutionCostsView : ItemView {
15
16    public new RegionDetailData Content {
17      get { return (RegionDetailData)base.Content; }
18      set { base.Content = value; }
19    }
20   
21    public BioBoostSolutionCostsView() {
22      InitializeComponent();
23    }
24
25    protected override void OnContentChanged() {
26      base.OnContentChanged();
27      if (Content == null) {
28        Clear();
29      } else {
30        Clear();
31        Populate(Content.Solution, Content.RegionName);
32      }
33    }
34
35    private Dictionary<string, double> costs;
36    private Dictionary<String, double> values;
37    private Dictionary<string, List<string>> strings;
38
39    private void AddCost(string name, double value) {
40      // ReSharper disable once CompareOfFloatsByEqualityOperator
41      if (value == 0) return;
42      double oldValue = 0;
43      costs.TryGetValue(name, out oldValue);
44      costs[name] = oldValue + value;
45    }
46
47    private void AddSummableValue(string name, double value) {
48      // ReSharper disable once CompareOfFloatsByEqualityOperator
49      if (value == 0) return;
50      double oldValue = 0;
51      values.TryGetValue(name, out oldValue);
52      values[name] = oldValue + value;
53    }
54
55    private void AddString(string name, string value) {
56      if (string.IsNullOrEmpty(value) || value == "0") return;
57      List<String> oldValues;
58      if (!strings.TryGetValue(name, out oldValues)) {
59        oldValues = new List<string>();
60        strings[name] = oldValues;
61      }
62      if (oldValues.Count < 2) {
63        oldValues.Add(value);
64      }
65    }
66   
67
68    private void AddSupplierCosts(string name, DoubleArray values, List<int> supplierIndices) {
69      if (values == null) return;
70      double sum = 0;
71      foreach (var supplierIdx in supplierIndices) {
72        sum += values[supplierIdx];
73      }
74      AddCost(name + " Sum", sum);
75      if (supplierIndices.Count > 1)
76        AddString(name + " Avg", (sum/supplierIndices.Count).ToString(CultureInfo.InvariantCulture));
77    }
78
79    private void AddSupplierValues(string name, DoubleArray values, List<int> supplierIndices) {
80      if (values == null) return;
81      double sum = 0;
82      foreach (var supplierIdx in supplierIndices) {
83        sum += values[supplierIdx];
84      }
85      AddSummableValue(name + " Sum", sum);
86      if (supplierIndices.Count > 1)
87        AddString(name + " Avg", (sum/supplierIndices.Count).ToString(CultureInfo.InvariantCulture));
88    }
89
90
91    private void Populate(BioBoostCompoundSolution solution, string regionName) {
92      costsColumn2.DefaultCellStyle.Font = new Font("Lucida Console", 7);
93      costsColumn2.DefaultCellStyle.Format = "N0";
94      costsColumn2.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
95
96      costs = new Dictionary<string, double>();
97      values = new Dictionary<string, double>();
98      strings = new Dictionary<string, List<string>>();
99      foreach (var locationName in solution.LocationNames) {
100        if (regionName == BioBoostCompoundSolutionView.AllRegionsName || locationName.StartsWith(regionName))
101          AddRegion(solution, locationName);
102      }
103
104      var totalCost = 0d;
105      foreach (var cost in costs) {
106        costsDataGridView.Rows.Add(cost.Key, cost.Value);
107        totalCost += cost.Value;
108      }
109
110      foreach (var value in values) {
111        costsDataGridView.Rows.Add(value.Key, value.Value);
112      }
113
114      foreach (var value in strings) {
115        if (value.Value.Count == 1) {
116          double doubleValue;
117          if (Double.TryParse(value.Value[0], NumberStyles.Float, CultureInfo.InvariantCulture, out doubleValue)) {
118            costsDataGridView.Rows.Add(value.Key, doubleValue);
119          } else {
120          costsDataGridView.Rows.Add(value.Key, value.Value[0]);
121          }
122        } else {
123          //costsDataGridView.Rows.Add(value.Key, string.Join(",", value.Value));
124        }
125      }
126
127      /* var total = 0d;
128      foreach (var kvp in solution.DoubleValues) {
129        var name = kvp.Key;
130        if (name.EndsWith(BioBoostProblem.CostsName) || name.EndsWith(BioBoostProblem.CostName)) {
131          var sum = 0d;
132          for (int i = 0; i < kvp.Value.Length; i++) {
133            if (solution.LocationNames[i].StartsWith(regionName) || regionName.Equals(BioBoostCompoundSolutionView.AllRegionsName)) {
134              sum += kvp.Value[i];
135            }
136          }
137          if (name.EndsWith(BioBoostProblem.CostName)) {
138            name = name.Replace(BioBoostProblem.CostName, string.Empty);
139          } else {
140            name = name.Replace(BioBoostProblem.CostsName, string.Empty);
141          }
142          costsDataGridView.Rows.Add(name, sum);
143          total += sum;
144        }
145      } */
146      costsTextBox1.Text = totalCost.ToString("N0");
147    }
148
149    private void AddRegion(BioBoostCompoundSolution solution, string regionName) {
150      var supplierCollector = new SupplierCollector(solution, regionName);
151      foreach (var product in supplierCollector.Products) {
152        AddSummableValue(LayerDescriptor.ConverterCapacities.NameWithPrefix(product.Name), product.ConverterCapacity);
153        AddSummableValue(LayerDescriptor.StorageCapacities.NameWithPrefix(product.Name), product.StorageCapacity);
154        AddCost(LayerDescriptor.ConversionCosts.NameWithPrefix(product.Name), product.ConversionCost);
155        AddCost(LayerDescriptor.StorageCost.NameWithPrefix(product.Name), product.StorageCost);
156        AddCost(LayerDescriptor.ConversionPlantConstructionCost.NameWithPrefix(product.Name), product.ConversionPlantConstructionCost);
157        AddCost(LayerDescriptor.ConversionPlantOperationCost.NameWithPrefix(product.Name), product.ConversionPlantOperationCost);
158        AddString(LayerDescriptor.TransportTargets.NameWithPrefix(product.Name), product.TargetName);
159        AddSupplierValues(LayerDescriptor.AmountsTransportedFromSource.NameWithPrefix(product.Name), product.TransportedAmounts, product.SuppliersIndices);
160        AddSupplierValues(LayerDescriptor.TransportDistance.NameWithPrefix(product.Name), product.TransportDistances, product.SuppliersIndices);
161        AddSupplierValues(LayerDescriptor.Tkm.NameWithPrefix(product.Name), product.Tkm, product.SuppliersIndices);
162        AddSupplierValues(LayerDescriptor.RelativeCostAtSource.NameWithPrefix(product.Name), product.RelativeProductionCosts, product.SuppliersIndices); // TODO: really?
163        AddSupplierCosts(LayerDescriptor.TotalCostsAtSource.NameWithPrefix(product.Name), product.AcquisitionCosts, product.SuppliersIndices);
164        AddSupplierCosts(LayerDescriptor.DischargeCosts.NameWithPrefix(product.Name), product.DischargeCosts, product.SuppliersIndices);
165        AddSupplierCosts(LayerDescriptor.TransportCosts.NameWithPrefix(product.Name), product.TransportCosts, product.SuppliersIndices);
166        AddSupplierCosts(LayerDescriptor.HandlingCosts.NameWithPrefix(product.Name), product.HandlingCosts, product.SuppliersIndices);
167      }
168    }
169
170    private void Clear() {
171      costsDataGridView.Rows.Clear();
172      costsTextBox1.Text = string.Empty;
173    }
174  }
175}
Note: See TracBrowser for help on using the repository browser.