#region License Information /* HeuristicLab * Copyright (C) 2002-2016 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.Linq; using System.Windows.Forms; using HeuristicLab.Collections; using HeuristicLab.MainForm; using HeuristicLab.Problems.DataAnalysis; using HeuristicLab.Problems.DataAnalysis.Views; using HeuristicLab.Visualization.ChartControlsExtensions; namespace HeuristicLab.Algorithms.DataAnalysis.Views { [View("Gradient View")] [Content(typeof(IRegressionSolution))] public partial class RegressionSolutionGradientView : DataAnalysisSolutionEvaluationView { private const int DrawingSteps = 1000; private readonly List variableNames; private readonly ObservableList trackbars; private ModifiableDataset sharedFixedVariables; private int ActiveDimension { get { return trackbars.FindIndex(tb => tb.Checked); } } public new IRegressionSolution Content { get { return (IRegressionSolution)base.Content; } set { base.Content = value; } } public RegressionSolutionGradientView() : base() { variableNames = new List(); trackbars = new ObservableList(); trackbars.ItemsAdded += (sender, args) => { args.Items.Select(i => i.Value).ForEach(RegisterEvents); }; trackbars.ItemsRemoved += (sender, args) => { args.Items.Select(i => i.Value).ForEach(DeregisterEvents); }; trackbars.CollectionReset += (sender, args) => { args.OldItems.Select(i => i.Value).ForEach(DeregisterEvents); args.Items.Select(i => i.Value).ForEach(RegisterEvents); }; InitializeComponent(); // Avoid additional horizontal scrollbar var vertScrollWidth = SystemInformation.VerticalScrollBarWidth; tableLayoutPanel.Padding = new Padding(0, 0, vertScrollWidth, 0); } private void UpdateConfigurationControls() { variableNames.Clear(); trackbars.Clear(); tableLayoutPanel.RowCount = 0; tableLayoutPanel.Controls.Clear(); if (Content == null) return; variableNames.AddRange(Content.ProblemData.AllowedInputVariables); var newTrackbars = CreateConfiguration(); sharedFixedVariables = new ModifiableDataset(variableNames, newTrackbars.Select(tb => new List(1) { (double)tb.Value })); gradientChart.Configure(new[] { Content }, sharedFixedVariables, variableNames.First(), DrawingSteps); gradientChart.UpdateChart(); // Add to table and observable lists tableLayoutPanel.RowCount = variableNames.Count; while (tableLayoutPanel.RowStyles.Count < variableNames.Count) tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize)); for (int i = 0; i < newTrackbars.Count; i++) { // events registered automatically trackbars.Add(newTrackbars[i]); tableLayoutPanel.Controls.Add(newTrackbars[i], 0, i); } trackbars.First().Checked = true; } private IList CreateConfiguration() { var ranges = new List(); foreach (string variableName in variableNames) { var values = Content.ProblemData.Dataset.GetDoubleValues(variableName, Content.ProblemData.AllIndices); double min, max, interval; ChartUtil.CalculateAxisInterval(values.Min(), values.Max(), 10, out min, out max, out interval); ranges.Add(new DoubleLimit(min, max)); } var newTrackbars = new List(); for (int i = 0; i < variableNames.Count; i++) { var name = variableNames[i]; var trainingData = Content.ProblemData.Dataset.GetDoubleValues(name, Content.ProblemData.TrainingIndices).ToList(); var dimensionTrackbar = new DensityTrackbar(name, ranges[i], trainingData) { Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right }; newTrackbars.Add(dimensionTrackbar); } return newTrackbars; } private void RegisterEvents(DensityTrackbar trackbar) { trackbar.CheckedChanged += trackbar_CheckedChanged; trackbar.ValueChanged += trackbar_ValueChanged; trackbar.LimitsChanged += trackbar_LimitsChanged; } private void DeregisterEvents(DensityTrackbar trackbar) { trackbar.CheckedChanged -= trackbar_CheckedChanged; trackbar.ValueChanged -= trackbar_ValueChanged; trackbar.LimitsChanged -= trackbar_LimitsChanged; } private void trackbar_CheckedChanged(object sender, EventArgs e) { var trackBar = sender as DensityTrackbar; if (trackBar == null || !trackBar.Checked) return; // Uncheck all others foreach (var tb in trackbars.Except(new[] { trackBar })) tb.Checked = false; gradientChart.FreeVariable = variableNames[trackbars.IndexOf(trackBar)]; } private void trackbar_LimitsChanged(object sender, EventArgs e) { // Todo adapt bounds } private void trackbar_ValueChanged(object sender, EventArgs e) { var trackBar = sender as DensityTrackbar; if (trackBar == null) return; sharedFixedVariables.SetVariableValue((double)trackBar.Value, variableNames[trackbars.IndexOf(trackBar)], 0); gradientChart.UpdateChart(); } #region Events protected override void RegisterContentEvents() { base.RegisterContentEvents(); Content.ModelChanged += Content_ModelChanged; Content.ProblemDataChanged += Content_ProblemDataChanged; } protected override void DeregisterContentEvents() { base.DeregisterContentEvents(); Content.ModelChanged -= Content_ModelChanged; Content.ProblemDataChanged -= Content_ProblemDataChanged; } protected override void OnContentChanged() { base.OnContentChanged(); UpdateConfigurationControls(); } private void Content_ModelChanged(object sender, EventArgs e) { UpdateConfigurationControls(); } private void Content_ProblemDataChanged(object sender, EventArgs e) { UpdateConfigurationControls(); } #endregion } internal static class Extensions { public static void ForEach(this IEnumerable source, Action action) { foreach (T item in source) action(item); } } }