Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers.Views/3.3/ParameterInfluenceView.cs @ 9754

Last change on this file since 9754 was 9731, checked in by ascheibe, 11 years ago

#1886 cleaned up references

File size: 11.2 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.Drawing;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Analysis.AlgorithmBehavior.Analyzers;
28using HeuristicLab.Core.Views;
29using HeuristicLab.Data;
30using HeuristicLab.MainForm;
31using HeuristicLab.Optimization;
32
33namespace HeuristicLab.Analysis.AlgorithmBehavior.Views {
34  [View("Parameter Influence View")]
35  [Content(typeof(RunCollection), false)]
36  public sealed partial class ParameterInfluenceView : ItemView {
37    public ParameterInfluenceView() {
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      resultComboBox.Items.Clear();
54      parameterComboBox.Items.Clear();
55
56      if (Content != null) {
57        UpdateResultComboBox();
58        UpdateParameterComboBox();
59      }
60    }
61
62    #region events
63    protected override void RegisterContentEvents() {
64      base.RegisterContentEvents();
65      Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
66      Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
67      Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
68
69      RegisterRunEvents(Content);
70    }
71
72    private void RegisterRunEvents(IEnumerable<IRun> runs) {
73      foreach (IRun run in runs)
74        run.Changed += new EventHandler(run_Changed);
75    }
76
77    protected override void DeregisterContentEvents() {
78      base.DeregisterContentEvents();
79      Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
80      Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
81      Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
82
83      DeregisterRunEvents(Content);
84    }
85
86    private void DeregisterRunEvents(IEnumerable<IRun> runs) {
87      foreach (IRun run in runs)
88        run.Changed -= new EventHandler(run_Changed);
89    }
90
91    private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
92      DeregisterRunEvents(e.OldItems);
93      RegisterRunEvents(e.Items);
94      //RebuildInfluenceDataTable();
95    }
96
97    private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
98      DeregisterRunEvents(e.Items);
99      //RebuildInfluenceDataTable();
100    }
101
102    private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
103      RegisterRunEvents(e.Items);
104      //RebuildInfluenceDataTable();
105    }
106
107    private void run_Changed(object sender, EventArgs e) {
108      if (InvokeRequired)
109        this.Invoke(new EventHandler(run_Changed), sender, e);
110      else {
111        IRun run = (IRun)sender;
112        UpdateRun(run);
113      }
114    }
115    #endregion
116
117    private void UpdateRun(IRun run) {
118      //TODO: hacky di hack... this is baaaadddd
119      //RebuildInfluenceDataTable();
120    }
121
122    private void UpdateResultComboBox() {
123      resultComboBox.Items.Clear();
124      var results = (from run in Content
125                     from result in run.Results
126                     select result.Key).Distinct().ToArray();
127
128      resultComboBox.Items.AddRange(results);
129      if (resultComboBox.Items.Count > 0) resultComboBox.SelectedItem = resultComboBox.Items[0];
130    }
131
132    private void UpdateParameterComboBox() {
133      parameterComboBox.Items.Clear();
134      var parameters = (from run in Content
135                        from param in run.Parameters
136                        select param.Key).Distinct().ToArray();
137
138      parameterComboBox.Items.AddRange(parameters);
139      if (parameterComboBox.Items.Count > 0) parameterComboBox.SelectedItem = parameterComboBox.Items[0];
140    }
141
142    private List<string> GetColumnNames() {
143      string resultName = (string)resultComboBox.SelectedItem;
144      var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
145      var parameters = (from run in runs
146                        from p in run.Parameters
147                        select p.Key).Distinct();
148
149      parameters = parameters.Where(x => x != "Seed" && x != "Algorithm Name");
150
151      List<string> ret = new List<string>();
152      foreach (string para in parameters) {
153        var r = runs.Where(x => x.Parameters.ContainsKey(para));
154        var vals = r.First().Parameters[para];
155        if (vals.GetType().GetMember("Value").Count() > 0) {
156          var cnt = r.Select(x => ((dynamic)x.Parameters[para]).Value).Distinct();
157          if (cnt.Count() > 1)
158            ret.Add(para);
159        }
160      }
161      return ret;
162    }
163
164    private int CalculateParameterVariations() {
165      string parameterName = (string)parameterComboBox.SelectedItem;
166      var runs = Content.Where(x => x.Parameters.ContainsKey(parameterName) && x.Visible);
167
168      var r = runs.Where(x => x.Parameters.ContainsKey(parameterName));
169      var vals = r.First().Parameters[parameterName];
170      if (vals.GetType().GetMember("Value").Count() > 0) {
171        var cnt = r.Select(x => ((dynamic)x.Parameters[parameterName]).Value).Distinct();
172        return cnt.Count();
173      }
174
175      return 0;
176    }
177
178    private List<string> GetRowNames() {
179      string parameterName = (string)parameterComboBox.SelectedItem;
180      var runs = Content.Where(x => x.Parameters.ContainsKey(parameterName) && x.Visible);
181      List<string> ret = new List<string>();
182
183      var r = runs.Where(x => x.Parameters.ContainsKey(parameterName));
184      var vals = r.First().Parameters[parameterName];
185      if (vals.GetType().GetMember("Value").Count() > 0) {
186        var cnt = r.Select(x => ((dynamic)x.Parameters[parameterName]).Value).Distinct();
187        ret.AddRange(cnt.Select(x => (string)x.ToString()));
188      }
189
190      return ret;
191    }
192
193    private bool ContainsParameterValue(IRun run, string parameterName, string valueName) {
194      if (!run.Parameters.ContainsKey(parameterName))
195        return false;
196
197      var para = run.Parameters[parameterName];
198
199      if (para.GetType().GetMember("Value").Count() > 0) {
200        var val = ((dynamic)para).Value;
201        if (val.ToString() == valueName) {
202          return true;
203        }
204      }
205      return false;
206    }
207
208    private List<dynamic> GetParameterVariatons(IEnumerable<IRun> runs, string parameterName) {
209      List<dynamic> vals = new List<dynamic>();
210
211      var ps = runs.Select(x => (dynamic)x.Parameters[parameterName]);
212
213      foreach (var p in ps) {
214        if (p.GetType().GetMember("Value").Length > 0) {
215          var val = ((dynamic)p).Value;
216          if (!vals.Contains(val)) {
217            vals.Add(val);
218          }
219        }
220      }
221      return vals;
222    }
223
224    private void RebuildInfluenceDataTable() {
225      string resultName = (string)resultComboBox.SelectedItem;
226      string parameterName = (string)parameterComboBox.SelectedItem;
227
228      var columnNames = GetColumnNames().ToArray();
229
230      var tmpRowNames = GetRowNames();
231      tmpRowNames.Add("Overall");
232      var rowNames = tmpRowNames.ToArray();
233
234      var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
235
236      StringMatrix dt = new StringMatrix(CalculateParameterVariations() + 1, columnNames.Count());
237      dt.RowNames = rowNames;
238      dt.ColumnNames = columnNames;
239
240      int i = 0, j = 0;
241      foreach (var rowName in rowNames) {
242        j = 0;
243        foreach (var columnName in columnNames) {
244          IEnumerable<IRun> curRuns = null;
245          if (i + 1 == rowNames.Count()) {
246            //calculate Overall
247            curRuns = runs.Where(x => x.Parameters.ContainsKey(columnName));
248          } else {
249            curRuns = runs.Where(x => x.Parameters.ContainsKey(columnName) && ContainsParameterValue(x, parameterName, rowName));
250          }
251
252          var parameterVals = GetParameterVariatons(curRuns, columnName);
253
254          List<double[]> results = new List<double[]>();
255          foreach (var cpVal in parameterVals) {
256            var pvRuns = curRuns.Where(x => ((dynamic)x.Parameters[columnName]).Value == cpVal);
257            var ress = pvRuns.Select(x => (double)((dynamic)x.Results[resultName]).Value).ToArray();
258            results.Add(ress);
259          }
260
261          if (results.Count() > 1) {
262           double pval = KruskalWallis.Test(results.ToArray());
263           dt[i, j] = pval.ToString();
264          } else {
265            dt[i, j] = string.Empty;
266          }
267          j++;
268        }
269        i++;
270      }
271
272      stringConvertibleMatrixView.Content = dt;
273    }
274
275    private void calculateInfluence_Click(object sender, EventArgs e) {
276      RebuildInfluenceDataTable();
277    }
278
279    private void colorButton_Click(object sender, EventArgs e) {
280      string resultName = (string)resultComboBox.SelectedItem;
281      string rowName = (string)parameterComboBox.SelectedItem;
282      var runs = Content.Where(x => x.Results.ContainsKey(resultName) && x.Visible);
283      Dictionary<int, double> values = new Dictionary<int, double>();
284
285      if (stringConvertibleMatrixView.DataGridView.CurrentCell != null) {
286        int curIndex = stringConvertibleMatrixView.DataGridView.CurrentCell.ColumnIndex;
287
288        for (int i = 0; i < stringConvertibleMatrixView.Content.Rows; i++) {
289          values[i] = double.Parse(stringConvertibleMatrixView.Content.GetValue(i, curIndex));
290        }
291
292        var orderedValues = values.OrderBy(x => x.Value);
293
294        for (int i = 0; i < orderedValues.Count(); i++) {
295          var row = stringConvertibleMatrixView.DataGridView.Rows[orderedValues.ElementAt(i).Key];
296
297          int r = (int)Math.Round((double)i / (double)orderedValues.Count() * 255.0);
298          int g = (int)Math.Round(((double)orderedValues.Count() - (double)i) / (double)orderedValues.Count() * 255.0);
299
300          Color color = Color.FromArgb(r, g, 0);
301          row.DefaultCellStyle.ForeColor = color;
302        }
303      }
304    }
305  }
306}
Note: See TracBrowser for help on using the repository browser.