Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Views/3.4/Controls/GradientChart.cs @ 14118

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

#2597:

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