Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Algorithms.DataAnalysis.Views/3.4/CrossValidationView.cs @ 10145

Last change on this file since 10145 was 5898, checked in by mkommend, 14 years ago

#1454: Updated TypeSelector to handle logical connection operators when discovering multiple types.

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.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      } 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      }
277      problemTypeSelectorDialog.TypeSelector.Configure(new List<Type>() { Content.ProblemType, Content.Algorithm.ProblemType }, false, true, true);
278      if (problemTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
279        Content.Problem = (IDataAnalysisProblem)problemTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
280      }
281    }
282
283    private void openProblemButton_Click(object sender, EventArgs e) {
284      openFileDialog.Title = "Open Problem";
285      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
286        newProblemButton.Enabled = openProblemButton.Enabled = false;
287        algorithmProblemViewHost.Enabled = false;
288
289        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
290          try {
291            if (error != null) throw error;
292            IDataAnalysisProblem problem = content as IDataAnalysisProblem;
293            if (problem == null && (Content.Algorithm.ProblemType.IsAssignableFrom(content.GetType())))
294              Invoke(new Action(() =>
295                MessageBox.Show(this, "The selected file does not contain a DataAnalysisProblem problem.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error)));
296            else
297              Content.Problem = problem;
298          }
299          catch (Exception ex) {
300            Invoke(new Action(() => ErrorHandling.ShowErrorDialog(this, ex)));
301          }
302          finally {
303            Invoke(new Action(delegate() {
304              algorithmProblemViewHost.Enabled = true;
305              newProblemButton.Enabled = openProblemButton.Enabled = true;
306            }));
307          }
308        });
309      }
310    }
311
312    private void algorithmTabPage_DragEnterOver(object sender, DragEventArgs e) {
313      e.Effect = DragDropEffects.None;
314      IAlgorithm algorithm = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IAlgorithm;
315      if (!ReadOnly && algorithm != null &&
316        (algorithm.ProblemType != null || Content.ProblemType.IsAssignableFrom(algorithm.Problem.GetType()))) {
317        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
318        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
319        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
320        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
321        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
322      }
323    }
324    private void algorithmTabPage_DragDrop(object sender, DragEventArgs e) {
325      if (e.Effect != DragDropEffects.None) {
326        IAlgorithm algorithm = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IAlgorithm;
327        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) algorithm = (IAlgorithm)algorithm.Clone();
328        Content.Algorithm = algorithm;
329      }
330    }
331
332    private void algorithmProblemTabPage_DragEnterOver(object sender, DragEventArgs e) {
333      e.Effect = DragDropEffects.None;
334      if (ReadOnly) return;
335      IProblem problem = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IProblem;
336      if (problem != null && Content.ProblemType.IsAssignableFrom(problem.GetType()) &&
337        Content.Algorithm.ProblemType.IsAssignableFrom(problem.GetType())) {
338        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
339        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
340        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
341        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
342        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
343      }
344    }
345    private void algorithmProblemTabPage_DragDrop(object sender, DragEventArgs e) {
346      if (e.Effect != DragDropEffects.None) {
347        IDataAnalysisProblem problem = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IDataAnalysisProblem;
348        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) problem = (IDataAnalysisProblem)problem.Clone();
349        Content.Problem = problem;
350      }
351    }
352
353    private void storeAlgorithmInEachRunCheckBox_CheckedChanged(object sender, EventArgs e) {
354      if (Content != null) Content.StoreAlgorithmInEachRun = storeAlgorithmInEachRunCheckBox.Checked;
355    }
356    #endregion
357
358  }
359}
Note: See TracBrowser for help on using the repository browser.