[7980] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9835] | 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[7980] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
[9240] | 25 | using System.Text;
|
---|
[7980] | 26 | using System.Windows.Forms;
|
---|
| 27 | using HeuristicLab.Analysis;
|
---|
| 28 | using HeuristicLab.Collections;
|
---|
| 29 | using HeuristicLab.Core.Views;
|
---|
| 30 | using HeuristicLab.MainForm;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Optimization.Views {
|
---|
| 33 | [View("RunCollection DataTableView")]
|
---|
| 34 | [Content(typeof(RunCollection), false)]
|
---|
| 35 | public partial class RunCollectionDataTableView : ItemView {
|
---|
| 36 | private const string AllDataRows = "All DataRows";
|
---|
| 37 |
|
---|
| 38 | public new RunCollection Content {
|
---|
| 39 | get { return (RunCollection)base.Content; }
|
---|
| 40 | set { base.Content = value; }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[8108] | 43 | private int rowNumber = 0;
|
---|
[7980] | 44 | private bool suppressUpdates;
|
---|
| 45 | private readonly Dictionary<IRun, IEnumerable<DataRow>> runMapping;
|
---|
| 46 | private readonly DataTable combinedDataTable;
|
---|
| 47 | public DataTable CombinedDataTable {
|
---|
| 48 | get { return combinedDataTable; }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[8108] | 51 | public RunCollectionDataTableView() {
|
---|
| 52 | InitializeComponent();
|
---|
| 53 | runMapping = new Dictionary<IRun, IEnumerable<DataRow>>();
|
---|
| 54 | combinedDataTable = new DataTable("Combined DataTable", "A data table containing data rows from multiple runs.");
|
---|
| 55 | viewHost.Content = combinedDataTable;
|
---|
| 56 | suppressUpdates = false;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[7980] | 59 | #region Content events
|
---|
| 60 | protected override void RegisterContentEvents() {
|
---|
| 61 | base.RegisterContentEvents();
|
---|
| 62 | Content.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
|
---|
| 63 | Content.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
|
---|
| 64 | Content.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
|
---|
| 65 | Content.UpdateOfRunsInProgressChanged += new EventHandler(Content_UpdateOfRunsInProgressChanged);
|
---|
[8962] | 66 | Content.OptimizerNameChanged += new EventHandler(Content_AlgorithmNameChanged);
|
---|
[7980] | 67 | }
|
---|
| 68 | protected override void DeregisterContentEvents() {
|
---|
| 69 | Content.ItemsAdded -= new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
|
---|
| 70 | Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
|
---|
| 71 | Content.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
|
---|
| 72 | Content.UpdateOfRunsInProgressChanged -= new EventHandler(Content_UpdateOfRunsInProgressChanged);
|
---|
[8962] | 73 | Content.OptimizerNameChanged -= new EventHandler(Content_AlgorithmNameChanged);
|
---|
[7980] | 74 | base.DeregisterContentEvents();
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | private void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
| 78 | if (InvokeRequired) {
|
---|
| 79 | Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded), sender, e);
|
---|
| 80 | return;
|
---|
| 81 | }
|
---|
| 82 | AddRuns(e.Items);
|
---|
| 83 | }
|
---|
| 84 | private void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
| 85 | if (InvokeRequired) {
|
---|
| 86 | Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved), sender, e);
|
---|
| 87 | return;
|
---|
| 88 | }
|
---|
| 89 | RemoveRuns(e.Items);
|
---|
| 90 | }
|
---|
| 91 | private void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
| 92 | if (InvokeRequired) {
|
---|
| 93 | Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset), sender, e);
|
---|
| 94 | return;
|
---|
| 95 | }
|
---|
| 96 | RemoveRuns(e.OldItems);
|
---|
| 97 | AddRuns(e.Items);
|
---|
| 98 | }
|
---|
[8738] | 99 | private void Content_AlgorithmNameChanged(object sender, EventArgs e) {
|
---|
| 100 | if (InvokeRequired)
|
---|
| 101 | Invoke(new EventHandler(Content_AlgorithmNameChanged), sender, e);
|
---|
| 102 | else UpdateCaption();
|
---|
| 103 | }
|
---|
[7980] | 104 | private void Content_UpdateOfRunsInProgressChanged(object sender, EventArgs e) {
|
---|
| 105 | if (InvokeRequired) {
|
---|
| 106 | Invoke(new EventHandler(Content_UpdateOfRunsInProgressChanged), sender, e);
|
---|
| 107 | return;
|
---|
| 108 | }
|
---|
| 109 | suppressUpdates = Content.UpdateOfRunsInProgress;
|
---|
| 110 | if (!suppressUpdates) UpdateRuns(Content);
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | private void RegisterRunEvents(IRun run) {
|
---|
| 114 | run.Changed += new System.EventHandler(run_Changed);
|
---|
| 115 | }
|
---|
| 116 | private void DeregisterRunEvents(IRun run) {
|
---|
| 117 | run.Changed -= new System.EventHandler(run_Changed);
|
---|
| 118 | }
|
---|
| 119 | private void run_Changed(object sender, EventArgs e) {
|
---|
| 120 | if (suppressUpdates) return;
|
---|
[8108] | 121 | var run = (IRun)sender;
|
---|
| 122 | UpdateRuns(new IRun[] { run });
|
---|
[7980] | 123 | }
|
---|
| 124 | #endregion
|
---|
| 125 |
|
---|
| 126 | protected override void OnContentChanged() {
|
---|
| 127 | base.OnContentChanged();
|
---|
| 128 | dataTableComboBox.Items.Clear();
|
---|
| 129 | dataRowComboBox.Items.Clear();
|
---|
| 130 | combinedDataTable.Rows.Clear();
|
---|
| 131 | runMapping.Clear();
|
---|
| 132 |
|
---|
[8738] | 133 | UpdateCaption();
|
---|
[7980] | 134 | if (Content != null) {
|
---|
| 135 | UpdateDataTableComboBox();
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | private void RebuildCombinedDataTable() {
|
---|
| 140 | RemoveRuns(Content);
|
---|
[8108] | 141 | rowNumber = 0;
|
---|
[7980] | 142 | AddRuns(Content);
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | private void AddRuns(IEnumerable<IRun> runs) {
|
---|
| 146 | foreach (var run in runs) {
|
---|
| 147 | runMapping[run] = ExtractDataRowsFromRun(run).ToList();
|
---|
| 148 | RegisterRunEvents(run);
|
---|
| 149 | }
|
---|
| 150 | var dataRows = runs.Where(r => r.Visible && runMapping.ContainsKey(r)).SelectMany(r => runMapping[r]);
|
---|
| 151 | combinedDataTable.Rows.AddRange(dataRows);
|
---|
| 152 | }
|
---|
[8108] | 153 |
|
---|
[7980] | 154 | private void RemoveRuns(IEnumerable<IRun> runs) {
|
---|
| 155 | var dataRows = runs.Where(r => runMapping.ContainsKey(r)).SelectMany(r => runMapping[r]).ToList();
|
---|
| 156 | foreach (var run in runs) {
|
---|
| 157 | if (!runMapping.ContainsKey(run)) continue;
|
---|
| 158 | runMapping.Remove(run);
|
---|
| 159 | DeregisterRunEvents(run);
|
---|
| 160 | }
|
---|
| 161 | combinedDataTable.Rows.RemoveRange(dataRows);
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | private void UpdateRuns(IEnumerable<IRun> runs) {
|
---|
| 165 | if (suppressUpdates) return;
|
---|
| 166 | foreach (var run in runs) {
|
---|
| 167 | //update color
|
---|
| 168 | foreach (var dataRow in runMapping[run]) {
|
---|
| 169 | dataRow.VisualProperties.Color = run.Color;
|
---|
| 170 | }
|
---|
| 171 | //update visibility - remove and add all rows to keep the same order as before
|
---|
| 172 | combinedDataTable.Rows.Clear();
|
---|
| 173 | combinedDataTable.Rows.AddRange(runMapping.Where(mapping => mapping.Key.Visible).SelectMany(mapping => mapping.Value));
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | private IEnumerable<DataRow> ExtractDataRowsFromRun(IRun run) {
|
---|
[8108] | 178 | var resultName = (string)dataTableComboBox.SelectedItem;
|
---|
| 179 | var rowName = (string)dataRowComboBox.SelectedItem;
|
---|
[7980] | 180 | if (!run.Results.ContainsKey(resultName)) yield break;
|
---|
| 181 |
|
---|
[8108] | 182 | var dataTable = (DataTable)run.Results[resultName];
|
---|
[7980] | 183 | foreach (var dataRow in dataTable.Rows) {
|
---|
| 184 | if (dataRow.Name != rowName && rowName != AllDataRows) continue;
|
---|
| 185 | rowNumber++;
|
---|
| 186 | var clonedRow = (DataRow)dataRow.Clone();
|
---|
| 187 | //row names must be unique -> add incremented number to the row name
|
---|
| 188 | clonedRow.Name = run.Name + "." + dataRow.Name + rowNumber;
|
---|
| 189 | clonedRow.VisualProperties.DisplayName = run.Name + "." + dataRow.Name;
|
---|
| 190 | clonedRow.VisualProperties.Color = run.Color;
|
---|
| 191 | yield return clonedRow;
|
---|
| 192 | }
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | private void UpdateDataTableComboBox() {
|
---|
[7986] | 196 | dataTableComboBox.Items.Clear();
|
---|
[7980] | 197 | var dataTables = (from run in Content
|
---|
| 198 | from result in run.Results
|
---|
| 199 | where result.Value is DataTable
|
---|
| 200 | select result.Key).Distinct().ToArray();
|
---|
| 201 |
|
---|
| 202 | dataTableComboBox.Items.AddRange(dataTables);
|
---|
| 203 | if (dataTableComboBox.Items.Count > 0) dataTableComboBox.SelectedItem = dataTableComboBox.Items[0];
|
---|
| 204 | }
|
---|
| 205 |
|
---|
[8738] | 206 | private void UpdateCaption() {
|
---|
[8962] | 207 | Caption = Content != null ? Content.OptimizerName + " Data Table" : ViewAttribute.GetViewName(GetType());
|
---|
[8738] | 208 | }
|
---|
| 209 |
|
---|
[7980] | 210 | private void UpdateDataRowComboBox() {
|
---|
[7986] | 211 | dataRowComboBox.Items.Clear();
|
---|
[8108] | 212 | var resultName = (string)dataTableComboBox.SelectedItem;
|
---|
[7980] | 213 | var dataTables = from run in Content
|
---|
| 214 | where run.Results.ContainsKey(resultName)
|
---|
| 215 | select run.Results[resultName] as DataTable;
|
---|
| 216 | var rowNames = (from dataTable in dataTables
|
---|
| 217 | from row in dataTable.Rows
|
---|
| 218 | select row.Name).Distinct().ToArray();
|
---|
| 219 |
|
---|
| 220 | dataRowComboBox.Items.AddRange(rowNames);
|
---|
| 221 | dataRowComboBox.Items.Add(AllDataRows);
|
---|
| 222 | if (dataRowComboBox.Items.Count > 0) dataRowComboBox.SelectedItem = dataRowComboBox.Items[0];
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | private void dataTableComboBox_SelectedIndexChanged(object sender, System.EventArgs e) {
|
---|
| 226 | UpdateDataRowComboBox();
|
---|
| 227 | }
|
---|
| 228 | private void dataRowComboBox_SelectedIndexChanged(object sender, System.EventArgs e) {
|
---|
[9240] | 229 | CombinedDataTable.Rows.Clear(); // to also clear the mean values row
|
---|
[8925] | 230 | RebuildCombinedDataTable();
|
---|
[7980] | 231 | }
|
---|
[9240] | 232 |
|
---|
| 233 | private void meanButton_Click(object sender, EventArgs e) {
|
---|
| 234 | var caption = (string)dataRowComboBox.SelectedItem + " Mean Values";
|
---|
| 235 |
|
---|
| 236 | var rows = combinedDataTable.Rows.ToList();
|
---|
| 237 |
|
---|
| 238 | if (combinedDataTable.Rows.ContainsKey(caption))
|
---|
| 239 | combinedDataTable.Rows.Remove(caption);
|
---|
| 240 |
|
---|
| 241 | // add an additional data row with the mean values
|
---|
| 242 | int maxCount = combinedDataTable.Rows.Max(row => row.Values.Count);
|
---|
| 243 | int rowCount = combinedDataTable.Rows.Count;
|
---|
| 244 | var meanValues = new List<double>();
|
---|
| 245 |
|
---|
| 246 | for (int i = 0; i != maxCount; ++i) {
|
---|
| 247 | double mean = 0;
|
---|
| 248 | for (int j = 0; j != rowCount; ++j) {
|
---|
| 249 | mean += i < rows[j].Values.Count ? rows[j].Values[i] : 0;
|
---|
| 250 | }
|
---|
| 251 | meanValues.Add(mean / rowCount);
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | combinedDataTable.Rows.Add(new DataRow(caption));
|
---|
| 255 | combinedDataTable.Rows[caption].Values.AddRange(meanValues);
|
---|
| 256 | var sb = new StringBuilder();
|
---|
| 257 | foreach (var val in meanValues)
|
---|
| 258 | sb.AppendLine(val.ToString());
|
---|
| 259 | Clipboard.SetText(sb.ToString());
|
---|
| 260 | }
|
---|
[7980] | 261 | }
|
---|
| 262 | }
|
---|