Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Sliding Window GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/SlidingWindow/SlidingWindowBestSolutionsCollectionView.cs @ 10724

Last change on this file since 10724 was 10724, checked in by bburlacu, 10 years ago

#1837: Moved sliding window views into separate folder, re-added title to heat map view.

File size: 7.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Core.Views;
28using HeuristicLab.Data;
29using HeuristicLab.MainForm;
30using HeuristicLab.Optimization;
31using HeuristicLab.Optimization.Views;
32
33namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
34  [View("Sliding Window Best Solutions View")]
35  [Content(typeof(SlidingWindowBestSolutionsCollection), true)]
36  public partial class SlidingWindowBestSolutionsCollectionView : ItemView {
37    public new SlidingWindowBestSolutionsCollection Content {
38      get { return (SlidingWindowBestSolutionsCollection)base.Content; }
39      set { base.Content = value; }
40    }
41
42    protected override void OnContentChanged() {
43      base.OnContentChanged();
44      if (Content != null) {
45        switch (Content.QualityMeasure) {
46          case SlidingWindowBestSolutionsCollection.QualityMeasures.PEARSON:
47            comboBox1.SelectedIndex = comboBox1.Items.IndexOf("Pearson R2");
48            break;
49          case SlidingWindowBestSolutionsCollection.QualityMeasures.MSE:
50            comboBox1.SelectedIndex = comboBox1.Items.IndexOf("Mean Squared Error");
51            break;
52        }
53        if (Content.SlidingWindowQualities != null) {
54          SetMatrixContent(Content.SlidingWindowQualities);
55        } else if (!Content.QualitiesCalculationInProgress) {
56          Content.CalculateQualities();
57        }
58      }
59    }
60
61    private readonly Progress progress;
62
63    protected override void DeregisterContentEvents() {
64      // TODO: Deregister your event handlers here
65      enhancedStringConvertibleMatrixView.DataGridView.CellDoubleClick -= enhancedStringConvertibleMatrixView_cellDoubleClick;
66      Content.QualitiesCalculationCompleted -= bestSolutionsCollectionQualitiesCalculated;
67      Content.QualitiesCalculationProgress -= bestSolutionCollectionQualitiesCalculationProgress;
68      Content.QualitiesUpdated -= bestSolutionCollectionQualitiesUpdated;
69      Content.QualitiesCalculationStarted -= bestSolutionsCollectionQualtiesCalculationStarted;
70      base.DeregisterContentEvents();
71    }
72
73    protected override void RegisterContentEvents() {
74      base.RegisterContentEvents();
75      // TODO: Register your event handlers here
76      enhancedStringConvertibleMatrixView.DataGridView.CellDoubleClick += enhancedStringConvertibleMatrixView_cellDoubleClick;
77      Content.QualitiesCalculationCompleted += bestSolutionsCollectionQualitiesCalculated;
78      Content.QualitiesCalculationProgress += bestSolutionCollectionQualitiesCalculationProgress;
79      Content.QualitiesUpdated += bestSolutionCollectionQualitiesUpdated;
80      Content.QualitiesCalculationStarted += bestSolutionsCollectionQualtiesCalculationStarted;
81    }
82
83    public SlidingWindowBestSolutionsCollectionView() {
84      InitializeComponent();
85      progress = new Progress();
86      MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().AddOperationProgressToView(this, progress);
87    }
88
89    #region Event Handlers (Content)
90    private void bestSolutionsCollectionQualtiesCalculationStarted(object sender, EventArgs e) {
91      progress.Start("Calculating best solutions qualities...");
92    }
93
94    private void bestSolutionsCollectionQualitiesCalculated(object sender, RunWorkerCompletedEventArgs e) {
95      if (e.Cancelled || e.Error != null) {
96        progress.Cancel();
97        return;
98      }
99
100      SetMatrixContent(Content.SlidingWindowQualities);
101      progress.Finish();
102    }
103
104    private void bestSolutionCollectionQualitiesCalculationProgress(object sender, ProgressChangedEventArgs e) {
105      progress.ProgressValue = e.ProgressPercentage / 100.0;
106    }
107
108    private void bestSolutionCollectionQualitiesUpdated(object sender, EventArgs e) {
109      SetMatrixContent(Content.SlidingWindowQualities);
110    }
111    #endregion
112
113    protected override void SetEnabledStateOfControls() {
114      base.SetEnabledStateOfControls();
115      // TODO: Enable or disable controls based on whether the content is null or the view is set readonly
116    }
117
118    private void SetMatrixContent(double[,] values) {
119      var qualitiesMap = new DoubleMatrix(values); // add an extra columns for the training data
120      qualitiesMap.ColumnNames = Enumerable.Range(0, qualitiesMap.Columns - 1).Select(x => Content.SlidingWindowRanges[x].Start + " - " + Content.SlidingWindowRanges[x].End).Concat(new List<string> { "Training" });
121      qualitiesMap.RowNames = Enumerable.Range(1, qualitiesMap.Rows).Select(x => "M" + x);
122      double min = 0, max = 0;
123      if (Content.QualityMeasure == SlidingWindowBestSolutionsCollection.QualityMeasures.PEARSON) {
124        max = 1.0;
125      } else {
126        foreach (var q in qualitiesMap) {
127          if (min > q) min = q;
128          if (max < q) max = q;
129        }
130      }
131
132      enhancedStringConvertibleMatrixView.Minimum = min;
133      enhancedStringConvertibleMatrixView.Maximum = max;
134      enhancedStringConvertibleMatrixView.Content = qualitiesMap;
135    }
136
137    #region Event Handlers (child controls)
138    private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e) {
139      var combobox = (ComboBox)sender;
140      switch ((string)combobox.SelectedItem) {
141        case "Pearson R2": {
142            Content.QualityMeasure = SlidingWindowBestSolutionsCollection.QualityMeasures.PEARSON;
143            break;
144          }
145        case "Mean Squared Error": {
146            Content.QualityMeasure = SlidingWindowBestSolutionsCollection.QualityMeasures.MSE;
147            break;
148          }
149      }
150    }
151
152    public void enhancedStringConvertibleMatrixView_cellDoubleClick(object sender, DataGridViewCellEventArgs e) {
153      var dataGridView = (DataGridView)sender;
154      var cell = dataGridView.SelectedCells[0];
155      var bestSolutions = Content.SlidingWindowBestSolutions.Values.ToList();
156      var tree = bestSolutions[cell.RowIndex];
157      var model = Content.CreateModel(tree, Content.Interpreter);
158      var solution = Content.CreateSolution(model, Content.ProblemData);
159      var result = new Result("Sliding Window Solution", solution);
160      var view = MainFormManager.MainForm.ShowContent(result, typeof(ResultView));
161      if (view != null) {
162        view.ReadOnly = ReadOnly;
163        view.Locked = Locked;
164      }
165    }
166    #endregion
167  }
168}
Note: See TracBrowser for help on using the repository browser.