Free cookie consent management tool by TermsFeed Policy Generator

source: branches/crossvalidation-2434/HeuristicLab.Algorithms.DataAnalysis.Views/3.4/CrossValidationView.cs @ 13578

Last change on this file since 13578 was 12821, checked in by gkronber, 9 years ago

#2434 fixed problems in persistence and cloning and 'data binding' between content and view

File size: 19.7 KB
RevLine 
[5617]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[5617]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;
[5850]23using System.Collections.Generic;
[5617]24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Core.Views;
29using HeuristicLab.MainForm;
30using HeuristicLab.Optimization;
31using HeuristicLab.PluginInfrastructure;
32using HeuristicLab.Problems.DataAnalysis;
33
34namespace HeuristicLab.Algorithms.DataAnalysis.Views {
[5834]35  [View("CrossValidation View")]
[5617]36  [Content(typeof(CrossValidation), true)]
37  public sealed partial class CrossValidationView : NamedItemView {
38    private TypeSelectorDialog algorithmTypeSelectorDialog;
39    private TypeSelectorDialog problemTypeSelectorDialog;
40
41    public CrossValidationView() {
42      InitializeComponent();
43    }
44
45    public new CrossValidation Content {
46      get { return (CrossValidation)base.Content; }
47      set { base.Content = value; }
48    }
49
50    protected override void OnContentChanged() {
51      base.OnContentChanged();
52      if (Content == null) {
53        workersNumericUpDown.Value = 1;
54        foldsNumericUpDown.Value = 2;
55        samplesStartStringConvertibleValueView.Content = null;
56        samplesEndStringConvertibleValueView.Content = null;
57        algorithmNamedItemView.Content = null;
58        algorithmProblemViewHost.Content = null;
59        algorithmParameterCollectionView.Content = null;
60        resultCollectionView.Content = null;
61        runCollectionView.Content = null;
62        storeAlgorithmInEachRunCheckBox.Checked = true;
63      } else {
[12821]64        disableUpdateToContent = true; // don't propagate changes from GUI elements back to the content
[5617]65        Locked = ReadOnly = Content.ExecutionState == ExecutionState.Started;
66        workersNumericUpDown.Value = Content.NumberOfWorkers.Value;
67        samplesStartStringConvertibleValueView.Content = Content.SamplesStart;
68        samplesEndStringConvertibleValueView.Content = Content.SamplesEnd;
69        UpdateAlgorithmView();
70        UpdateProblemView();
[12821]71        UpdateFolds();
[5617]72        runCollectionView.Content = Content.Runs;
73        algorithmParameterCollectionView.Content = ((IParameterizedNamedItem)Content).Parameters;
74        resultCollectionView.Content = Content.Results;
75        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
76        storeAlgorithmInEachRunCheckBox.Checked = Content.StoreAlgorithmInEachRun;
[12821]77        disableUpdateToContent = false;
[5617]78      }
79    }
80
81    protected override void RegisterContentEvents() {
82      base.RegisterContentEvents();
83      Content.AlgorithmChanged += new EventHandler(Content_AlgorithmChanged);
84      Content.ProblemChanged += new EventHandler(Content_ProblemChanged);
[12800]85      if (Content.Problem != null) Content.Problem.Reset += Content_ProblemReset;
86      Content.FoldsChanged += Content_FoldsChanged;
[5617]87      Content.NumberOfWorkers.ValueChanged += new EventHandler(Content_NumberOfWorker_ValueChanged);
88
89      Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
90      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
91      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
92      Content.StoreAlgorithmInEachRunChanged += new EventHandler(Content_StoreAlgorithmInEachRunChanged);
93    }
94
95    protected override void DeregisterContentEvents() {
96      Content.AlgorithmChanged -= new EventHandler(Content_AlgorithmChanged);
97      Content.ProblemChanged -= new EventHandler(Content_ProblemChanged);
[12800]98      if (Content.Problem != null) Content.Problem.Reset -= Content_ProblemReset;
99      Content.FoldsChanged -= Content_FoldsChanged;
[5617]100      Content.NumberOfWorkers.ValueChanged -= new EventHandler(Content_NumberOfWorker_ValueChanged);
101
102      Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
103      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
104      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
105      Content.StoreAlgorithmInEachRunChanged -= new EventHandler(Content_StoreAlgorithmInEachRunChanged);
106      base.DeregisterContentEvents();
107    }
108
109    protected override void OnClosed(FormClosedEventArgs e) {
110      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) {
111        //The content must be stopped if no other view showing the content is available
112        var optimizers = MainFormManager.MainForm.Views.OfType<IContentView>().Where(v => v != this).Select(v => v.Content).OfType<IOptimizer>();
113        if (!optimizers.Contains(Content)) {
114          var nestedOptimizers = optimizers.SelectMany(opt => opt.NestedOptimizers);
115          if (!nestedOptimizers.Contains(Content)) Content.Stop();
116        }
117      }
118      base.OnClosed(e);
119    }
120
121    protected override void SetEnabledStateOfControls() {
122      if (InvokeRequired) Invoke((Action)SetEnabledStateOfControls);
123      else {
124        base.SetEnabledStateOfControls();
125        this.Enabled = Content != null;
126
127        if (Content != null) {
128          storeAlgorithmInEachRunCheckBox.Enabled = !ReadOnly;
[8830]129          openAlgorithmButton.Enabled = !ReadOnly;
130          newAlgorithmButton.Enabled = !ReadOnly;
[5617]131
132          algorithmNamedItemView.Enabled = Content.Algorithm != null && (Content.ExecutionState == ExecutionState.Prepared || Content.ExecutionState == ExecutionState.Stopped);
133          algorithmTabControl.Enabled = Content.Algorithm != null && (Content.ExecutionState == ExecutionState.Prepared || Content.ExecutionState == ExecutionState.Stopped);
134          foldsNumericUpDown.Enabled = Content.ExecutionState == ExecutionState.Prepared;
135          samplesStartStringConvertibleValueView.Enabled = Content.ExecutionState == ExecutionState.Prepared;
136          samplesEndStringConvertibleValueView.Enabled = Content.ExecutionState == ExecutionState.Prepared;
137          workersNumericUpDown.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
[12800]138          partitionVariableLabel.Enabled = (Content.ExecutionState == ExecutionState.Prepared);
139          partitionVariableComboBox.Enabled = (Content.ExecutionState == ExecutionState.Prepared);
[5617]140
141          startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
142          pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
143          stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
144          resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
145        }
146      }
147    }
148
149    #region Content Events
150    private void Content_AlgorithmChanged(object sender, EventArgs e) {
[12821]151      disableUpdateToContent = true;
[5617]152      UpdateAlgorithmView();
[12800]153      UpdateFolds();
[5617]154      UpdateProblemView();
155      SetEnabledStateOfControls();
[12821]156      disableUpdateToContent = false;
[5617]157    }
158    private void UpdateAlgorithmView() {
[12800]159      if (InvokeRequired) Invoke((Action)UpdateAlgorithmView);
160      else {
161        algorithmNamedItemView.Content = Content.Algorithm;
162        UpdateProblemView();
163      }
[5617]164    }
165
166    private void Content_ProblemChanged(object sender, EventArgs e) {
[12800]167      if (Content.Problem != null) Content.Problem.Reset += Content_ProblemReset;
[12821]168      disableUpdateToContent = true;
[12800]169      UpdateFolds();
[5617]170      UpdateProblemView();
171      SetEnabledStateOfControls();
[12821]172      disableUpdateToContent = false;
[5617]173    }
[12800]174
175    private void Content_ProblemReset(object sender, EventArgs e) {
[12821]176      disableUpdateToContent = true;
[12800]177      UpdateFolds();
[12821]178      disableUpdateToContent = false;
[12800]179    }
180
181
[5617]182    private void UpdateProblemView() {
[12800]183      if (InvokeRequired) Invoke((Action)UpdateProblemView);
184      else {
185        algorithmProblemViewHost.Content = Content.Problem;
186      }
[5617]187    }
188
[12800]189    private void Content_FoldsChanged(object sender, EventArgs e) {
[12821]190      disableUpdateToContent = true;
[12800]191      UpdateFolds();
[12821]192      disableUpdateToContent = false;
[5617]193    }
[12800]194
195    private bool disableUpdateToContent = false; // to prevent changing Content.Folds or Content.PartitionVariable while updating the GUI from the content
196    private void UpdateFolds() {
197      if (InvokeRequired) Invoke((Action)UpdateFolds);
198      else {
199        disableUpdateToContent = true;
200        foldsNumericUpDown.Value = Content.Folds;
201
202        partitionVariableComboBox.BeginUpdate();
203        partitionVariableComboBox.Items.Clear();
204        partitionVariableComboBox.Items.Add(CrossValidation.NoPartitionVariable);
[12821]205        if (Content.Problem != null) {
206          var ds = Content.Problem.ProblemData.Dataset;
207          var rows = Enumerable.Range(Content.SamplesStart.Value, Content.SamplesEnd.Value - Content.SamplesStart.Value);
208          foreach (var variable in Content.Problem.ProblemData.Dataset.DoubleVariables) {
209            if (Content.Problem.ProblemData.AllowedInputVariables.Contains(variable)) continue;
210            int distinctValues = ds.GetDoubleValues(variable, rows).Distinct().Count();
211            if (distinctValues < 2 || distinctValues > 20) continue;
212            partitionVariableComboBox.Items.Add(variable);
213          }
[12800]214        }
215        partitionVariableComboBox.SelectedItem = Content.PartitionVariable;
216        partitionVariableComboBox.EndUpdate();
217        disableUpdateToContent = false;
218      }
219    }
220
[5617]221    private void Content_NumberOfWorker_ValueChanged(object sender, EventArgs e) {
222      workersNumericUpDown.Value = Content.NumberOfWorkers.Value;
223    }
224
225    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
226      if (InvokeRequired)
227        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
228      else {
229        Locked = ReadOnly = Content.ExecutionState == ExecutionState.Started;
230        SetEnabledStateOfControls();
231      }
232    }
233
234    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
235      if (InvokeRequired)
236        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
237      else
238        executionTimeTextBox.Text = Content == null ? "-" : Content.ExecutionTime.ToString();
239    }
240    private void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
241      if (InvokeRequired)
242        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
243      else
244        ErrorHandling.ShowErrorDialog(this, e.Value);
245    }
246    private void Content_StoreAlgorithmInEachRunChanged(object sender, EventArgs e) {
247      if (InvokeRequired)
248        Invoke(new EventHandler(Content_StoreAlgorithmInEachRunChanged), sender, e);
249      else
250        storeAlgorithmInEachRunCheckBox.Checked = Content.StoreAlgorithmInEachRun;
251    }
252    #endregion
253
254    #region GUI events
255    private void foldsNumericUpDown_Validated(object sender, EventArgs e) {
256      if (foldsNumericUpDown.Text == string.Empty)
257        foldsNumericUpDown.Text = foldsNumericUpDown.Value.ToString();
258    }
259    private void foldsNumericUpDown_ValueChanged(object sender, EventArgs e) {
[12800]260      if (Content != null && !disableUpdateToContent) {
261        Content.Folds = (int)foldsNumericUpDown.Value;
262      }
[5617]263    }
264
265    private void workersNumericUpDown_Validated(object sender, EventArgs e) {
266      if (workersNumericUpDown.Text == string.Empty)
267        workersNumericUpDown.Text = workersNumericUpDown.Value.ToString();
268    }
269    private void workersNumericUpDown_ValueChanged(object sender, EventArgs e) {
[12821]270      if (Content != null && !disableUpdateToContent)
[5617]271        Content.NumberOfWorkers.Value = (int)workersNumericUpDown.Value;
272    }
273
274    private void startButton_Click(object sender, EventArgs e) {
275      Content.Start();
276    }
277    private void pauseButton_Click(object sender, EventArgs e) {
278      Content.Pause();
279    }
280    private void stopButton_Click(object sender, EventArgs e) {
281      Content.Stop();
282    }
283    private void resetButton_Click(object sender, EventArgs e) {
284      Content.Prepare(false);
285    }
286
287    private void newAlgorithmButton_Click(object sender, EventArgs e) {
288      if (algorithmTypeSelectorDialog == null) {
289        algorithmTypeSelectorDialog = new TypeSelectorDialog();
290        algorithmTypeSelectorDialog.Caption = "Select Algorithm";
291        algorithmTypeSelectorDialog.TypeSelector.Caption = "Available Algorithms";
292        algorithmTypeSelectorDialog.TypeSelector.Configure(typeof(IAlgorithm), false, true);
293      }
294      if (algorithmTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
295        try {
296          Content.Algorithm = (IAlgorithm)algorithmTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
[12800]297        } catch (Exception ex) {
[5617]298          ErrorHandling.ShowErrorDialog(this, ex);
299        }
300      }
301    }
302
303    private void openAlgorithmButton_Click(object sender, EventArgs e) {
304      openFileDialog.Title = "Open Algorithm";
305      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
306        algorithmTabControl.Enabled = false;
307
308        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
309          try {
310            if (error != null) throw error;
311            IAlgorithm algorithm = content as IAlgorithm;
[5850]312            if (algorithm == null || !(algorithm.Problem is IDataAnalysisProblem))
[5617]313              MessageBox.Show(this, "The selected file does not contain an algorithm or the problem of the algorithm is not a DataAnalysisProblem.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
314            else
315              Content.Algorithm = algorithm;
[12800]316          } catch (Exception ex) {
[5617]317            ErrorHandling.ShowErrorDialog(this, ex);
[12800]318          } finally {
[5617]319            Invoke(new Action(delegate() {
320              algorithmTabControl.Enabled = true;
321            }));
322          }
323        });
324      }
325    }
326
327    private void newProblemButton_Click(object sender, EventArgs e) {
328      if (problemTypeSelectorDialog == null) {
329        problemTypeSelectorDialog = new TypeSelectorDialog();
330        problemTypeSelectorDialog.Caption = "Select Problem";
331        problemTypeSelectorDialog.TypeSelector.Caption = "Available Problems";
332      }
[5898]333      problemTypeSelectorDialog.TypeSelector.Configure(new List<Type>() { Content.ProblemType, Content.Algorithm.ProblemType }, false, true, true);
[5617]334      if (problemTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
[5850]335        Content.Problem = (IDataAnalysisProblem)problemTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
[5617]336      }
337    }
338
339    private void openProblemButton_Click(object sender, EventArgs e) {
340      openFileDialog.Title = "Open Problem";
341      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
342        newProblemButton.Enabled = openProblemButton.Enabled = false;
343        algorithmProblemViewHost.Enabled = false;
344
345        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
346          try {
347            if (error != null) throw error;
[5772]348            IDataAnalysisProblem problem = content as IDataAnalysisProblem;
[5617]349            if (problem == null && (Content.Algorithm.ProblemType.IsAssignableFrom(content.GetType())))
350              Invoke(new Action(() =>
351                MessageBox.Show(this, "The selected file does not contain a DataAnalysisProblem problem.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error)));
352            else
353              Content.Problem = problem;
[12800]354          } catch (Exception ex) {
[5617]355            Invoke(new Action(() => ErrorHandling.ShowErrorDialog(this, ex)));
[12800]356          } finally {
[5617]357            Invoke(new Action(delegate() {
358              algorithmProblemViewHost.Enabled = true;
359              newProblemButton.Enabled = openProblemButton.Enabled = true;
360            }));
361          }
362        });
363      }
364    }
365
366    private void algorithmTabPage_DragEnterOver(object sender, DragEventArgs e) {
367      e.Effect = DragDropEffects.None;
[5837]368      IAlgorithm algorithm = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IAlgorithm;
[5813]369      if (!ReadOnly && algorithm != null &&
[5834]370        (algorithm.ProblemType != null || Content.ProblemType.IsAssignableFrom(algorithm.Problem.GetType()))) {
[5617]371        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
372        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
[5813]373        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
374        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
375        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
[5617]376      }
377    }
378    private void algorithmTabPage_DragDrop(object sender, DragEventArgs e) {
379      if (e.Effect != DragDropEffects.None) {
[5837]380        IAlgorithm algorithm = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IAlgorithm;
[5617]381        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) algorithm = (IAlgorithm)algorithm.Clone();
382        Content.Algorithm = algorithm;
383      }
384    }
385
386    private void algorithmProblemTabPage_DragEnterOver(object sender, DragEventArgs e) {
387      e.Effect = DragDropEffects.None;
388      if (ReadOnly) return;
[5837]389      IProblem problem = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IProblem;
[5813]390      if (problem != null && Content.ProblemType.IsAssignableFrom(problem.GetType()) &&
391        Content.Algorithm.ProblemType.IsAssignableFrom(problem.GetType())) {
[5617]392        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
393        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
394        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
395        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
396        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
397      }
398    }
399    private void algorithmProblemTabPage_DragDrop(object sender, DragEventArgs e) {
400      if (e.Effect != DragDropEffects.None) {
[5837]401        IDataAnalysisProblem problem = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IDataAnalysisProblem;
[5813]402        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) problem = (IDataAnalysisProblem)problem.Clone();
[5617]403        Content.Problem = problem;
404      }
405    }
406
407    private void storeAlgorithmInEachRunCheckBox_CheckedChanged(object sender, EventArgs e) {
408      if (Content != null) Content.StoreAlgorithmInEachRun = storeAlgorithmInEachRunCheckBox.Checked;
409    }
410    #endregion
411
[12800]412    private void partitionVariableComboBox_SelectionChangeCommitted(object sender, EventArgs e) {
413      // when the user changes the selection in the GUI
414      if (Content != null && !disableUpdateToContent)
415        Content.PartitionVariable = (string)partitionVariableComboBox.SelectedItem;
416    }
[5617]417  }
418}
Note: See TracBrowser for help on using the repository browser.