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.Collections.Generic;
|
---|
24 | using System.ComponentModel;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Threading;
|
---|
27 | using System.Windows.Forms;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Problems.Instances;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Optimizer {
|
---|
32 | public partial class CreateExperimentDialog : Form {
|
---|
33 | private IOptimizer optimizer;
|
---|
34 | public IOptimizer Optimizer {
|
---|
35 | get { return optimizer; }
|
---|
36 | set {
|
---|
37 | optimizer = value;
|
---|
38 | experiment = null;
|
---|
39 | okButton.Enabled = optimizer != null;
|
---|
40 | FillOrHideInstanceListView();
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | private Experiment experiment;
|
---|
45 | public Experiment Experiment {
|
---|
46 | get { return experiment; }
|
---|
47 | }
|
---|
48 |
|
---|
49 | private bool createBatchRun;
|
---|
50 | private int repetitions;
|
---|
51 | private EventWaitHandle backgroundWorkerWaitHandle = new ManualResetEvent(false);
|
---|
52 | private bool suppressListViewEventHandling;
|
---|
53 |
|
---|
54 | public CreateExperimentDialog() : this(null) { }
|
---|
55 | public CreateExperimentDialog(IOptimizer optimizer) {
|
---|
56 | InitializeComponent();
|
---|
57 | createBatchRun = createBatchRunCheckBox.Checked;
|
---|
58 | 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 | }
|
---|
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 | }
|
---|
140 | }
|
---|
141 | private void okButton_Click(object sender, EventArgs e) {
|
---|
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);
|
---|
154 | } else {
|
---|
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 | }
|
---|
169 | }
|
---|
170 | }
|
---|
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) {
|
---|
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 | }
|
---|
227 | }
|
---|
228 | }
|
---|