#region License Information /* HeuristicLab * Copyright (C) 2002-2012 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.Drawing; using System.Linq; using System.Windows.Forms; using HeuristicLab.Analysis.AlgorithmBehavior.Analyzers; using HeuristicLab.Common; using HeuristicLab.Core.Views; using HeuristicLab.Data; using HeuristicLab.MainForm; using HeuristicLab.Optimization; namespace HeuristicLab.Analysis.AlgorithmBehavior.Views { [View("RunCollection Statistical Tabular View")] [Content(typeof(RunCollection), false)] public sealed partial class RunCollectionStatisticalTabularView : ItemView { public RunCollectionStatisticalTabularView() { InitializeComponent(); stringConvertibleMatrixView.DataGridView.RowHeaderMouseDoubleClick += new DataGridViewCellMouseEventHandler(DataGridView_RowHeaderMouseDoubleClick); } void DataGridView_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) { if (e.RowIndex >= 0) { IRun run = Content.ElementAt(e.RowIndex); IContentView view = MainFormManager.MainForm.ShowContent(run); if (view != null) { view.ReadOnly = this.ReadOnly; view.Locked = this.Locked; } } } /// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { stringConvertibleMatrixView.DataGridView.RowHeaderMouseDoubleClick -= new DataGridViewCellMouseEventHandler(DataGridView_RowHeaderMouseDoubleClick); components.Dispose(); } base.Dispose(disposing); } public new RunCollection Content { get { return (RunCollection)base.Content; } set { base.Content = value; } } public override bool ReadOnly { get { return true; } set { /*not needed because results are always readonly */} } protected override void OnContentChanged() { base.OnContentChanged(); dataTableComboBox.Items.Clear(); dataRowComboBox.Items.Clear(); if (Content != null) { UpdateDataTableComboBox(); } } #region events protected override void RegisterContentEvents() { base.RegisterContentEvents(); Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler(Content_ItemsAdded); Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler(Content_ItemsRemoved); Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler(Content_CollectionReset); RegisterRunEvents(Content); } private void RegisterRunEvents(IEnumerable runs) { foreach (IRun run in runs) run.Changed += new EventHandler(run_Changed); } protected override void DeregisterContentEvents() { base.DeregisterContentEvents(); Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler(Content_ItemsAdded); Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler(Content_ItemsRemoved); Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler(Content_CollectionReset); DeregisterRunEvents(Content); } private void DeregisterRunEvents(IEnumerable runs) { foreach (IRun run in runs) run.Changed -= new EventHandler(run_Changed); } private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs e) { DeregisterRunEvents(e.OldItems); RegisterRunEvents(e.Items); RebuildCombinedDataTable(); } private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs e) { DeregisterRunEvents(e.Items); RebuildCombinedDataTable(); } private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs e) { RegisterRunEvents(e.Items); RebuildCombinedDataTable(); } private void run_Changed(object sender, EventArgs e) { if (InvokeRequired) this.Invoke(new EventHandler(run_Changed), sender, e); else { IRun run = (IRun)sender; UpdateRun(run); } } #endregion private void UpdateRun(IRun run) { //TODO: hacky di hack... this is baaaadddd RebuildCombinedDataTable(); } private void dataTableComboBox_SelectedIndexChanged(object sender, EventArgs e) { UpdateDataRowComboBox(); } private void dataRowComboBox_SelectedIndexChanged(object sender, EventArgs e) { RebuildCombinedDataTable(); } private void UpdateDataRowComboBox() { dataRowComboBox.Items.Clear(); var resultName = (string)dataTableComboBox.SelectedItem; var dataTables = from run in Content where run.Results.ContainsKey(resultName) select run.Results[resultName] as DataTable; var rowNames = (from dataTable in dataTables from row in dataTable.Rows select row.Name).Distinct().ToArray(); dataRowComboBox.Items.AddRange(rowNames); if (dataRowComboBox.Items.Count > 0) dataRowComboBox.SelectedItem = dataRowComboBox.Items[0]; } private void UpdateDataTableComboBox() { dataTableComboBox.Items.Clear(); var dataTables = (from run in Content from result in run.Results where result.Value is DataTable select result.Key).Distinct().ToArray(); dataTableComboBox.Items.AddRange(dataTables); if (dataTableComboBox.Items.Count > 0) dataTableComboBox.SelectedItem = dataTableComboBox.Items[0]; } private void RebuildCombinedDataTable() { string resultName = (string)dataTableComboBox.SelectedItem; string rowName = (string)dataRowComboBox.SelectedItem; string[] columnNames = new string[] { "Count", "Minimum", "Maximum", "Average", "Median", "Standard Deviation", "Variance", "25th Percentile", "75th Percentile", "Gradient", "Relative Error", "Avg. of Upper 25 %", " Avg. of Lower 25 %", "Avg. of First 25 %", "Avg. of Last 25 %" }; var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible); StringMatrix dt = new StringMatrix(runs.Count(), columnNames.Count()); dt.RowNames = runs.Select(x => x.Name); dt.ColumnNames = columnNames; int i = 0; foreach (Run run in runs) { DataTable resTable = (DataTable)run.Results[resultName]; DataRow row = resTable.Rows[rowName]; var values = row.Values.AsEnumerable(); double cnt = values.Count(); double min = values.Min(); double max = values.Max(); double avg = values.Average(); double median = values.Median(); double stdDev = values.StandardDeviation(); double variance = values.Variance(); double percentile25 = values.Percentile(0.25); double percentile75 = values.Percentile(0.75); double k, d, r; LinearLeastSquaresFitting.Calculate(values.ToArray(), out k, out d); r = LinearLeastSquaresFitting.CalculateError(values.ToArray(), k, d); double lowerAvg = values.OrderBy(x => x).Take((int)(values.Count() * 0.25)).Average(); double upperAvg = values.OrderByDescending(x => x).Take((int)(values.Count() * 0.25)).Average(); double firstAvg = values.Take((int)(values.Count() * 0.25)).Average(); double lastAvg = values.Skip((int)(values.Count() * 0.75)).Average(); dt[i, 0] = cnt.ToString(); dt[i, 1] = min.ToString(); dt[i, 2] = max.ToString(); dt[i, 3] = avg.ToString(); dt[i, 4] = median.ToString(); dt[i, 5] = stdDev.ToString(); dt[i, 6] = variance.ToString(); dt[i, 7] = percentile25.ToString(); dt[i, 8] = percentile75.ToString(); dt[i, 9] = k.ToString(); dt[i, 10] = r.ToString(); dt[i, 11] = upperAvg.ToString(); dt[i, 12] = lowerAvg.ToString(); dt[i, 13] = firstAvg.ToString(); dt[i, 14] = lastAvg.ToString(); i++; } stringConvertibleMatrixView.Content = dt; } private void addLineToChart_Click(object sender, EventArgs e) { string resultName = (string)dataTableComboBox.SelectedItem; string rowName = (string)dataRowComboBox.SelectedItem; var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible); foreach (Run run in runs) { DataTable resTable = (DataTable)run.Results[resultName]; DataRow row = resTable.Rows[rowName]; var values = row.Values.ToArray(); double k, d; LinearLeastSquaresFitting.Calculate(values, out k, out d); DataRow newRow = new DataRow(row.Name + " Fitted Line"); for (int i = 0; i < values.Count(); i++) { newRow.Values.Add(k * i + d); } if (!resTable.Rows.ContainsKey(newRow.Name)) resTable.Rows.Add(newRow); } } private void addValuesButton_Click(object sender, EventArgs e) { string resultName = (string)dataTableComboBox.SelectedItem; string rowName = (string)dataRowComboBox.SelectedItem; var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible); StringMatrix sm = (StringMatrix)stringConvertibleMatrixView.Content; for (int i = 0; i < runs.Count(); i++) { IRun run = runs.ElementAt(i); for (int j = 0; j < sm.ColumnNames.Count(); j++) { string newResultName = resultName + " " + rowName + " " + sm.ColumnNames.ElementAt(j); if (!run.Results.ContainsKey(newResultName)) run.Results.Add(new KeyValuePair(newResultName, new DoubleValue(double.Parse(sm[i, j])))); } } //update column names of run collection Content.Modify(); } private void colorButton_Click(object sender, EventArgs e) { string resultName = (string)dataTableComboBox.SelectedItem; string rowName = (string)dataRowComboBox.SelectedItem; var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible); Dictionary values = new Dictionary(); if (stringConvertibleMatrixView.DataGridView.CurrentCell != null) { int curIndex = stringConvertibleMatrixView.DataGridView.CurrentCell.ColumnIndex; for (int i = 0; i < stringConvertibleMatrixView.Content.Rows; i++) { values[i] = double.Parse(stringConvertibleMatrixView.Content.GetValue(i, curIndex)); } var orderedValues = values.OrderBy(x => x.Value); for (int i = 0; i < orderedValues.Count(); i++) { var row = stringConvertibleMatrixView.DataGridView.Rows[orderedValues.ElementAt(i).Key]; int r = (int)Math.Round((double)i / (double)orderedValues.Count() * 255.0); int g = (int)Math.Round(((double)orderedValues.Count() - (double)i) / (double)orderedValues.Count() * 255.0); Color color = Color.FromArgb(r, g, 0); row.DefaultCellStyle.ForeColor = color; } } } } }