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, 8 years ago

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

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