Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Views/3.3/RunCollectionVariableImpactView.cs @ 3969

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

implemented first version of RunCollectionVariableImpactView (ticket #1011)

File size: 5.4 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;
35
36namespace HeuristicLab.Optimization.Views {
37  [Content(typeof(RunCollection), false)]
38  [View("RunCollection Variable Impact View")]
39  public partial class RunCollectionVariableImpactView : AsynchronousContentView {
40    private const string variableImpactResultName = "Integrated variable frequencies";
41    public RunCollectionVariableImpactView() {
42      InitializeComponent();
43    }
44
45    public new RunCollection Content {
46      get { return (RunCollection)base.Content; }
47      set { base.Content = value; }
48    }
49
50    protected override void RegisterContentEvents() {
51      base.RegisterContentEvents();
52      this.Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
53      this.Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
54      this.Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
55    }
56    protected override void DeregisterContentEvents() {
57      base.RegisterContentEvents();
58      this.Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
59      this.Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
60      this.Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
61    }
62
63    protected override void OnContentChanged() {
64      base.OnContentChanged();
65      this.UpdateData();
66    }
67    private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
68      this.UpdateData();
69    }
70    private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
71      this.UpdateData();
72    }
73    private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
74      this.UpdateData();
75    }
76
77    private void UpdateData() {
78      matrixView.Content = CalculateVariableImpactMatrix();
79    }
80
81    private DoubleMatrix CalculateVariableImpactMatrix() {
82      DoubleMatrix matrix = null;
83      if (Content != null) {
84        IEnumerable<IRun> runsWithVariables = Content.Where(r => r.Results.ContainsKey(variableImpactResultName));
85        IEnumerable<DoubleMatrix> variableImpacts = (from run in runsWithVariables
86                                                     select run.Results[variableImpactResultName]).Cast<DoubleMatrix>();
87        List<string> variableNames = (from varImpact in variableImpacts
88                                      from variableName in varImpact.RowNames
89                                      select variableName).Distinct().ToList();
90        List<string> columnNames = runsWithVariables.Select(r => r.Name).ToList();
91        columnNames.AddRange( new List<string> { "Mean", "Median", "StdDev" });
92        int runs = runsWithVariables.Count();
93
94        matrix = new DoubleMatrix(variableNames.Count, runs + 3);
95        matrix.SortableView = true;
96        matrix.RowNames = variableNames;
97        matrix.ColumnNames = columnNames;
98
99        foreach (IRun run in runsWithVariables) {
100          DoubleMatrix runVariableImpacts = (DoubleMatrix)run.Results[variableImpactResultName];
101          for (int i = 0; i < runVariableImpacts.Rows; i++) {
102            int rowIndex = variableNames.FindIndex(s => s == runVariableImpacts.RowNames.ElementAt(i));
103            int columnIndex = columnNames.FindIndex(s => s == run.Name);
104            matrix[rowIndex, columnIndex] = runVariableImpacts[i, 0];
105          }
106        }
107
108        for (int variableIndex = 0; variableIndex < matrix.Rows; variableIndex++) {
109          List<double> impacts = new List<double>();
110          for (int runIndex = 0; runIndex < runs; runIndex++)
111            impacts.Add(matrix[variableIndex, runIndex]);
112          matrix[variableIndex, runs] = impacts.Average();
113          matrix[variableIndex, runs + 1] = impacts.Median();
114          matrix[variableIndex, runs + 2] = impacts.StandardDeviation();
115        }
116      }
117      return matrix;
118    }
119  }
120}
Note: See TracBrowser for help on using the repository browser.