Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PerformanceComparison/HeuristicLab.Optimization.Views/3.3/IRRestarterView.cs @ 12888

Last change on this file since 12888 was 12825, checked in by abeham, 9 years ago

#2431:

  • removed output of RTs, RTus, ...
  • added 2nd row to charts that state restarts until a target was achieved
  • added option to include or exclude solution results
File size: 14.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.ComponentModel;
24using System.Globalization;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Analysis;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Core.Views;
31using HeuristicLab.MainForm;
32using HeuristicLab.PluginInfrastructure;
33
34namespace HeuristicLab.Optimization.Views {
35  [View("Independent Random Restarter View")]
36  [Content(typeof(IndepdentRandomRestarter), IsDefaultView = true)]
37  public partial class IndependentRandomRestarterView : IOptimizerView {
38    protected TypeSelectorDialog algorithmTypeSelectorDialog;
39    protected virtual bool SuppressEvents { get; set; }
40
41    public new IndepdentRandomRestarter Content {
42      get { return (IndepdentRandomRestarter)base.Content; }
43      set { base.Content = value; }
44    }
45
46    public IndependentRandomRestarterView() {
47      InitializeComponent();
48      terminationComboBox.Items.AddRange(Enum.GetValues(typeof(TerminationCriterium)).Cast<object>().ToArray());
49    }
50
51    protected override void Dispose(bool disposing) {
52      if (disposing) {
53        if (algorithmTypeSelectorDialog != null) algorithmTypeSelectorDialog.Dispose();
54        if (components != null) components.Dispose();
55      }
56      base.Dispose(disposing);
57    }
58
59    protected override void DeregisterContentEvents() {
60      Content.PropertyChanged -= Content_PropertyChanged;
61      base.DeregisterContentEvents();
62    }
63    protected override void RegisterContentEvents() {
64      base.RegisterContentEvents();
65      Content.PropertyChanged += Content_PropertyChanged;
66    }
67
68    protected override void OnContentChanged() {
69      base.OnContentChanged();
70      SuppressEvents = true;
71      try {
72        if (Content == null) {
73          maxExecutionTimeTextBox.Text = String.Empty;
74          maxEvaluationsTextBox.Text = String.Empty;
75          moveCostPerSolutionTextBox.Text = String.Empty;
76          targetValueTextBox.Text = String.Empty;
77          maximizationCheckBox.CheckState = CheckState.Indeterminate;
78          terminationComboBox.SelectedIndex = -1;
79          storeBestSolutionCheckBox.CheckState = CheckState.Indeterminate;
80
81          algorithmViewHost.Content = null;
82          currentRunView.Content = null;
83          runsView.Content = null;
84        } else {
85          maxExecutionTimeTextBox.Text = TimeSpanHelper.FormatNatural(Content.MaximumExecutionTime);
86          maxEvaluationsTextBox.Text = Content.MaximumEvaluations.ToString();
87          moveCostPerSolutionTextBox.Text = Content.MoveCostPerSolution.ToString(CultureInfo.CurrentCulture.NumberFormat);
88          targetValueTextBox.Text = Content.TargetValue.ToString(CultureInfo.CurrentCulture.NumberFormat);
89          maximizationCheckBox.Checked = Content.Maximization;
90          terminationComboBox.SelectedItem = Content.TerminationCriterium;
91          storeBestSolutionCheckBox.Checked = Content.StoreSolutionInRun;
92
93          algorithmViewHost.Content = Content.Algorithm;
94          currentRunView.Content = Content.CurrentRun;
95          runsView.Content = Content.Runs;
96        }
97      } finally { SuppressEvents = false; }
98    }
99
100    protected override void SetEnabledStateOfControls() {
101      base.SetEnabledStateOfControls();
102      maxExecutionTimeTextBox.Enabled = Content != null && !ReadOnly && !Locked;
103      maxEvaluationsTextBox.Enabled = Content != null && !ReadOnly && !Locked;
104      moveCostPerSolutionTextBox.Enabled = Content != null && !ReadOnly && !Locked;
105      targetValueTextBox.Enabled = Content != null && !ReadOnly && !Locked;
106      maximizationCheckBox.Enabled = Content != null && !ReadOnly && !Locked;
107      terminationComboBox.Enabled = Content != null && !ReadOnly && !Locked;
108      storeBestSolutionCheckBox.Enabled = Content != null && !ReadOnly && !Locked;
109      newAlgorithmButton.Enabled = Content != null && !ReadOnly;
110      openAlgorithmButton.Enabled = Content != null && !ReadOnly;
111      algorithmViewHost.Enabled = Content != null;
112      currentRunView.Enabled = Content != null;
113      runsView.Enabled = Content != null;
114    }
115
116    protected override void OnClosed(FormClosedEventArgs e) {
117      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) {
118        //The content must be stopped if no other view showing the content is available
119        var optimizers = MainFormManager.MainForm.Views.OfType<IContentView>().Where(v => v != this).Select(v => v.Content).OfType<IAlgorithm>();
120        if (!optimizers.Contains(Content.Algorithm)) {
121          var nestedOptimizers = optimizers.SelectMany(opt => opt.NestedOptimizers);
122          if (!nestedOptimizers.Contains(Content)) Content.Stop();
123        }
124      }
125      base.OnClosed(e);
126    }
127
128    #region Event Handlers
129    #region Content events
130    private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) {
131      if (InvokeRequired) {
132        Invoke((Action<object, PropertyChangedEventArgs>)Content_PropertyChanged, sender, e);
133        return;
134      }
135      SuppressEvents = true;
136      try {
137        switch (e.PropertyName) {
138          case "TerminationCriterium": terminationComboBox.SelectedItem = Content.TerminationCriterium; break;
139          case "MaximumExecutionTime": maxExecutionTimeTextBox.Text = TimeSpanHelper.FormatNatural(Content.MaximumExecutionTime); break;
140          case "MaximumEvaluations": maxEvaluationsTextBox.Text = Content.MaximumEvaluations.ToString(); break;
141          case "TargetValue": targetValueTextBox.Text = Content.TargetValue.ToString(CultureInfo.CurrentCulture.NumberFormat); break;
142          case "Maximization": maximizationCheckBox.Checked = Content.Maximization; break;
143          case "MoveCostPerSolution": moveCostPerSolutionTextBox.Text = Content.MoveCostPerSolution.ToString(CultureInfo.CurrentCulture.NumberFormat); break;
144          case "StoreSolutionInRun": storeBestSolutionCheckBox.Checked = Content.StoreSolutionInRun; break;
145          case "Algorithm": algorithmViewHost.Content = Content.Algorithm; break;
146          case "CurrentRun": currentRunView.Content = Content.CurrentRun; break;
147          case "Runs": runsView.Content = Content.Runs; break;
148        }
149      } finally { SuppressEvents = false; }
150    }
151    #endregion
152
153    #region Control events
154    private void maxExecutionTimeTextBox_Validating(object sender, CancelEventArgs e) {
155      if (SuppressEvents) return;
156      if (InvokeRequired) {
157        Invoke((Action<object, CancelEventArgs>)maxExecutionTimeTextBox_Validating, sender, e);
158        return;
159      }
160      TimeSpan ts;
161      if (!TimeSpanHelper.TryGetFromNaturalFormat(maxExecutionTimeTextBox.Text, out ts)) {
162        e.Cancel = !maxExecutionTimeTextBox.ReadOnly && maxExecutionTimeTextBox.Enabled;
163        errorProvider.SetError(maxExecutionTimeTextBox, "Please enter a valid time span, e.g. 20 seconds ; 45s ; 4min ; 1h ; 3 hours ; 2 days ; 4d");
164      } else {
165        Content.MaximumExecutionTime = ts;
166        e.Cancel = false;
167        errorProvider.SetError(maxExecutionTimeTextBox, null);
168      }
169    }
170
171    private void maxEvaluationsTextBox_Validating(object sender, CancelEventArgs e) {
172      if (SuppressEvents) return;
173      if (InvokeRequired) {
174        Invoke((Action<object, CancelEventArgs>)maxEvaluationsTextBox_Validating, sender, e);
175        return;
176      }
177      int value;
178      if (!int.TryParse(maxEvaluationsTextBox.Text, out value)) {
179        e.Cancel = !maxEvaluationsTextBox.ReadOnly && maxEvaluationsTextBox.Enabled;
180        errorProvider.SetError(maxEvaluationsTextBox, "Please enter a valid integer number.");
181      } else {
182        Content.MaximumEvaluations = value;
183        e.Cancel = false;
184        errorProvider.SetError(maxEvaluationsTextBox, null);
185      }
186    }
187
188    private void moveCostPerSolutionTextBox_Validating(object sender, CancelEventArgs e) {
189      if (SuppressEvents) return;
190      if (InvokeRequired) {
191        Invoke((Action<object, CancelEventArgs>)moveCostPerSolutionTextBox_Validating, sender, e);
192        return;
193      }
194      double value;
195      if (!double.TryParse(moveCostPerSolutionTextBox.Text, out value)) {
196        e.Cancel = !moveCostPerSolutionTextBox.ReadOnly && moveCostPerSolutionTextBox.Enabled;
197        errorProvider.SetError(moveCostPerSolutionTextBox, "Please enter a valid integer number.");
198      } else {
199        Content.MoveCostPerSolution = value;
200        e.Cancel = false;
201        errorProvider.SetError(moveCostPerSolutionTextBox, null);
202      }
203    }
204
205    private void targetValueTextBox_Validating(object sender, CancelEventArgs e) {
206      if (SuppressEvents) return;
207      if (InvokeRequired) {
208        Invoke((Action<object, CancelEventArgs>)targetValueTextBox_Validating, sender, e);
209        return;
210      }
211      double value;
212      if (!double.TryParse(targetValueTextBox.Text, out value)) {
213        e.Cancel = !targetValueTextBox.ReadOnly && targetValueTextBox.Enabled;
214        errorProvider.SetError(targetValueTextBox, "Please enter a valid integer number.");
215      } else {
216        Content.TargetValue = value;
217        e.Cancel = false;
218        errorProvider.SetError(targetValueTextBox, null);
219      }
220    }
221
222    private void maximizationCheckBox_CheckedChanged(object sender, EventArgs e) {
223      if (SuppressEvents) return;
224      if (InvokeRequired) {
225        Invoke((Action<object, EventArgs>)maximizationCheckBox_CheckedChanged, sender, e);
226        return;
227      }
228      Content.Maximization = maximizationCheckBox.Checked;
229    }
230
231    private void terminationComboBox_SelectedIndexChanged(object sender, EventArgs e) {
232      if (SuppressEvents) return;
233      if (InvokeRequired) {
234        Invoke((Action<object, EventArgs>)terminationComboBox_SelectedIndexChanged, sender, e);
235        return;
236      }
237      Content.TerminationCriterium = (TerminationCriterium)terminationComboBox.SelectedItem;
238    }
239
240    private void storeBestSolutionCheckBox_CheckedChanged(object sender, EventArgs e) {
241      if (SuppressEvents) return;
242      if (InvokeRequired) {
243        Invoke((Action<object, EventArgs>)storeBestSolutionCheckBox_CheckedChanged, sender, e);
244        return;
245      }
246      Content.StoreSolutionInRun = storeBestSolutionCheckBox.Checked;
247    }
248
249    private void newAlgorithmButton_Click(object sender, EventArgs e) {
250      if (algorithmTypeSelectorDialog == null) {
251        algorithmTypeSelectorDialog = new TypeSelectorDialog { Caption = "Select Algorithm" };
252        algorithmTypeSelectorDialog.TypeSelector.Caption = "Available Algorithms";
253        algorithmTypeSelectorDialog.TypeSelector.Configure(typeof(IAlgorithm)
254          , showNotInstantiableTypes: false, showGenericTypes: false);
255      }
256      if (algorithmTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
257        try {
258          Content.Algorithm = (IAlgorithm)algorithmTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
259        } catch (Exception ex) {
260          ErrorHandling.ShowErrorDialog(this, ex);
261        }
262      }
263    }
264
265    private void openAlgorithmButton_Click(object sender, EventArgs e) {
266      openFileDialog.Title = "Open Algorithm";
267      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
268        newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = false;
269        algorithmViewHost.Enabled = false;
270
271        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
272          try {
273            if (error != null) throw error;
274            var algorithm = content as IAlgorithm;
275            if (algorithm == null)
276              MessageBox.Show(this, "The selected file does not contain an algorithm.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
277            else
278              Content.Algorithm = algorithm;
279          } catch (Exception ex) {
280            ErrorHandling.ShowErrorDialog(this, ex);
281          } finally {
282            Invoke(new Action(delegate() {
283              algorithmViewHost.Enabled = true;
284              newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = true;
285            }));
286          }
287        });
288      }
289    }
290
291    private void algorithmTabPage_DragEnterOver(object sender, DragEventArgs e) {
292      e.Effect = DragDropEffects.None;
293      var alg = e.Data.GetData(Constants.DragDropDataFormat) as IAlgorithm;
294      if (!ReadOnly && alg != null) {
295        if (!typeof(ISingleObjectiveHeuristicOptimizationProblem).IsAssignableFrom(alg.ProblemType))
296          return;
297        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
298        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
299        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
300        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
301        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
302      }
303    }
304
305    private void algorithmTabPage_DragDrop(object sender, DragEventArgs e) {
306      if (e.Effect != DragDropEffects.None) {
307        var algorithm = e.Data.GetData(Constants.DragDropDataFormat) as IAlgorithm;
308        if (e.Effect.HasFlag(DragDropEffects.Copy)) algorithm = (IAlgorithm)algorithm.Clone();
309        Content.Algorithm = algorithm;
310      }
311    }
312    #endregion
313    #endregion
314  }
315}
Note: See TracBrowser for help on using the repository browser.