Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2597:

  • merged recent trunk changes for GetUsedVariablesForPrediction method.
  • Merged chart from RegressionSolutionGradientView and existing GradientChart.
  • Used the GradientChart in the RegressionSolutionGradientView.
File size: 7.2 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(IConfidenceBoundRegressionSolution))]
35  public partial class RegressionSolutionGradientView
36    : DataAnalysisSolutionEvaluationView {
37
38    private const int DrawingSteps = 1000;
39
40    private const string EstimatedMeanSeriesName = "Estimated Mean";
41    private const string EstimatedVarianceSeriesName = "95% Conficence Interval";
42
43    private readonly List<string> variableNames;
44    private readonly ObservableList<DensityTrackbar> trackbars;
45    private ModifiableDataset sharedFixedVariables;
46
47    private int ActiveDimension {
48      get { return trackbars.FindIndex(tb => tb.Checked); }
49    }
50
51    public new IConfidenceBoundRegressionSolution Content {
52      get { return (IConfidenceBoundRegressionSolution)base.Content; }
53      set { base.Content = value; }
54    }
55
56    public RegressionSolutionGradientView()
57      : base() {
58      variableNames = new List<string>();
59
60      trackbars = new ObservableList<DensityTrackbar>();
61      trackbars.ItemsAdded += (sender, args) => {
62        ForEach(args.Items.Select(i => i.Value), RegisterEvents);
63      };
64      trackbars.ItemsRemoved += (sender, args) => {
65        ForEach(args.Items.Select(i => i.Value), DeregisterEvents);
66      };
67      trackbars.CollectionReset += (sender, args) => {
68        ForEach(args.OldItems.Select(i => i.Value), DeregisterEvents);
69        ForEach(args.Items.Select(i => i.Value), RegisterEvents);
70      };
71
72      InitializeComponent();
73
74      // Avoid additional horizontal scrollbar
75      var vertScrollWidth = SystemInformation.VerticalScrollBarWidth;
76      tableLayoutPanel.Padding = new Padding(0, 0, vertScrollWidth, 0);
77    }
78
79    private void UpdateConfigurationControls() {
80      variableNames.Clear();
81      trackbars.Clear();
82
83      tableLayoutPanel.RowCount = 0;
84      tableLayoutPanel.Controls.Clear();
85
86      if (Content == null) return;
87
88      variableNames.AddRange(Content.ProblemData.AllowedInputVariables);
89
90      var newTrackbars = CreateConfiguration();
91
92      sharedFixedVariables = new ModifiableDataset(variableNames, newTrackbars.Select(tb => new List<double>(1) { (double)tb.Value }));
93      gradientChart.Configure(new[] { Content }, sharedFixedVariables, variableNames.First(), DrawingSteps);
94      gradientChart.UpdateChart();
95
96      // Add to table and observable lists     
97      tableLayoutPanel.RowCount = variableNames.Count;
98      while (tableLayoutPanel.RowStyles.Count < variableNames.Count)
99        tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
100      for (int i = 0; i < newTrackbars.Count; i++) {
101        // events registered automatically
102        trackbars.Add(newTrackbars[i]);
103        tableLayoutPanel.Controls.Add(newTrackbars[i], 0, i);
104      }
105
106      trackbars.First().Checked = true;
107    }
108
109    private IList<DensityTrackbar> CreateConfiguration() {
110      var ranges = new List<DoubleLimit>();
111      foreach (string variableName in variableNames) {
112        var values = Content.ProblemData.Dataset.GetDoubleValues(variableName, Content.ProblemData.AllIndices);
113        double min, max, interval;
114        ChartUtil.CalculateAxisInterval(values.Min(), values.Max(), 10, out min, out max, out interval);
115        ranges.Add(new DoubleLimit(min, max));
116      }
117
118      var newTrackbars = new List<DensityTrackbar>();
119      for (int i = 0; i < variableNames.Count; i++) {
120        var name = variableNames[i];
121        var trainingData = Content.ProblemData.Dataset.GetDoubleValues(name, Content.ProblemData.TrainingIndices).ToList();
122
123        var dimensionTrackbar = new DensityTrackbar(name, ranges[i], trainingData) {
124          Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right
125        };
126        newTrackbars.Add(dimensionTrackbar);
127      }
128
129      return newTrackbars;
130    }
131
132    private void RegisterEvents(DensityTrackbar trackbar) {
133      trackbar.CheckedChanged += trackbar_CheckedChanged;
134      trackbar.ValueChanged += trackbar_ValueChanged;
135      trackbar.LimitsChanged += trackbar_LimitsChanged;
136    }
137    private void DeregisterEvents(DensityTrackbar trackbar) {
138      trackbar.CheckedChanged -= trackbar_CheckedChanged;
139      trackbar.ValueChanged -= trackbar_ValueChanged;
140      trackbar.LimitsChanged -= trackbar_LimitsChanged;
141    }
142
143    private void trackbar_CheckedChanged(object sender, EventArgs e) {
144      var trackBar = sender as DensityTrackbar;
145      if (trackBar == null || !trackBar.Checked) return;
146      // Uncheck all others
147      foreach (var tb in trackbars.Except(new[] { trackBar }))
148        tb.Checked = false;
149      gradientChart.FreeVariable = variableNames[trackbars.IndexOf(trackBar)];
150    }
151
152    private void trackbar_LimitsChanged(object sender, EventArgs e) {
153      // Todo adapt bounds
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.UpdateChart();
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    public static void ForEach<T>(IEnumerable<T> source, Action<T> action) {
191      foreach (T item in source)
192        action(item);
193    }
194  }
195}
Note: See TracBrowser for help on using the repository browser.