Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.Orienteering/HeuristicLab.Algorithms.DataAnalysis.Views/3.4/CrossValidationView.cs @ 11868

Last change on this file since 11868 was 11185, checked in by pfleck, 10 years ago

#2208 merged trunk and updated version info

File size: 17.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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          openAlgorithmButton.Enabled = !ReadOnly;
126          newAlgorithmButton.Enabled = !ReadOnly;
127
128          algorithmNamedItemView.Enabled = Content.Algorithm != null && (Content.ExecutionState == ExecutionState.Prepared || Content.ExecutionState == ExecutionState.Stopped);
129          algorithmTabControl.Enabled = Content.Algorithm != null && (Content.ExecutionState == ExecutionState.Prepared || Content.ExecutionState == ExecutionState.Stopped);
130          foldsNumericUpDown.Enabled = Content.ExecutionState == ExecutionState.Prepared;
131          samplesStartStringConvertibleValueView.Enabled = Content.ExecutionState == ExecutionState.Prepared;
132          samplesEndStringConvertibleValueView.Enabled = Content.ExecutionState == ExecutionState.Prepared;
133          workersNumericUpDown.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
134
135          startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
136          pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
137          stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
138          resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
139        }
140      }
141    }
142
143    #region Content Events
144    private void Content_AlgorithmChanged(object sender, EventArgs e) {
145      UpdateAlgorithmView();
146      UpdateProblemView();
147      SetEnabledStateOfControls();
148    }
149    private void UpdateAlgorithmView() {
150      algorithmNamedItemView.Content = Content.Algorithm;
151      UpdateProblemView();
152    }
153
154    private void Content_ProblemChanged(object sender, EventArgs e) {
155      UpdateProblemView();
156      SetEnabledStateOfControls();
157    }
158    private void UpdateProblemView() {
159      algorithmProblemViewHost.Content = Content.Problem;
160    }
161
162    private void Content_Folds_ValueChanged(object sender, EventArgs e) {
163      foldsNumericUpDown.Value = Content.Folds.Value;
164    }
165    private void Content_NumberOfWorker_ValueChanged(object sender, EventArgs e) {
166      workersNumericUpDown.Value = Content.NumberOfWorkers.Value;
167    }
168
169    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
170      if (InvokeRequired)
171        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
172      else {
173        Locked = ReadOnly = Content.ExecutionState == ExecutionState.Started;
174        SetEnabledStateOfControls();
175      }
176    }
177
178    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
179      if (InvokeRequired)
180        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
181      else
182        executionTimeTextBox.Text = Content == null ? "-" : Content.ExecutionTime.ToString();
183    }
184    private void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
185      if (InvokeRequired)
186        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
187      else
188        ErrorHandling.ShowErrorDialog(this, e.Value);
189    }
190    private void Content_StoreAlgorithmInEachRunChanged(object sender, EventArgs e) {
191      if (InvokeRequired)
192        Invoke(new EventHandler(Content_StoreAlgorithmInEachRunChanged), sender, e);
193      else
194        storeAlgorithmInEachRunCheckBox.Checked = Content.StoreAlgorithmInEachRun;
195    }
196    #endregion
197
198    #region GUI events
199    private void foldsNumericUpDown_Validated(object sender, EventArgs e) {
200      if (foldsNumericUpDown.Text == string.Empty)
201        foldsNumericUpDown.Text = foldsNumericUpDown.Value.ToString();
202    }
203    private void foldsNumericUpDown_ValueChanged(object sender, EventArgs e) {
204      if (Content != null)
205        Content.Folds.Value = (int)foldsNumericUpDown.Value;
206    }
207
208    private void workersNumericUpDown_Validated(object sender, EventArgs e) {
209      if (workersNumericUpDown.Text == string.Empty)
210        workersNumericUpDown.Text = workersNumericUpDown.Value.ToString();
211    }
212    private void workersNumericUpDown_ValueChanged(object sender, EventArgs e) {
213      if (Content != null)
214        Content.NumberOfWorkers.Value = (int)workersNumericUpDown.Value;
215    }
216
217    private void startButton_Click(object sender, EventArgs e) {
218      Content.Start();
219    }
220    private void pauseButton_Click(object sender, EventArgs e) {
221      Content.Pause();
222    }
223    private void stopButton_Click(object sender, EventArgs e) {
224      Content.Stop();
225    }
226    private void resetButton_Click(object sender, EventArgs e) {
227      Content.Prepare(false);
228    }
229
230    private void newAlgorithmButton_Click(object sender, EventArgs e) {
231      if (algorithmTypeSelectorDialog == null) {
232        algorithmTypeSelectorDialog = new TypeSelectorDialog();
233        algorithmTypeSelectorDialog.Caption = "Select Algorithm";
234        algorithmTypeSelectorDialog.TypeSelector.Caption = "Available Algorithms";
235        algorithmTypeSelectorDialog.TypeSelector.Configure(typeof(IAlgorithm), false, true);
236      }
237      if (algorithmTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
238        try {
239          Content.Algorithm = (IAlgorithm)algorithmTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
240        }
241        catch (Exception ex) {
242          ErrorHandling.ShowErrorDialog(this, ex);
243        }
244      }
245    }
246
247    private void openAlgorithmButton_Click(object sender, EventArgs e) {
248      openFileDialog.Title = "Open Algorithm";
249      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
250        algorithmTabControl.Enabled = false;
251
252        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
253          try {
254            if (error != null) throw error;
255            IAlgorithm algorithm = content as IAlgorithm;
256            if (algorithm == null || !(algorithm.Problem is IDataAnalysisProblem))
257              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);
258            else
259              Content.Algorithm = algorithm;
260          }
261          catch (Exception ex) {
262            ErrorHandling.ShowErrorDialog(this, ex);
263          }
264          finally {
265            Invoke(new Action(delegate() {
266              algorithmTabControl.Enabled = true;
267            }));
268          }
269        });
270      }
271    }
272
273    private void newProblemButton_Click(object sender, EventArgs e) {
274      if (problemTypeSelectorDialog == null) {
275        problemTypeSelectorDialog = new TypeSelectorDialog();
276        problemTypeSelectorDialog.Caption = "Select Problem";
277        problemTypeSelectorDialog.TypeSelector.Caption = "Available Problems";
278      }
279      problemTypeSelectorDialog.TypeSelector.Configure(new List<Type>() { Content.ProblemType, Content.Algorithm.ProblemType }, false, true, true);
280      if (problemTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
281        Content.Problem = (IDataAnalysisProblem)problemTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
282      }
283    }
284
285    private void openProblemButton_Click(object sender, EventArgs e) {
286      openFileDialog.Title = "Open Problem";
287      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
288        newProblemButton.Enabled = openProblemButton.Enabled = false;
289        algorithmProblemViewHost.Enabled = false;
290
291        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
292          try {
293            if (error != null) throw error;
294            IDataAnalysisProblem problem = content as IDataAnalysisProblem;
295            if (problem == null && (Content.Algorithm.ProblemType.IsAssignableFrom(content.GetType())))
296              Invoke(new Action(() =>
297                MessageBox.Show(this, "The selected file does not contain a DataAnalysisProblem problem.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error)));
298            else
299              Content.Problem = problem;
300          }
301          catch (Exception ex) {
302            Invoke(new Action(() => ErrorHandling.ShowErrorDialog(this, ex)));
303          }
304          finally {
305            Invoke(new Action(delegate() {
306              algorithmProblemViewHost.Enabled = true;
307              newProblemButton.Enabled = openProblemButton.Enabled = true;
308            }));
309          }
310        });
311      }
312    }
313
314    private void algorithmTabPage_DragEnterOver(object sender, DragEventArgs e) {
315      e.Effect = DragDropEffects.None;
316      IAlgorithm algorithm = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IAlgorithm;
317      if (!ReadOnly && algorithm != null &&
318        (algorithm.ProblemType != null || Content.ProblemType.IsAssignableFrom(algorithm.Problem.GetType()))) {
319        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
320        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
321        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
322        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
323        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
324      }
325    }
326    private void algorithmTabPage_DragDrop(object sender, DragEventArgs e) {
327      if (e.Effect != DragDropEffects.None) {
328        IAlgorithm algorithm = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IAlgorithm;
329        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) algorithm = (IAlgorithm)algorithm.Clone();
330        Content.Algorithm = algorithm;
331      }
332    }
333
334    private void algorithmProblemTabPage_DragEnterOver(object sender, DragEventArgs e) {
335      e.Effect = DragDropEffects.None;
336      if (ReadOnly) return;
337      IProblem problem = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IProblem;
338      if (problem != null && Content.ProblemType.IsAssignableFrom(problem.GetType()) &&
339        Content.Algorithm.ProblemType.IsAssignableFrom(problem.GetType())) {
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 & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
343        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
344        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
345      }
346    }
347    private void algorithmProblemTabPage_DragDrop(object sender, DragEventArgs e) {
348      if (e.Effect != DragDropEffects.None) {
349        IDataAnalysisProblem problem = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IDataAnalysisProblem;
350        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) problem = (IDataAnalysisProblem)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.