Changeset 7846
- Timestamp:
- 05/18/12 14:08:11 (13 years ago)
- Location:
- trunk/sources
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Optimizer/3.3/CreateExperimentDialog.Designer.cs
r7841 r7846 46 46 private void InitializeComponent() { 47 47 this.components = new System.ComponentModel.Container(); 48 System.Windows.Forms.ListViewGroup listViewGroup1 = new System.Windows.Forms.ListViewGroup("QAPLIB", System.Windows.Forms.HorizontalAlignment.Left);49 System.Windows.Forms.ListViewGroup listViewGroup2 = new System.Windows.Forms.ListViewGroup("TSPLIB", System.Windows.Forms.HorizontalAlignment.Left);50 System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem("bur26a");51 System.Windows.Forms.ListViewItem listViewItem2 = new System.Windows.Forms.ListViewItem("esc32a");52 System.Windows.Forms.ListViewItem listViewItem3 = new System.Windows.Forms.ListViewItem("berlin52");53 System.Windows.Forms.ListViewItem listViewItem4 = new System.Windows.Forms.ListViewItem("ch130");54 48 this.okButton = new System.Windows.Forms.Button(); 55 49 this.cancelButton = new System.Windows.Forms.Button(); … … 62 56 this.instancesListView = new System.Windows.Forms.ListView(); 63 57 this.columnHeader = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); 58 this.experimentCreationBackgroundWorker = new System.ComponentModel.BackgroundWorker(); 59 this.experimentCreationProgressBar = new System.Windows.Forms.ProgressBar(); 60 this.progressLabel = new System.Windows.Forms.Label(); 61 this.selectAllCheckBox = new System.Windows.Forms.CheckBox(); 62 this.selectNoneCheckBox = new System.Windows.Forms.CheckBox(); 64 63 ((System.ComponentModel.ISupportInitialize)(this.repetitionsNumericUpDown)).BeginInit(); 65 64 this.SuspendLayout(); … … 164 163 this.instancesListView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] { 165 164 this.columnHeader}); 166 listViewGroup1.Header = "QAPLIB";167 listViewGroup1.Name = "QAPLIBHeader";168 listViewGroup2.Header = "TSPLIB";169 listViewGroup2.Name = "TSPLIBHeader";170 listViewGroup2.Tag = "";171 this.instancesListView.Groups.AddRange(new System.Windows.Forms.ListViewGroup[] {172 listViewGroup1,173 listViewGroup2});174 165 this.instancesListView.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None; 175 listViewItem1.Checked = true; 176 listViewItem1.Group = listViewGroup1; 177 listViewItem1.StateImageIndex = 1; 178 listViewItem2.Checked = true; 179 listViewItem2.Group = listViewGroup1; 180 listViewItem2.StateImageIndex = 1; 181 listViewItem3.Group = listViewGroup2; 182 listViewItem3.StateImageIndex = 0; 183 listViewItem4.Group = listViewGroup2; 184 listViewItem4.StateImageIndex = 0; 185 this.instancesListView.Items.AddRange(new System.Windows.Forms.ListViewItem[] { 186 listViewItem1, 187 listViewItem2, 188 listViewItem3, 189 listViewItem4}); 190 this.instancesListView.Location = new System.Drawing.Point(113, 54); 166 this.instancesListView.Location = new System.Drawing.Point(113, 77); 191 167 this.instancesListView.Name = "instancesListView"; 192 this.instancesListView.Size = new System.Drawing.Size(156, 1 99);168 this.instancesListView.Size = new System.Drawing.Size(156, 176); 193 169 this.instancesListView.TabIndex = 6; 194 170 this.instancesListView.UseCompatibleStateImageBehavior = false; 195 171 this.instancesListView.View = System.Windows.Forms.View.Details; 172 this.instancesListView.ItemChecked += new System.Windows.Forms.ItemCheckedEventHandler(this.instancesListView_ItemChecked); 196 173 // 197 174 // columnHeader 198 175 // 199 176 this.columnHeader.Width = 150; 177 // 178 // experimentCreationBackgroundWorker 179 // 180 this.experimentCreationBackgroundWorker.WorkerReportsProgress = true; 181 this.experimentCreationBackgroundWorker.WorkerSupportsCancellation = true; 182 this.experimentCreationBackgroundWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(this.experimentCreationBackgroundWorker_DoWork); 183 this.experimentCreationBackgroundWorker.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.experimentCreationBackgroundWorker_ProgressChanged); 184 this.experimentCreationBackgroundWorker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.experimentCreationBackgroundWorker_RunWorkerCompleted); 185 // 186 // experimentCreationProgressBar 187 // 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); 191 this.experimentCreationProgressBar.Name = "experimentCreationProgressBar"; 192 this.experimentCreationProgressBar.Size = new System.Drawing.Size(92, 23); 193 this.experimentCreationProgressBar.Style = System.Windows.Forms.ProgressBarStyle.Continuous; 194 this.experimentCreationProgressBar.TabIndex = 7; 195 this.experimentCreationProgressBar.Visible = false; 196 // 197 // progressLabel 198 // 199 this.progressLabel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); 200 this.progressLabel.BackColor = System.Drawing.SystemColors.Control; 201 this.progressLabel.Location = new System.Drawing.Point(113, 259); 202 this.progressLabel.Name = "progressLabel"; 203 this.progressLabel.Size = new System.Drawing.Size(75, 23); 204 this.progressLabel.TabIndex = 8; 205 this.progressLabel.Text = "label1"; 206 this.progressLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; 207 this.progressLabel.Visible = false; 208 // 209 // selectAllCheckBox 210 // 211 this.selectAllCheckBox.AutoSize = true; 212 this.selectAllCheckBox.Location = new System.Drawing.Point(113, 54); 213 this.selectAllCheckBox.Name = "selectAllCheckBox"; 214 this.selectAllCheckBox.Size = new System.Drawing.Size(67, 17); 215 this.selectAllCheckBox.TabIndex = 9; 216 this.selectAllCheckBox.Text = "select all"; 217 this.selectAllCheckBox.UseVisualStyleBackColor = true; 218 this.selectAllCheckBox.CheckedChanged += new System.EventHandler(this.selectAllCheckBox_CheckedChanged); 219 // 220 // selectNoneCheckBox 221 // 222 this.selectNoneCheckBox.AutoSize = true; 223 this.selectNoneCheckBox.Location = new System.Drawing.Point(186, 54); 224 this.selectNoneCheckBox.Name = "selectNoneCheckBox"; 225 this.selectNoneCheckBox.Size = new System.Drawing.Size(81, 17); 226 this.selectNoneCheckBox.TabIndex = 9; 227 this.selectNoneCheckBox.Text = "select none"; 228 this.selectNoneCheckBox.UseVisualStyleBackColor = true; 229 this.selectNoneCheckBox.CheckedChanged += new System.EventHandler(this.selectNoneCheckBox_CheckedChanged); 200 230 // 201 231 // CreateExperimentDialog … … 206 236 this.CancelButton = this.cancelButton; 207 237 this.ClientSize = new System.Drawing.Size(281, 294); 238 this.Controls.Add(this.selectNoneCheckBox); 239 this.Controls.Add(this.selectAllCheckBox); 208 240 this.Controls.Add(this.instancesListView); 209 241 this.Controls.Add(this.repetitionsNumericUpDown); … … 214 246 this.Controls.Add(this.cancelButton); 215 247 this.Controls.Add(this.okButton); 248 this.Controls.Add(this.progressLabel); 249 this.Controls.Add(this.experimentCreationProgressBar); 216 250 this.MaximizeBox = false; 217 251 this.MinimizeBox = false; … … 221 255 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; 222 256 this.Text = "Create Experiment"; 257 this.TopMost = true; 258 this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.CreateExperimentDialog_FormClosing); 223 259 ((System.ComponentModel.ISupportInitialize)(this.repetitionsNumericUpDown)).EndInit(); 224 260 this.ResumeLayout(false); … … 239 275 private System.Windows.Forms.ListView instancesListView; 240 276 private System.Windows.Forms.ColumnHeader columnHeader; 277 private System.ComponentModel.BackgroundWorker experimentCreationBackgroundWorker; 278 private System.Windows.Forms.ProgressBar experimentCreationProgressBar; 279 private System.Windows.Forms.Label progressLabel; 280 private System.Windows.Forms.CheckBox selectAllCheckBox; 281 private System.Windows.Forms.CheckBox selectNoneCheckBox; 241 282 242 283 } -
trunk/sources/HeuristicLab.Optimizer/3.3/CreateExperimentDialog.cs
r7841 r7846 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.ComponentModel; 24 25 using System.Linq; 26 using System.Threading; 25 27 using System.Windows.Forms; 26 28 using HeuristicLab.Optimization; 27 using HeuristicLab.PluginInfrastructure;28 29 using HeuristicLab.Problems.Instances; 29 30 … … 37 38 experiment = null; 38 39 okButton.Enabled = optimizer != null; 40 FillOrHideInstanceListView(); 39 41 } 40 42 } … … 45 47 } 46 48 49 private bool createBatchRun; 50 private int repetitions; 51 private EventWaitHandle backgroundWorkerWaitHandle = new ManualResetEvent(false); 52 private bool suppressListViewEventHandling; 53 47 54 public CreateExperimentDialog() : this(null) { } 48 55 public CreateExperimentDialog(IOptimizer optimizer) { 49 56 InitializeComponent(); 57 createBatchRun = createBatchRunCheckBox.Checked; 58 repetitions = (int)repetitionsNumericUpDown.Value; 50 59 Optimizer = optimizer; 51 experiment = null;52 instancesListView.Items.Clear();53 instancesListView.Groups.Clear();54 FillOrHideInstanceListView();55 60 } 56 61 57 62 private void FillOrHideInstanceListView() { 58 if ( Optimizer != null && optimizer is IAlgorithm) {63 if (optimizer != null && optimizer is IAlgorithm) { 59 64 var algorithm = (IAlgorithm)Optimizer; 60 65 if (algorithm.Problem != null) { 61 var instanceProviders = GetProblemInstanceProviders(algorithm.Problem);66 var instanceProviders = ProblemInstanceManager.GetProviders(algorithm.Problem); 62 67 if (instanceProviders.Any()) { 63 foreach (var provider in instanceProviders) { 64 var group = new ListViewGroup(provider.Name, provider.Name); 65 group.Tag = provider; 66 instancesListView.Groups.Add(group); 67 IEnumerable<IDataDescriptor> descriptors = ((dynamic)provider).GetDataDescriptors(); 68 foreach (var d in descriptors) { 69 var item = new ListViewItem(d.Name, group); 70 item.Checked = true; 71 item.Tag = d; 72 instancesListView.Items.Add(item); 73 } 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; 74 76 } 75 instancesListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);76 if (instancesListView.Items.Count > 0) return;77 77 } 78 78 } 79 79 } 80 selectAllCheckBox.Visible = false; 81 selectNoneCheckBox.Visible = false; 80 82 instancesLabel.Visible = false; 81 83 instancesListView.Visible = false; … … 83 85 } 84 86 85 private IEnumerable<IProblemInstanceProvider> GetProblemInstanceProviders(IProblem problem) { 86 var consumerTypes = problem.GetType().GetInterfaces() 87 .Where(x => x.IsGenericType 88 && x.GetGenericTypeDefinition() == typeof(IProblemInstanceConsumer<>)); 89 90 if (consumerTypes.Any()) { 91 var instanceTypes = consumerTypes 92 .Select(x => x.GetGenericArguments().First()) 93 .Select(x => typeof(IProblemInstanceProvider<>).MakeGenericType(x)); 94 95 foreach (var type in instanceTypes) { 96 foreach (var provider in ApplicationManager.Manager.GetInstances(type)) 97 yield return (IProblemInstanceProvider)provider; 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); 98 96 } 99 97 } 98 instancesListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent); 99 selectAllCheckBox.Checked = true; 100 100 } 101 101 102 102 private void createBatchRunCheckBox_CheckedChanged(object sender, EventArgs e) { 103 103 repetitionsNumericUpDown.Enabled = createBatchRunCheckBox.Checked; 104 createBatchRun = createBatchRunCheckBox.Checked; 104 105 } 105 106 private void repetitionsNumericUpDown_Validated(object sender, EventArgs e) { 106 107 if (repetitionsNumericUpDown.Text == string.Empty) 107 108 repetitionsNumericUpDown.Text = repetitionsNumericUpDown.Value.ToString(); 109 repetitions = (int)repetitionsNumericUpDown.Value; 110 } 111 private void selectAllCheckBox_CheckedChanged(object sender, EventArgs e) { 112 if (selectAllCheckBox.Checked) { 113 selectNoneCheckBox.Checked = false; 114 if (instancesListView.CheckedItems.Count == instancesListView.Items.Count) return; 115 try { 116 suppressListViewEventHandling = true; 117 foreach (var item in instancesListView.Items.OfType<ListViewItem>()) { 118 item.Checked = true; 119 } 120 } finally { suppressListViewEventHandling = false; } 121 } 122 } 123 private void selectNoneCheckBox_CheckedChanged(object sender, EventArgs e) { 124 if (selectNoneCheckBox.Checked) { 125 selectAllCheckBox.Checked = false; 126 if (instancesListView.CheckedItems.Count == 0) return; 127 try { 128 suppressListViewEventHandling = true; 129 foreach (var item in instancesListView.Items.OfType<ListViewItem>()) { 130 item.Checked = false; 131 } 132 } finally { suppressListViewEventHandling = false; } 133 } 134 } 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 } 108 140 } 109 141 private void okButton_Click(object sender, EventArgs e) { 110 experiment = new Experiment(); 111 if (instancesListView.CheckedItems.Count == 0) { 112 AddOptimizer((IOptimizer)Optimizer.Clone()); 142 SetMode(createExperiment: true); 143 experimentCreationBackgroundWorker.RunWorkerAsync(GetSelectedInstances()); 144 backgroundWorkerWaitHandle.WaitOne(); 145 } 146 private void experimentCreationBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) { 147 backgroundWorkerWaitHandle.Set(); 148 experimentCreationBackgroundWorker.ReportProgress(0, string.Empty); 149 var items = (Dictionary<IProblemInstanceProvider, List<IDataDescriptor>>)e.Argument; 150 var localExperiment = new Experiment(); 151 if (items.Count == 0) { 152 AddOptimizer((IOptimizer)Optimizer.Clone(), localExperiment); 153 experimentCreationBackgroundWorker.ReportProgress(100, string.Empty); 113 154 } else { 114 foreach (var item in instancesListView.CheckedItems.OfType<ListViewItem>()) { 115 var descriptor = (IDataDescriptor)item.Tag; 116 var provider = (IProblemInstanceProvider)item.Group.Tag; 117 var algorithm = (IAlgorithm)Optimizer.Clone(); 118 ((dynamic)algorithm.Problem).Load(((dynamic)provider).LoadData(descriptor)); 119 AddOptimizer(algorithm); 155 int counter = 0, total = items.SelectMany(x => x.Value).Count(); 156 foreach (var provider in items.Keys) { 157 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); 163 if (experimentCreationBackgroundWorker.CancellationPending) { 164 e.Cancel = true; 165 localExperiment = null; 166 break; 167 } 168 } 120 169 } 121 170 } 122 Experiment.Prepare(true); 123 } 124 125 private void AddOptimizer(IOptimizer optimizer) { 126 if (createBatchRunCheckBox.Checked) { 171 if (localExperiment != null) localExperiment.Prepare(true); 172 e.Result = localExperiment; 173 } 174 private void experimentCreationBackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { 175 experimentCreationProgressBar.Value = e.ProgressPercentage; 176 progressLabel.Text = (string)e.UserState; 177 } 178 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) { 127 197 var batchRun = new BatchRun(); 128 batchRun.Repetitions = (int)repetitionsNumericUpDown.Value;198 batchRun.Repetitions = repetitions; 129 199 batchRun.Optimizer = optimizer; 130 200 experiment.Optimizers.Add(batchRun); … … 133 203 } 134 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 } 135 227 } 136 228 } -
trunk/sources/HeuristicLab.Problems.Instances.Views/3.3/ProblemInstanceConsumerView.cs
r7805 r7846 28 28 using HeuristicLab.MainForm; 29 29 using HeuristicLab.MainForm.WindowsForms; 30 using HeuristicLab.PluginInfrastructure;31 30 32 31 namespace HeuristicLab.Problems.Instances.Views { … … 63 62 } else { 64 63 problemInstanceProviderComboBox.DisplayMember = "Name"; 65 ProblemInstanceProviders = GetProblemInstanceProviders();64 ProblemInstanceProviders = ProblemInstanceManager.GetProviders(Content); 66 65 problemInstanceProviderComboBox.DataSource = ProblemInstanceProviders.OrderBy(x => x.Name).ToList(); 67 66 } 68 67 SetEnabledStateOfControls(); 69 }70 71 private IEnumerable<IProblemInstanceProvider> GetProblemInstanceProviders() {72 var consumerTypes = Content.GetType().GetInterfaces()73 .Where(x => x.IsGenericType74 && x.GetGenericTypeDefinition() == typeof(IProblemInstanceConsumer<>));75 76 if (consumerTypes.Any()) {77 var instanceTypes = consumerTypes78 .Select(x => x.GetGenericArguments().First())79 .Select(x => typeof(IProblemInstanceProvider<>).MakeGenericType(x));80 81 foreach (var type in instanceTypes) {82 foreach (var provider in ApplicationManager.Manager.GetInstances(type))83 yield return (IProblemInstanceProvider)provider;84 }85 }86 68 } 87 69 -
trunk/sources/HeuristicLab.Problems.Instances/3.3/HeuristicLab.Problems.Instances-3.3.csproj
r7825 r7846 105 105 </PropertyGroup> 106 106 <ItemGroup> 107 <Reference Include="Microsoft.CSharp" /> 107 108 <Reference Include="System" /> 108 109 <Reference Include="System.Core" /> … … 111 112 <ItemGroup> 112 113 <None Include="Plugin.cs.frame" /> 114 <Compile Include="ProblemInstanceManager.cs" /> 113 115 <Compile Include="IProblemInstanceExporter.cs" /> 114 116 <Compile Include="TableFileParser.cs" />
Note: See TracChangeset
for help on using the changeset viewer.