Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.RegressionSolutionGradientView/HeuristicLab.Problems.DataAnalysis.Views/3.4/RegressionSolutionGradientView.cs @ 13840

Last change on this file since 13840 was 13840, checked in by pfleck, 9 years ago

#2597 Implemented cancellation of chart update when new update is called during calculation.

File size: 7.1 KB
RevLine 
[13812]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Linq;
25using System.Windows.Forms;
[13816]26using HeuristicLab.Collections;
[13812]27using HeuristicLab.MainForm;
28using HeuristicLab.Problems.DataAnalysis;
29using HeuristicLab.Problems.DataAnalysis.Views;
30using HeuristicLab.Visualization.ChartControlsExtensions;
31
32namespace HeuristicLab.Algorithms.DataAnalysis.Views {
[13824]33  [View("Gradient View")]
[13837]34  [Content(typeof(IRegressionSolution))]
[13836]35  public partial class RegressionSolutionGradientView : DataAnalysisSolutionEvaluationView {
[13816]36    private const int DrawingSteps = 1000;
37
[13831]38    private readonly List<string> variableNames;
39    private readonly ObservableList<DensityTrackbar> trackbars;
40    private ModifiableDataset sharedFixedVariables;
[13812]41
42    private int ActiveDimension {
[13831]43      get { return trackbars.FindIndex(tb => tb.Checked); }
[13812]44    }
45
[13837]46    public new IRegressionSolution Content {
47      get { return (IRegressionSolution)base.Content; }
[13812]48      set { base.Content = value; }
49    }
50
[13824]51    public RegressionSolutionGradientView()
[13816]52      : base() {
[13831]53      variableNames = new List<string>();
[13816]54
[13831]55      trackbars = new ObservableList<DensityTrackbar>();
56      trackbars.ItemsAdded += (sender, args) => {
[13837]57        args.Items.Select(i => i.Value).ForEach(RegisterEvents);
[13816]58      };
[13831]59      trackbars.ItemsRemoved += (sender, args) => {
[13837]60        args.Items.Select(i => i.Value).ForEach(DeregisterEvents);
[13816]61      };
[13831]62      trackbars.CollectionReset += (sender, args) => {
[13837]63        args.OldItems.Select(i => i.Value).ForEach(DeregisterEvents);
64        args.Items.Select(i => i.Value).ForEach(RegisterEvents);
[13816]65      };
66
[13812]67      InitializeComponent();
68
[13816]69      // Avoid additional horizontal scrollbar
70      var vertScrollWidth = SystemInformation.VerticalScrollBarWidth;
71      tableLayoutPanel.Padding = new Padding(0, 0, vertScrollWidth, 0);
[13812]72    }
73
[13831]74    private void UpdateConfigurationControls() {
75      variableNames.Clear();
76      trackbars.Clear();
[13812]77
[13831]78      tableLayoutPanel.RowCount = 0;
79      tableLayoutPanel.Controls.Clear();
[13812]80
[13831]81      if (Content == null) return;
[13812]82
[13831]83      variableNames.AddRange(Content.ProblemData.AllowedInputVariables);
[13812]84
[13831]85      var newTrackbars = CreateConfiguration();
[13812]86
[13831]87      sharedFixedVariables = new ModifiableDataset(variableNames, newTrackbars.Select(tb => new List<double>(1) { (double)tb.Value }));
88      gradientChart.Configure(new[] { Content }, sharedFixedVariables, variableNames.First(), DrawingSteps);
[13840]89      gradientChart.UpdateChart();
[13812]90
[13831]91      // Add to table and observable lists     
92      tableLayoutPanel.RowCount = variableNames.Count;
93      while (tableLayoutPanel.RowStyles.Count < variableNames.Count)
94        tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
95      for (int i = 0; i < newTrackbars.Count; i++) {
96        // events registered automatically
97        trackbars.Add(newTrackbars[i]);
98        tableLayoutPanel.Controls.Add(newTrackbars[i], 0, i);
99      }
[13812]100
[13831]101      trackbars.First().Checked = true;
[13812]102    }
103
[13831]104    private IList<DensityTrackbar> CreateConfiguration() {
[13816]105      var ranges = new List<DoubleLimit>();
[13831]106      foreach (string variableName in variableNames) {
107        var values = Content.ProblemData.Dataset.GetDoubleValues(variableName, Content.ProblemData.AllIndices);
[13812]108        double min, max, interval;
109        ChartUtil.CalculateAxisInterval(values.Min(), values.Max(), 10, out min, out max, out interval);
[13816]110        ranges.Add(new DoubleLimit(min, max));
[13812]111      }
112
[13831]113      var newTrackbars = new List<DensityTrackbar>();
114      for (int i = 0; i < variableNames.Count; i++) {
115        var name = variableNames[i];
116        var trainingData = Content.ProblemData.Dataset.GetDoubleValues(name, Content.ProblemData.TrainingIndices).ToList();
[13812]117
[13831]118        var dimensionTrackbar = new DensityTrackbar(name, ranges[i], trainingData) {
119          Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right
120        };
121        newTrackbars.Add(dimensionTrackbar);
[13812]122      }
123
[13831]124      return newTrackbars;
[13812]125    }
126
[13816]127    private void RegisterEvents(DensityTrackbar trackbar) {
[13831]128      trackbar.CheckedChanged += trackbar_CheckedChanged;
129      trackbar.ValueChanged += trackbar_ValueChanged;
130      trackbar.LimitsChanged += trackbar_LimitsChanged;
[13812]131    }
[13816]132    private void DeregisterEvents(DensityTrackbar trackbar) {
[13831]133      trackbar.CheckedChanged -= trackbar_CheckedChanged;
134      trackbar.ValueChanged -= trackbar_ValueChanged;
135      trackbar.LimitsChanged -= trackbar_LimitsChanged;
[13816]136    }
[13812]137
[13831]138    private void trackbar_CheckedChanged(object sender, EventArgs e) {
139      var trackBar = sender as DensityTrackbar;
140      if (trackBar == null || !trackBar.Checked) return;
[13816]141      // Uncheck all others
[13831]142      foreach (var tb in trackbars.Except(new[] { trackBar }))
[13816]143        tb.Checked = false;
[13831]144      gradientChart.FreeVariable = variableNames[trackbars.IndexOf(trackBar)];
[13812]145    }
[13816]146
[13831]147    private void trackbar_LimitsChanged(object sender, EventArgs e) {
148      // Todo adapt bounds
[13812]149    }
[13816]150
[13840]151    private void trackbar_ValueChanged(object sender, EventArgs e) {
[13831]152      var trackBar = sender as DensityTrackbar;
153      if (trackBar == null) return;
154      sharedFixedVariables.SetVariableValue((double)trackBar.Value, variableNames[trackbars.IndexOf(trackBar)], 0);
[13840]155      gradientChart.UpdateChart();
[13812]156    }
157
158    #region Events
159    protected override void RegisterContentEvents() {
160      base.RegisterContentEvents();
[13816]161      Content.ModelChanged += Content_ModelChanged;
162      Content.ProblemDataChanged += Content_ProblemDataChanged;
[13812]163    }
[13816]164
[13812]165    protected override void DeregisterContentEvents() {
166      base.DeregisterContentEvents();
[13816]167      Content.ModelChanged -= Content_ModelChanged;
168      Content.ProblemDataChanged -= Content_ProblemDataChanged;
[13812]169    }
170
171    protected override void OnContentChanged() {
172      base.OnContentChanged();
173      UpdateConfigurationControls();
174    }
[13816]175
[13812]176    private void Content_ModelChanged(object sender, EventArgs e) {
[13831]177      UpdateConfigurationControls();
[13812]178    }
[13816]179
[13812]180    private void Content_ProblemDataChanged(object sender, EventArgs e) {
181      UpdateConfigurationControls();
182    }
183    #endregion
184
[13837]185
186  }
187
188  internal static class Extensions {
189    public static void ForEach<T>(this IEnumerable<T> source, Action<T> action) {
[13816]190      foreach (T item in source)
191        action(item);
192    }
[13812]193  }
194}
Note: See TracBrowser for help on using the repository browser.