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 |
|
---|
22 | using System;
|
---|
23 | using System.ComponentModel;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Windows.Forms;
|
---|
26 | using HeuristicLab.Collections;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Common.Resources;
|
---|
29 | using HeuristicLab.Core;
|
---|
30 | using HeuristicLab.Core.Views;
|
---|
31 | using HeuristicLab.MainForm;
|
---|
32 | using HeuristicLab.MainForm.WindowsForms;
|
---|
33 | using HeuristicLab.PluginInfrastructure;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.Optimization.Views {
|
---|
36 | [View("TimeLimit Run View")]
|
---|
37 | [Content(typeof(TimeLimitRun), IsDefaultView = true)]
|
---|
38 | public partial class TimeLimitRunView : IOptimizerView {
|
---|
39 | protected TypeSelectorDialog algorithmTypeSelectorDialog;
|
---|
40 | protected virtual bool SuppressEvents { get; set; }
|
---|
41 |
|
---|
42 | public new TimeLimitRun Content {
|
---|
43 | get { return (TimeLimitRun)base.Content; }
|
---|
44 | set { base.Content = value; }
|
---|
45 | }
|
---|
46 |
|
---|
47 | public TimeLimitRunView() {
|
---|
48 | InitializeComponent();
|
---|
49 | snapshotButton.Text = String.Empty;
|
---|
50 | snapshotButton.Image = VSImageLibrary.Breakpoint;
|
---|
51 | }
|
---|
52 |
|
---|
53 | protected override void Dispose(bool disposing) {
|
---|
54 | if (disposing) {
|
---|
55 | if (algorithmTypeSelectorDialog != null) algorithmTypeSelectorDialog.Dispose();
|
---|
56 | if (components != null) components.Dispose();
|
---|
57 | }
|
---|
58 | base.Dispose(disposing);
|
---|
59 | }
|
---|
60 |
|
---|
61 | protected override void DeregisterContentEvents() {
|
---|
62 | Content.PropertyChanged -= Content_PropertyChanged;
|
---|
63 | Content.SnapshotTimes.ItemsAdded -= Content_SnapshotTimes_Changed;
|
---|
64 | Content.SnapshotTimes.ItemsMoved -= Content_SnapshotTimes_Changed;
|
---|
65 | Content.SnapshotTimes.ItemsRemoved -= Content_SnapshotTimes_Changed;
|
---|
66 | Content.SnapshotTimes.ItemsReplaced -= Content_SnapshotTimes_Changed;
|
---|
67 | Content.SnapshotTimes.CollectionReset -= Content_SnapshotTimes_Changed;
|
---|
68 | base.DeregisterContentEvents();
|
---|
69 | }
|
---|
70 | protected override void RegisterContentEvents() {
|
---|
71 | base.RegisterContentEvents();
|
---|
72 | Content.PropertyChanged += Content_PropertyChanged;
|
---|
73 | Content.SnapshotTimes.ItemsAdded += Content_SnapshotTimes_Changed;
|
---|
74 | Content.SnapshotTimes.ItemsMoved += Content_SnapshotTimes_Changed;
|
---|
75 | Content.SnapshotTimes.ItemsRemoved += Content_SnapshotTimes_Changed;
|
---|
76 | Content.SnapshotTimes.ItemsReplaced += Content_SnapshotTimes_Changed;
|
---|
77 | Content.SnapshotTimes.CollectionReset += Content_SnapshotTimes_Changed;
|
---|
78 | }
|
---|
79 |
|
---|
80 | protected override void OnContentChanged() {
|
---|
81 | base.OnContentChanged();
|
---|
82 | SuppressEvents = true;
|
---|
83 | try {
|
---|
84 | if (Content == null) {
|
---|
85 | timeLimitTextBox.Text = FormatTimeSpan(TimeSpan.FromSeconds(60));
|
---|
86 | snapshotsTextBox.Text = String.Empty;
|
---|
87 | storeAlgorithmInEachSnapshotCheckBox.Checked = false;
|
---|
88 | algorithmViewHost.Content = null;
|
---|
89 | snapshotsView.Content = null;
|
---|
90 | runsView.Content = null;
|
---|
91 | } else {
|
---|
92 | timeLimitTextBox.Text = FormatTimeSpan(Content.MaximumExecutionTime);
|
---|
93 | snapshotsTextBox.Text = String.Join(" ; ", Content.SnapshotTimes.Select(x => FormatTimeSpan(x, true)));
|
---|
94 | storeAlgorithmInEachSnapshotCheckBox.Checked = Content.StoreAlgorithmInEachSnapshot;
|
---|
95 | algorithmViewHost.Content = Content.Algorithm;
|
---|
96 | snapshotsView.Content = Content.Snapshots;
|
---|
97 | runsView.Content = Content.Runs;
|
---|
98 | }
|
---|
99 | } finally { SuppressEvents = false; }
|
---|
100 | }
|
---|
101 |
|
---|
102 | protected override void SetEnabledStateOfControls() {
|
---|
103 | base.SetEnabledStateOfControls();
|
---|
104 | timeLimitTextBox.Enabled = Content != null && !ReadOnly;
|
---|
105 | snapshotsTextBox.Enabled = Content != null && !ReadOnly;
|
---|
106 | storeAlgorithmInEachSnapshotCheckBox.Enabled = Content != null && !ReadOnly;
|
---|
107 | newAlgorithmButton.Enabled = Content != null && !ReadOnly;
|
---|
108 | openAlgorithmButton.Enabled = Content != null && !ReadOnly;
|
---|
109 | algorithmViewHost.Enabled = Content != null;
|
---|
110 | snapshotsView.Enabled = Content != null;
|
---|
111 | runsView.Enabled = Content != null;
|
---|
112 | }
|
---|
113 |
|
---|
114 | protected override void SetEnabledStateOfExecutableButtons() {
|
---|
115 | base.SetEnabledStateOfExecutableButtons();
|
---|
116 | snapshotButton.Enabled = Content != null && Content.Algorithm != null && Content.ExecutionState == ExecutionState.Paused;
|
---|
117 | }
|
---|
118 |
|
---|
119 | protected override void OnClosed(FormClosedEventArgs e) {
|
---|
120 | if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) {
|
---|
121 | //The content must be stopped if no other view showing the content is available
|
---|
122 | var optimizers = MainFormManager.MainForm.Views.OfType<IContentView>().Where(v => v != this).Select(v => v.Content).OfType<IAlgorithm>();
|
---|
123 | if (!optimizers.Contains(Content.Algorithm)) {
|
---|
124 | var nestedOptimizers = optimizers.SelectMany(opt => opt.NestedOptimizers);
|
---|
125 | if (!nestedOptimizers.Contains(Content)) Content.Stop();
|
---|
126 | }
|
---|
127 | }
|
---|
128 | base.OnClosed(e);
|
---|
129 | }
|
---|
130 |
|
---|
131 | #region Event Handlers
|
---|
132 | #region Content events
|
---|
133 | private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) {
|
---|
134 | switch (e.PropertyName) {
|
---|
135 | case "MaximumExecutionTime":
|
---|
136 | SuppressEvents = true;
|
---|
137 | try {
|
---|
138 | timeLimitTextBox.Text = FormatTimeSpan(Content.MaximumExecutionTime);
|
---|
139 | } finally { SuppressEvents = false; }
|
---|
140 | break;
|
---|
141 | case "SnapshotTimes":
|
---|
142 | SuppressEvents = true;
|
---|
143 | try {
|
---|
144 | snapshotsTextBox.Text = string.Join(" ; ", Content.SnapshotTimes.Select(x => FormatTimeSpan(x, true)));
|
---|
145 | Content.SnapshotTimes.ItemsAdded += Content_SnapshotTimes_Changed;
|
---|
146 | Content.SnapshotTimes.ItemsMoved += Content_SnapshotTimes_Changed;
|
---|
147 | Content.SnapshotTimes.ItemsRemoved += Content_SnapshotTimes_Changed;
|
---|
148 | Content.SnapshotTimes.ItemsReplaced += Content_SnapshotTimes_Changed;
|
---|
149 | Content.SnapshotTimes.CollectionReset += Content_SnapshotTimes_Changed;
|
---|
150 | } finally { SuppressEvents = false; }
|
---|
151 | break;
|
---|
152 | case "StoreAlgorithmInEachSnapshot":
|
---|
153 | SuppressEvents = true;
|
---|
154 | try {
|
---|
155 | storeAlgorithmInEachSnapshotCheckBox.Checked = Content.StoreAlgorithmInEachSnapshot;
|
---|
156 | } finally { SuppressEvents = false; }
|
---|
157 | break;
|
---|
158 | case "Algorithm":
|
---|
159 | SuppressEvents = true;
|
---|
160 | try {
|
---|
161 | algorithmViewHost.Content = Content.Algorithm;
|
---|
162 | } finally { SuppressEvents = false; }
|
---|
163 | break;
|
---|
164 | case "Snapshots":
|
---|
165 | SuppressEvents = true;
|
---|
166 | try {
|
---|
167 | snapshotsView.Content = Content.Snapshots;
|
---|
168 | } finally { SuppressEvents = false; }
|
---|
169 | break;
|
---|
170 | case "Runs":
|
---|
171 | SuppressEvents = true;
|
---|
172 | try {
|
---|
173 | runsView.Content = Content.Runs;
|
---|
174 | } finally { SuppressEvents = false; }
|
---|
175 | break;
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | private void Content_SnapshotTimes_Changed(object sender, EventArgs e) {
|
---|
180 | SuppressEvents = true;
|
---|
181 | try {
|
---|
182 | snapshotsTextBox.Text = string.Join(" ; ", Content.SnapshotTimes.Select(x => FormatTimeSpan(x, true)));
|
---|
183 | } finally { SuppressEvents = false; }
|
---|
184 | }
|
---|
185 | #endregion
|
---|
186 |
|
---|
187 | #region Control events
|
---|
188 | private void timeLimitTextBox_Validating(object sender, CancelEventArgs e) {
|
---|
189 | if (SuppressEvents) return;
|
---|
190 | TimeSpan ts;
|
---|
191 | if (!TryGetTimeSpanFromFormat(timeLimitTextBox.Text, out ts)) {
|
---|
192 | e.Cancel = true;
|
---|
193 | errorProvider.SetError(timeLimitTextBox, "Please enter a valid time span, e.g. 60, 20s, 45 seconds, 3h, 1 hour, 4 d, 2 days");
|
---|
194 | } else {
|
---|
195 | Content.MaximumExecutionTime = ts;
|
---|
196 | e.Cancel = false;
|
---|
197 | errorProvider.SetError(timeLimitTextBox, String.Empty);
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | private void snapshotsTextBox_Validating(object sender, CancelEventArgs e) {
|
---|
202 | if (SuppressEvents) return;
|
---|
203 | e.Cancel = false;
|
---|
204 | errorProvider.SetError(timeLimitTextBox, String.Empty);
|
---|
205 |
|
---|
206 | var tokens = snapshotsTextBox.Text.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
|
---|
207 |
|
---|
208 | var snapshotTimes = new ObservableList<TimeSpan>();
|
---|
209 | foreach (var t in tokens) {
|
---|
210 | TimeSpan value;
|
---|
211 | if (!TryGetTimeSpanFromFormat(t, out value)) {
|
---|
212 | e.Cancel = true;
|
---|
213 | errorProvider.SetError(snapshotsTextBox, "Error parsing " + t + ", please provide a valid time span, e.g. 60, 20s, 45 seconds, 3h, 1 hour, 4 d, 2 days");
|
---|
214 | return;
|
---|
215 | } else {
|
---|
216 | snapshotTimes.Add(value);
|
---|
217 | }
|
---|
218 | }
|
---|
219 | Content.SnapshotTimes = snapshotTimes;
|
---|
220 | }
|
---|
221 |
|
---|
222 | private void storeAlgorithmInEachSnapshotCheckBox_CheckedChanged(object sender, EventArgs e) {
|
---|
223 | if (SuppressEvents) return;
|
---|
224 | SuppressEvents = true;
|
---|
225 | try {
|
---|
226 | Content.StoreAlgorithmInEachSnapshot = storeAlgorithmInEachSnapshotCheckBox.Checked;
|
---|
227 | } finally { SuppressEvents = false; }
|
---|
228 | }
|
---|
229 |
|
---|
230 | private void newAlgorithmButton_Click(object sender, EventArgs e) {
|
---|
231 | if (algorithmTypeSelectorDialog == null) {
|
---|
232 | algorithmTypeSelectorDialog = new TypeSelectorDialog { Caption = "Select Algorithm" };
|
---|
233 | algorithmTypeSelectorDialog.TypeSelector.Caption = "Available Algorithms";
|
---|
234 | algorithmTypeSelectorDialog.TypeSelector.Configure(typeof(IAlgorithm), false, true);
|
---|
235 | }
|
---|
236 | if (algorithmTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
237 | try {
|
---|
238 | Content.Algorithm = (IAlgorithm)algorithmTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
|
---|
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 | newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = false;
|
---|
249 | algorithmViewHost.Enabled = false;
|
---|
250 |
|
---|
251 | ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
|
---|
252 | try {
|
---|
253 | if (error != null) throw error;
|
---|
254 | var algorithm = content as IAlgorithm;
|
---|
255 | if (algorithm == null)
|
---|
256 | MessageBox.Show(this, "The selected file does not contain an algorithm.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
257 | else
|
---|
258 | Content.Algorithm = algorithm;
|
---|
259 | } catch (Exception ex) {
|
---|
260 | ErrorHandling.ShowErrorDialog(this, ex);
|
---|
261 | } finally {
|
---|
262 | Invoke(new Action(delegate() {
|
---|
263 | algorithmViewHost.Enabled = true;
|
---|
264 | newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = true;
|
---|
265 | }));
|
---|
266 | }
|
---|
267 | });
|
---|
268 | }
|
---|
269 | }
|
---|
270 |
|
---|
271 | private void algorithmTabPage_DragEnterOver(object sender, DragEventArgs e) {
|
---|
272 | e.Effect = DragDropEffects.None;
|
---|
273 | if (!ReadOnly && (e.Data.GetData(Constants.DragDropDataFormat) is IAlgorithm)) {
|
---|
274 | if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link; // ALT key
|
---|
275 | else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key
|
---|
276 | else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
|
---|
277 | else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
|
---|
278 | else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
|
---|
279 | }
|
---|
280 | }
|
---|
281 |
|
---|
282 | private void algorithmTabPage_DragDrop(object sender, DragEventArgs e) {
|
---|
283 | if (e.Effect != DragDropEffects.None) {
|
---|
284 | var algorithm = e.Data.GetData(Constants.DragDropDataFormat) as IAlgorithm;
|
---|
285 | if (e.Effect.HasFlag(DragDropEffects.Copy)) algorithm = (IAlgorithm)algorithm.Clone();
|
---|
286 | Content.Algorithm = algorithm;
|
---|
287 | }
|
---|
288 | }
|
---|
289 |
|
---|
290 | private void snapshotButton_Click(object sender, EventArgs e) {
|
---|
291 | Content.Snapshot();
|
---|
292 | }
|
---|
293 |
|
---|
294 | private void sequenceButton_Click(object sender, EventArgs e) {
|
---|
295 | using (var dialog = new DefineArithmeticProgressionDialog(false, 1, Content.MaximumExecutionTime.TotalSeconds, 1)) {
|
---|
296 | if (dialog.ShowDialog() == DialogResult.OK) {
|
---|
297 | Content.SnapshotTimes = new ObservableList<TimeSpan>(dialog.Values.Select(TimeSpan.FromSeconds));
|
---|
298 | }
|
---|
299 | }
|
---|
300 | }
|
---|
301 | #endregion
|
---|
302 | #endregion
|
---|
303 |
|
---|
304 | private string FormatTimeSpan(TimeSpan ts, bool abbrevUnit = false) {
|
---|
305 | if (ts.TotalSeconds < 180) return ts.TotalSeconds + (abbrevUnit ? "s" : " seconds");
|
---|
306 | if (ts.TotalMinutes < 180) return ts.TotalMinutes + (abbrevUnit ? "min" : " minutes");
|
---|
307 | if (ts.TotalHours < 96) return ts.TotalHours + (abbrevUnit ? "h" : " hours");
|
---|
308 | return ts.TotalDays + (abbrevUnit ? "d" : " days");
|
---|
309 | }
|
---|
310 |
|
---|
311 | private bool TryGetTimeSpanFromFormat(string text, out TimeSpan result) {
|
---|
312 | double value;
|
---|
313 | text = text.Trim();
|
---|
314 | var length = text.Length;
|
---|
315 | while (!double.TryParse(text.Substring(0, length), out value)) {
|
---|
316 | length--;
|
---|
317 | if (length <= 0) {
|
---|
318 | value = double.NaN;
|
---|
319 | break;
|
---|
320 | }
|
---|
321 | }
|
---|
322 | if (double.IsNaN(value)) {
|
---|
323 | result = TimeSpan.Zero;
|
---|
324 | return false;
|
---|
325 | } else {
|
---|
326 | var remaining = String.Empty;
|
---|
327 | if (length < text.Length) remaining = text.Substring(length, text.Length - length);
|
---|
328 | switch (remaining.Trim()) {
|
---|
329 | case "d":
|
---|
330 | case "day":
|
---|
331 | case "days": result = TimeSpan.FromDays(value); break;
|
---|
332 | case "h":
|
---|
333 | case "hour":
|
---|
334 | case "hours": result = TimeSpan.FromHours(value); break;
|
---|
335 | case "min":
|
---|
336 | case "minute":
|
---|
337 | case "minutes": result = TimeSpan.FromMinutes(value); break;
|
---|
338 | default: result = TimeSpan.FromSeconds(value); break;
|
---|
339 | }
|
---|
340 | return true;
|
---|
341 | }
|
---|
342 | }
|
---|
343 | }
|
---|
344 | }
|
---|