Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6193 was 5837, checked in by swagner, 14 years ago

Implemented review comments of mkommend (#1112)

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