#region License Information
/* HeuristicLab
* Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using HeuristicLab.Analysis;
using HeuristicLab.Core.Views;
using HeuristicLab.MainForm;
using HeuristicLab.Optimization;
using HeuristicLab.Optimization.Views;
namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
[View("Sliding Window Best Solutions HeatMap Control")]
[Content(typeof(SlidingWindowBestSolutionsCollection), false)]
public partial class SlidingWindowBestSolutionsCollectionHeatMapControl : ItemView {
public new SlidingWindowBestSolutionsCollection Content {
get { return (SlidingWindowBestSolutionsCollection)base.Content; }
set { base.Content = value; }
}
protected override void OnContentChanged() {
base.OnContentChanged();
if (Content != null) {
// set the dropdown value to the correct value
switch (Content.QualityMeasure) {
case SlidingWindowBestSolutionsCollection.QualityMeasures.PEARSON:
comboBox1.SelectedIndex = comboBox1.Items.IndexOf("Pearson R2");
break;
case SlidingWindowBestSolutionsCollection.QualityMeasures.MSE:
comboBox1.SelectedIndex = comboBox1.Items.IndexOf("Mean Squared Error");
break;
}
if (Content.SlidingWindowQualities != null) {
SetHeatmapContent(Content.SlidingWindowQualities);
} else if (!Content.QualitiesCalculationInProgress) {
Content.CalculateQualities();
}
}
}
private readonly IProgress progress; // use this to show some progress when the sliding window qualities are calculated for each model
public SlidingWindowBestSolutionsCollectionHeatMapControl() {
InitializeComponent();
progress = new Progress();
MainFormManager.GetMainForm().AddOperationProgressToView(this, progress);
}
protected override void DeregisterContentEvents() {
// TODO: Deregister your event handlers here
Content.QualitiesCalculationCompleted -= bestSolutionsCollectionQualitiesCalculated;
Content.QualitiesCalculationProgress -= bestSolutionCollectionQualitiesCalculationProgress;
Content.QualitiesUpdated -= bestSolutionCollectionQualitiesUpdated;
Content.QualitiesCalculationStarted -= bestSolutionsCollectionQualtiesCalculationStarted;
base.DeregisterContentEvents();
}
protected override void RegisterContentEvents() {
base.RegisterContentEvents();
// TODO: Register your event handlers here
Content.QualitiesCalculationCompleted += bestSolutionsCollectionQualitiesCalculated;
Content.QualitiesCalculationProgress += bestSolutionCollectionQualitiesCalculationProgress;
Content.QualitiesUpdated += bestSolutionCollectionQualitiesUpdated;
Content.QualitiesCalculationStarted += bestSolutionsCollectionQualtiesCalculationStarted;
}
private void bestSolutionsCollectionQualitiesCalculated(object sender, RunWorkerCompletedEventArgs e) {
if (e.Cancelled || e.Error != null) {
progress.Cancel();
return;
}
SetHeatmapContent(Content.SlidingWindowQualities);
progress.Finish();
}
private void bestSolutionCollectionQualitiesUpdated(object sender, EventArgs e) {
SetHeatmapContent(Content.SlidingWindowQualities);
}
private void SetHeatmapContent(double[,] values) {
double min = 0, max = 0;
if (Content.QualityMeasure == SlidingWindowBestSolutionsCollection.QualityMeasures.PEARSON) {
max = 1.0;
} else {
foreach (var q in values) {
if (min > q) min = q;
if (max < q) max = q;
}
}
heatMapView.Content = new HeatMap(values, "Best Sliding Window Solutions", min, max);
}
private void bestSolutionsCollectionQualtiesCalculationStarted(object sender, EventArgs e) {
progress.Start("Calculating best solutions qualities...");
}
private void bestSolutionCollectionQualitiesCalculationProgress(object sender, ProgressChangedEventArgs e) {
progress.ProgressValue = e.ProgressPercentage / 100.0;
}
protected override void SetEnabledStateOfControls() {
base.SetEnabledStateOfControls();
// TODO: Enable or disable controls based on whether the content is null or the view is set readonly
}
#region Event Handlers (child controls)
public void heatMapView_cellDoubleClick(object sender, DataGridViewCellEventArgs e) {
var dataGridView = (DataGridView)sender;
var cell = dataGridView.SelectedCells[0];
var bestSolutions = Content.SlidingWindowBestSolutions.Values.ToList();
var tree = bestSolutions[cell.RowIndex];
var model = Content.CreateModel(tree, Content.Interpreter);
var solution = Content.CreateSolution(model, Content.ProblemData);
var result = new Result("Sliding Window Solution", solution);
var view = MainFormManager.MainForm.ShowContent(result, typeof(ResultView));
if (view != null) {
view.ReadOnly = ReadOnly;
view.Locked = Locked;
}
}
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e) {
var combobox = (ComboBox)sender;
switch ((string)combobox.SelectedItem) {
case "Pearson R2": {
Content.QualityMeasure = SlidingWindowBestSolutionsCollection.QualityMeasures.PEARSON;
break;
}
case "Mean Squared Error": {
Content.QualityMeasure = SlidingWindowBestSolutionsCollection.QualityMeasures.MSE;
break;
}
}
}
#endregion
}
}