Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.DataAnalysis.Views/3.4/CrossValidationView.cs @ 18199

Last change on this file since 18199 was 17468, checked in by jkarder, 5 years ago

#3060: merged r17445 into stable

File size: 17.5 KB
RevLine 
[5617]1#region License Information
2/* HeuristicLab
[17181]3 * Copyright (C) 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;
[15150]63        shuffleSamplesCheckBox.Checked = false;
[5617]64      } else {
65        Locked = ReadOnly = Content.ExecutionState == ExecutionState.Started;
66        workersNumericUpDown.Value = Content.NumberOfWorkers.Value;
67        foldsNumericUpDown.Value = Content.Folds.Value;
68        samplesStartStringConvertibleValueView.Content = Content.SamplesStart;
69        samplesEndStringConvertibleValueView.Content = Content.SamplesEnd;
70        UpdateAlgorithmView();
71        UpdateProblemView();
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;
[15150]77        shuffleSamplesCheckBox.Checked = Content.ShuffleSamples.Value;
[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);
85      Content.Folds.ValueChanged += new EventHandler(Content_Folds_ValueChanged);
86      Content.NumberOfWorkers.ValueChanged += new EventHandler(Content_NumberOfWorker_ValueChanged);
87
88      Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
89      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
90      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
91      Content.StoreAlgorithmInEachRunChanged += new EventHandler(Content_StoreAlgorithmInEachRunChanged);
92    }
93
94    protected override void DeregisterContentEvents() {
95      Content.AlgorithmChanged -= new EventHandler(Content_AlgorithmChanged);
96      Content.ProblemChanged -= new EventHandler(Content_ProblemChanged);
97      Content.Folds.ValueChanged -= new EventHandler(Content_Folds_ValueChanged);
98      Content.NumberOfWorkers.ValueChanged -= new EventHandler(Content_NumberOfWorker_ValueChanged);
99
100      Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
101      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
102      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
103      Content.StoreAlgorithmInEachRunChanged -= new EventHandler(Content_StoreAlgorithmInEachRunChanged);
104      base.DeregisterContentEvents();
105    }
106
107    protected override void OnClosed(FormClosedEventArgs e) {
108      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) {
109        //The content must be stopped if no other view showing the content is available
110        var optimizers = MainFormManager.MainForm.Views.OfType<IContentView>().Where(v => v != this).Select(v => v.Content).OfType<IOptimizer>();
111        if (!optimizers.Contains(Content)) {
112          var nestedOptimizers = optimizers.SelectMany(opt => opt.NestedOptimizers);
113          if (!nestedOptimizers.Contains(Content)) Content.Stop();
114        }
115      }
116      base.OnClosed(e);
117    }
118
119    protected override void SetEnabledStateOfControls() {
120      if (InvokeRequired) Invoke((Action)SetEnabledStateOfControls);
121      else {
122        base.SetEnabledStateOfControls();
123        this.Enabled = Content != null;
124
125        if (Content != null) {
126          storeAlgorithmInEachRunCheckBox.Enabled = !ReadOnly;
[8830]127          openAlgorithmButton.Enabled = !ReadOnly;
128          newAlgorithmButton.Enabled = !ReadOnly;
[5617]129
130          algorithmNamedItemView.Enabled = Content.Algorithm != null && (Content.ExecutionState == ExecutionState.Prepared || Content.ExecutionState == ExecutionState.Stopped);
131          algorithmTabControl.Enabled = Content.Algorithm != null && (Content.ExecutionState == ExecutionState.Prepared || Content.ExecutionState == ExecutionState.Stopped);
132          foldsNumericUpDown.Enabled = Content.ExecutionState == ExecutionState.Prepared;
133          samplesStartStringConvertibleValueView.Enabled = Content.ExecutionState == ExecutionState.Prepared;
134          samplesEndStringConvertibleValueView.Enabled = Content.ExecutionState == ExecutionState.Prepared;
135          workersNumericUpDown.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
136
137          startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
138          pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
139          stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
140          resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
[15150]141          // prevent changing the shuffle if the algorithm is not finished
142          shuffleSamplesCheckBox.Enabled = Content.ExecutionState == ExecutionState.Prepared;
[5617]143        }
144      }
145    }
146
147    #region Content Events
148    private void Content_AlgorithmChanged(object sender, EventArgs e) {
149      UpdateAlgorithmView();
150      UpdateProblemView();
151      SetEnabledStateOfControls();
152    }
153    private void UpdateAlgorithmView() {
154      algorithmNamedItemView.Content = Content.Algorithm;
155      UpdateProblemView();
156    }
157
158    private void Content_ProblemChanged(object sender, EventArgs e) {
159      UpdateProblemView();
160      SetEnabledStateOfControls();
161    }
162    private void UpdateProblemView() {
163      algorithmProblemViewHost.Content = Content.Problem;
164    }
165
166    private void Content_Folds_ValueChanged(object sender, EventArgs e) {
167      foldsNumericUpDown.Value = Content.Folds.Value;
168    }
169    private void Content_NumberOfWorker_ValueChanged(object sender, EventArgs e) {
170      workersNumericUpDown.Value = Content.NumberOfWorkers.Value;
171    }
172
173    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
174      if (InvokeRequired)
175        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
176      else {
177        Locked = ReadOnly = Content.ExecutionState == ExecutionState.Started;
178        SetEnabledStateOfControls();
179      }
180    }
181
182    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
183      if (InvokeRequired)
184        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
185      else
186        executionTimeTextBox.Text = Content == null ? "-" : Content.ExecutionTime.ToString();
187    }
188    private void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
189      if (InvokeRequired)
190        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
191      else
192        ErrorHandling.ShowErrorDialog(this, e.Value);
193    }
194    private void Content_StoreAlgorithmInEachRunChanged(object sender, EventArgs e) {
195      if (InvokeRequired)
196        Invoke(new EventHandler(Content_StoreAlgorithmInEachRunChanged), sender, e);
197      else
198        storeAlgorithmInEachRunCheckBox.Checked = Content.StoreAlgorithmInEachRun;
199    }
200    #endregion
201
202    #region GUI events
[15150]203    private void shuffleSamplesCheckBox_CheckedChanged(object sender, EventArgs e) {
204      Content.ShuffleSamples.Value = shuffleSamplesCheckBox.Checked;
205    }
[5617]206    private void foldsNumericUpDown_Validated(object sender, EventArgs e) {
207      if (foldsNumericUpDown.Text == string.Empty)
208        foldsNumericUpDown.Text = foldsNumericUpDown.Value.ToString();
209    }
210    private void foldsNumericUpDown_ValueChanged(object sender, EventArgs e) {
211      if (Content != null)
212        Content.Folds.Value = (int)foldsNumericUpDown.Value;
213    }
214
215    private void workersNumericUpDown_Validated(object sender, EventArgs e) {
216      if (workersNumericUpDown.Text == string.Empty)
217        workersNumericUpDown.Text = workersNumericUpDown.Value.ToString();
218    }
219    private void workersNumericUpDown_ValueChanged(object sender, EventArgs e) {
220      if (Content != null)
221        Content.NumberOfWorkers.Value = (int)workersNumericUpDown.Value;
222    }
223
[15384]224    private async void startButton_Click(object sender, EventArgs e) {
[17468]225      try {
226        await Content.StartAsync();
227      } catch (Exception ex) {
228        ErrorHandling.ShowErrorDialog(this, ex);
229      }
[5617]230    }
231    private void pauseButton_Click(object sender, EventArgs e) {
232      Content.Pause();
233    }
234    private void stopButton_Click(object sender, EventArgs e) {
235      Content.Stop();
236    }
237    private void resetButton_Click(object sender, EventArgs e) {
238      Content.Prepare(false);
239    }
240
241    private void newAlgorithmButton_Click(object sender, EventArgs e) {
242      if (algorithmTypeSelectorDialog == null) {
243        algorithmTypeSelectorDialog = new TypeSelectorDialog();
244        algorithmTypeSelectorDialog.Caption = "Select Algorithm";
245        algorithmTypeSelectorDialog.TypeSelector.Caption = "Available Algorithms";
246        algorithmTypeSelectorDialog.TypeSelector.Configure(typeof(IAlgorithm), false, true);
247      }
248      if (algorithmTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
249        try {
250          Content.Algorithm = (IAlgorithm)algorithmTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
[15150]251        } catch (Exception ex) {
[5617]252          ErrorHandling.ShowErrorDialog(this, ex);
253        }
254      }
255    }
256
257    private void openAlgorithmButton_Click(object sender, EventArgs e) {
258      openFileDialog.Title = "Open Algorithm";
259      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
260        algorithmTabControl.Enabled = false;
261
[15150]262        ContentManager.LoadAsync(openFileDialog.FileName, delegate (IStorableContent content, Exception error) {
[5617]263          try {
264            if (error != null) throw error;
265            IAlgorithm algorithm = content as IAlgorithm;
[5850]266            if (algorithm == null || !(algorithm.Problem is IDataAnalysisProblem))
[5617]267              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);
268            else
269              Content.Algorithm = algorithm;
[15150]270          } catch (Exception ex) {
[5617]271            ErrorHandling.ShowErrorDialog(this, ex);
[15150]272          } finally {
273            Invoke(new Action(delegate () {
[5617]274              algorithmTabControl.Enabled = true;
275            }));
276          }
277        });
278      }
279    }
280
281    private void newProblemButton_Click(object sender, EventArgs e) {
282      if (problemTypeSelectorDialog == null) {
283        problemTypeSelectorDialog = new TypeSelectorDialog();
284        problemTypeSelectorDialog.Caption = "Select Problem";
285        problemTypeSelectorDialog.TypeSelector.Caption = "Available Problems";
286      }
[5898]287      problemTypeSelectorDialog.TypeSelector.Configure(new List<Type>() { Content.ProblemType, Content.Algorithm.ProblemType }, false, true, true);
[5617]288      if (problemTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
[5850]289        Content.Problem = (IDataAnalysisProblem)problemTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
[5617]290      }
291    }
292
293    private void openProblemButton_Click(object sender, EventArgs e) {
294      openFileDialog.Title = "Open Problem";
295      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
296        newProblemButton.Enabled = openProblemButton.Enabled = false;
297        algorithmProblemViewHost.Enabled = false;
298
[15150]299        ContentManager.LoadAsync(openFileDialog.FileName, delegate (IStorableContent content, Exception error) {
[5617]300          try {
301            if (error != null) throw error;
[5772]302            IDataAnalysisProblem problem = content as IDataAnalysisProblem;
[5617]303            if (problem == null && (Content.Algorithm.ProblemType.IsAssignableFrom(content.GetType())))
304              Invoke(new Action(() =>
305                MessageBox.Show(this, "The selected file does not contain a DataAnalysisProblem problem.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error)));
306            else
307              Content.Problem = problem;
[15150]308          } catch (Exception ex) {
[5617]309            Invoke(new Action(() => ErrorHandling.ShowErrorDialog(this, ex)));
[15150]310          } finally {
311            Invoke(new Action(delegate () {
[5617]312              algorithmProblemViewHost.Enabled = true;
313              newProblemButton.Enabled = openProblemButton.Enabled = true;
314            }));
315          }
316        });
317      }
318    }
319
320    private void algorithmTabPage_DragEnterOver(object sender, DragEventArgs e) {
321      e.Effect = DragDropEffects.None;
[5837]322      IAlgorithm algorithm = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IAlgorithm;
[5813]323      if (!ReadOnly && algorithm != null &&
[5834]324        (algorithm.ProblemType != null || Content.ProblemType.IsAssignableFrom(algorithm.Problem.GetType()))) {
[5617]325        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
326        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
[5813]327        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
328        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
329        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
[5617]330      }
331    }
332    private void algorithmTabPage_DragDrop(object sender, DragEventArgs e) {
333      if (e.Effect != DragDropEffects.None) {
[5837]334        IAlgorithm algorithm = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IAlgorithm;
[5617]335        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) algorithm = (IAlgorithm)algorithm.Clone();
336        Content.Algorithm = algorithm;
337      }
338    }
339
340    private void algorithmProblemTabPage_DragEnterOver(object sender, DragEventArgs e) {
341      e.Effect = DragDropEffects.None;
342      if (ReadOnly) return;
[5837]343      IProblem problem = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IProblem;
[5813]344      if (problem != null && Content.ProblemType.IsAssignableFrom(problem.GetType()) &&
345        Content.Algorithm.ProblemType.IsAssignableFrom(problem.GetType())) {
[5617]346        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
347        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
348        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
349        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
350        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
351      }
352    }
353    private void algorithmProblemTabPage_DragDrop(object sender, DragEventArgs e) {
354      if (e.Effect != DragDropEffects.None) {
[5837]355        IDataAnalysisProblem problem = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IDataAnalysisProblem;
[5813]356        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) problem = (IDataAnalysisProblem)problem.Clone();
[5617]357        Content.Problem = problem;
358      }
359    }
360
361    private void storeAlgorithmInEachRunCheckBox_CheckedChanged(object sender, EventArgs e) {
362      if (Content != null) Content.StoreAlgorithmInEachRun = storeAlgorithmInEachRunCheckBox.Checked;
363    }
364    #endregion
365  }
366}
Note: See TracBrowser for help on using the repository browser.