Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4108 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 5.8 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.Collections.Generic;
23using System.Linq;
24using System.Windows.Forms;
25using alglib;
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.Results.ContainsKey(variableImpactResultName)).ToList();
81        IEnumerable<DoubleMatrix> variableImpacts = (from run in runsWithVariables
82                                                     select run.Results[variableImpactResultName]).Cast<DoubleMatrix>();
83        List<string> variableNames = (from varImpact in variableImpacts
84                                      from variableName in varImpact.RowNames
85                                      select variableName).Distinct().ToList();
86        List<string> statictics = new List<string> { "Mean", "Median", "StdDev", "pValue Mean<0", "pValue Median<0" };
87        List<string> columnNames = runsWithVariables.Select(r => r.Name).ToList();
88        columnNames.AddRange(statictics);
89        int runs = runsWithVariables.Count();
90
91        matrix = new DoubleMatrix(variableNames.Count, runs + statictics.Count);
92        matrix.SortableView = true;
93        matrix.RowNames = variableNames;
94        matrix.ColumnNames = columnNames;
95
96        for (int i = 0; i < runsWithVariables.Count; i++) {
97          IRun run = runsWithVariables[i];
98          DoubleMatrix runVariableImpacts = (DoubleMatrix)run.Results[variableImpactResultName];
99          for (int j = 0; j < runVariableImpacts.Rows; j++) {
100            int rowIndex = variableNames.FindIndex(s => s == runVariableImpacts.RowNames.ElementAt(j));
101            matrix[rowIndex, i] = runVariableImpacts[j, 0];
102          }
103        }
104
105        for (int variableIndex = 0; variableIndex < matrix.Rows; variableIndex++) {
106          List<double> impacts = new List<double>();
107          for (int runIndex = 0; runIndex < runs; runIndex++)
108            impacts.Add(matrix[variableIndex, runIndex]);
109          matrix[variableIndex, runs] = impacts.Average();
110          matrix[variableIndex, runs + 1] = impacts.Median();
111          matrix[variableIndex, runs + 2] = impacts.StandardDeviation();
112          double leftTail = 0; double rightTail = 0; double bothTails = 0;
113          double[] impactsArray = impacts.ToArray();
114          studentttests.studentttest1(ref impactsArray, impactsArray.Length, 0, ref bothTails, ref leftTail, ref rightTail);
115          matrix[variableIndex, runs + 3] = rightTail;
116          wsr.wilcoxonsignedranktest(impactsArray, impactsArray.Length, 0, ref bothTails, ref leftTail, ref rightTail);
117          matrix[variableIndex, runs + 4] = rightTail;
118        }
119      }
120      return matrix;
121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.