Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis.Views/3.3/CrossValidationView.cs @ 4906

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

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

File size: 16.4 KB
RevLine 
[4536]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 {
[4542]33  [View("Cross Validation View")]
[4536]34  [Content(typeof(CrossValidation), true)]
[4542]35  public sealed partial class CrossValidationView : NamedItemView {
[4536]36    private TypeSelectorDialog algorithmTypeSelectorDialog;
37    private TypeSelectorDialog problemTypeSelectorDialog;
38
39    public CrossValidationView() {
40      InitializeComponent();
[4542]41      workersNumericUpDown.Maximum = Environment.ProcessorCount * 2;
[4536]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) {
[4567]52        workersNumericUpDown.Value = 1;
53        foldsNumericUpDown.Value = 2;
[4536]54        samplesStartStringConvertibleValueView.Content = null;
55        samplesEndStringConvertibleValueView.Content = null;
56        algorithmNamedItemView.Content = null;
57        algorithmProblemViewHost.Content = null;
58        algorithmParameterCollectionView.Content = null;
[4567]59        resultCollectionView.Content = null;
[4561]60        runCollectionView.Content = null;
61        storeAlgorithmInEachRunCheckBox.Checked = true;
[4536]62      } else {
[4542]63        Locked = ReadOnly = Content.ExecutionState == ExecutionState.Started;
[4536]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();
[4561]70        runCollectionView.Content = Content.Runs;
71        algorithmParameterCollectionView.Content = ((IParameterizedNamedItem)Content).Parameters;
[4567]72        resultCollectionView.Content = Content.Results;
[4536]73        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
[4561]74        storeAlgorithmInEachRunCheckBox.Checked = Content.StoreAlgorithmInEachRun;
[4536]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);
[4561]88      Content.StoreAlgorithmInEachRunChanged += new EventHandler(Content_StoreAlgorithmInEachRunChanged);
[4536]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);
[4542]99      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
[4561]100      Content.StoreAlgorithmInEachRunChanged -= new EventHandler(Content_StoreAlgorithmInEachRunChanged);
101      base.DeregisterContentEvents();
[4536]102    }
103
104    protected override void SetEnabledStateOfControls() {
105      if (InvokeRequired) Invoke((Action)SetEnabledStateOfControls);
106      else {
107        base.SetEnabledStateOfControls();
108        this.Enabled = Content != null;
[4561]109
[4536]110        if (Content != null) {
[4561]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;
[4536]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 {
[4542]158        Locked = ReadOnly = Content.ExecutionState == ExecutionState.Started;
[4561]159        SetEnabledStateOfControls();
[4536]160      }
161    }
[4542]162
[4536]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    }
[4561]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    }
[4536]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) {
[4563]194      if (workersNumericUpDown.Text == string.Empty)
[4536]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";
[4563]263        problemTypeSelectorDialog.TypeSelector.Configure(Content.ProblemType, false, true);
[4536]264      }
265      if (problemTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
266        try {
[4563]267          Content.Problem = (ISingleObjectiveDataAnalysisProblem)problemTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
[4536]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;
[4563]284            ISingleObjectiveDataAnalysisProblem problem = content as ISingleObjectiveDataAnalysisProblem;
[4542]285            if (problem == null && (Content.Algorithm.ProblemType.IsAssignableFrom(content.GetType())))
[4536]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;
[4561]306      if (ReadOnly) return;
[4536]307      Type type = e.Data.GetData("Type") as Type;
[4561]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())) {
[4536]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
[4542]326    private void algorithmProblemTabPage_DragEnterOver(object sender, DragEventArgs e) {
[4536]327      e.Effect = DragDropEffects.None;
[4561]328      if (ReadOnly) return;
[4536]329      Type type = e.Data.GetData("Type") as Type;
[4561]330      if ((type != null) && (Content.ProblemType.IsAssignableFrom(type)) &&
[4542]331        (Content.Algorithm.ProblemType.IsAssignableFrom(type))) {
[4536]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    }
[4542]339    private void algorithmProblemTabPage_DragDrop(object sender, DragEventArgs e) {
[4536]340      if (e.Effect != DragDropEffects.None) {
[4563]341        ISingleObjectiveDataAnalysisProblem problem = e.Data.GetData("Value") as ISingleObjectiveDataAnalysisProblem;
342        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) problem = (ISingleObjectiveDataAnalysisProblem)problem.Clone();
[4536]343        Content.Problem = problem;
344      }
345    }
[4561]346
347    private void storeAlgorithmInEachRunCheckBox_CheckedChanged(object sender, EventArgs e) {
348      if (Content != null) Content.StoreAlgorithmInEachRun = storeAlgorithmInEachRunCheckBox.Checked;
349    }
[4536]350    #endregion
[4567]351
[4536]352  }
353}
Note: See TracBrowser for help on using the repository browser.