Changeset 7885
- Timestamp:
- 05/24/12 10:22:51 (13 years ago)
- Location:
- trunk/sources/HeuristicLab.Optimizer/3.3
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Optimizer/3.3/CreateExperimentDialog.Designer.cs
r7846 r7885 61 61 this.selectAllCheckBox = new System.Windows.Forms.CheckBox(); 62 62 this.selectNoneCheckBox = new System.Windows.Forms.CheckBox(); 63 this.instanceDiscoveryBackgroundWorker = new System.ComponentModel.BackgroundWorker(); 63 64 ((System.ComponentModel.ISupportInitialize)(this.repetitionsNumericUpDown)).BeginInit(); 64 65 this.SuspendLayout(); … … 181 182 this.experimentCreationBackgroundWorker.WorkerSupportsCancellation = true; 182 183 this.experimentCreationBackgroundWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(this.experimentCreationBackgroundWorker_DoWork); 183 this.experimentCreationBackgroundWorker.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this. experimentCreationBackgroundWorker_ProgressChanged);184 this.experimentCreationBackgroundWorker.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.backgroundWorker_ProgressChanged); 184 185 this.experimentCreationBackgroundWorker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.experimentCreationBackgroundWorker_RunWorkerCompleted); 185 186 // 186 187 // experimentCreationProgressBar 187 188 // 188 this.experimentCreationProgressBar.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 189 | System.Windows.Forms.AnchorStyles.Right))); 190 this.experimentCreationProgressBar.Location = new System.Drawing.Point(15, 259); 189 this.experimentCreationProgressBar.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right))); 190 this.experimentCreationProgressBar.Location = new System.Drawing.Point(128, 142); 191 191 this.experimentCreationProgressBar.Name = "experimentCreationProgressBar"; 192 this.experimentCreationProgressBar.Size = new System.Drawing.Size( 92, 23);192 this.experimentCreationProgressBar.Size = new System.Drawing.Size(128, 23); 193 193 this.experimentCreationProgressBar.Style = System.Windows.Forms.ProgressBarStyle.Continuous; 194 194 this.experimentCreationProgressBar.TabIndex = 7; … … 197 197 // progressLabel 198 198 // 199 this.progressLabel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles. Bottom| System.Windows.Forms.AnchorStyles.Right)));199 this.progressLabel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right))); 200 200 this.progressLabel.BackColor = System.Drawing.SystemColors.Control; 201 this.progressLabel.Location = new System.Drawing.Point(1 13, 259);201 this.progressLabel.Location = new System.Drawing.Point(128, 168); 202 202 this.progressLabel.Name = "progressLabel"; 203 this.progressLabel.Size = new System.Drawing.Size( 75, 23);203 this.progressLabel.Size = new System.Drawing.Size(128, 23); 204 204 this.progressLabel.TabIndex = 8; 205 205 this.progressLabel.Text = "label1"; 206 this.progressLabel.TextAlign = System.Drawing.ContentAlignment.Middle Left;206 this.progressLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; 207 207 this.progressLabel.Visible = false; 208 208 // … … 229 229 this.selectNoneCheckBox.CheckedChanged += new System.EventHandler(this.selectNoneCheckBox_CheckedChanged); 230 230 // 231 // instanceDiscoveryBackgroundWorker 232 // 233 this.instanceDiscoveryBackgroundWorker.WorkerReportsProgress = true; 234 this.instanceDiscoveryBackgroundWorker.WorkerSupportsCancellation = true; 235 this.instanceDiscoveryBackgroundWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(this.instanceDiscoveryBackgroundWorker_DoWork); 236 this.instanceDiscoveryBackgroundWorker.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.backgroundWorker_ProgressChanged); 237 this.instanceDiscoveryBackgroundWorker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.instanceDiscoveryBackgroundWorker_RunWorkerCompleted); 238 // 231 239 // CreateExperimentDialog 232 240 // … … 236 244 this.CancelButton = this.cancelButton; 237 245 this.ClientSize = new System.Drawing.Size(281, 294); 246 this.Controls.Add(this.progressLabel); 247 this.Controls.Add(this.experimentCreationProgressBar); 238 248 this.Controls.Add(this.selectNoneCheckBox); 239 249 this.Controls.Add(this.selectAllCheckBox); … … 246 256 this.Controls.Add(this.cancelButton); 247 257 this.Controls.Add(this.okButton); 248 this.Controls.Add(this.progressLabel);249 this.Controls.Add(this.experimentCreationProgressBar);250 258 this.MaximizeBox = false; 251 259 this.MinimizeBox = false; … … 257 265 this.TopMost = true; 258 266 this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.CreateExperimentDialog_FormClosing); 267 this.Load += new System.EventHandler(this.CreateExperimentDialog_Load); 259 268 ((System.ComponentModel.ISupportInitialize)(this.repetitionsNumericUpDown)).EndInit(); 260 269 this.ResumeLayout(false); … … 280 289 private System.Windows.Forms.CheckBox selectAllCheckBox; 281 290 private System.Windows.Forms.CheckBox selectNoneCheckBox; 291 private System.ComponentModel.BackgroundWorker instanceDiscoveryBackgroundWorker; 282 292 283 293 } -
trunk/sources/HeuristicLab.Optimizer/3.3/CreateExperimentDialog.cs
r7846 r7885 24 24 using System.ComponentModel; 25 25 using System.Linq; 26 using System.Text; 26 27 using System.Threading; 27 28 using System.Windows.Forms; … … 38 39 experiment = null; 39 40 okButton.Enabled = optimizer != null; 40 FillOrHideInstanceListView();41 SetInstanceListViewVisibility(); 41 42 } 42 43 } … … 57 58 createBatchRun = createBatchRunCheckBox.Checked; 58 59 repetitions = (int)repetitionsNumericUpDown.Value; 59 Optimizer = optimizer; 60 } 61 62 private void FillOrHideInstanceListView() { 63 if (optimizer != null && optimizer is IAlgorithm) { 64 var algorithm = (IAlgorithm)Optimizer; 65 if (algorithm.Problem != null) { 66 var instanceProviders = ProblemInstanceManager.GetProviders(algorithm.Problem); 67 if (instanceProviders.Any()) { 68 FillInstanceListView(instanceProviders); 69 if (instancesListView.Items.Count > 0) { 70 selectAllCheckBox.Visible = true; 71 selectNoneCheckBox.Visible = true; 72 instancesLabel.Visible = true; 73 instancesListView.Visible = true; 74 Height = 330; 75 return; 76 } 77 } 78 } 79 } 80 selectAllCheckBox.Visible = false; 81 selectNoneCheckBox.Visible = false; 82 instancesLabel.Visible = false; 83 instancesListView.Visible = false; 84 Height = 130; 85 } 86 87 private void FillInstanceListView(IEnumerable<IProblemInstanceProvider> instanceProviders) { 88 foreach (var provider in instanceProviders) { 89 var group = new ListViewGroup(provider.Name, provider.Name); 90 group.Tag = provider; 91 instancesListView.Groups.Add(group); 92 foreach (var d in ProblemInstanceManager.GetDataDescriptors(provider)) { 93 var item = new ListViewItem(d.Name, group); 94 item.Tag = d; 95 instancesListView.Items.Add(item); 96 } 97 } 98 instancesListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent); 99 selectAllCheckBox.Checked = true; 100 } 101 102 private void createBatchRunCheckBox_CheckedChanged(object sender, EventArgs e) { 103 repetitionsNumericUpDown.Enabled = createBatchRunCheckBox.Checked; 104 createBatchRun = createBatchRunCheckBox.Checked; 105 } 106 private void repetitionsNumericUpDown_Validated(object sender, EventArgs e) { 107 if (repetitionsNumericUpDown.Text == string.Empty) 108 repetitionsNumericUpDown.Text = repetitionsNumericUpDown.Value.ToString(); 109 repetitions = (int)repetitionsNumericUpDown.Value; 110 } 60 // do not set the Optimizer property here, because we want to delay instance discovery to the time when the form loads 61 this.optimizer = optimizer; 62 this.experiment = null; 63 okButton.Enabled = optimizer != null; 64 } 65 66 #region Event handlers 67 private void CreateExperimentDialog_Load(object sender, EventArgs e) { 68 SetInstanceListViewVisibility(); 69 } 70 71 private void CreateExperimentDialog_FormClosing(object sender, FormClosingEventArgs e) { 72 if (experimentCreationBackgroundWorker.IsBusy) { 73 if (DialogResult != System.Windows.Forms.DialogResult.OK) { 74 if (experimentCreationBackgroundWorker.IsBusy) experimentCreationBackgroundWorker.CancelAsync(); 75 if (instanceDiscoveryBackgroundWorker.IsBusy) instanceDiscoveryBackgroundWorker.CancelAsync(); 76 } 77 e.Cancel = true; 78 } 79 } 80 81 private void okButton_Click(object sender, EventArgs e) { 82 SetMode(locked: true); 83 experimentCreationBackgroundWorker.RunWorkerAsync(GetSelectedInstances()); 84 backgroundWorkerWaitHandle.WaitOne(); // make sure the background worker has started before exiting 85 } 86 87 private void instancesListView_ItemChecked(object sender, ItemCheckedEventArgs e) { 88 if (!suppressListViewEventHandling) { 89 selectAllCheckBox.Checked = instancesListView.Items.Count == instancesListView.CheckedItems.Count; 90 selectNoneCheckBox.Checked = instancesListView.CheckedItems.Count == 0; 91 } 92 } 93 111 94 private void selectAllCheckBox_CheckedChanged(object sender, EventArgs e) { 112 95 if (selectAllCheckBox.Checked) { … … 121 104 } 122 105 } 106 123 107 private void selectNoneCheckBox_CheckedChanged(object sender, EventArgs e) { 124 108 if (selectNoneCheckBox.Checked) { … … 133 117 } 134 118 } 135 private void instancesListView_ItemChecked(object sender, ItemCheckedEventArgs e) { 136 if (!suppressListViewEventHandling) { 137 selectAllCheckBox.Checked = instancesListView.Items.Count == instancesListView.CheckedItems.Count; 138 selectNoneCheckBox.Checked = instancesListView.CheckedItems.Count == 0; 139 } 140 } 141 private void okButton_Click(object sender, EventArgs e) { 142 SetMode(createExperiment: true); 143 experimentCreationBackgroundWorker.RunWorkerAsync(GetSelectedInstances()); 144 backgroundWorkerWaitHandle.WaitOne(); 145 } 119 120 private void createBatchRunCheckBox_CheckedChanged(object sender, EventArgs e) { 121 repetitionsNumericUpDown.Enabled = createBatchRunCheckBox.Checked; 122 createBatchRun = createBatchRunCheckBox.Checked; 123 } 124 125 private void repetitionsNumericUpDown_Validated(object sender, EventArgs e) { 126 if (repetitionsNumericUpDown.Text == string.Empty) 127 repetitionsNumericUpDown.Text = repetitionsNumericUpDown.Value.ToString(); 128 repetitions = (int)repetitionsNumericUpDown.Value; 129 } 130 #endregion 131 132 #region Helpers 133 private void SetInstanceListViewVisibility() { 134 bool instancesAvailable = optimizer != null 135 && optimizer is IAlgorithm 136 && ((IAlgorithm)optimizer).Problem != null 137 && ProblemInstanceManager.GetProviders(((IAlgorithm)optimizer).Problem).Any(); 138 selectAllCheckBox.Visible = instancesAvailable; 139 selectNoneCheckBox.Visible = instancesAvailable; 140 instancesLabel.Visible = instancesAvailable; 141 instancesListView.Visible = instancesAvailable; 142 if (instancesAvailable) { 143 Height = 330; 144 FillInstanceListViewAsync(); 145 } else Height = 130; 146 } 147 148 private void FillInstanceListViewAsync() { 149 SetMode(locked: true); 150 var instanceProviders = ProblemInstanceManager.GetProviders(((IAlgorithm)Optimizer).Problem); 151 instanceDiscoveryBackgroundWorker.RunWorkerAsync(instanceProviders); 152 } 153 154 private void AddOptimizer(IOptimizer optimizer, Experiment experiment) { 155 if (createBatchRun) { 156 var batchRun = new BatchRun(); 157 batchRun.Repetitions = repetitions; 158 batchRun.Optimizer = optimizer; 159 experiment.Optimizers.Add(batchRun); 160 } else { 161 experiment.Optimizers.Add(optimizer); 162 } 163 } 164 165 private void SetMode(bool locked) { 166 createBatchRunCheckBox.Enabled = !locked; 167 repetitionsNumericUpDown.Enabled = !locked; 168 selectAllCheckBox.Enabled = !locked; 169 selectNoneCheckBox.Enabled = !locked; 170 instancesListView.Enabled = !locked; 171 instancesListView.Visible = !locked; 172 okButton.Enabled = !locked; 173 okButton.Visible = !locked; 174 progressLabel.Visible = locked; 175 experimentCreationProgressBar.Visible = locked; 176 } 177 178 private Dictionary<IProblemInstanceProvider, List<IDataDescriptor>> GetSelectedInstances() { 179 var selectedInstances = new Dictionary<IProblemInstanceProvider, List<IDataDescriptor>>(); 180 foreach (var checkedItem in instancesListView.CheckedItems.OfType<ListViewItem>()) { 181 if (!selectedInstances.ContainsKey((IProblemInstanceProvider)checkedItem.Group.Tag)) 182 selectedInstances.Add((IProblemInstanceProvider)checkedItem.Group.Tag, new List<IDataDescriptor>()); 183 selectedInstances[(IProblemInstanceProvider)checkedItem.Group.Tag].Add((IDataDescriptor)checkedItem.Tag); 184 } 185 return selectedInstances; 186 } 187 #endregion 188 189 #region Background workers 190 private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { 191 experimentCreationProgressBar.Value = e.ProgressPercentage; 192 progressLabel.Text = (string)e.UserState; 193 } 194 195 private void instanceDiscoveryBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) { 196 double progress = 0; 197 instanceDiscoveryBackgroundWorker.ReportProgress((int)progress, string.Empty); 198 var instanceProviders = ((IEnumerable<IProblemInstanceProvider>)e.Argument).ToArray(); 199 ListViewGroup[] groups = new ListViewGroup[instanceProviders.Length]; 200 for (int i = 0; i < instanceProviders.Length; i++) { 201 var provider = instanceProviders[i]; 202 groups[i] = new ListViewGroup(provider.Name, provider.Name) { Tag = provider }; 203 } 204 e.Result = groups; 205 for (int i = 0; i < groups.Length; i++) { 206 var group = groups[i]; 207 var provider = group.Tag as IProblemInstanceProvider; 208 progress = (100.0 * i) / groups.Length; 209 instanceDiscoveryBackgroundWorker.ReportProgress((int)progress, provider.Name); 210 var descriptors = ProblemInstanceManager.GetDataDescriptors(provider).ToArray(); 211 for (int j = 0; j < descriptors.Length; j++) { 212 #region Check cancellation request 213 if (instanceDiscoveryBackgroundWorker.CancellationPending) { 214 e.Cancel = true; 215 return; 216 } 217 #endregion 218 var d = descriptors[j]; 219 progress += 1.0 / (descriptors.Length * groups.Length); 220 instanceDiscoveryBackgroundWorker.ReportProgress((int)progress, d.Name); 221 var item = new ListViewItem(d.Name, group) { Tag = d }; 222 } 223 } 224 instanceDiscoveryBackgroundWorker.ReportProgress(100, string.Empty); 225 } 226 227 private void instanceDiscoveryBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { 228 try { 229 // unfortunately it's not enough to just add the groups, the items need to be added separately 230 foreach (var group in (ListViewGroup[])e.Result) { 231 instancesListView.Groups.Add(group); 232 instancesListView.Items.AddRange(group.Items); 233 } 234 instancesListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent); 235 selectAllCheckBox.Checked = true; 236 } catch { } 237 try { 238 SetMode(locked: false); 239 if (e.Error != null) MessageBox.Show(e.Error.Message, "Error occurred", MessageBoxButtons.OK, MessageBoxIcon.Error); 240 } catch { } 241 } 242 243 private StringBuilder failedInstances; 146 244 private void experimentCreationBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) { 147 245 backgroundWorkerWaitHandle.Set(); 148 246 experimentCreationBackgroundWorker.ReportProgress(0, string.Empty); 247 failedInstances = new StringBuilder(); 149 248 var items = (Dictionary<IProblemInstanceProvider, List<IDataDescriptor>>)e.Argument; 150 249 var localExperiment = new Experiment(); … … 156 255 foreach (var provider in items.Keys) { 157 256 foreach (var descriptor in items[provider]) { 158 var algorithm = (IAlgorithm)Optimizer.Clone(); 159 ProblemInstanceManager.LoadData(provider, descriptor, (IProblemInstanceConsumer)algorithm.Problem); 160 AddOptimizer(algorithm, localExperiment); 161 counter++; 162 experimentCreationBackgroundWorker.ReportProgress((int)Math.Round(100.0 * counter / total), descriptor.Name); 257 #region Check cancellation request 163 258 if (experimentCreationBackgroundWorker.CancellationPending) { 164 259 e.Cancel = true; 165 260 localExperiment = null; 166 break;261 return; 167 262 } 263 #endregion 264 var algorithm = (IAlgorithm)Optimizer.Clone(); 265 bool failed = false; 266 try { 267 ProblemInstanceManager.LoadData(provider, descriptor, (IProblemInstanceConsumer)algorithm.Problem); 268 } catch (Exception ex) { 269 failedInstances.AppendLine(descriptor.Name + ": " + ex.Message); 270 failed = true; 271 } 272 if (!failed) { 273 AddOptimizer(algorithm, localExperiment); 274 counter++; 275 experimentCreationBackgroundWorker.ReportProgress((int)Math.Round(100.0 * counter / total), descriptor.Name); 276 } else experimentCreationBackgroundWorker.ReportProgress((int)Math.Round(100.0 * counter / total), "Loading failed (" + descriptor.Name + ")"); 168 277 } 169 278 } … … 172 281 e.Result = localExperiment; 173 282 } 174 private void experimentCreationBackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { 175 experimentCreationProgressBar.Value = e.ProgressPercentage; 176 progressLabel.Text = (string)e.UserState; 177 } 283 178 284 private void experimentCreationBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { 179 SetMode(createExperiment: false); 180 if (e.Error != null) MessageBox.Show(e.Error.Message, "Error occurred", MessageBoxButtons.OK, MessageBoxIcon.Error); 181 if (!e.Cancelled && e.Error == null) { 182 experiment = (Experiment)e.Result; 183 DialogResult = System.Windows.Forms.DialogResult.OK; 184 Close(); 185 } 186 } 187 private void CreateExperimentDialog_FormClosing(object sender, FormClosingEventArgs e) { 188 if (experimentCreationBackgroundWorker.IsBusy) { 189 if (DialogResult != System.Windows.Forms.DialogResult.OK) 190 experimentCreationBackgroundWorker.CancelAsync(); 191 e.Cancel = true; 192 } 193 } 194 195 private void AddOptimizer(IOptimizer optimizer, Experiment experiment) { 196 if (createBatchRun) { 197 var batchRun = new BatchRun(); 198 batchRun.Repetitions = repetitions; 199 batchRun.Optimizer = optimizer; 200 experiment.Optimizers.Add(batchRun); 201 } else { 202 experiment.Optimizers.Add(optimizer); 203 } 204 } 205 206 private void SetMode(bool createExperiment) { 207 createBatchRunCheckBox.Enabled = !createExperiment; 208 repetitionsNumericUpDown.Enabled = !createExperiment; 209 selectAllCheckBox.Enabled = !createExperiment; 210 selectNoneCheckBox.Enabled = !createExperiment; 211 instancesListView.Enabled = !createExperiment; 212 okButton.Enabled = !createExperiment; 213 okButton.Visible = !createExperiment; 214 progressLabel.Visible = createExperiment; 215 experimentCreationProgressBar.Visible = createExperiment; 216 } 217 218 private Dictionary<IProblemInstanceProvider, List<IDataDescriptor>> GetSelectedInstances() { 219 var selectedInstances = new Dictionary<IProblemInstanceProvider, List<IDataDescriptor>>(); 220 foreach (var checkedItem in instancesListView.CheckedItems.OfType<ListViewItem>()) { 221 if (!selectedInstances.ContainsKey((IProblemInstanceProvider)checkedItem.Group.Tag)) 222 selectedInstances.Add((IProblemInstanceProvider)checkedItem.Group.Tag, new List<IDataDescriptor>()); 223 selectedInstances[(IProblemInstanceProvider)checkedItem.Group.Tag].Add((IDataDescriptor)checkedItem.Tag); 224 } 225 return selectedInstances; 226 } 285 try { 286 SetMode(locked: false); 287 if (e.Error != null) MessageBox.Show(e.Error.Message, "Error occurred", MessageBoxButtons.OK, MessageBoxIcon.Error); 288 if (failedInstances.Length > 0) MessageBox.Show("Some instances could not be loaded: " + Environment.NewLine + failedInstances.ToString(), "Some instances failed to load", MessageBoxButtons.OK, MessageBoxIcon.Error); 289 if (!e.Cancelled && e.Error == null) { 290 experiment = (Experiment)e.Result; 291 DialogResult = System.Windows.Forms.DialogResult.OK; 292 Close(); 293 } 294 } catch { } 295 } 296 #endregion 227 297 } 228 298 }
Note: See TracChangeset
for help on using the changeset viewer.