Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Problems.DataAnalysis.Views/3.3/RunCollectionVariableImpactView.cs @ 6625

Last change on this file since 6625 was 4385, checked in by mkommend, 14 years ago

Added check for visibility of Runs in the VariableImpactView (ticket #1186).

File size: 9.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Common;
27using HeuristicLab.Data;
28using HeuristicLab.MainForm;
29using HeuristicLab.MainForm.WindowsForms;
30using HeuristicLab.Optimization;
31
32namespace HeuristicLab.Problems.DataAnalysis.Views {
33  [Content(typeof(RunCollection), false)]
34  [View("RunCollection Variable Impact View")]
35  public partial class RunCollectionVariableImpactView : AsynchronousContentView {
36    private const string variableImpactResultName = "Integrated variable frequencies";
37    public RunCollectionVariableImpactView() {
38      InitializeComponent();
39    }
40
41    public new RunCollection Content {
42      get { return (RunCollection)base.Content; }
43      set { base.Content = value; }
44    }
45
46    protected override void RegisterContentEvents() {
47      base.RegisterContentEvents();
48      this.Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
49      this.Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
50      this.Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
51    }
52    protected override void DeregisterContentEvents() {
53      base.RegisterContentEvents();
54      this.Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
55      this.Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
56      this.Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
57    }
58
59    protected override void OnContentChanged() {
60      base.OnContentChanged();
61      this.UpdateData();
62    }
63    private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
64      this.UpdateData();
65    }
66    private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
67      this.UpdateData();
68    }
69    private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
70      this.UpdateData();
71    }
72
73    private void UpdateData() {
74      matrixView.Content = CalculateVariableImpactMatrix();
75    }
76
77    private DoubleMatrix CalculateVariableImpactMatrix() {
78      DoubleMatrix matrix = null;
79      if (Content != null) {
80        List<IRun> runsWithVariables = Content.Where(r => r.Visible && r.Results.ContainsKey(variableImpactResultName)).ToList();
81        IEnumerable<DoubleMatrix> allVariableImpacts = (from run in runsWithVariables
82                                                        select run.Results[variableImpactResultName]).Cast<DoubleMatrix>();
83        IEnumerable<string> variableNames = (from variableImpact in allVariableImpacts
84                                             from variableName in variableImpact.RowNames
85                                             select variableName)
86                                            .Distinct();
87        // filter variableNames: only include names that have at least one non-zero value in a run
88        List<string> variableNamesList = (from variableName in variableNames
89                                          where GetVariableImpacts(variableName, allVariableImpacts).Any(x => !x.IsAlmost(0.0))
90                                          select variableName)
91                                         .ToList();
92
93        List<string> statictics = new List<string> { "Median Rank", "Mean", "StdDev", "pValue" };
94        List<string> columnNames = runsWithVariables.Select(r => r.Name).ToList();
95        columnNames.AddRange(statictics);
96        int runs = runsWithVariables.Count();
97
98        matrix = new DoubleMatrix(variableNamesList.Count, runs + statictics.Count);
99        matrix.SortableView = true;
100        matrix.RowNames = variableNamesList;
101        matrix.ColumnNames = columnNames;
102
103        for (int i = 0; i < runsWithVariables.Count; i++) {
104          IRun run = runsWithVariables[i];
105          DoubleMatrix runVariableImpacts = (DoubleMatrix)run.Results[variableImpactResultName];
106          for (int j = 0; j < runVariableImpacts.Rows; j++) {
107            int rowIndex = variableNamesList.FindIndex(s => s == runVariableImpacts.RowNames.ElementAt(j));
108            if (rowIndex > -1) {
109              matrix[rowIndex, i] = runVariableImpacts[j, 0];
110            }
111          }
112        }
113
114        List<List<double>> variableImpactsOverRuns = (from variableName in variableNamesList
115                                                      select GetVariableImpacts(variableName, allVariableImpacts).ToList())
116                                                     .ToList();
117        List<List<double>> variableRanks = (from variableName in variableNamesList
118                                            select GetVariableImpactRanks(variableName, allVariableImpacts).ToList())
119                                        .ToList();
120        if (variableImpactsOverRuns.Count() > 0) {
121          // the variable with the worst median impact value is chosen as the reference variable
122          // this is problematic if all variables are relevant, however works often in practice
123          List<double> referenceImpacts = (from impacts in variableImpactsOverRuns
124                                           let avg = impacts.Median()
125                                           orderby avg
126                                           select impacts)
127                                           .First();
128          // for all variables
129          for (int row = 0; row < variableImpactsOverRuns.Count; row++) {
130            // median rank
131            matrix[row, runs] = variableRanks[row].Median();
132            // also show mean and std.dev. of relative variable impacts to indicate the relative difference in impacts of variables
133            matrix[row, runs + 1] = variableImpactsOverRuns[row].Average();
134            matrix[row, runs + 2] = variableImpactsOverRuns[row].StandardDeviation();
135
136            double leftTail = 0; double rightTail = 0; double bothTails = 0;
137            // calc differences of impacts for current variable and reference variable
138            double[] z = new double[referenceImpacts.Count];
139            for (int i = 0; i < z.Length; i++) {
140              z[i] = variableImpactsOverRuns[row][i] - referenceImpacts[i];
141            }
142            // wilcoxon signed rank test is used because the impact values of two variables in a single run are not independent
143            alglib.wsr.wilcoxonsignedranktest(z, z.Length, 0, ref bothTails, ref leftTail, ref rightTail);
144            matrix[row, runs + 3] = bothTails;
145          }
146        }
147      }
148      return matrix;
149    }
150
151    private IEnumerable<double> GetVariableImpactRanks(string variableName, IEnumerable<DoubleMatrix> allVariableImpacts) {
152      foreach (DoubleMatrix runVariableImpacts in allVariableImpacts) {
153        // certainly not yet very efficient because ranks are computed multiple times for the same run
154        string[] variableNames = runVariableImpacts.RowNames.ToArray();
155        double[] values = (from row in Enumerable.Range(0, runVariableImpacts.Rows)
156                           select runVariableImpacts[row, 0] * -1)
157                          .ToArray();
158        Array.Sort(values, variableNames);
159        // calculate ranks
160        double[] ranks = new double[values.Length];
161        // check for tied ranks
162        int i = 0;
163        while (i < values.Length) {
164          ranks[i] = i + 1;
165          int j = i + 1;
166          while (j < values.Length && values[i].IsAlmost(values[j])) {
167            ranks[j] = ranks[i];
168            j++;
169          }
170          i = j;
171        }
172        int rankIndex = 0;
173        foreach (string rowVariableName in variableNames) {
174          if (rowVariableName == variableName)
175            yield return ranks[rankIndex];
176          rankIndex++;
177        }
178      }
179    }
180
181    private IEnumerable<double> GetVariableImpacts(string variableName, IEnumerable<DoubleMatrix> allVariableImpacts) {
182      foreach (DoubleMatrix runVariableImpacts in allVariableImpacts) {
183        int row = 0;
184        foreach (string rowName in runVariableImpacts.RowNames) {
185          if (rowName == variableName)
186            yield return runVariableImpacts[row, 0];
187          row++;
188        }
189      }
190    }
191
192  }
193}
Note: See TracBrowser for help on using the repository browser.