#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.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Windows.Forms; using HeuristicLab.Core.Views; using HeuristicLab.Data; using HeuristicLab.MainForm; using HeuristicLab.Optimization; using HeuristicLab.Optimization.Views; namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views { [View("Sliding Window Best Solutions View")] [Content(typeof(SlidingWindowBestSolutionsCollection), true)] public partial class SlidingWindowBestSolutionsCollectionView : ItemView { public new SlidingWindowBestSolutionsCollection Content { get { return (SlidingWindowBestSolutionsCollection)base.Content; } set { base.Content = value; } } protected override void OnContentChanged() { base.OnContentChanged(); if (Content != null) { 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) { SetMatrixContent(Content.SlidingWindowQualities); } else if (!Content.QualitiesCalculationInProgress) { Content.CalculateQualities(); } } } private readonly Progress progress; protected override void DeregisterContentEvents() { // TODO: Deregister your event handlers here enhancedStringConvertibleMatrixView.DataGridView.CellDoubleClick -= enhancedStringConvertibleMatrixView_cellDoubleClick; 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 enhancedStringConvertibleMatrixView.DataGridView.CellDoubleClick += enhancedStringConvertibleMatrixView_cellDoubleClick; Content.QualitiesCalculationCompleted += bestSolutionsCollectionQualitiesCalculated; Content.QualitiesCalculationProgress += bestSolutionCollectionQualitiesCalculationProgress; Content.QualitiesUpdated += bestSolutionCollectionQualitiesUpdated; Content.QualitiesCalculationStarted += bestSolutionsCollectionQualtiesCalculationStarted; } public SlidingWindowBestSolutionsCollectionView() { InitializeComponent(); progress = new Progress(); MainFormManager.GetMainForm().AddOperationProgressToView(this, progress); } #region Event Handlers (Content) private void bestSolutionsCollectionQualtiesCalculationStarted(object sender, EventArgs e) { progress.Start("Calculating best solutions qualities..."); } private void bestSolutionsCollectionQualitiesCalculated(object sender, RunWorkerCompletedEventArgs e) { if (e.Cancelled || e.Error != null) { progress.Cancel(); return; } SetMatrixContent(Content.SlidingWindowQualities); progress.Finish(); } private void bestSolutionCollectionQualitiesCalculationProgress(object sender, ProgressChangedEventArgs e) { progress.ProgressValue = e.ProgressPercentage / 100.0; } private void bestSolutionCollectionQualitiesUpdated(object sender, EventArgs e) { SetMatrixContent(Content.SlidingWindowQualities); } #endregion protected override void SetEnabledStateOfControls() { base.SetEnabledStateOfControls(); // TODO: Enable or disable controls based on whether the content is null or the view is set readonly } private void SetMatrixContent(double[,] values) { var qualitiesMap = new DoubleMatrix(values); // add an extra columns for the training data qualitiesMap.ColumnNames = Enumerable.Range(0, qualitiesMap.Columns - 1).Select(x => Content.SlidingWindowRanges[x].Start + " - " + Content.SlidingWindowRanges[x].End).Concat(new List { "Training" }); qualitiesMap.RowNames = Enumerable.Range(1, qualitiesMap.Rows).Select(x => "M" + x); double min = 0, max = 0; if (Content.QualityMeasure == SlidingWindowBestSolutionsCollection.QualityMeasures.PEARSON) { max = 1.0; } else { foreach (var q in qualitiesMap) { if (min > q) min = q; if (max < q) max = q; } } enhancedStringConvertibleMatrixView.Minimum = min; enhancedStringConvertibleMatrixView.Maximum = max; enhancedStringConvertibleMatrixView.Content = qualitiesMap; } #region Event Handlers (child controls) 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; } } } public void enhancedStringConvertibleMatrixView_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; } } #endregion } }