Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13842 was 13842, checked in by pfleck, 8 years ago

#2597

  • Reduced memory consumption greatly by reusing existing datapoints from existing series instead of creating new series on update.
  • Rearranged methods and properties in GradientChart.
  • Added properties to set fixed axis limits instead of calculation.
File size: 7.4 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 async 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      await gradientChart.RecalculateAsync();
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      gradientChart.RecalculateAsync();
146    }
147
148    private void trackbar_LimitsChanged(object sender, EventArgs e) {
149      var trackBar = sender as DensityTrackbar;
150      if (trackBar == null || !trackBar.Checked) return;
151      gradientChart.FixedXAxisMin = trackBar.Limits.Lower;
152      gradientChart.FixedXAxisMax = trackBar.Limits.Upper;
153      gradientChart.RecalculateAsync();
154    }
155
156    private void trackbar_ValueChanged(object sender, EventArgs e) {
157      var trackBar = sender as DensityTrackbar;
158      if (trackBar == null) return;
159      sharedFixedVariables.SetVariableValue((double)trackBar.Value, variableNames[trackbars.IndexOf(trackBar)], 0);
160      gradientChart.RecalculateAsync();
161    }
162
163    #region Events
164    protected override void RegisterContentEvents() {
165      base.RegisterContentEvents();
166      Content.ModelChanged += Content_ModelChanged;
167      Content.ProblemDataChanged += Content_ProblemDataChanged;
168    }
169
170    protected override void DeregisterContentEvents() {
171      base.DeregisterContentEvents();
172      Content.ModelChanged -= Content_ModelChanged;
173      Content.ProblemDataChanged -= Content_ProblemDataChanged;
174    }
175
176    protected override void OnContentChanged() {
177      base.OnContentChanged();
178      UpdateConfigurationControls();
179    }
180
181    private void Content_ModelChanged(object sender, EventArgs e) {
182      UpdateConfigurationControls();
183    }
184
185    private void Content_ProblemDataChanged(object sender, EventArgs e) {
186      UpdateConfigurationControls();
187    }
188    #endregion
189  }
190
191  internal static class Extensions {
192    public static void ForEach<T>(this IEnumerable<T> source, Action<T> action) {
193      foreach (T item in source)
194        action(item);
195    }
196  }
197}
Note: See TracBrowser for help on using the repository browser.