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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.Globalization;
|
---|
26 | using System.Linq;
|
---|
27 | using System.Threading;
|
---|
28 | using System.Threading.Tasks;
|
---|
29 | using System.Windows.Forms;
|
---|
30 | using System.Windows.Forms.DataVisualization.Charting;
|
---|
31 | using HeuristicLab.Common;
|
---|
32 | using HeuristicLab.MainForm.WindowsForms;
|
---|
33 | using HeuristicLab.Visualization.ChartControlsExtensions;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.Problems.DataAnalysis.Views {
|
---|
36 | public partial class GradientChart : UserControl {
|
---|
37 | private ModifiableDataset sharedFixedVariables; // used for syncronising variable values between charts
|
---|
38 | private ModifiableDataset internalDataset; // holds the x values for each point drawn
|
---|
39 |
|
---|
40 | private CancellationTokenSource cancelCurrentRecalculateSource;
|
---|
41 |
|
---|
42 | private readonly List<IRegressionSolution> solutions;
|
---|
43 | private readonly Dictionary<IRegressionSolution, Series> seriesCache;
|
---|
44 | private readonly Dictionary<IRegressionSolution, Series> ciSeriesCache;
|
---|
45 |
|
---|
46 | #region Properties
|
---|
47 | public bool ShowLegend {
|
---|
48 | get { return chart.Legends[0].Enabled; }
|
---|
49 | set { chart.Legends[0].Enabled = value; }
|
---|
50 | }
|
---|
51 | public bool ShowXAxisLabel {
|
---|
52 | get { return chart.ChartAreas[0].AxisX.Enabled == AxisEnabled.True; }
|
---|
53 | set { chart.ChartAreas[0].AxisX.Enabled = value ? AxisEnabled.True : AxisEnabled.False; }
|
---|
54 | }
|
---|
55 | public bool ShowYAxisLabel {
|
---|
56 | get { return chart.ChartAreas[0].AxisY.Enabled == AxisEnabled.True; }
|
---|
57 | set { chart.ChartAreas[0].AxisY.Enabled = value ? AxisEnabled.True : AxisEnabled.False; }
|
---|
58 | }
|
---|
59 | public bool ShowCursor {
|
---|
60 | get { return chart.Annotations[0].Visible; }
|
---|
61 | set { chart.Annotations[0].Visible = value; }
|
---|
62 | }
|
---|
63 |
|
---|
64 | private int xAxisTicks = 5;
|
---|
65 | public int XAxisTicks {
|
---|
66 | get { return xAxisTicks; }
|
---|
67 | set {
|
---|
68 | if (value != xAxisTicks) {
|
---|
69 | xAxisTicks = value;
|
---|
70 | SetupAxis(chart.ChartAreas[0].AxisX, trainingMin, trainingMax, XAxisTicks, FixedXAxisMin, FixedXAxisMax);
|
---|
71 | RecalculateInternalDataset();
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|
75 | private double? fixedXAxisMin;
|
---|
76 | public double? FixedXAxisMin {
|
---|
77 | get { return fixedXAxisMin; }
|
---|
78 | set {
|
---|
79 | if ((value.HasValue && fixedXAxisMin.HasValue && !value.Value.IsAlmost(fixedXAxisMin.Value)) || (value.HasValue != fixedXAxisMin.HasValue)) {
|
---|
80 | fixedXAxisMin = value;
|
---|
81 | SetupAxis(chart.ChartAreas[0].AxisX, trainingMin, trainingMax, XAxisTicks, FixedXAxisMin, FixedXAxisMax);
|
---|
82 | RecalculateInternalDataset();
|
---|
83 | }
|
---|
84 | }
|
---|
85 | }
|
---|
86 | private double? fixedXAxisMax;
|
---|
87 | public double? FixedXAxisMax {
|
---|
88 | get { return fixedXAxisMax; }
|
---|
89 | set {
|
---|
90 | if ((value.HasValue && fixedXAxisMax.HasValue && !value.Value.IsAlmost(fixedXAxisMax.Value)) || (value.HasValue != fixedXAxisMax.HasValue)) {
|
---|
91 | fixedXAxisMax = value;
|
---|
92 | SetupAxis(chart.ChartAreas[0].AxisX, trainingMin, trainingMax, XAxisTicks, FixedXAxisMin, FixedXAxisMax);
|
---|
93 | RecalculateInternalDataset();
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | private int yAxisTicks = 5;
|
---|
99 | public int YAxisTicks {
|
---|
100 | get { return yAxisTicks; }
|
---|
101 | set {
|
---|
102 | if (value != yAxisTicks) {
|
---|
103 | yAxisTicks = value;
|
---|
104 | SetupAxis(chart.ChartAreas[0].AxisY, yMin, yMax, YAxisTicks, FixedYAxisMin, FixedYAxisMax);
|
---|
105 | RecalculateInternalDataset();
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|
109 | private double? fixedYAxisMin;
|
---|
110 | public double? FixedYAxisMin {
|
---|
111 | get { return fixedYAxisMin; }
|
---|
112 | set {
|
---|
113 | if ((value.HasValue && fixedYAxisMin.HasValue && !value.Value.IsAlmost(fixedYAxisMin.Value)) || (value.HasValue != fixedYAxisMin.HasValue)) {
|
---|
114 | fixedYAxisMin = value;
|
---|
115 | SetupAxis(chart.ChartAreas[0].AxisY, yMin, yMax, YAxisTicks, FixedYAxisMin, FixedYAxisMax);
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|
119 | private double? fixedYAxisMax;
|
---|
120 | public double? FixedYAxisMax {
|
---|
121 | get { return fixedYAxisMax; }
|
---|
122 | set {
|
---|
123 | if ((value.HasValue && fixedYAxisMax.HasValue && !value.Value.IsAlmost(fixedYAxisMax.Value)) || (value.HasValue != fixedYAxisMax.HasValue)) {
|
---|
124 | fixedYAxisMax = value;
|
---|
125 | SetupAxis(chart.ChartAreas[0].AxisY, yMin, yMax, YAxisTicks, FixedYAxisMin, FixedYAxisMax);
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | private double trainingMin = 1;
|
---|
131 | private double trainingMax = -1;
|
---|
132 |
|
---|
133 | private int drawingSteps = 1000;
|
---|
134 | public int DrawingSteps {
|
---|
135 | get { return drawingSteps; }
|
---|
136 | set {
|
---|
137 | if (value != drawingSteps) {
|
---|
138 | drawingSteps = value;
|
---|
139 | RecalculateInternalDataset();
|
---|
140 | ResizeAllSeriesData();
|
---|
141 | }
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | private string freeVariable;
|
---|
146 | public string FreeVariable {
|
---|
147 | get { return freeVariable; }
|
---|
148 | set {
|
---|
149 | if (value == freeVariable) return;
|
---|
150 | if (solutions.Any(s => !s.ProblemData.Dataset.DoubleVariables.Contains(value))) {
|
---|
151 | throw new ArgumentException("Variable does not exist in the ProblemData of the Solutions.");
|
---|
152 | }
|
---|
153 | freeVariable = value;
|
---|
154 | RecalculateInternalDataset();
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | private double yMin;
|
---|
159 | public double YMin {
|
---|
160 | get { return yMin; }
|
---|
161 | }
|
---|
162 | private double yMax;
|
---|
163 | public double YMax {
|
---|
164 | get { return yMax; }
|
---|
165 | }
|
---|
166 |
|
---|
167 | private VerticalLineAnnotation VerticalLineAnnotation {
|
---|
168 | get { return (VerticalLineAnnotation)chart.Annotations.SingleOrDefault(x => x is VerticalLineAnnotation); }
|
---|
169 | }
|
---|
170 | #endregion
|
---|
171 |
|
---|
172 | public GradientChart() {
|
---|
173 | InitializeComponent();
|
---|
174 |
|
---|
175 | solutions = new List<IRegressionSolution>();
|
---|
176 | seriesCache = new Dictionary<IRegressionSolution, Series>();
|
---|
177 | ciSeriesCache = new Dictionary<IRegressionSolution, Series>();
|
---|
178 |
|
---|
179 | // Configure axis
|
---|
180 | chart.CustomizeAllChartAreas();
|
---|
181 | chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
|
---|
182 | chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
|
---|
183 | chart.ChartAreas[0].CursorX.Interval = 0;
|
---|
184 |
|
---|
185 | chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
|
---|
186 | chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
|
---|
187 | chart.ChartAreas[0].CursorY.Interval = 0;
|
---|
188 |
|
---|
189 | Disposed += GradientChart_Disposed;
|
---|
190 | }
|
---|
191 | private void GradientChart_Disposed(object sender, EventArgs e) {
|
---|
192 | if (cancelCurrentRecalculateSource != null)
|
---|
193 | cancelCurrentRecalculateSource.Cancel();
|
---|
194 | }
|
---|
195 |
|
---|
196 | public void Configure(IEnumerable<IRegressionSolution> solutions, ModifiableDataset sharedFixedVariables, string freeVariable, int drawingSteps, bool initializeAxisRanges = true) {
|
---|
197 | if (!SolutionsCompatible(solutions))
|
---|
198 | throw new ArgumentException("Solutions are not compatible with the problem data.");
|
---|
199 | this.freeVariable = freeVariable;
|
---|
200 | this.drawingSteps = drawingSteps;
|
---|
201 |
|
---|
202 | this.solutions.Clear();
|
---|
203 | this.solutions.AddRange(solutions);
|
---|
204 |
|
---|
205 | // add an event such that whenever a value is changed in the shared dataset,
|
---|
206 | // this change is reflected in the internal dataset (where the value becomes a whole column)
|
---|
207 | if (this.sharedFixedVariables != null)
|
---|
208 | this.sharedFixedVariables.ItemChanged -= sharedFixedVariables_ItemChanged;
|
---|
209 | this.sharedFixedVariables = sharedFixedVariables;
|
---|
210 | this.sharedFixedVariables.ItemChanged += sharedFixedVariables_ItemChanged;
|
---|
211 |
|
---|
212 | RecalculateTrainingLimits(initializeAxisRanges);
|
---|
213 | RecalculateInternalDataset();
|
---|
214 |
|
---|
215 | chart.Series.Clear();
|
---|
216 | seriesCache.Clear();
|
---|
217 | ciSeriesCache.Clear();
|
---|
218 | foreach (var solution in this.solutions) {
|
---|
219 | var series = CreateSeries(solution);
|
---|
220 | seriesCache.Add(solution, series.Item1);
|
---|
221 | if (series.Item2 != null)
|
---|
222 | ciSeriesCache.Add(solution, series.Item2);
|
---|
223 | }
|
---|
224 |
|
---|
225 | ResizeAllSeriesData();
|
---|
226 | OrderAndColorSeries();
|
---|
227 | }
|
---|
228 |
|
---|
229 | public async Task RecalculateAsync(bool updateOnFinish = true, bool resetYAxis = true) {
|
---|
230 | if (IsDisposed
|
---|
231 | || sharedFixedVariables == null || !solutions.Any() || string.IsNullOrEmpty(freeVariable)
|
---|
232 | || trainingMin.IsAlmost(trainingMax) || trainingMin > trainingMax || drawingSteps == 0)
|
---|
233 | return;
|
---|
234 |
|
---|
235 | statusLabel.Visible = true;
|
---|
236 | Update(); // immediately show label
|
---|
237 |
|
---|
238 | // cancel previous recalculate call
|
---|
239 | if (cancelCurrentRecalculateSource != null)
|
---|
240 | cancelCurrentRecalculateSource.Cancel();
|
---|
241 | cancelCurrentRecalculateSource = new CancellationTokenSource();
|
---|
242 |
|
---|
243 | // Set cursor and x-axis
|
---|
244 | var defaultValue = sharedFixedVariables.GetDoubleValue(freeVariable, 0);
|
---|
245 | VerticalLineAnnotation.X = defaultValue;
|
---|
246 | chart.ChartAreas[0].AxisX.Title = FreeVariable + " : " + defaultValue.ToString("N3", CultureInfo.CurrentCulture);
|
---|
247 |
|
---|
248 | // Update series
|
---|
249 | var cancellationToken = cancelCurrentRecalculateSource.Token;
|
---|
250 | try {
|
---|
251 | var limits = await UpdateAllSeriesDataAsync(cancellationToken);
|
---|
252 | //cancellationToken.ThrowIfCancellationRequested();
|
---|
253 |
|
---|
254 | yMin = limits.Lower;
|
---|
255 | yMax = limits.Upper;
|
---|
256 | // Set y-axis
|
---|
257 | if (resetYAxis)
|
---|
258 | SetupAxis(chart.ChartAreas[0].AxisY, yMin, yMax, YAxisTicks, FixedYAxisMin, FixedYAxisMax);
|
---|
259 |
|
---|
260 | UpdateOutOfTrainingRangeStripLines();
|
---|
261 |
|
---|
262 | statusLabel.Visible = false;
|
---|
263 | if (updateOnFinish)
|
---|
264 | Update();
|
---|
265 | }
|
---|
266 | catch (OperationCanceledException) { }
|
---|
267 | catch (AggregateException ae) {
|
---|
268 | if (!ae.InnerExceptions.Any(e => e is OperationCanceledException))
|
---|
269 | throw;
|
---|
270 | }
|
---|
271 | }
|
---|
272 |
|
---|
273 | private void SetupAxis(Axis axis, double minValue, double maxValue, int ticks, double? fixedAxisMin, double? fixedAxisMax) {
|
---|
274 | double axisMin, axisMax, axisInterval;
|
---|
275 | ChartUtil.CalculateAxisInterval(minValue, maxValue, ticks, out axisMin, out axisMax, out axisInterval);
|
---|
276 | axis.Minimum = fixedAxisMin ?? axisMin;
|
---|
277 | axis.Maximum = fixedAxisMax ?? axisMax;
|
---|
278 | axis.Interval = (axis.Maximum - axis.Minimum) / ticks;
|
---|
279 |
|
---|
280 | try {
|
---|
281 | chart.ChartAreas[0].RecalculateAxesScale();
|
---|
282 | }
|
---|
283 | catch (InvalidOperationException) {
|
---|
284 | // Can occur if eg. axis min == axis max
|
---|
285 | }
|
---|
286 | }
|
---|
287 |
|
---|
288 | private void RecalculateTrainingLimits(bool initializeAxisRanges) {
|
---|
289 | trainingMin = solutions.Select(s => s.ProblemData.Dataset.GetDoubleValues(freeVariable, s.ProblemData.TrainingIndices).Min()).Max();
|
---|
290 | trainingMax = solutions.Select(s => s.ProblemData.Dataset.GetDoubleValues(freeVariable, s.ProblemData.TrainingIndices).Max()).Min();
|
---|
291 |
|
---|
292 | if (initializeAxisRanges) {
|
---|
293 | double xmin, xmax, xinterval;
|
---|
294 | ChartUtil.CalculateAxisInterval(trainingMin, trainingMax, XAxisTicks, out xmin, out xmax, out xinterval);
|
---|
295 | FixedXAxisMin = xmin;
|
---|
296 | FixedXAxisMax = xmax;
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|
300 | private void RecalculateInternalDataset() {
|
---|
301 | if (sharedFixedVariables == null)
|
---|
302 | return;
|
---|
303 |
|
---|
304 | // we expand the range in order to get nice tick intervals on the x axis
|
---|
305 | double xmin, xmax, xinterval;
|
---|
306 | ChartUtil.CalculateAxisInterval(trainingMin, trainingMax, XAxisTicks, out xmin, out xmax, out xinterval);
|
---|
307 |
|
---|
308 | if (FixedXAxisMin.HasValue) xmin = FixedXAxisMin.Value;
|
---|
309 | if (FixedXAxisMax.HasValue) xmax = FixedXAxisMax.Value;
|
---|
310 | double step = (xmax - xmin) / drawingSteps;
|
---|
311 |
|
---|
312 | var xvalues = new List<double>();
|
---|
313 | for (int i = 0; i < drawingSteps; i++)
|
---|
314 | xvalues.Add(xmin + i * step);
|
---|
315 |
|
---|
316 | var variables = sharedFixedVariables.DoubleVariables.ToList();
|
---|
317 | internalDataset = new ModifiableDataset(variables,
|
---|
318 | variables.Select(x => x == FreeVariable
|
---|
319 | ? xvalues
|
---|
320 | : Enumerable.Repeat(sharedFixedVariables.GetDoubleValue(x, 0), xvalues.Count).ToList()
|
---|
321 | )
|
---|
322 | );
|
---|
323 | }
|
---|
324 |
|
---|
325 | private Tuple<Series, Series> CreateSeries(IRegressionSolution solution) {
|
---|
326 | var series = new Series {
|
---|
327 | ChartType = SeriesChartType.Line,
|
---|
328 | Name = solution.ProblemData.TargetVariable + " " + solutions.IndexOf(solution)
|
---|
329 | };
|
---|
330 | series.LegendText = series.Name;
|
---|
331 |
|
---|
332 | var confidenceBoundSolution = solution as IConfidenceBoundRegressionSolution;
|
---|
333 | Series confidenceIntervalSeries = null;
|
---|
334 | if (confidenceBoundSolution != null) {
|
---|
335 | confidenceIntervalSeries = new Series {
|
---|
336 | ChartType = SeriesChartType.Range,
|
---|
337 | YValuesPerPoint = 2,
|
---|
338 | Name = "95% Conf. Interval " + series.Name,
|
---|
339 | IsVisibleInLegend = false
|
---|
340 | };
|
---|
341 | }
|
---|
342 | return Tuple.Create(series, confidenceIntervalSeries);
|
---|
343 | }
|
---|
344 |
|
---|
345 | private void OrderAndColorSeries() {
|
---|
346 | chart.SuspendRepaint();
|
---|
347 |
|
---|
348 | chart.Series.Clear();
|
---|
349 | // Add mean series for applying palette colors
|
---|
350 | foreach (var solution in solutions) {
|
---|
351 | chart.Series.Add(seriesCache[solution]);
|
---|
352 | }
|
---|
353 |
|
---|
354 | chart.Palette = ChartColorPalette.BrightPastel;
|
---|
355 | chart.ApplyPaletteColors();
|
---|
356 | chart.Palette = ChartColorPalette.None;
|
---|
357 |
|
---|
358 | // Add confidence interval series before its coresponding series for correct z index
|
---|
359 | foreach (var solution in solutions) {
|
---|
360 | Series ciSeries;
|
---|
361 | if (ciSeriesCache.TryGetValue(solution, out ciSeries)) {
|
---|
362 | var series = seriesCache[solution];
|
---|
363 | ciSeries.Color = Color.FromArgb(40, series.Color);
|
---|
364 | int idx = chart.Series.IndexOf(seriesCache[solution]);
|
---|
365 | chart.Series.Insert(idx, ciSeries);
|
---|
366 | }
|
---|
367 | }
|
---|
368 |
|
---|
369 | chart.ResumeRepaint(true);
|
---|
370 | }
|
---|
371 |
|
---|
372 | private async Task<DoubleLimit> UpdateAllSeriesDataAsync(CancellationToken cancellationToken) {
|
---|
373 | var updateTasks = solutions.Select(solution => UpdateSeriesDataAsync(solution, cancellationToken));
|
---|
374 |
|
---|
375 | double min = double.MaxValue, max = double.MinValue;
|
---|
376 | foreach (var update in updateTasks) {
|
---|
377 | var limit = await update;
|
---|
378 | if (limit.Lower < min) min = limit.Lower;
|
---|
379 | if (limit.Upper > max) max = limit.Upper;
|
---|
380 | }
|
---|
381 |
|
---|
382 | return new DoubleLimit(min, max);
|
---|
383 | }
|
---|
384 |
|
---|
385 | private Task<DoubleLimit> UpdateSeriesDataAsync(IRegressionSolution solution, CancellationToken cancellationToken) {
|
---|
386 | return Task.Run(() => {
|
---|
387 | var xvalues = internalDataset.GetDoubleValues(FreeVariable).ToList();
|
---|
388 | var yvalues = solution.Model.GetEstimatedValues(internalDataset, Enumerable.Range(0, internalDataset.Rows)).ToList();
|
---|
389 |
|
---|
390 | double min = double.MaxValue, max = double.MinValue;
|
---|
391 |
|
---|
392 | var series = seriesCache[solution];
|
---|
393 | for (int i = 0; i < xvalues.Count; i++) {
|
---|
394 | series.Points[i].SetValueXY(xvalues[i], yvalues[i]);
|
---|
395 | if (yvalues[i] < min) min = yvalues[i];
|
---|
396 | if (yvalues[i] > max) max = yvalues[i];
|
---|
397 | }
|
---|
398 |
|
---|
399 | var confidenceBoundSolution = solution as IConfidenceBoundRegressionSolution;
|
---|
400 | if (confidenceBoundSolution != null) {
|
---|
401 | var confidenceIntervalSeries = ciSeriesCache[solution];
|
---|
402 |
|
---|
403 | cancellationToken.ThrowIfCancellationRequested();
|
---|
404 | var variances =
|
---|
405 | confidenceBoundSolution.Model.GetEstimatedVariances(internalDataset,
|
---|
406 | Enumerable.Range(0, internalDataset.Rows)).ToList();
|
---|
407 | for (int i = 0; i < xvalues.Count; i++) {
|
---|
408 | var lower = yvalues[i] - 1.96 * Math.Sqrt(variances[i]);
|
---|
409 | var upper = yvalues[i] + 1.96 * Math.Sqrt(variances[i]);
|
---|
410 | confidenceIntervalSeries.Points[i].SetValueXY(xvalues[i], lower, upper);
|
---|
411 | if (lower < min) min = lower;
|
---|
412 | if (upper > max) max = upper;
|
---|
413 | }
|
---|
414 | }
|
---|
415 |
|
---|
416 | cancellationToken.ThrowIfCancellationRequested();
|
---|
417 | return new DoubleLimit(min, max);
|
---|
418 | }, cancellationToken);
|
---|
419 | }
|
---|
420 |
|
---|
421 | private void ResizeAllSeriesData() {
|
---|
422 | if (internalDataset == null)
|
---|
423 | return;
|
---|
424 |
|
---|
425 | var xvalues = internalDataset.GetDoubleValues(FreeVariable).ToList();
|
---|
426 | foreach (var solution in solutions)
|
---|
427 | ResizeSeriesData(solution, xvalues);
|
---|
428 | }
|
---|
429 | private void ResizeSeriesData(IRegressionSolution solution, IList<double> xvalues = null) {
|
---|
430 | if (xvalues == null)
|
---|
431 | xvalues = internalDataset.GetDoubleValues(FreeVariable).ToList();
|
---|
432 |
|
---|
433 | var series = seriesCache[solution];
|
---|
434 | series.Points.SuspendUpdates();
|
---|
435 | for (int i = 0; i < xvalues.Count; i++)
|
---|
436 | series.Points.Add(new DataPoint(xvalues[i], 0.0));
|
---|
437 | series.Points.ResumeUpdates();
|
---|
438 |
|
---|
439 | Series confidenceIntervalSeries;
|
---|
440 | if (ciSeriesCache.TryGetValue(solution, out confidenceIntervalSeries)) {
|
---|
441 | confidenceIntervalSeries.Points.SuspendUpdates();
|
---|
442 | for (int i = 0; i < xvalues.Count; i++)
|
---|
443 | confidenceIntervalSeries.Points.Add(new DataPoint(xvalues[i], new[] { -1.0, 1.0 }));
|
---|
444 | confidenceIntervalSeries.Points.ResumeUpdates();
|
---|
445 | }
|
---|
446 | }
|
---|
447 |
|
---|
448 | public async Task AddSolutionAsync(IRegressionSolution solution) {
|
---|
449 | if (!SolutionsCompatible(solutions.Concat(new[] { solution })))
|
---|
450 | throw new ArgumentException("The solution is not compatible with the problem data.");
|
---|
451 | if (solutions.Contains(solution))
|
---|
452 | return;
|
---|
453 |
|
---|
454 | solutions.Add(solution);
|
---|
455 | RecalculateTrainingLimits(true);
|
---|
456 |
|
---|
457 | var series = CreateSeries(solution);
|
---|
458 | seriesCache.Add(solution, series.Item1);
|
---|
459 | if (series.Item2 != null)
|
---|
460 | ciSeriesCache.Add(solution, series.Item2);
|
---|
461 |
|
---|
462 | ResizeSeriesData(solution);
|
---|
463 | OrderAndColorSeries();
|
---|
464 |
|
---|
465 | await RecalculateAsync();
|
---|
466 | }
|
---|
467 | public async Task RemoveSolutionAsync(IRegressionSolution solution) {
|
---|
468 | if (!solutions.Remove(solution))
|
---|
469 | return;
|
---|
470 |
|
---|
471 | RecalculateTrainingLimits(true);
|
---|
472 |
|
---|
473 | seriesCache.Remove(solution);
|
---|
474 | ciSeriesCache.Remove(solution);
|
---|
475 |
|
---|
476 | await RecalculateAsync();
|
---|
477 | }
|
---|
478 |
|
---|
479 | private static bool SolutionsCompatible(IEnumerable<IRegressionSolution> solutions) {
|
---|
480 | foreach (var solution1 in solutions) {
|
---|
481 | var variables1 = solution1.ProblemData.Dataset.DoubleVariables;
|
---|
482 | foreach (var solution2 in solutions) {
|
---|
483 | if (solution1 == solution2)
|
---|
484 | continue;
|
---|
485 | var variables2 = solution2.ProblemData.Dataset.DoubleVariables;
|
---|
486 | if (!variables1.All(variables2.Contains))
|
---|
487 | return false;
|
---|
488 | }
|
---|
489 | }
|
---|
490 | return true;
|
---|
491 | }
|
---|
492 |
|
---|
493 | private void UpdateOutOfTrainingRangeStripLines() {
|
---|
494 | var axisX = chart.ChartAreas[0].AxisX;
|
---|
495 | var lowerStripLine = axisX.StripLines[0];
|
---|
496 | var upperStripLine = axisX.StripLines[1];
|
---|
497 |
|
---|
498 | lowerStripLine.IntervalOffset = axisX.Minimum;
|
---|
499 | lowerStripLine.StripWidth = trainingMin - axisX.Minimum;
|
---|
500 |
|
---|
501 | upperStripLine.IntervalOffset = trainingMax;
|
---|
502 | upperStripLine.StripWidth = axisX.Maximum - trainingMax;
|
---|
503 | }
|
---|
504 |
|
---|
505 | #region Events
|
---|
506 | public event EventHandler VariableValueChanged;
|
---|
507 | public void OnVariableValueChanged(object sender, EventArgs args) {
|
---|
508 | var changed = VariableValueChanged;
|
---|
509 | if (changed == null) return;
|
---|
510 | changed(sender, args);
|
---|
511 | }
|
---|
512 |
|
---|
513 | private void sharedFixedVariables_ItemChanged(object o, EventArgs<int, int> e) {
|
---|
514 | if (o != sharedFixedVariables) return;
|
---|
515 | var variables = sharedFixedVariables.DoubleVariables.ToList();
|
---|
516 | var rowIndex = e.Value;
|
---|
517 | var columnIndex = e.Value2;
|
---|
518 |
|
---|
519 | var variableName = variables[columnIndex];
|
---|
520 | if (variableName == FreeVariable) return;
|
---|
521 | var v = sharedFixedVariables.GetDoubleValue(variableName, rowIndex);
|
---|
522 | var values = new List<double>(Enumerable.Repeat(v, DrawingSteps));
|
---|
523 | internalDataset.ReplaceVariable(variableName, values);
|
---|
524 | }
|
---|
525 |
|
---|
526 | private double oldCurserPosition = double.NaN;
|
---|
527 | private void chart_AnnotationPositionChanging(object sender, AnnotationPositionChangingEventArgs e) {
|
---|
528 | var step = (trainingMax - trainingMin) / drawingSteps;
|
---|
529 | double newLocation = step * (long)Math.Round(e.NewLocationX / step);
|
---|
530 | var axisX = chart.ChartAreas[0].AxisX;
|
---|
531 | if (newLocation > axisX.Maximum)
|
---|
532 | newLocation = axisX.Maximum;
|
---|
533 | if (newLocation < axisX.Minimum)
|
---|
534 | newLocation = axisX.Minimum;
|
---|
535 |
|
---|
536 | e.NewLocationX = newLocation;
|
---|
537 |
|
---|
538 | var annotation = VerticalLineAnnotation;
|
---|
539 | var x = annotation.X;
|
---|
540 | sharedFixedVariables.SetVariableValue(x, FreeVariable, 0);
|
---|
541 |
|
---|
542 | chart.ChartAreas[0].AxisX.Title = FreeVariable + " : " + x.ToString("N3", CultureInfo.CurrentCulture);
|
---|
543 | chart.Update();
|
---|
544 |
|
---|
545 | if (!oldCurserPosition.IsAlmost(e.NewLocationX))
|
---|
546 | OnVariableValueChanged(this, EventArgs.Empty);
|
---|
547 | oldCurserPosition = e.NewLocationX;
|
---|
548 | }
|
---|
549 |
|
---|
550 | private void chart_MouseMove(object sender, MouseEventArgs e) {
|
---|
551 | bool hitCursor = chart.HitTest(e.X, e.Y).ChartElementType == ChartElementType.Annotation;
|
---|
552 | chart.Cursor = hitCursor ? Cursors.VSplit : Cursors.Default;
|
---|
553 | }
|
---|
554 |
|
---|
555 | private void chart_FormatNumber(object sender, FormatNumberEventArgs e) {
|
---|
556 | if (e.ElementType == ChartElementType.AxisLabels) {
|
---|
557 | switch (e.Format) {
|
---|
558 | case "CustomAxisXFormat":
|
---|
559 | break;
|
---|
560 | case "CustomAxisYFormat":
|
---|
561 | var v = e.Value;
|
---|
562 | e.LocalizedValue = string.Format("{0,5}", v);
|
---|
563 | break;
|
---|
564 | default:
|
---|
565 | break;
|
---|
566 | }
|
---|
567 | }
|
---|
568 | }
|
---|
569 |
|
---|
570 | private void chart_DragDrop(object sender, DragEventArgs e) {
|
---|
571 | var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
|
---|
572 | if (data != null) {
|
---|
573 | var solution = data as IRegressionSolution;
|
---|
574 | if (!solutions.Contains(solution))
|
---|
575 | AddSolutionAsync(solution);
|
---|
576 | }
|
---|
577 | }
|
---|
578 | private void chart_DragEnter(object sender, DragEventArgs e) {
|
---|
579 | if (!e.Data.GetDataPresent(HeuristicLab.Common.Constants.DragDropDataFormat)) return;
|
---|
580 | e.Effect = DragDropEffects.None;
|
---|
581 |
|
---|
582 | var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
|
---|
583 | var regressionSolution = data as IRegressionSolution;
|
---|
584 | if (regressionSolution != null) {
|
---|
585 | e.Effect = DragDropEffects.Copy;
|
---|
586 | }
|
---|
587 | }
|
---|
588 | #endregion
|
---|
589 | }
|
---|
590 | }
|
---|