1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 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.Drawing;
|
---|
26 | using System.Data;
|
---|
27 | using System.Text;
|
---|
28 | using System.Windows.Forms;
|
---|
29 | using HeuristicLab.PluginInfrastructure;
|
---|
30 | using HeuristicLab.Core;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.ES {
|
---|
33 | /// <summary>
|
---|
34 | /// Class for visual representation of an <see cref="ES"/>.
|
---|
35 | /// </summary>
|
---|
36 | public partial class ESEditor : EditorBase {
|
---|
37 | private ChooseOperatorDialog chooseOperatorDialog;
|
---|
38 |
|
---|
39 | /// <summary>
|
---|
40 | /// Gets or sets the evolution strategy to display.
|
---|
41 | /// </summary>
|
---|
42 | /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="EditorBase"/>.
|
---|
43 | /// No own data storage present.</remarks>
|
---|
44 | public ES ES {
|
---|
45 | get { return (ES)Item; }
|
---|
46 | set { base.Item = value; }
|
---|
47 | }
|
---|
48 |
|
---|
49 | /// <summary>
|
---|
50 | /// Initializes a new instance of <see cref="ESEditor"/>.
|
---|
51 | /// </summary>
|
---|
52 | public ESEditor() {
|
---|
53 | InitializeComponent();
|
---|
54 | problemDimensionTextBox.Text = "1";
|
---|
55 | }
|
---|
56 | /// <summary>
|
---|
57 | /// Initializes a new instance of <see cref="ESEditor"/> with the given <paramref name="es"/>.
|
---|
58 | /// </summary>
|
---|
59 | /// <param name="es">The evolution strategy to display.</param>
|
---|
60 | public ESEditor(ES es)
|
---|
61 | : this() {
|
---|
62 | ES = es;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /// <summary>
|
---|
66 | /// Removes all event handlers from the underlying <see cref="ES"/>.
|
---|
67 | /// </summary>
|
---|
68 | /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="EditorBase"/>.</remarks>
|
---|
69 | protected override void RemoveItemEvents() {
|
---|
70 | ES.Engine.ExceptionOccurred -= new EventHandler<ExceptionEventArgs>(Engine_ExceptionOccurred);
|
---|
71 | ES.Engine.Finished -= new EventHandler(Engine_Finished);
|
---|
72 | ES.Changed -= new EventHandler(ES_Changed);
|
---|
73 | scopeView.Scope = null;
|
---|
74 | base.RemoveItemEvents();
|
---|
75 | }
|
---|
76 | /// <summary>
|
---|
77 | /// Adds event handlers to the underlying <see cref="ES"/>.
|
---|
78 | /// </summary>
|
---|
79 | /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="EditorBase"/>.</remarks>
|
---|
80 | protected override void AddItemEvents() {
|
---|
81 | base.AddItemEvents();
|
---|
82 | ES.Engine.ExceptionOccurred += new EventHandler<ExceptionEventArgs>(Engine_ExceptionOccurred);
|
---|
83 | ES.Engine.Finished += new EventHandler(Engine_Finished);
|
---|
84 | ES.Changed += new EventHandler(ES_Changed);
|
---|
85 | SetDataBinding();
|
---|
86 | scopeView.Scope = ES.Engine.GlobalScope;
|
---|
87 | }
|
---|
88 |
|
---|
89 | void ES_Changed(object sender, EventArgs e) {
|
---|
90 | // neither Refresh() nor Update() work
|
---|
91 | randomSeedTextBox.Text = ES.Seed.ToString();
|
---|
92 | muTextBox.Text = ES.Mu.ToString();
|
---|
93 | rhoTextBox.Text = ES.Rho.ToString();
|
---|
94 | lambdaTextBox.Text = ES.Lambda.ToString();
|
---|
95 | learningRateTextBox.Text = ES.LearningRate.ToString();
|
---|
96 | generalLearningRateTextBox.Text = ES.GeneralLearningRate.ToString();
|
---|
97 | }
|
---|
98 |
|
---|
99 | /// <summary>
|
---|
100 | /// Updates all controls with the latest data of the model.
|
---|
101 | /// </summary>
|
---|
102 | /// <remarks>Calls <see cref="ViewBase.UpdateControls"/> of base class <see cref="EditorBase"/>.</remarks>
|
---|
103 | protected override void UpdateControls() {
|
---|
104 | base.UpdateControls();
|
---|
105 | if (ES == null) {
|
---|
106 | tabControl.Enabled = false;
|
---|
107 | } else {
|
---|
108 | tabControl.Enabled = true;
|
---|
109 | problemInitializationTextBox.Text = ES.ProblemInjector.GetType().Name;
|
---|
110 | solutionGenerationTextBox.Text = ES.SolutionGenerator.GetType().Name;
|
---|
111 | mutationTextBox.Text = ES.Mutator.GetType().Name;
|
---|
112 | evaluationTextBox.Text = ES.Evaluator.GetType().Name;
|
---|
113 | recombinationTextBox.Text = ES.Recombinator.GetType().Name;
|
---|
114 | initialMutationStrengthVectorTextBox.Text = ArrayToString<double>(ES.ShakingFactors);
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | private void SetDataBinding() {
|
---|
119 | setRandomSeedRandomlyCheckBox.DataBindings.Add("Checked", ES, "SetSeedRandomly");
|
---|
120 | randomSeedTextBox.DataBindings.Add("Text", ES, "Seed");
|
---|
121 | muTextBox.DataBindings.Add("Text", ES, "Mu");
|
---|
122 | rhoTextBox.DataBindings.Add("Text", ES, "Rho");
|
---|
123 | lambdaTextBox.DataBindings.Add("Text", ES, "Lambda");
|
---|
124 | maximumGenerationsTextBox.DataBindings.Add("Text", ES, "MaximumGenerations");
|
---|
125 | generalLearningRateTextBox.DataBindings.Add("Text", ES, "GeneralLearningRate");
|
---|
126 | learningRateTextBox.DataBindings.Add("Text", ES, "LearningRate");
|
---|
127 | }
|
---|
128 |
|
---|
129 | #region Button Events
|
---|
130 | private void viewProblemInitializationButton_Click(object sender, EventArgs e) {
|
---|
131 | IView view = ES.ProblemInjector.CreateView();
|
---|
132 | if (view != null)
|
---|
133 | PluginManager.ControlManager.ShowControl(view);
|
---|
134 | }
|
---|
135 | private void viewSolutionGenerationButton_Click(object sender, EventArgs e) {
|
---|
136 | IView view = ES.SolutionGenerator.CreateView();
|
---|
137 | if (view != null)
|
---|
138 | PluginManager.ControlManager.ShowControl(view);
|
---|
139 | }
|
---|
140 | private void viewMutationButton_Click(object sender, EventArgs e) {
|
---|
141 | IView view = ES.Mutator.CreateView();
|
---|
142 | if (view != null)
|
---|
143 | PluginManager.ControlManager.ShowControl(view);
|
---|
144 | }
|
---|
145 | private void viewEvaluationButton_Click(object sender, EventArgs e) {
|
---|
146 | IView view = ES.Evaluator.CreateView();
|
---|
147 | if (view != null)
|
---|
148 | PluginManager.ControlManager.ShowControl(view);
|
---|
149 | }
|
---|
150 | private void viewRecombinationButton_Click(object sender, EventArgs e) {
|
---|
151 | IView view = ES.Recombinator.CreateView();
|
---|
152 | if (view != null)
|
---|
153 | PluginManager.ControlManager.ShowControl(view);
|
---|
154 | }
|
---|
155 | private void setProblemInitializationButton_Click(object sender, EventArgs e) {
|
---|
156 | if (chooseOperatorDialog == null) chooseOperatorDialog = new ChooseOperatorDialog();
|
---|
157 | if (chooseOperatorDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
158 | ES.ProblemInjector = chooseOperatorDialog.Operator;
|
---|
159 | problemInitializationTextBox.Text = ES.ProblemInjector.GetType().Name;
|
---|
160 | }
|
---|
161 | }
|
---|
162 | private void setSolutionGenerationButton_Click(object sender, EventArgs e) {
|
---|
163 | if (chooseOperatorDialog == null) chooseOperatorDialog = new ChooseOperatorDialog();
|
---|
164 | if (chooseOperatorDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
165 | ES.SolutionGenerator= chooseOperatorDialog.Operator;
|
---|
166 | solutionGenerationTextBox.Text = ES.SolutionGenerator.GetType().Name;
|
---|
167 | }
|
---|
168 | }
|
---|
169 | private void setMutationButton_Click(object sender, EventArgs e) {
|
---|
170 | if (chooseOperatorDialog == null) chooseOperatorDialog = new ChooseOperatorDialog();
|
---|
171 | if (chooseOperatorDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
172 | ES.Mutator = chooseOperatorDialog.Operator;
|
---|
173 | mutationTextBox.Text = ES.Mutator.GetType().Name;
|
---|
174 | }
|
---|
175 | }
|
---|
176 | private void setEvaluationButton_Click(object sender, EventArgs e) {
|
---|
177 | if (chooseOperatorDialog == null) chooseOperatorDialog = new ChooseOperatorDialog();
|
---|
178 | if (chooseOperatorDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
179 | ES.Evaluator = chooseOperatorDialog.Operator;
|
---|
180 | evaluationTextBox.Text = ES.Evaluator.GetType().Name;
|
---|
181 | }
|
---|
182 | }
|
---|
183 | private void setRecombinationButton_Click(object sender, EventArgs e) {
|
---|
184 | if (chooseOperatorDialog == null) chooseOperatorDialog = new ChooseOperatorDialog();
|
---|
185 | if (chooseOperatorDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
186 | ES.Recombinator = chooseOperatorDialog.Operator;
|
---|
187 | recombinationTextBox.Text = ES.Recombinator.GetType().Name;
|
---|
188 | }
|
---|
189 | }
|
---|
190 | private void executeButton_Click(object sender, EventArgs e) {
|
---|
191 | executeButton.Enabled = false;
|
---|
192 | abortButton.Enabled = true;
|
---|
193 | ES.Engine.Execute();
|
---|
194 | }
|
---|
195 | private void abortButton_Click(object sender, EventArgs e) {
|
---|
196 | ES.Engine.Abort();
|
---|
197 | }
|
---|
198 | private void resetButton_Click(object sender, EventArgs e) {
|
---|
199 | ES.Engine.Reset();
|
---|
200 | }
|
---|
201 | private void cloneEngineButton_Click(object sender, EventArgs e) {
|
---|
202 | IEngine clone = (IEngine)ES.Engine.Clone();
|
---|
203 | IEditor editor = ((IEditable)clone).CreateEditor();
|
---|
204 | PluginManager.ControlManager.ShowControl(editor);
|
---|
205 | }
|
---|
206 | #endregion
|
---|
207 |
|
---|
208 | #region Engine Events
|
---|
209 | private delegate void OnExceptionEventDelegate(object sender, ExceptionEventArgs e);
|
---|
210 | private void Engine_ExceptionOccurred(object sender, ExceptionEventArgs e) {
|
---|
211 | if (InvokeRequired)
|
---|
212 | Invoke(new OnExceptionEventDelegate(Engine_ExceptionOccurred), sender, e);
|
---|
213 | else
|
---|
214 | Auxiliary.ShowErrorMessageBox(e.Exception);
|
---|
215 | }
|
---|
216 | private void Engine_Finished(object sender, EventArgs e) {
|
---|
217 | scopeView.Refresh();
|
---|
218 | if (executeButton.InvokeRequired) {
|
---|
219 | executeButton.Invoke(new MethodInvoker(EnableExecute));
|
---|
220 | } else {
|
---|
221 | executeButton.Enabled = true;
|
---|
222 | abortButton.Enabled = false;
|
---|
223 | }
|
---|
224 | }
|
---|
225 | private void EnableExecute() {
|
---|
226 | executeButton.Enabled = true;
|
---|
227 | abortButton.Enabled = false;
|
---|
228 | }
|
---|
229 | #endregion
|
---|
230 |
|
---|
231 | #region RadioButton Events
|
---|
232 | private void plusRadioButton_CheckedChanged(object sender, EventArgs e) {
|
---|
233 | if (plusRadioButton.Checked) ES.PlusNotation = true;
|
---|
234 | }
|
---|
235 |
|
---|
236 | private void commaRadioButton_CheckedChanged(object sender, EventArgs e) {
|
---|
237 | if (commaRadioButton.Checked) ES.PlusNotation = false;
|
---|
238 | }
|
---|
239 | #endregion
|
---|
240 |
|
---|
241 | private string ArrayToString<T>(T[] array) {
|
---|
242 | StringBuilder s = new StringBuilder();
|
---|
243 | foreach (T element in array)
|
---|
244 | s.Append(element + "; ");
|
---|
245 | s.Remove(s.Length - 2, 2);
|
---|
246 | return s.ToString();
|
---|
247 | }
|
---|
248 |
|
---|
249 | private double[] StringToDoubleArray(string str) {
|
---|
250 |
|
---|
251 | string[] s = str.Split(new char[] { ';' });
|
---|
252 | double[] tmp = new double[s.Length];
|
---|
253 | try {
|
---|
254 | for (int i = 0; i < s.Length; i++) {
|
---|
255 | tmp[i] = double.Parse(s[i]);
|
---|
256 | }
|
---|
257 | } catch (FormatException) {
|
---|
258 | return null;
|
---|
259 | }
|
---|
260 | return tmp;
|
---|
261 | }
|
---|
262 |
|
---|
263 | private void initialMutationStrengthVectorTextBox_Validated(object sender, EventArgs e) {
|
---|
264 | double[] tmp = StringToDoubleArray(initialMutationStrengthVectorTextBox.Text);
|
---|
265 | if (tmp != null) ES.ShakingFactors = tmp;
|
---|
266 | else MessageBox.Show("Please use colons \";\" (without the quotes) to delimite the items like this: " + (1.2).ToString() + ";" + (1.1).ToString() + ";" + (3.453).ToString());
|
---|
267 | int dim = int.Parse(problemDimensionTextBox.Text);
|
---|
268 | if (ES.ShakingFactors.Length != dim) {
|
---|
269 | problemDimensionTextBox.Text = ES.ShakingFactors.Length.ToString();
|
---|
270 | }
|
---|
271 | Refresh();
|
---|
272 | }
|
---|
273 |
|
---|
274 | private void problemDimensionTextBox_Validated(object sender, EventArgs e) {
|
---|
275 | double[] tmp = StringToDoubleArray(initialMutationStrengthVectorTextBox.Text);
|
---|
276 | if (tmp != null) {
|
---|
277 | int dim = 0;
|
---|
278 | try {
|
---|
279 | dim = int.Parse(problemDimensionTextBox.Text);
|
---|
280 | if (dim < 1) throw new FormatException();
|
---|
281 | } catch (FormatException) {
|
---|
282 | MessageBox.Show("Problem Dimension must contain an integer > 0");
|
---|
283 | }
|
---|
284 | double[] shakingFactors = new double[dim];
|
---|
285 | for (int i = 0; i < dim; i++) {
|
---|
286 | shakingFactors[i] = tmp[i % tmp.Length];
|
---|
287 | }
|
---|
288 | ES.ShakingFactors = shakingFactors;
|
---|
289 | Refresh();
|
---|
290 | }
|
---|
291 | }
|
---|
292 | }
|
---|
293 | }
|
---|