Changeset 12804 for branches/PerformanceComparison/HeuristicLab.Optimization.Views/3.3/IRRestarterView.cs
- Timestamp:
- 07/26/15 21:32:52 (9 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/PerformanceComparison/HeuristicLab.Optimization.Views/3.3/IRRestarterView.cs
r12764 r12804 22 22 using System; 23 23 using System.ComponentModel; 24 using System.Globalization; 24 25 using System.Linq; 25 using System.Text.RegularExpressions;26 26 using System.Windows.Forms; 27 using HeuristicLab. Collections;27 using HeuristicLab.Analysis; 28 28 using HeuristicLab.Common; 29 using HeuristicLab.Common.Resources;30 29 using HeuristicLab.Core; 31 30 using HeuristicLab.Core.Views; 32 31 using HeuristicLab.MainForm; 33 using HeuristicLab.MainForm.WindowsForms;34 32 using HeuristicLab.PluginInfrastructure; 35 33 36 34 namespace HeuristicLab.Optimization.Views { 37 [View(" TimeLimit RunView")]38 [Content(typeof( TimeLimitRun), IsDefaultView = true)]39 public partial class TimeLimitRunView : IOptimizerView {35 [View("Independent Random Restarter View")] 36 [Content(typeof(IndepdentRandomRestarter), IsDefaultView = true)] 37 public partial class IndependentRandomRestarterView : IOptimizerView { 40 38 protected TypeSelectorDialog algorithmTypeSelectorDialog; 41 39 protected virtual bool SuppressEvents { get; set; } 42 40 43 public new TimeLimitRunContent {44 get { return ( TimeLimitRun)base.Content; }41 public new IndepdentRandomRestarter Content { 42 get { return (IndepdentRandomRestarter)base.Content; } 45 43 set { base.Content = value; } 46 44 } 47 45 48 public TimeLimitRunView() {46 public IndependentRandomRestarterView() { 49 47 InitializeComponent(); 50 snapshotButton.Text = String.Empty; 51 snapshotButton.Image = VSImageLibrary.Camera; 48 terminationComboBox.Items.AddRange(Enum.GetValues(typeof(TerminationCriterium)).Cast<object>().ToArray()); 52 49 } 53 50 … … 62 59 protected override void DeregisterContentEvents() { 63 60 Content.PropertyChanged -= Content_PropertyChanged; 64 Content.SnapshotTimes.ItemsAdded -= Content_SnapshotTimes_Changed;65 Content.SnapshotTimes.ItemsMoved -= Content_SnapshotTimes_Changed;66 Content.SnapshotTimes.ItemsRemoved -= Content_SnapshotTimes_Changed;67 Content.SnapshotTimes.ItemsReplaced -= Content_SnapshotTimes_Changed;68 Content.SnapshotTimes.CollectionReset -= Content_SnapshotTimes_Changed;69 61 base.DeregisterContentEvents(); 70 62 } … … 72 64 base.RegisterContentEvents(); 73 65 Content.PropertyChanged += Content_PropertyChanged; 74 Content.SnapshotTimes.ItemsAdded += Content_SnapshotTimes_Changed;75 Content.SnapshotTimes.ItemsMoved += Content_SnapshotTimes_Changed;76 Content.SnapshotTimes.ItemsRemoved += Content_SnapshotTimes_Changed;77 Content.SnapshotTimes.ItemsReplaced += Content_SnapshotTimes_Changed;78 Content.SnapshotTimes.CollectionReset += Content_SnapshotTimes_Changed;79 66 } 80 67 … … 84 71 try { 85 72 if (Content == null) { 86 timeLimitTextBox.Text = TimeSpanHelper.FormatNatural(TimeSpan.FromSeconds(60)); 87 snapshotsTextBox.Text = String.Empty; 88 storeAlgorithmInEachSnapshotCheckBox.Checked = false; 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 89 80 algorithmViewHost.Content = null; 90 snapshotsView.Content = null;81 currentRunView.Content = null; 91 82 runsView.Content = null; 92 83 } else { 93 timeLimitTextBox.Text = TimeSpanHelper.FormatNatural(Content.MaximumExecutionTime); 94 snapshotsTextBox.Text = String.Join(" ; ", Content.SnapshotTimes.Select(x => TimeSpanHelper.FormatNatural(x, true))); 95 storeAlgorithmInEachSnapshotCheckBox.Checked = Content.StoreAlgorithmInEachSnapshot; 84 maxExecutionTimeTextBox.Text = TimeSpanHelper.FormatNatural(Content.MaximumExecutionTime); 85 maxEvaluationsTextBox.Text = Content.MaximumEvaluations.ToString(); 86 moveCostPerSolutionTextBox.Text = Content.MoveCostPerSolution.ToString(CultureInfo.CurrentCulture.NumberFormat); 87 targetValueTextBox.Text = Content.TargetValue.ToString(CultureInfo.CurrentCulture.NumberFormat); 88 maximizationCheckBox.Checked = Content.Maximization; 89 terminationComboBox.SelectedItem = Content.TerminationCriterium; 90 96 91 algorithmViewHost.Content = Content.Algorithm; 97 snapshotsView.Content = Content.Snapshots;92 currentRunView.Content = Content.CurrentRun; 98 93 runsView.Content = Content.Runs; 99 94 } … … 103 98 protected override void SetEnabledStateOfControls() { 104 99 base.SetEnabledStateOfControls(); 105 timeLimitTextBox.Enabled = Content != null && !ReadOnly; 106 snapshotsTextBox.Enabled = Content != null && !ReadOnly; 107 storeAlgorithmInEachSnapshotCheckBox.Enabled = Content != null && !ReadOnly; 100 maxExecutionTimeTextBox.Enabled = Content != null && !ReadOnly && !Locked; 101 maxEvaluationsTextBox.Enabled = Content != null && !ReadOnly && !Locked; 102 moveCostPerSolutionTextBox.Enabled = Content != null && !ReadOnly && !Locked; 103 targetValueTextBox.Enabled = Content != null && !ReadOnly && !Locked; 104 maximizationCheckBox.Enabled = Content != null && !ReadOnly && !Locked; 105 terminationComboBox.Enabled = Content != null && !ReadOnly && !Locked; 108 106 newAlgorithmButton.Enabled = Content != null && !ReadOnly; 109 107 openAlgorithmButton.Enabled = Content != null && !ReadOnly; 110 108 algorithmViewHost.Enabled = Content != null; 111 snapshotsView.Enabled = Content != null;109 currentRunView.Enabled = Content != null; 112 110 runsView.Enabled = Content != null; 113 }114 115 protected override void SetEnabledStateOfExecutableButtons() {116 base.SetEnabledStateOfExecutableButtons();117 snapshotButton.Enabled = Content != null && Content.Algorithm != null && Content.ExecutionState == ExecutionState.Paused;118 111 } 119 112 … … 133 126 #region Content events 134 127 private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) { 135 switch (e.PropertyName) {136 case "MaximumExecutionTime":137 SuppressEvents = true;138 try {139 timeLimitTextBox.Text = TimeSpanHelper.FormatNatural(Content.MaximumExecutionTime);140 } finally { SuppressEvents = false; }141 break;142 case "SnapshotTimes":143 SuppressEvents = true;144 try {145 if (Content.SnapshotTimes.Any())146 snapshotsTextBox.Text = String.Join(" ; ", Content.SnapshotTimes.Select(x => TimeSpanHelper.FormatNatural(x, true)));147 else snapshotsTextBox.Text = String.Empty;148 Content.SnapshotTimes.ItemsAdded += Content_SnapshotTimes_Changed;149 Content.SnapshotTimes.ItemsMoved += Content_SnapshotTimes_Changed;150 Content.SnapshotTimes.ItemsRemoved += Content_SnapshotTimes_Changed;151 Content.SnapshotTimes.ItemsReplaced += Content_SnapshotTimes_Changed;152 Content.SnapshotTimes.CollectionReset += Content_SnapshotTimes_Changed;153 } finally { SuppressEvents = false; }154 break;155 case "StoreAlgorithmInEachSnapshot":156 SuppressEvents = true;157 try {158 storeAlgorithmInEachSnapshotCheckBox.Checked = Content.StoreAlgorithmInEachSnapshot;159 } finally { SuppressEvents = false; }160 break;161 case "Algorithm":162 SuppressEvents = true;163 try {164 algorithmViewHost.Content = Content.Algorithm;165 } finally { SuppressEvents = false; }166 break;167 case "Snapshots":168 SuppressEvents = true;169 try {170 snapshotsView.Content = Content.Snapshots;171 } finally { SuppressEvents = false; }172 break;173 case "Runs":174 SuppressEvents = true;175 try {176 runsView.Content = Content.Runs;177 } finally { SuppressEvents = false; }178 break;179 }180 }181 182 private void Content_SnapshotTimes_Changed(object sender, EventArgs e) {183 128 SuppressEvents = true; 184 129 try { 185 if (Content.SnapshotTimes.Any()) 186 snapshotsTextBox.Text = string.Join(" ; ", Content.SnapshotTimes.Select(x => TimeSpanHelper.FormatNatural(x, true))); 187 else snapshotsTextBox.Text = String.Empty; 130 switch (e.PropertyName) { 131 case "TerminationCriterium": terminationComboBox.SelectedItem = Content.TerminationCriterium; break; 132 case "MaximumExecutionTime": maxExecutionTimeTextBox.Text = TimeSpanHelper.FormatNatural(Content.MaximumExecutionTime); break; 133 case "MaximumEvaluations": maxEvaluationsTextBox.Text = Content.MaximumEvaluations.ToString(); break; 134 case "TargetValue": targetValueTextBox.Text = Content.TargetValue.ToString(CultureInfo.CurrentCulture.NumberFormat); break; 135 case "Maximization": maximizationCheckBox.Checked = Content.Maximization; break; 136 case "MoveCostPerSolution": moveCostPerSolutionTextBox.Text = Content.MoveCostPerSolution.ToString(CultureInfo.CurrentCulture.NumberFormat); break; 137 case "Algorithm": algorithmViewHost.Content = Content.Algorithm; break; 138 case "CurrentRun": currentRunView.Content = Content.CurrentRun; break; 139 case "Runs": runsView.Content = Content.Runs; break; 140 } 188 141 } finally { SuppressEvents = false; } 189 142 } … … 191 144 192 145 #region Control events 193 private void timeLimitTextBox_Validating(object sender, CancelEventArgs e) { 194 if (SuppressEvents) return; 146 private void maxExecutionTimeTextBox_Validating(object sender, CancelEventArgs e) { 147 if (SuppressEvents) return; 148 if (InvokeRequired) { 149 Invoke((Action<object, CancelEventArgs>)maxExecutionTimeTextBox_Validating, sender, e); 150 return; 151 } 195 152 TimeSpan ts; 196 if (!TimeSpanHelper.TryGetFromNaturalFormat( timeLimitTextBox.Text, out ts)) {197 e.Cancel = ! timeLimitTextBox.ReadOnly && timeLimitTextBox.Enabled;198 errorProvider.SetError( timeLimitTextBox, "Please enter a valid time span, e.g. 20 seconds ; 45s ; 4min ; 1h ; 3 hours ; 2 days ; 4d");153 if (!TimeSpanHelper.TryGetFromNaturalFormat(maxExecutionTimeTextBox.Text, out ts)) { 154 e.Cancel = !maxExecutionTimeTextBox.ReadOnly && maxExecutionTimeTextBox.Enabled; 155 errorProvider.SetError(maxExecutionTimeTextBox, "Please enter a valid time span, e.g. 20 seconds ; 45s ; 4min ; 1h ; 3 hours ; 2 days ; 4d"); 199 156 } else { 200 157 Content.MaximumExecutionTime = ts; 201 158 e.Cancel = false; 202 errorProvider.SetError(timeLimitTextBox, null); 203 } 204 } 205 206 private void snapshotsTextBox_Validating(object sender, CancelEventArgs e) { 207 if (SuppressEvents) return; 208 e.Cancel = false; 209 errorProvider.SetError(snapshotsTextBox, null); 210 211 var snapshotTimes = new ObservableList<TimeSpan>(); 212 var matches = Regex.Matches(snapshotsTextBox.Text, @"(\d+[ ;,\t]*\w+)"); 213 foreach (Match m in matches) { 214 TimeSpan value; 215 if (!TimeSpanHelper.TryGetFromNaturalFormat(m.Value, out value)) { 216 e.Cancel = !snapshotsTextBox.ReadOnly && snapshotsTextBox.Enabled; // don't cancel an operation that cannot be edited 217 errorProvider.SetError(snapshotsTextBox, "Error parsing " + m.Value + ", please provide a valid time span, e.g. 20 seconds ; 45s ; 4min ; 1h ; 3 hours ; 2 days ; 4d"); 218 return; 219 } else { 220 snapshotTimes.Add(value); 221 } 222 } 223 Content.SnapshotTimes = snapshotTimes; 224 } 225 226 private void storeAlgorithmInEachSnapshotCheckBox_CheckedChanged(object sender, EventArgs e) { 227 if (SuppressEvents) return; 228 SuppressEvents = true; 229 try { 230 Content.StoreAlgorithmInEachSnapshot = storeAlgorithmInEachSnapshotCheckBox.Checked; 231 } finally { SuppressEvents = false; } 159 errorProvider.SetError(maxExecutionTimeTextBox, null); 160 } 161 } 162 163 private void maxEvaluationsTextBox_Validating(object sender, CancelEventArgs e) { 164 if (SuppressEvents) return; 165 if (InvokeRequired) { 166 Invoke((Action<object, CancelEventArgs>)maxEvaluationsTextBox_Validating, sender, e); 167 return; 168 } 169 int value; 170 if (!int.TryParse(maxEvaluationsTextBox.Text, out value)) { 171 e.Cancel = !maxEvaluationsTextBox.ReadOnly && maxEvaluationsTextBox.Enabled; 172 errorProvider.SetError(maxEvaluationsTextBox, "Please enter a valid integer number."); 173 } else { 174 Content.MaximumEvaluations = value; 175 e.Cancel = false; 176 errorProvider.SetError(maxEvaluationsTextBox, null); 177 } 178 } 179 180 private void moveCostPerSolutionTextBox_Validating(object sender, CancelEventArgs e) { 181 if (SuppressEvents) return; 182 if (InvokeRequired) { 183 Invoke((Action<object, CancelEventArgs>)moveCostPerSolutionTextBox_Validating, sender, e); 184 return; 185 } 186 double value; 187 if (!double.TryParse(moveCostPerSolutionTextBox.Text, out value)) { 188 e.Cancel = !moveCostPerSolutionTextBox.ReadOnly && moveCostPerSolutionTextBox.Enabled; 189 errorProvider.SetError(moveCostPerSolutionTextBox, "Please enter a valid integer number."); 190 } else { 191 Content.MoveCostPerSolution = value; 192 e.Cancel = false; 193 errorProvider.SetError(moveCostPerSolutionTextBox, null); 194 } 195 } 196 197 private void targetValueTextBox_Validating(object sender, CancelEventArgs e) { 198 if (SuppressEvents) return; 199 if (InvokeRequired) { 200 Invoke((Action<object, CancelEventArgs>)targetValueTextBox_Validating, sender, e); 201 return; 202 } 203 double value; 204 if (!double.TryParse(targetValueTextBox.Text, out value)) { 205 e.Cancel = !targetValueTextBox.ReadOnly && targetValueTextBox.Enabled; 206 errorProvider.SetError(targetValueTextBox, "Please enter a valid integer number."); 207 } else { 208 Content.TargetValue = value; 209 e.Cancel = false; 210 errorProvider.SetError(targetValueTextBox, null); 211 } 212 } 213 214 private void maximizationCheckBox_CheckedChanged(object sender, EventArgs e) { 215 if (SuppressEvents) return; 216 if (InvokeRequired) { 217 Invoke((Action<object, EventArgs>)maximizationCheckBox_CheckedChanged, sender, e); 218 return; 219 } 220 Content.Maximization = maximizationCheckBox.Checked; 221 } 222 223 private void terminationComboBox_SelectedIndexChanged(object sender, EventArgs e) { 224 if (SuppressEvents) return; 225 if (InvokeRequired) { 226 Invoke((Action<object, EventArgs>)terminationComboBox_SelectedIndexChanged, sender, e); 227 return; 228 } 229 Content.TerminationCriterium = (TerminationCriterium)terminationComboBox.SelectedItem; 232 230 } 233 231 … … 236 234 algorithmTypeSelectorDialog = new TypeSelectorDialog { Caption = "Select Algorithm" }; 237 235 algorithmTypeSelectorDialog.TypeSelector.Caption = "Available Algorithms"; 238 algorithmTypeSelectorDialog.TypeSelector.Configure(typeof(IAlgorithm), false, true); 236 algorithmTypeSelectorDialog.TypeSelector.Configure(typeof(IAlgorithm) 237 , showNotInstantiableTypes: false, showGenericTypes: false); 239 238 } 240 239 if (algorithmTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) { … … 275 274 private void algorithmTabPage_DragEnterOver(object sender, DragEventArgs e) { 276 275 e.Effect = DragDropEffects.None; 277 if (!ReadOnly && (e.Data.GetData(Constants.DragDropDataFormat) is IAlgorithm)) { 276 var alg = e.Data.GetData(Constants.DragDropDataFormat) as IAlgorithm; 277 if (!ReadOnly && alg != null) { 278 if (!typeof(ISingleObjectiveHeuristicOptimizationProblem).IsAssignableFrom(alg.ProblemType)) 279 return; 278 280 if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link; // ALT key 279 281 else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key … … 291 293 } 292 294 } 293 294 private void snapshotButton_Click(object sender, EventArgs e) {295 Content.Snapshot();296 }297 298 private void sequenceButton_Click(object sender, EventArgs e) {299 using (var dialog = new DefineArithmeticTimeSpanProgressionDialog(TimeSpan.FromSeconds(1), Content.MaximumExecutionTime, TimeSpan.FromSeconds(1))) {300 if (dialog.ShowDialog() == DialogResult.OK) {301 if (dialog.Values.Any())302 Content.SnapshotTimes = new ObservableList<TimeSpan>(dialog.Values);303 else Content.SnapshotTimes = new ObservableList<TimeSpan>();304 }305 }306 }307 295 #endregion 308 296 #endregion
Note: See TracChangeset
for help on using the changeset viewer.