#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Globalization;
using HeuristicLab.BioBoost.Representation;
using HeuristicLab.Core.Views;
using HeuristicLab.Data;
using HeuristicLab.MainForm;
using System.Drawing;
using System.Windows.Forms;
namespace HeuristicLab.BioBoost.Views {
[Content(typeof(RegionDetailData), IsDefaultView = false)]
public partial class BioBoostSolutionCostsView : ItemView {
public new RegionDetailData Content {
get { return (RegionDetailData)base.Content; }
set { base.Content = value; }
}
public BioBoostSolutionCostsView() {
InitializeComponent();
}
protected override void OnContentChanged() {
base.OnContentChanged();
if (Content == null) {
Clear();
} else {
Clear();
Populate(Content.Solution, Content.RegionName);
}
}
private Dictionary costs;
private Dictionary values;
private Dictionary> strings;
private void AddCost(string name, double value) {
// ReSharper disable once CompareOfFloatsByEqualityOperator
if (value == 0) return;
double oldValue = 0;
costs.TryGetValue(name, out oldValue);
costs[name] = oldValue + value;
}
private void AddSummableValue(string name, double value) {
// ReSharper disable once CompareOfFloatsByEqualityOperator
if (value == 0) return;
double oldValue = 0;
values.TryGetValue(name, out oldValue);
values[name] = oldValue + value;
}
private void AddString(string name, string value) {
if (string.IsNullOrEmpty(value) || value == "0") return;
List oldValues;
if (!strings.TryGetValue(name, out oldValues)) {
oldValues = new List();
strings[name] = oldValues;
}
if (oldValues.Count < 2) {
oldValues.Add(value);
}
}
private void AddSupplierCosts(string name, DoubleArray values, List supplierIndices) {
if (values == null) return;
double sum = 0;
foreach (var supplierIdx in supplierIndices) {
sum += values[supplierIdx];
}
AddCost(name + " Sum", sum);
if (supplierIndices.Count > 1)
AddString(name + " Avg", (sum/supplierIndices.Count).ToString(CultureInfo.InvariantCulture));
}
private void AddSupplierValues(string name, DoubleArray values, List supplierIndices) {
if (values == null) return;
double sum = 0;
foreach (var supplierIdx in supplierIndices) {
sum += values[supplierIdx];
}
AddSummableValue(name + " Sum", sum);
if (supplierIndices.Count > 1)
AddString(name + " Avg", (sum/supplierIndices.Count).ToString(CultureInfo.InvariantCulture));
}
private void Populate(BioBoostCompoundSolution solution, string regionName) {
costsColumn2.DefaultCellStyle.Font = new Font("Lucida Console", 7);
costsColumn2.DefaultCellStyle.Format = "N0";
costsColumn2.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
costs = new Dictionary();
values = new Dictionary();
strings = new Dictionary>();
foreach (var locationName in solution.LocationNames) {
if (regionName == BioBoostCompoundSolutionView.AllRegionsName || locationName.StartsWith(regionName))
AddRegion(solution, locationName);
}
var totalCost = 0d;
foreach (var cost in costs) {
costsDataGridView.Rows.Add(cost.Key, cost.Value);
totalCost += cost.Value;
}
foreach (var value in values) {
costsDataGridView.Rows.Add(value.Key, value.Value);
}
foreach (var value in strings) {
if (value.Value.Count == 1) {
double doubleValue;
if (Double.TryParse(value.Value[0], NumberStyles.Float, CultureInfo.InvariantCulture, out doubleValue)) {
costsDataGridView.Rows.Add(value.Key, doubleValue);
} else {
costsDataGridView.Rows.Add(value.Key, value.Value[0]);
}
} else {
//costsDataGridView.Rows.Add(value.Key, string.Join(",", value.Value));
}
}
/* var total = 0d;
foreach (var kvp in solution.DoubleValues) {
var name = kvp.Key;
if (name.EndsWith(BioBoostProblem.CostsName) || name.EndsWith(BioBoostProblem.CostName)) {
var sum = 0d;
for (int i = 0; i < kvp.Value.Length; i++) {
if (solution.LocationNames[i].StartsWith(regionName) || regionName.Equals(BioBoostCompoundSolutionView.AllRegionsName)) {
sum += kvp.Value[i];
}
}
if (name.EndsWith(BioBoostProblem.CostName)) {
name = name.Replace(BioBoostProblem.CostName, string.Empty);
} else {
name = name.Replace(BioBoostProblem.CostsName, string.Empty);
}
costsDataGridView.Rows.Add(name, sum);
total += sum;
}
} */
costsTextBox1.Text = totalCost.ToString("N0");
}
private void AddRegion(BioBoostCompoundSolution solution, string regionName) {
var supplierCollector = new SupplierCollector(solution, regionName);
foreach (var product in supplierCollector.Products) {
AddSummableValue(LayerDescriptor.ConverterCapacities.NameWithPrefix(product.Name), product.ConverterCapacity);
AddSummableValue(LayerDescriptor.StorageCapacities.NameWithPrefix(product.Name), product.StorageCapacity);
AddCost(LayerDescriptor.ConversionCosts.NameWithPrefix(product.Name), product.ConversionCost);
AddCost(LayerDescriptor.StorageCost.NameWithPrefix(product.Name), product.StorageCost);
AddCost(LayerDescriptor.ConversionPlantConstructionCost.NameWithPrefix(product.Name), product.ConversionPlantConstructionCost);
AddCost(LayerDescriptor.ConversionPlantOperationCost.NameWithPrefix(product.Name), product.ConversionPlantOperationCost);
AddString(LayerDescriptor.TransportTargets.NameWithPrefix(product.Name), product.TargetName);
AddSupplierValues(LayerDescriptor.AmountsTransportedFromSource.NameWithPrefix(product.Name), product.TransportedAmounts, product.SuppliersIndices);
AddSupplierValues(LayerDescriptor.TransportDistance.NameWithPrefix(product.Name), product.TransportDistances, product.SuppliersIndices);
AddSupplierValues(LayerDescriptor.Tkm.NameWithPrefix(product.Name), product.Tkm, product.SuppliersIndices);
AddSupplierValues(LayerDescriptor.RelativeCostAtSource.NameWithPrefix(product.Name), product.RelativeProductionCosts, product.SuppliersIndices); // TODO: really?
AddSupplierCosts(LayerDescriptor.TotalCostsAtSource.NameWithPrefix(product.Name), product.AcquisitionCosts, product.SuppliersIndices);
AddSupplierCosts(LayerDescriptor.DischargeCosts.NameWithPrefix(product.Name), product.DischargeCosts, product.SuppliersIndices);
AddSupplierCosts(LayerDescriptor.TransportCosts.NameWithPrefix(product.Name), product.TransportCosts, product.SuppliersIndices);
AddSupplierCosts(LayerDescriptor.HandlingCosts.NameWithPrefix(product.Name), product.HandlingCosts, product.SuppliersIndices);
}
}
private void Clear() {
costsDataGridView.Rows.Clear();
costsTextBox1.Text = string.Empty;
}
}
}