Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers/3.3/RunCollectionStatisticalTabularView.cs @ 8522

Last change on this file since 8522 was 8522, checked in by ascheibe, 12 years ago

#1886 added relative error and possibility to add fitted line to charts

File size: 8.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Analysis.AlgorithmBehavior.Analyzers;
27using HeuristicLab.Common;
28using HeuristicLab.Core.Views;
29using HeuristicLab.Data;
30using HeuristicLab.MainForm;
31using HeuristicLab.Optimization;
32
33namespace HeuristicLab.Analysis.AlgorithmBehavior {
34  [View("RunCollection Statistical Tabular View")]
35  [Content(typeof(RunCollection), false)]
36  public sealed partial class RunCollectionStatisticalTabularView : ItemView {
37    public RunCollectionStatisticalTabularView() {
38      InitializeComponent();
39    }
40
41    public new RunCollection Content {
42      get { return (RunCollection)base.Content; }
43      set { base.Content = value; }
44    }
45
46    public override bool ReadOnly {
47      get { return true; }
48      set { /*not needed because results are always readonly */}
49    }
50
51    protected override void OnContentChanged() {
52      base.OnContentChanged();
53      dataTableComboBox.Items.Clear();
54      dataRowComboBox.Items.Clear();
55
56      if (Content != null) {
57        UpdateDataTableComboBox();
58      }
59    }
60
61    #region events
62    protected override void RegisterContentEvents() {
63      base.RegisterContentEvents();
64      Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
65      Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
66      Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
67
68      RegisterRunEvents(Content);
69    }
70
71    private void RegisterRunEvents(IEnumerable<IRun> runs) {
72      foreach (IRun run in runs)
73        run.Changed += new EventHandler(run_Changed);
74    }
75
76    protected override void DeregisterContentEvents() {
77      base.DeregisterContentEvents();
78      Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
79      Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
80      Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
81
82      DeregisterRunEvents(Content);
83    }
84
85    private void DeregisterRunEvents(IEnumerable<IRun> runs) {
86      foreach (IRun run in runs)
87        run.Changed -= new EventHandler(run_Changed);
88    }
89
90    private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
91      DeregisterRunEvents(e.OldItems);
92      RegisterRunEvents(e.Items);
93      RebuildCombinedDataTable();
94    }
95
96    private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
97      DeregisterRunEvents(e.Items);
98      RebuildCombinedDataTable();
99    }
100
101    private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
102      RegisterRunEvents(e.Items);
103      RebuildCombinedDataTable();
104    }
105
106    private void run_Changed(object sender, EventArgs e) {
107      if (InvokeRequired)
108        this.Invoke(new EventHandler(run_Changed), sender, e);
109      else {
110        IRun run = (IRun)sender;
111        UpdateRun(run);
112      }
113    }
114    #endregion
115
116    private void UpdateRun(IRun run) {
117      //TODO: hacky di hack... this is baaaadddd
118      RebuildCombinedDataTable();
119    }
120
121    private void dataTableComboBox_SelectedIndexChanged(object sender, EventArgs e) {
122      UpdateDataRowComboBox();
123    }
124
125    private void dataRowComboBox_SelectedIndexChanged(object sender, EventArgs e) {
126      RebuildCombinedDataTable();
127    }
128
129    private void UpdateDataRowComboBox() {
130      dataRowComboBox.Items.Clear();
131      var resultName = (string)dataTableComboBox.SelectedItem;
132      var dataTables = from run in Content
133                       where run.Results.ContainsKey(resultName)
134                       select run.Results[resultName] as DataTable;
135      var rowNames = (from dataTable in dataTables
136                      from row in dataTable.Rows
137                      select row.Name).Distinct().ToArray();
138
139      dataRowComboBox.Items.AddRange(rowNames);
140      if (dataRowComboBox.Items.Count > 0) dataRowComboBox.SelectedItem = dataRowComboBox.Items[0];
141    }
142
143    private void UpdateDataTableComboBox() {
144      dataTableComboBox.Items.Clear();
145      var dataTables = (from run in Content
146                        from result in run.Results
147                        where result.Value is DataTable
148                        select result.Key).Distinct().ToArray();
149
150      dataTableComboBox.Items.AddRange(dataTables);
151      if (dataTableComboBox.Items.Count > 0) dataTableComboBox.SelectedItem = dataTableComboBox.Items[0];
152    }
153
154    private void RebuildCombinedDataTable() {
155      string resultName = (string)dataTableComboBox.SelectedItem;
156      string rowName = (string)dataRowComboBox.SelectedItem;
157
158      string[] columnNames = new string[] { "Count", "Minimum", "Maximum", "Average", "Median", "Standard Deviation", "Variance", "25th Percentile", "75th Percentile", "Gradient", "Relative Error" };
159
160      var runs = Content.Where(x => x.Results.ContainsKey(resultName));
161
162      StringMatrix dt = new StringMatrix(runs.Count(), columnNames.Count());
163      dt.RowNames = runs.Select(x => x.Name);
164      dt.ColumnNames = columnNames;
165
166      int i = 0;
167      foreach (Run run in runs) {
168        DataTable resTable = (DataTable)run.Results[resultName];
169        DataRow row = resTable.Rows[rowName];
170        var values = row.Values.AsEnumerable();
171
172        double cnt = values.Count();
173        double min = values.Min();
174        double max = values.Max();
175        double avg = values.Average();
176        double median = values.Median();
177        double stdDev = values.StandardDeviation();
178        double variance = values.Variance();
179        double percentile25 = values.Percentile(0.25);
180        double percentile75 = values.Percentile(0.75);
181        double k, d, r;
182        LinearLeastSquaresFitting.Calculate(values.ToArray(), out k, out d);
183        r = LinearLeastSquaresFitting.CalculateError(values.ToArray(), k, d);
184
185        dt[i, 0] = cnt.ToString();
186        dt[i, 1] = min.ToString();
187        dt[i, 2] = max.ToString();
188        dt[i, 3] = avg.ToString();
189        dt[i, 4] = median.ToString();
190        dt[i, 5] = stdDev.ToString();
191        dt[i, 6] = variance.ToString();
192        dt[i, 7] = percentile25.ToString();
193        dt[i, 8] = percentile75.ToString();
194        dt[i, 9] = k.ToString();
195        dt[i, 10] = r.ToString();
196
197        i++;
198      }
199
200      stringConvertibleMatrixView.Content = dt;
201    }
202
203    private void addLineToChart_Click(object sender, EventArgs e) {
204      string resultName = (string)dataTableComboBox.SelectedItem;
205      string rowName = (string)dataRowComboBox.SelectedItem;
206      var runs = Content.Where(x => x.Results.ContainsKey(resultName));
207
208      foreach (Run run in runs) {
209        DataTable resTable = (DataTable)run.Results[resultName];
210        DataRow row = resTable.Rows[rowName];
211        var values = row.Values.ToArray();
212        double k, d;
213        LinearLeastSquaresFitting.Calculate(values, out k, out d);
214
215        DataRow newRow = new DataRow(row.Name + " Fitted Line");
216        for (int i = 0; i < values.Count(); i++) {
217          newRow.Values.Add(k * i + d);
218        }
219
220        if (!resTable.Rows.ContainsKey(newRow.Name))
221          resTable.Rows.Add(newRow);
222      }
223    }
224  }
225}
Note: See TracBrowser for help on using the repository browser.