Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Views/3.3/RunCollectionVariableImpactView.cs @ 4040

Last change on this file since 4040 was 4037, checked in by mkommend, 14 years ago

corrected namespace of RunCollectionVariableImpactView and changed test direction (ticket #1011)

File size: 6.0 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.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Linq;
28using System.Text;
29using System.Windows.Forms;
30using HeuristicLab.MainForm;
31using HeuristicLab.MainForm.WindowsForms;
32using HeuristicLab.Core;
33using HeuristicLab.Common;
34using HeuristicLab.Data;
35using alglib;
36using HeuristicLab.Optimization;
37
38namespace HeuristicLab.Problems.DataAnalysis.Views {
39  [Content(typeof(RunCollection), false)]
40  [View("RunCollection Variable Impact View")]
41  public partial class RunCollectionVariableImpactView : AsynchronousContentView {
42    private const string variableImpactResultName = "Integrated variable frequencies";
43    public RunCollectionVariableImpactView() {
44      InitializeComponent();
45    }
46
47    public new RunCollection Content {
48      get { return (RunCollection)base.Content; }
49      set { base.Content = value; }
50    }
51
52    protected override void RegisterContentEvents() {
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    protected override void DeregisterContentEvents() {
59      base.RegisterContentEvents();
60      this.Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
61      this.Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
62      this.Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
63    }
64
65    protected override void OnContentChanged() {
66      base.OnContentChanged();
67      this.UpdateData();
68    }
69    private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
70      this.UpdateData();
71    }
72    private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
73      this.UpdateData();
74    }
75    private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
76      this.UpdateData();
77    }
78
79    private void UpdateData() {
80      matrixView.Content = CalculateVariableImpactMatrix();
81    }
82
83    private DoubleMatrix CalculateVariableImpactMatrix() {
84      DoubleMatrix matrix = null;
85      if (Content != null) {
86        List<IRun> runsWithVariables = Content.Where(r => r.Results.ContainsKey(variableImpactResultName)).ToList() ;
87        IEnumerable<DoubleMatrix> variableImpacts = (from run in runsWithVariables
88                                                     select run.Results[variableImpactResultName]).Cast<DoubleMatrix>();
89        List<string> variableNames = (from varImpact in variableImpacts
90                                      from variableName in varImpact.RowNames
91                                      select variableName).Distinct().ToList();
92        List<string> statictics = new List<string> { "Mean", "Median", "StdDev", "pValue Mean<0", "pValue Median<0" };
93        List<string> columnNames = runsWithVariables.Select(r => r.Name).ToList();
94        columnNames.AddRange(statictics);
95        int runs = runsWithVariables.Count();
96
97        matrix = new DoubleMatrix(variableNames.Count, runs + statictics.Count);
98        matrix.SortableView = true;
99        matrix.RowNames = variableNames;
100        matrix.ColumnNames = columnNames;
101
102        for(int i = 0; i< runsWithVariables.Count; i++) {
103          IRun run = runsWithVariables[i];
104          DoubleMatrix runVariableImpacts = (DoubleMatrix)run.Results[variableImpactResultName];
105          for (int j = 0; j < runVariableImpacts.Rows; j++) {
106            int rowIndex = variableNames.FindIndex(s => s == runVariableImpacts.RowNames.ElementAt(j));
107            matrix[rowIndex, i] = runVariableImpacts[j, 0];
108          }
109        }
110
111        for (int variableIndex = 0; variableIndex < matrix.Rows; variableIndex++) {
112          List<double> impacts = new List<double>();
113          for (int runIndex = 0; runIndex < runs; runIndex++)
114            impacts.Add(matrix[variableIndex, runIndex]);
115          matrix[variableIndex, runs] = impacts.Average();
116          matrix[variableIndex, runs + 1] = impacts.Median();
117          matrix[variableIndex, runs + 2] = impacts.StandardDeviation();
118          double leftTail = 0; double rightTail = 0; double bothTails = 0;
119          double[] impactsArray = impacts.ToArray();
120          studentttests.studentttest1(ref impactsArray, impactsArray.Length, 0, ref bothTails, ref leftTail, ref rightTail);
121          matrix[variableIndex, runs + 3] = rightTail;
122          wsr.wilcoxonsignedranktest(impactsArray, impactsArray.Length, 0, ref bothTails, ref leftTail, ref rightTail);
123          matrix[variableIndex, runs + 4] = rightTail;
124        }
125      }
126      return matrix;
127    }
128  }
129}
Note: See TracBrowser for help on using the repository browser.