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.Linq;
|
---|
25 | using System.Windows.Forms;
|
---|
26 | using HeuristicLab.Optimization;
|
---|
27 | using HeuristicLab.PluginInfrastructure;
|
---|
28 | using HeuristicLab.Problems.Instances;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Optimizer {
|
---|
31 | public partial class CreateExperimentDialog : Form {
|
---|
32 | private IOptimizer optimizer;
|
---|
33 | public IOptimizer Optimizer {
|
---|
34 | get { return optimizer; }
|
---|
35 | set {
|
---|
36 | optimizer = value;
|
---|
37 | experiment = null;
|
---|
38 | okButton.Enabled = optimizer != null;
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | private Experiment experiment;
|
---|
43 | public Experiment Experiment {
|
---|
44 | get { return experiment; }
|
---|
45 | }
|
---|
46 |
|
---|
47 | public CreateExperimentDialog() : this(null) { }
|
---|
48 | public CreateExperimentDialog(IOptimizer optimizer) {
|
---|
49 | InitializeComponent();
|
---|
50 | Optimizer = optimizer;
|
---|
51 | experiment = null;
|
---|
52 | instancesListView.Items.Clear();
|
---|
53 | instancesListView.Groups.Clear();
|
---|
54 | FillOrHideInstanceListView();
|
---|
55 | }
|
---|
56 |
|
---|
57 | private void FillOrHideInstanceListView() {
|
---|
58 | if (Optimizer != null && optimizer is IAlgorithm) {
|
---|
59 | var algorithm = (IAlgorithm)Optimizer;
|
---|
60 | if (algorithm.Problem != null) {
|
---|
61 | var instanceProviders = GetProblemInstanceProviders(algorithm.Problem);
|
---|
62 | 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 | }
|
---|
74 | }
|
---|
75 | instancesListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
|
---|
76 | if (instancesListView.Items.Count > 0) return;
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|
80 | instancesLabel.Visible = false;
|
---|
81 | instancesListView.Visible = false;
|
---|
82 | Height = 130;
|
---|
83 | }
|
---|
84 |
|
---|
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;
|
---|
98 | }
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | private void createBatchRunCheckBox_CheckedChanged(object sender, EventArgs e) {
|
---|
103 | repetitionsNumericUpDown.Enabled = createBatchRunCheckBox.Checked;
|
---|
104 | }
|
---|
105 | private void repetitionsNumericUpDown_Validated(object sender, EventArgs e) {
|
---|
106 | if (repetitionsNumericUpDown.Text == string.Empty)
|
---|
107 | repetitionsNumericUpDown.Text = repetitionsNumericUpDown.Value.ToString();
|
---|
108 | }
|
---|
109 | private void okButton_Click(object sender, EventArgs e) {
|
---|
110 | experiment = new Experiment();
|
---|
111 | if (instancesListView.CheckedItems.Count == 0) {
|
---|
112 | AddOptimizer((IOptimizer)Optimizer.Clone());
|
---|
113 | } 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);
|
---|
120 | }
|
---|
121 | }
|
---|
122 | Experiment.Prepare(true);
|
---|
123 | }
|
---|
124 |
|
---|
125 | private void AddOptimizer(IOptimizer optimizer) {
|
---|
126 | if (createBatchRunCheckBox.Checked) {
|
---|
127 | var batchRun = new BatchRun();
|
---|
128 | batchRun.Repetitions = (int)repetitionsNumericUpDown.Value;
|
---|
129 | batchRun.Optimizer = optimizer;
|
---|
130 | experiment.Optimizers.Add(batchRun);
|
---|
131 | } else {
|
---|
132 | experiment.Optimizers.Add(optimizer);
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|