Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9426 was 5275, checked in by gkronber, 14 years ago

Merged changes from trunk to data analysis exploration branch and added fractional distance metric evaluator. #1142

File size: 6.6 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 HeuristicLab.Common;
26using HeuristicLab.Data;
27using HeuristicLab.MainForm;
28using HeuristicLab.MainForm.WindowsForms;
29using HeuristicLab.Optimization;
30using System;
31using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic;
32using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
33using HeuristicLab.Problems.DataAnalysis.Evaluators;
34using HeuristicLab.Analysis;
35
36namespace HeuristicLab.Problems.DataAnalysis.Views {
37  [Content(typeof(RunCollection), false)]
38  [View("RunCollection Validation Trajectory View")]
39  public partial class RunCollectionValidationTrajectoryView : AsynchronousContentView {
40    private const string validationQualityResultName = "Best solution quality table";
41    private const string validationComplexityResultName = "Best solution complexity";
42    public RunCollectionValidationTrajectoryView() {
43      InitializeComponent();
44    }
45
46    public new RunCollection Content {
47      get { return (RunCollection)base.Content; }
48      set { base.Content = value; }
49    }
50
51    protected override void RegisterContentEvents() {
52      base.RegisterContentEvents();
53      this.Content.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
54      this.Content.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
55      this.Content.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
56    }
57    protected override void DeregisterContentEvents() {
58      base.RegisterContentEvents();
59      this.Content.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
60      this.Content.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
61      this.Content.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
62    }
63
64    protected override void OnContentChanged() {
65      base.OnContentChanged();
66      this.UpdateData();
67    }
68    private void Content_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
69      this.UpdateData();
70    }
71    private void Content_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
72      this.UpdateData();
73    }
74    private void Content_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IRun> e) {
75      this.UpdateData();
76    }
77
78    private void UpdateData() {
79      matrixView.Content = CalculateValidationStatisticsTable();
80    }
81
82    private DataTable CalculateValidationStatisticsTable() {
83      DataTable matrix = null;
84      if (Content != null) {
85        List<IRun> runsWithSolutions = (from run in Content
86                                        where run.Results.ContainsKey(validationComplexityResultName)
87                                        where run.Results.ContainsKey(validationQualityResultName)
88                                        select run)
89                                              .ToList();
90        IList<DataTable> allComplexityTables = (from run in Content
91                                                where run.Results.ContainsKey(validationComplexityResultName)
92                                                select run.Results[validationComplexityResultName])
93                                                      .Cast<DataTable>()
94                                                      .ToList();
95        IList<DataTable> allQualityTables = (from run in Content
96                                             where run.Results.ContainsKey(validationQualityResultName)
97                                             select run.Results[validationQualityResultName])
98                                                   .Cast<DataTable>()
99                                                   .ToList();
100
101        List<string> complexityAttributes = new List<string>() {
102          "Best solution size",
103          "Best solution height",
104          "Best solution variables",
105        };
106        List<string> qualityAttributes = new List<string>() {
107          "Training MSE",
108          "Test MSE",
109          "Training R²",
110          "Test R²"
111        };
112
113        matrix = new DataTable();
114        //matrix.RowNames = rowNames;
115        //matrix.ColumnNames = attributes;
116
117        for (int column = 0; column < complexityAttributes.Count; column++) {
118          string attribute = complexityAttributes[column];
119          matrix.Rows.Add(GetSolutionLockedAverage(attribute, allComplexityTables));
120        }
121        for (int column = 0; column < qualityAttributes.Count; column++) {
122          string attribute = qualityAttributes[column];
123          matrix.Rows.Add(GetSolutionLockedAverage(attribute, allQualityTables));
124        }
125      }
126      return matrix;
127    }
128
129    private DataRow GetSolutionLockedAverage(string attribute, IList<DataTable> runTables) {
130      DataRow dataRow = new DataRow(attribute);
131      List<List<double>> runLists = new List<List<double>>();
132      int n = 0;
133      foreach (DataTable dt in runTables) {
134        List<double> runList = new List<double>();
135        runList.AddRange(dt.Rows[attribute].Values);
136        runLists.Add(runList);
137        if (n < runList.Count) {
138          n = runList.Count;
139        }
140      }
141
142      for (int row = n; row > 0; row--) {
143        List<double> values = new List<double>();
144        foreach (List<double> runList in runLists) {
145          if (runList.Count >= row) {
146            values.Add(runList[runList.Count - row]);
147          }
148        }
149        dataRow.Values.Add(values.Median());
150      }
151
152      return dataRow;
153    }
154  }
155}
Note: See TracBrowser for help on using the repository browser.