Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 6.5 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.ComponentModel;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Analysis;
27using HeuristicLab.Core.Views;
28using HeuristicLab.MainForm;
29using HeuristicLab.Optimization;
30using HeuristicLab.Optimization.Views;
31
32namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
33  [View("Sliding Window Best Solutions HeatMap Control")]
34  [Content(typeof(SlidingWindowBestSolutionsCollection), false)]
35  public partial class SlidingWindowBestSolutionsCollectionHeatMapControl : ItemView {
36    public new SlidingWindowBestSolutionsCollection Content {
37      get { return (SlidingWindowBestSolutionsCollection)base.Content; }
38      set { base.Content = value; }
39    }
40
41    protected override void OnContentChanged() {
42      base.OnContentChanged();
43      if (Content != null) {
44        // set the dropdown value to the correct value
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          SetHeatmapContent(Content.SlidingWindowQualities);
55        } else if (!Content.QualitiesCalculationInProgress) {
56          Content.CalculateQualities();
57        }
58      }
59    }
60
61    private readonly IProgress progress; // use this to show some progress when the sliding window qualities are calculated for each model
62
63    public SlidingWindowBestSolutionsCollectionHeatMapControl() {
64      InitializeComponent();
65      progress = new Progress();
66      MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().AddOperationProgressToView(this, progress);
67    }
68
69    protected override void DeregisterContentEvents() {
70      // TODO: Deregister your event handlers here
71      Content.QualitiesCalculationCompleted -= bestSolutionsCollectionQualitiesCalculated;
72      Content.QualitiesCalculationProgress -= bestSolutionCollectionQualitiesCalculationProgress;
73      Content.QualitiesUpdated -= bestSolutionCollectionQualitiesUpdated;
74      Content.QualitiesCalculationStarted -= bestSolutionsCollectionQualtiesCalculationStarted;
75      base.DeregisterContentEvents();
76    }
77
78    protected override void RegisterContentEvents() {
79      base.RegisterContentEvents();
80      // TODO: Register your event handlers here
81      Content.QualitiesCalculationCompleted += bestSolutionsCollectionQualitiesCalculated;
82      Content.QualitiesCalculationProgress += bestSolutionCollectionQualitiesCalculationProgress;
83      Content.QualitiesUpdated += bestSolutionCollectionQualitiesUpdated;
84      Content.QualitiesCalculationStarted += bestSolutionsCollectionQualtiesCalculationStarted;
85    }
86
87    private void bestSolutionsCollectionQualitiesCalculated(object sender, RunWorkerCompletedEventArgs e) {
88      if (e.Cancelled || e.Error != null) {
89        progress.Cancel();
90        return;
91      }
92      SetHeatmapContent(Content.SlidingWindowQualities);
93      progress.Finish();
94    }
95
96    private void bestSolutionCollectionQualitiesUpdated(object sender, EventArgs e) {
97      SetHeatmapContent(Content.SlidingWindowQualities);
98    }
99
100    private void SetHeatmapContent(double[,] values) {
101      double min = 0, max = 0;
102      if (Content.QualityMeasure == SlidingWindowBestSolutionsCollection.QualityMeasures.PEARSON) {
103        max = 1.0;
104      } else {
105        foreach (var q in values) {
106          if (min > q) min = q;
107          if (max < q) max = q;
108        }
109      }
110
111      heatMapView.Content = new HeatMap(values, "Best Sliding Window Solutions", min, max);
112    }
113
114    private void bestSolutionsCollectionQualtiesCalculationStarted(object sender, EventArgs e) {
115      progress.Start("Calculating best solutions qualities...");
116    }
117
118    private void bestSolutionCollectionQualitiesCalculationProgress(object sender, ProgressChangedEventArgs e) {
119      progress.ProgressValue = e.ProgressPercentage / 100.0;
120    }
121
122    protected override void SetEnabledStateOfControls() {
123      base.SetEnabledStateOfControls();
124      // TODO: Enable or disable controls based on whether the content is null or the view is set readonly
125    }
126
127    #region Event Handlers (child controls)
128    public void heatMapView_cellDoubleClick(object sender, DataGridViewCellEventArgs e) {
129      var dataGridView = (DataGridView)sender;
130      var cell = dataGridView.SelectedCells[0];
131      var bestSolutions = Content.SlidingWindowBestSolutions.Values.ToList();
132      var tree = bestSolutions[cell.RowIndex];
133      var model = Content.CreateModel(tree, Content.Interpreter);
134      var solution = Content.CreateSolution(model, Content.ProblemData);
135      var result = new Result("Sliding Window Solution", solution);
136      var view = MainFormManager.MainForm.ShowContent(result, typeof(ResultView));
137      if (view != null) {
138        view.ReadOnly = ReadOnly;
139        view.Locked = Locked;
140      }
141    }
142
143    private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e) {
144      var combobox = (ComboBox)sender;
145      switch ((string)combobox.SelectedItem) {
146        case "Pearson R2": {
147            Content.QualityMeasure = SlidingWindowBestSolutionsCollection.QualityMeasures.PEARSON;
148            break;
149          }
150        case "Mean Squared Error": {
151            Content.QualityMeasure = SlidingWindowBestSolutionsCollection.QualityMeasures.MSE;
152            break;
153          }
154      }
155    }
156    #endregion
157  }
158}
Note: See TracBrowser for help on using the repository browser.