Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Views/3.3/RunCollectionValidationTrajectoryView.cs @ 4949

Last change on this file since 4949 was 4197, checked in by gkronber, 14 years ago

Added new files for data analysis feature exploration. #1142

File size: 6.7 KB
RevLine 
[4197]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;
31using System;
32using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic;
33using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
34using HeuristicLab.Problems.DataAnalysis.Evaluators;
35using HeuristicLab.Analysis;
36
37namespace HeuristicLab.Problems.DataAnalysis.Views {
38  [Content(typeof(RunCollection), false)]
39  [View("RunCollection Validation Trajectory View")]
40  public partial class RunCollectionValidationTrajectoryView : AsynchronousContentView {
41    private const string validationQualityResultName = "Best solution quality table";
42    private const string validationComplexityResultName = "Best solution complexity";
43    public RunCollectionValidationTrajectoryView() {
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 = CalculateValidationStatisticsTable();
81    }
82
83    private DataTable CalculateValidationStatisticsTable() {
84      DataTable matrix = null;
85      if (Content != null) {
86        List<IRun> runsWithSolutions = (from run in Content
87                                        where run.Results.ContainsKey(validationComplexityResultName)
88                                        where run.Results.ContainsKey(validationQualityResultName)
89                                        select run)
90                                              .ToList();
91        IList<DataTable> allComplexityTables = (from run in Content
92                                                where run.Results.ContainsKey(validationComplexityResultName)
93                                                select run.Results[validationComplexityResultName])
94                                                      .Cast<DataTable>()
95                                                      .ToList();
96        IList<DataTable> allQualityTables = (from run in Content
97                                             where run.Results.ContainsKey(validationQualityResultName)
98                                             select run.Results[validationQualityResultName])
99                                                   .Cast<DataTable>()
100                                                   .ToList();
101
102        List<string> complexityAttributes = new List<string>() {
103          "Best solution size",
104          "Best solution height",
105          "Best solution variables",
106        };
107        List<string> qualityAttributes = new List<string>() {
108          "Training MSE",
109          "Test MSE",
110          "Training R²",
111          "Test R²"
112        };
113
114        matrix = new DataTable();
115        //matrix.RowNames = rowNames;
116        //matrix.ColumnNames = attributes;
117
118        for (int column = 0; column < complexityAttributes.Count; column++) {
119          string attribute = complexityAttributes[column];
120          matrix.Rows.Add(GetSolutionLockedAverage(attribute, allComplexityTables));
121        }
122        for (int column = 0; column < qualityAttributes.Count; column++) {
123          string attribute = qualityAttributes[column];
124          matrix.Rows.Add(GetSolutionLockedAverage(attribute, allQualityTables));
125        }
126      }
127      return matrix;
128    }
129
130    private DataRow GetSolutionLockedAverage(string attribute, IList<DataTable> runTables) {
131      DataRow dataRow = new DataRow(attribute);
132      List<List<double>> runLists = new List<List<double>>();
133      int n = 0;
134      foreach (DataTable dt in runTables) {
135        List<double> runList = new List<double>();
136        runList.AddRange(dt.Rows[attribute].Values);
137        runLists.Add(runList);
138        if (n < runList.Count) {
139          n = runList.Count;
140        }
141      }
142
143      for (int row = n; row > 0; row--) {
144        List<double> values = new List<double>();
145        foreach (List<double> runList in runLists) {
146          if (runList.Count >= row) {
147            values.Add(runList[runList.Count - row]);
148          }
149        }
150        dataRow.Values.Add(values.Median());
151      }
152
153      return dataRow;
154    }
155  }
156}
Note: See TracBrowser for help on using the repository browser.