Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5427 was 5420, checked in by mkommend, 14 years ago

#1402 - Refactored OnClosed method of IOptimizer views.

File size: 17.0 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.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      if (ReadOnly) return;
320      Type type = e.Data.GetData("Type") as Type;
321      IAlgorithm algorithm = e.Data.GetData("Value") as IAlgorithm;
322      if ((type != null) && (typeof(IAlgorithm).IsAssignableFrom(type)) &&
323        algorithm != null && Content.ProblemType.IsAssignableFrom(algorithm.Problem.GetType())) {
324        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
325        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
326        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
327        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
328        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
329      }
330    }
331    private void algorithmTabPage_DragDrop(object sender, DragEventArgs e) {
332      if (e.Effect != DragDropEffects.None) {
333        IAlgorithm algorithm = e.Data.GetData("Value") as IAlgorithm;
334        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) algorithm = (IAlgorithm)algorithm.Clone();
335        Content.Algorithm = algorithm;
336      }
337    }
338
339    private void algorithmProblemTabPage_DragEnterOver(object sender, DragEventArgs e) {
340      e.Effect = DragDropEffects.None;
341      if (ReadOnly) return;
342      Type type = e.Data.GetData("Type") as Type;
343      if ((type != null) && (Content.ProblemType.IsAssignableFrom(type)) &&
344        (Content.Algorithm.ProblemType.IsAssignableFrom(type))) {
345        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
346        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
347        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
348        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
349        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
350      }
351    }
352    private void algorithmProblemTabPage_DragDrop(object sender, DragEventArgs e) {
353      if (e.Effect != DragDropEffects.None) {
354        ISingleObjectiveDataAnalysisProblem problem = e.Data.GetData("Value") as ISingleObjectiveDataAnalysisProblem;
355        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) problem = (ISingleObjectiveDataAnalysisProblem)problem.Clone();
356        Content.Problem = problem;
357      }
358    }
359
360    private void storeAlgorithmInEachRunCheckBox_CheckedChanged(object sender, EventArgs e) {
361      if (Content != null) Content.StoreAlgorithmInEachRun = storeAlgorithmInEachRunCheckBox.Checked;
362    }
363    #endregion
364
365  }
366}
Note: See TracBrowser for help on using the repository browser.