Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Algorithms.DataAnalysis.Views/3.3/CrossValidationView.cs @ 9282

Last change on this file since 9282 was 4569, checked in by mkommend, 14 years ago

Moved CrossValidation from branch to trunk (ticket #1199).

File size: 16.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Windows.Forms;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Core.Views;
27using HeuristicLab.MainForm;
28using HeuristicLab.Optimization;
29using HeuristicLab.PluginInfrastructure;
30using HeuristicLab.Problems.DataAnalysis;
31
32namespace HeuristicLab.Algorithms.DataAnalysis.Views {
33  [View("Cross Validation View")]
34  [Content(typeof(CrossValidation), true)]
35  public sealed partial class CrossValidationView : NamedItemView {
36    private TypeSelectorDialog algorithmTypeSelectorDialog;
37    private TypeSelectorDialog problemTypeSelectorDialog;
38
39    public CrossValidationView() {
40      InitializeComponent();
41      workersNumericUpDown.Maximum = Environment.ProcessorCount * 2;
42    }
43
44    public new CrossValidation Content {
45      get { return (CrossValidation)base.Content; }
46      set { base.Content = value; }
47    }
48
49    protected override void OnContentChanged() {
50      base.OnContentChanged();
51      if (Content == null) {
52        workersNumericUpDown.Value = 1;
53        foldsNumericUpDown.Value = 2;
54        samplesStartStringConvertibleValueView.Content = null;
55        samplesEndStringConvertibleValueView.Content = null;
56        algorithmNamedItemView.Content = null;
57        algorithmProblemViewHost.Content = null;
58        algorithmParameterCollectionView.Content = null;
59        resultCollectionView.Content = null;
60        runCollectionView.Content = null;
61        storeAlgorithmInEachRunCheckBox.Checked = true;
62      } else {
63        Locked = ReadOnly = Content.ExecutionState == ExecutionState.Started;
64        workersNumericUpDown.Value = Content.NumberOfWorkers.Value;
65        foldsNumericUpDown.Value = Content.Folds.Value;
66        samplesStartStringConvertibleValueView.Content = Content.SamplesStart;
67        samplesEndStringConvertibleValueView.Content = Content.SamplesEnd;
68        UpdateAlgorithmView();
69        UpdateProblemView();
70        runCollectionView.Content = Content.Runs;
71        algorithmParameterCollectionView.Content = ((IParameterizedNamedItem)Content).Parameters;
72        resultCollectionView.Content = Content.Results;
73        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
74        storeAlgorithmInEachRunCheckBox.Checked = Content.StoreAlgorithmInEachRun;
75      }
76    }
77
78    protected override void RegisterContentEvents() {
79      base.RegisterContentEvents();
80      Content.AlgorithmChanged += new EventHandler(Content_AlgorithmChanged);
81      Content.ProblemChanged += new EventHandler(Content_ProblemChanged);
82      Content.Folds.ValueChanged += new EventHandler(Content_Folds_ValueChanged);
83      Content.NumberOfWorkers.ValueChanged += new EventHandler(Content_NumberOfWorker_ValueChanged);
84
85      Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
86      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
87      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
88      Content.StoreAlgorithmInEachRunChanged += new EventHandler(Content_StoreAlgorithmInEachRunChanged);
89    }
90
91    protected override void DeregisterContentEvents() {
92      Content.AlgorithmChanged -= new EventHandler(Content_AlgorithmChanged);
93      Content.ProblemChanged -= new EventHandler(Content_ProblemChanged);
94      Content.Folds.ValueChanged -= new EventHandler(Content_Folds_ValueChanged);
95      Content.NumberOfWorkers.ValueChanged -= new EventHandler(Content_NumberOfWorker_ValueChanged);
96
97      Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
98      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
99      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
100      Content.StoreAlgorithmInEachRunChanged -= new EventHandler(Content_StoreAlgorithmInEachRunChanged);
101      base.DeregisterContentEvents();
102    }
103
104    protected override void SetEnabledStateOfControls() {
105      if (InvokeRequired) Invoke((Action)SetEnabledStateOfControls);
106      else {
107        base.SetEnabledStateOfControls();
108        this.Enabled = Content != null;
109
110        if (Content != null) {
111          storeAlgorithmInEachRunCheckBox.Enabled = !ReadOnly;
112
113          algorithmNamedItemView.Enabled = Content.Algorithm != null && (Content.ExecutionState == ExecutionState.Prepared || Content.ExecutionState == ExecutionState.Stopped);
114          algorithmTabControl.Enabled = Content.Algorithm != null && (Content.ExecutionState == ExecutionState.Prepared || Content.ExecutionState == ExecutionState.Stopped);
115          foldsNumericUpDown.Enabled = Content.ExecutionState == ExecutionState.Prepared;
116          samplesStartStringConvertibleValueView.Enabled = Content.ExecutionState == ExecutionState.Prepared;
117          samplesEndStringConvertibleValueView.Enabled = Content.ExecutionState == ExecutionState.Prepared;
118          workersNumericUpDown.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
119
120          startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
121          pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
122          stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
123          resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
124        }
125      }
126    }
127
128    #region Content Events
129    private void Content_AlgorithmChanged(object sender, EventArgs e) {
130      UpdateAlgorithmView();
131      UpdateProblemView();
132      SetEnabledStateOfControls();
133    }
134    private void UpdateAlgorithmView() {
135      algorithmNamedItemView.Content = Content.Algorithm;
136      UpdateProblemView();
137    }
138
139    private void Content_ProblemChanged(object sender, EventArgs e) {
140      UpdateProblemView();
141      SetEnabledStateOfControls();
142    }
143    private void UpdateProblemView() {
144      algorithmProblemViewHost.Content = Content.Problem;
145    }
146
147    private void Content_Folds_ValueChanged(object sender, EventArgs e) {
148      foldsNumericUpDown.Value = Content.Folds.Value;
149    }
150    private void Content_NumberOfWorker_ValueChanged(object sender, EventArgs e) {
151      workersNumericUpDown.Value = Content.NumberOfWorkers.Value;
152    }
153
154    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
155      if (InvokeRequired)
156        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
157      else {
158        Locked = ReadOnly = Content.ExecutionState == ExecutionState.Started;
159        SetEnabledStateOfControls();
160      }
161    }
162
163    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
164      if (InvokeRequired)
165        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
166      else
167        executionTimeTextBox.Text = Content == null ? "-" : Content.ExecutionTime.ToString();
168    }
169    private void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
170      if (InvokeRequired)
171        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
172      else
173        ErrorHandling.ShowErrorDialog(this, e.Value);
174    }
175    private void Content_StoreAlgorithmInEachRunChanged(object sender, EventArgs e) {
176      if (InvokeRequired)
177        Invoke(new EventHandler(Content_StoreAlgorithmInEachRunChanged), sender, e);
178      else
179        storeAlgorithmInEachRunCheckBox.Checked = Content.StoreAlgorithmInEachRun;
180    }
181    #endregion
182
183    #region GUI events
184    private void foldsNumericUpDown_Validated(object sender, EventArgs e) {
185      if (foldsNumericUpDown.Text == string.Empty)
186        foldsNumericUpDown.Text = foldsNumericUpDown.Value.ToString();
187    }
188    private void foldsNumericUpDown_ValueChanged(object sender, EventArgs e) {
189      if (Content != null)
190        Content.Folds.Value = (int)foldsNumericUpDown.Value;
191    }
192
193    private void workersNumericUpDown_Validated(object sender, EventArgs e) {
194      if (workersNumericUpDown.Text == string.Empty)
195        workersNumericUpDown.Text = workersNumericUpDown.Value.ToString();
196    }
197    private void workersNumericUpDown_ValueChanged(object sender, EventArgs e) {
198      if (Content != null)
199        Content.NumberOfWorkers.Value = (int)workersNumericUpDown.Value;
200    }
201
202    private void startButton_Click(object sender, EventArgs e) {
203      Content.Start();
204    }
205    private void pauseButton_Click(object sender, EventArgs e) {
206      Content.Pause();
207    }
208    private void stopButton_Click(object sender, EventArgs e) {
209      Content.Stop();
210    }
211    private void resetButton_Click(object sender, EventArgs e) {
212      Content.Prepare(false);
213    }
214
215    private void newAlgorithmButton_Click(object sender, EventArgs e) {
216      if (algorithmTypeSelectorDialog == null) {
217        algorithmTypeSelectorDialog = new TypeSelectorDialog();
218        algorithmTypeSelectorDialog.Caption = "Select Algorithm";
219        algorithmTypeSelectorDialog.TypeSelector.Caption = "Available Algorithms";
220        algorithmTypeSelectorDialog.TypeSelector.Configure(typeof(IAlgorithm), false, true);
221      }
222      if (algorithmTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
223        try {
224          Content.Algorithm = (IAlgorithm)algorithmTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
225        }
226        catch (Exception ex) {
227          ErrorHandling.ShowErrorDialog(this, ex);
228        }
229      }
230    }
231
232    private void openAlgorithmButton_Click(object sender, EventArgs e) {
233      openFileDialog.Title = "Open Algorithm";
234      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
235        algorithmTabControl.Enabled = false;
236
237        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
238          try {
239            if (error != null) throw error;
240            IAlgorithm algorithm = content as IAlgorithm;
241            if (algorithm == null || !(algorithm.Problem is IDataAnalysisProblem))
242              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);
243            else
244              Content.Algorithm = algorithm;
245          }
246          catch (Exception ex) {
247            ErrorHandling.ShowErrorDialog(this, ex);
248          }
249          finally {
250            Invoke(new Action(delegate() {
251              algorithmTabControl.Enabled = true;
252            }));
253          }
254        });
255      }
256    }
257
258    private void newProblemButton_Click(object sender, EventArgs e) {
259      if (problemTypeSelectorDialog == null) {
260        problemTypeSelectorDialog = new TypeSelectorDialog();
261        problemTypeSelectorDialog.Caption = "Select Problem";
262        problemTypeSelectorDialog.TypeSelector.Caption = "Available Problems";
263        problemTypeSelectorDialog.TypeSelector.Configure(Content.ProblemType, false, true);
264      }
265      if (problemTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
266        try {
267          Content.Problem = (ISingleObjectiveDataAnalysisProblem)problemTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
268        }
269        catch (Exception ex) {
270          ErrorHandling.ShowErrorDialog(this, ex);
271        }
272      }
273    }
274
275    private void openProblemButton_Click(object sender, EventArgs e) {
276      openFileDialog.Title = "Open Problem";
277      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
278        newProblemButton.Enabled = openProblemButton.Enabled = false;
279        algorithmProblemViewHost.Enabled = false;
280
281        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
282          try {
283            if (error != null) throw error;
284            ISingleObjectiveDataAnalysisProblem problem = content as ISingleObjectiveDataAnalysisProblem;
285            if (problem == null && (Content.Algorithm.ProblemType.IsAssignableFrom(content.GetType())))
286              Invoke(new Action(() =>
287                MessageBox.Show(this, "The selected file does not contain a DataAnalysisProblem problem.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error)));
288            else
289              Content.Problem = problem;
290          }
291          catch (Exception ex) {
292            Invoke(new Action(() => ErrorHandling.ShowErrorDialog(this, ex)));
293          }
294          finally {
295            Invoke(new Action(delegate() {
296              algorithmProblemViewHost.Enabled = true;
297              newProblemButton.Enabled = openProblemButton.Enabled = true;
298            }));
299          }
300        });
301      }
302    }
303
304    private void algorithmTabPage_DragEnterOver(object sender, DragEventArgs e) {
305      e.Effect = DragDropEffects.None;
306      if (ReadOnly) return;
307      Type type = e.Data.GetData("Type") as Type;
308      IAlgorithm algorithm = e.Data.GetData("Value") as IAlgorithm;
309      if ((type != null) && (typeof(IAlgorithm).IsAssignableFrom(type)) &&
310        algorithm != null && Content.ProblemType.IsAssignableFrom(algorithm.Problem.GetType())) {
311        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
312        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
313        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
314        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
315        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
316      }
317    }
318    private void algorithmTabPage_DragDrop(object sender, DragEventArgs e) {
319      if (e.Effect != DragDropEffects.None) {
320        IAlgorithm algorithm = e.Data.GetData("Value") as IAlgorithm;
321        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) algorithm = (IAlgorithm)algorithm.Clone();
322        Content.Algorithm = algorithm;
323      }
324    }
325
326    private void algorithmProblemTabPage_DragEnterOver(object sender, DragEventArgs e) {
327      e.Effect = DragDropEffects.None;
328      if (ReadOnly) return;
329      Type type = e.Data.GetData("Type") as Type;
330      if ((type != null) && (Content.ProblemType.IsAssignableFrom(type)) &&
331        (Content.Algorithm.ProblemType.IsAssignableFrom(type))) {
332        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
333        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
334        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
335        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
336        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
337      }
338    }
339    private void algorithmProblemTabPage_DragDrop(object sender, DragEventArgs e) {
340      if (e.Effect != DragDropEffects.None) {
341        ISingleObjectiveDataAnalysisProblem problem = e.Data.GetData("Value") as ISingleObjectiveDataAnalysisProblem;
342        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) problem = (ISingleObjectiveDataAnalysisProblem)problem.Clone();
343        Content.Problem = problem;
344      }
345    }
346
347    private void storeAlgorithmInEachRunCheckBox_CheckedChanged(object sender, EventArgs e) {
348      if (Content != null) Content.StoreAlgorithmInEachRun = storeAlgorithmInEachRunCheckBox.Checked;
349    }
350    #endregion
351
352  }
353}
Note: See TracBrowser for help on using the repository browser.