Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost.Views/3.3/BioBoostSummaryView.cs @ 14921

Last change on this file since 14921 was 13074, checked in by gkronber, 9 years ago

#2499: added license header and removed unused usings

File size: 6.8 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.ComponentModel;
24using System.Globalization;
25using System.Linq;
26using System.Text;
27using System.Windows.Forms;
28using HeuristicLab.Core.Views;
29using HeuristicLab.Data;
30using HeuristicLab.MainForm;
31
32namespace HeuristicLab.BioBoost.Views {
33
34  [Content(typeof(RegionDetailData), IsDefaultView = false)]
35  public partial class BioBoostSummaryView : ItemView {
36
37    public new RegionDetailData Content {
38      get { return (RegionDetailData)base.Content; }
39      set { base.Content = value; }
40    }
41
42    public BioBoostSummaryView() {
43      InitializeComponent();
44    }
45
46    protected override void OnContentChanged() {
47      base.OnContentChanged();
48      if (Content == null) {
49        Clear();
50      } else {
51        Clear();
52        Populate();
53      }
54    }
55
56    protected void Clear() {
57      treeView.Nodes.Clear();
58      gridView.Rows.Clear();
59      regionLabel.Text = "no region selected";
60      valueLabel.Text = "no value selected";
61    }
62
63    protected void Populate() {
64      foreach (var targets in Content.Solution.TransportTargets) {
65        var node = treeView.Nodes.Add(targets.Key);
66        node.Tag = node.Text;
67        for (int i = 0; i < targets.Value.Length; i++) {
68          if (targets.Value[i] != null && targets.Value[i].Count > 0) {
69            var innerNode = node.Nodes.Add(Content.Solution.LocationNames[i]);
70            innerNode.Tag = innerNode.Text;
71            foreach (var suppliers in targets.Value[i]) {
72              var supplierNode = innerNode.Nodes.Add(Content.Solution.LocationNames[suppliers.Value]);
73              supplierNode.Tag = supplierNode.Text;
74            }
75          }
76        }
77      }
78      valueLabel.Text = "no value selected";
79      regionLabel.Text = Content.RegionName ?? "no region selected";
80    }
81
82    private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
83      gridView.Rows.Clear();
84      if (treeView.SelectedNode != null) {
85        var i = GetLocationIndex((string)treeView.SelectedNode.Tag);
86        if (i >= 0) {
87          PopulateGridView(i);
88        }
89      }
90    }
91
92    private int GetLocationIndex(string regionName) {
93      return Content.Solution.LocationNames.IndexOfFirst(x => x == regionName);
94    }
95
96    private double? GetValue(string regionName, string valueName) {
97      if (valueName == null) return null;
98      var i = GetLocationIndex(regionName);
99      if (i < 0) return 0;
100      DoubleArray doubles;
101      IntArray ints;
102      if (Content.Solution.DoubleValues.TryGetValue(valueName, out doubles)) {
103        return doubles[i];
104      } else if (Content.Solution.IntValues.TryGetValue(valueName, out ints)) {
105        return ints[i];
106      }
107      return null;
108    }
109
110    private void PopulateGridView(int index) {
111      regionLabel.Text = Content.Solution.LocationNames[index];
112      foreach (var kvp in Content.Solution.DoubleValues.Where(kvp => kvp.Value[index] != 0)) {
113        gridView.Rows.Add(kvp.Key, kvp.Value[index]);
114      }
115      foreach (var kvp in Content.Solution.IntValues.Where(kvp => kvp.Value[index] != 0)) {
116        gridView.Rows.Add(kvp.Key, kvp.Value[index]);
117      }
118      foreach (var kvp in Content.Solution.StringValues.Where(kvp => !string.IsNullOrWhiteSpace(kvp.Value[index]))) {
119        gridView.Rows.Add(kvp.Key, kvp.Value[index]);
120      }
121      foreach (var kvp in from kvp
122                            in Content.Solution.TransportTargets
123                          where kvp.Value[index] != null
124                          let s = string.Join(", ", kvp.Value[index])
125                          where !string.IsNullOrWhiteSpace(s) select kvp) {
126        gridView.Rows.Add(kvp.Key, string.Join(", ", kvp.Value[index]));
127      }
128      gridView.Sort(NameColumn, ListSortDirection.Ascending);
129    }
130
131    private void gridView_CurrentCellChanged(object sender, EventArgs e) {
132      string valueName = null;
133      if (gridView.SelectedRows.Count > 0) {
134        valueName = (string) gridView.SelectedRows[0].Cells[0].Value;
135      }
136      foreach (TreeNode rootNodes in treeView.Nodes) {
137        UpdateAggregatedValues(rootNodes, valueName);
138      }
139    }
140
141    private static string F(double value, int significantDigits = 6) {
142      int msd = (int)Math.Ceiling(Math.Log10(value));
143      int decimals = 6 - msd;
144      if (decimals <= 0) {
145        return value.ToString("### ### ### ### ### ### ### ### ### ### ### ### ###", CultureInfo.InvariantCulture);
146      } else if (decimals > significantDigits) {
147        return value.ToString("g2");
148      } else {
149        StringBuilder sb = new StringBuilder();
150        for (int i = 0; i < msd; i+=3) {
151          if (sb.Length > 0) sb.Append(" ");
152          sb.Append("###");
153        }
154        sb.Append(".");
155        for (int i = 0; i < decimals; i += 3) {
156          if (sb[sb.Length-1] != '.') sb.Append(" ");
157          sb.Append("###");
158        }
159        return value.ToString(sb.ToString(), CultureInfo.InvariantCulture);
160      }
161    }
162
163    private double UpdateAggregatedValues(TreeNode node, string valueName) {
164      valueLabel.Text = valueName;
165      if (node.Nodes.Count == 0) {
166        var value = GetValue((string) node.Tag, valueName);
167        if (value.HasValue) {
168          node.Text = string.Format("{0} ({1})", node.Tag, F(value.Value));
169          return value.Value;
170        }
171        return 0;
172      }
173      double sum = 0;
174      int count = 0;
175      foreach (TreeNode subnode in node.Nodes) {
176        var subvalue = UpdateAggregatedValues(subnode, valueName);
177        sum += subvalue;
178        count++;
179      }
180      if (sum != 0) {
181        node.Text = string.Format("{0}   ({1} total,   {2} avg,    {3} count)", node.Tag, F(sum), F(sum/count), F(count));
182      } else {
183        var value = GetValue((string) node.Tag, valueName);
184        if (value.HasValue) {
185          node.Text = string.Format("{0} ({1})", node.Tag, F(value.Value));
186          return value.Value;
187        }
188        node.Text = (string) node.Tag;
189      }
190      return sum;
191    }
192
193  }
194}
Note: See TracBrowser for help on using the repository browser.