Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2389-EpsLexicase/HeuristicLab.Algorithms.DataAnalysis.Views/3.4/CrossValidationView.cs @ 16659

Last change on this file since 16659 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 17.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.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 {
35  [View("CrossValidation View")]
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        shuffleSamplesCheckBox.Checked = false;
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;
77        shuffleSamplesCheckBox.Checked = Content.ShuffleSamples.Value;
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;
127          openAlgorithmButton.Enabled = !ReadOnly;
128          newAlgorithmButton.Enabled = !ReadOnly;
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;
141          // prevent changing the shuffle if the algorithm is not finished
142          shuffleSamplesCheckBox.Enabled = Content.ExecutionState == ExecutionState.Prepared;
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
203    private void shuffleSamplesCheckBox_CheckedChanged(object sender, EventArgs e) {
204      Content.ShuffleSamples.Value = shuffleSamplesCheckBox.Checked;
205    }
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
224    private async void startButton_Click(object sender, EventArgs e) {
225      await Content.StartAsync();
226    }
227    private void pauseButton_Click(object sender, EventArgs e) {
228      Content.Pause();
229    }
230    private void stopButton_Click(object sender, EventArgs e) {
231      Content.Stop();
232    }
233    private void resetButton_Click(object sender, EventArgs e) {
234      Content.Prepare(false);
235    }
236
237    private void newAlgorithmButton_Click(object sender, EventArgs e) {
238      if (algorithmTypeSelectorDialog == null) {
239        algorithmTypeSelectorDialog = new TypeSelectorDialog();
240        algorithmTypeSelectorDialog.Caption = "Select Algorithm";
241        algorithmTypeSelectorDialog.TypeSelector.Caption = "Available Algorithms";
242        algorithmTypeSelectorDialog.TypeSelector.Configure(typeof(IAlgorithm), false, true);
243      }
244      if (algorithmTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
245        try {
246          Content.Algorithm = (IAlgorithm)algorithmTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
247        } catch (Exception ex) {
248          ErrorHandling.ShowErrorDialog(this, ex);
249        }
250      }
251    }
252
253    private void openAlgorithmButton_Click(object sender, EventArgs e) {
254      openFileDialog.Title = "Open Algorithm";
255      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
256        algorithmTabControl.Enabled = false;
257
258        ContentManager.LoadAsync(openFileDialog.FileName, delegate (IStorableContent content, Exception error) {
259          try {
260            if (error != null) throw error;
261            IAlgorithm algorithm = content as IAlgorithm;
262            if (algorithm == null || !(algorithm.Problem is IDataAnalysisProblem))
263              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);
264            else
265              Content.Algorithm = algorithm;
266          } catch (Exception ex) {
267            ErrorHandling.ShowErrorDialog(this, ex);
268          } finally {
269            Invoke(new Action(delegate () {
270              algorithmTabControl.Enabled = true;
271            }));
272          }
273        });
274      }
275    }
276
277    private void newProblemButton_Click(object sender, EventArgs e) {
278      if (problemTypeSelectorDialog == null) {
279        problemTypeSelectorDialog = new TypeSelectorDialog();
280        problemTypeSelectorDialog.Caption = "Select Problem";
281        problemTypeSelectorDialog.TypeSelector.Caption = "Available Problems";
282      }
283      problemTypeSelectorDialog.TypeSelector.Configure(new List<Type>() { Content.ProblemType, Content.Algorithm.ProblemType }, false, true, true);
284      if (problemTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
285        Content.Problem = (IDataAnalysisProblem)problemTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
286      }
287    }
288
289    private void openProblemButton_Click(object sender, EventArgs e) {
290      openFileDialog.Title = "Open Problem";
291      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
292        newProblemButton.Enabled = openProblemButton.Enabled = false;
293        algorithmProblemViewHost.Enabled = false;
294
295        ContentManager.LoadAsync(openFileDialog.FileName, delegate (IStorableContent content, Exception error) {
296          try {
297            if (error != null) throw error;
298            IDataAnalysisProblem problem = content as IDataAnalysisProblem;
299            if (problem == null && (Content.Algorithm.ProblemType.IsAssignableFrom(content.GetType())))
300              Invoke(new Action(() =>
301                MessageBox.Show(this, "The selected file does not contain a DataAnalysisProblem problem.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error)));
302            else
303              Content.Problem = problem;
304          } catch (Exception ex) {
305            Invoke(new Action(() => ErrorHandling.ShowErrorDialog(this, ex)));
306          } finally {
307            Invoke(new Action(delegate () {
308              algorithmProblemViewHost.Enabled = true;
309              newProblemButton.Enabled = openProblemButton.Enabled = true;
310            }));
311          }
312        });
313      }
314    }
315
316    private void algorithmTabPage_DragEnterOver(object sender, DragEventArgs e) {
317      e.Effect = DragDropEffects.None;
318      IAlgorithm algorithm = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IAlgorithm;
319      if (!ReadOnly && algorithm != null &&
320        (algorithm.ProblemType != null || Content.ProblemType.IsAssignableFrom(algorithm.Problem.GetType()))) {
321        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
322        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
323        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
324        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
325        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
326      }
327    }
328    private void algorithmTabPage_DragDrop(object sender, DragEventArgs e) {
329      if (e.Effect != DragDropEffects.None) {
330        IAlgorithm algorithm = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IAlgorithm;
331        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) algorithm = (IAlgorithm)algorithm.Clone();
332        Content.Algorithm = algorithm;
333      }
334    }
335
336    private void algorithmProblemTabPage_DragEnterOver(object sender, DragEventArgs e) {
337      e.Effect = DragDropEffects.None;
338      if (ReadOnly) return;
339      IProblem problem = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IProblem;
340      if (problem != null && Content.ProblemType.IsAssignableFrom(problem.GetType()) &&
341        Content.Algorithm.ProblemType.IsAssignableFrom(problem.GetType())) {
342        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
343        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
344        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
345        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
346        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
347      }
348    }
349    private void algorithmProblemTabPage_DragDrop(object sender, DragEventArgs e) {
350      if (e.Effect != DragDropEffects.None) {
351        IDataAnalysisProblem problem = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IDataAnalysisProblem;
352        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) problem = (IDataAnalysisProblem)problem.Clone();
353        Content.Problem = problem;
354      }
355    }
356
357    private void storeAlgorithmInEachRunCheckBox_CheckedChanged(object sender, EventArgs e) {
358      if (Content != null) Content.StoreAlgorithmInEachRun = storeAlgorithmInEachRunCheckBox.Checked;
359    }
360    #endregion
361  }
362}
Note: See TracBrowser for help on using the repository browser.