Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost.Views/3.3/BioBoostSolutionCostsView.cs @ 14604

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