Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ES/3.2/ESEditor.cs @ 2115

Last change on this file since 2115 was 2052, checked in by abeham, 15 years ago

Improved ES GUI according to #674

File size: 21.5 KB
RevLine 
[71]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
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Text;
28using System.Windows.Forms;
29using HeuristicLab.PluginInfrastructure;
30using HeuristicLab.Core;
31
32namespace HeuristicLab.ES {
[896]33  /// <summary>
34  /// Class for visual representation of an <see cref="ES"/>.
35  /// </summary>
[71]36  public partial class ESEditor : EditorBase {
37    private ChooseOperatorDialog chooseOperatorDialog;
[2052]38    private OperatorLibrary operatorLibrary;
39    private List<IOperator> problemInitializers;
40    private int selectedProblemInitializer;
41    private List<IOperator> solutionGenerators;
42    private int selectedSolutionGenerator;
43    private List<IOperator> mutators;
44    private int selectedMutator;
45    private List<IOperator> evaluators;
46    private int selectedEvaluator;
47    private List<IOperator> recombinators;
48    private int selectedRecombinator;
[71]49
[896]50    /// <summary>
51    /// Gets or sets the evolution strategy to display.
52    /// </summary>
53    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="EditorBase"/>.
54    /// No own data storage present.</remarks>
[71]55    public ES ES {
56      get { return (ES)Item; }
57      set { base.Item = value; }
58    }
59
[896]60    /// <summary>
61    /// Initializes a new instance of <see cref="ESEditor"/>.
62    /// </summary>
[71]63    public ESEditor() {
64      InitializeComponent();
[2052]65      problemInitializers = new List<IOperator>();
66      selectedProblemInitializer = -1;
67      solutionGenerators = new List<IOperator>();
68      selectedSolutionGenerator = -1;
69      mutators = new List<IOperator>();
70      selectedMutator = -1;
71      evaluators = new List<IOperator>();
72      selectedEvaluator = -1;
73      recombinators = new List<IOperator>();
74      selectedRecombinator = -1;
[71]75    }
[896]76    /// <summary>
77    /// Initializes a new instance of <see cref="ESEditor"/> with the given <paramref name="es"/>.
78    /// </summary>
79    /// <param name="es">The evolution strategy to display.</param>
[71]80    public ESEditor(ES es)
81      : this() {
82      ES = es;
83    }
84
[2052]85    private int AddOperator(ComboBox comboBox, IOperator iOperator, List<IOperator> list) {
86      for (int i = 0; i < comboBox.Items.Count - 1; i++) {
87        if ((comboBox.Items[i] as string).CompareTo(iOperator.Name) > 0) {
88          comboBox.Items.Insert(i, iOperator.Name);
89          list.Insert(i, iOperator);
90          return i;
91        }
92      }
93      int index = comboBox.Items.Count - 1;
94      comboBox.Items.Insert(index, iOperator.Name);
95      list.Add(iOperator);
96      return index;
97    }
98    private int SetOperator(ComboBox comboBox, IOperator iOperator, List<IOperator> list) {
99      int index;
100      if ((index = list.FindIndex(op => op.Name.Equals(iOperator.Name))) >= 0) {
101        comboBox.Items.RemoveAt(index);
102        list.RemoveAt(index);
103        return AddOperator(comboBox, iOperator, list);
104      } else return -1;
105    }
106
[896]107    /// <summary>
108    /// Removes all event handlers from the underlying <see cref="ES"/>.
109    /// </summary>
110    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="EditorBase"/>.</remarks>
[71]111    protected override void RemoveItemEvents() {
112      ES.Engine.ExceptionOccurred -= new EventHandler<ExceptionEventArgs>(Engine_ExceptionOccurred);
113      ES.Engine.Finished -= new EventHandler(Engine_Finished);
[78]114      ES.Changed -= new EventHandler(ES_Changed);
[71]115      scopeView.Scope = null;
116      base.RemoveItemEvents();
117    }
[896]118    /// <summary>
119    /// Adds event handlers to the underlying <see cref="ES"/>.
120    /// </summary>
121    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="EditorBase"/>.</remarks>
[71]122    protected override void AddItemEvents() {
123      base.AddItemEvents();
124      ES.Engine.ExceptionOccurred += new EventHandler<ExceptionEventArgs>(Engine_ExceptionOccurred);
125      ES.Engine.Finished += new EventHandler(Engine_Finished);
[78]126      ES.Changed += new EventHandler(ES_Changed);
[71]127      SetDataBinding();
128      scopeView.Scope = ES.Engine.GlobalScope;
129    }
130
[78]131    void ES_Changed(object sender, EventArgs e) {
132      // neither Refresh() nor Update() work
[98]133      randomSeedTextBox.Text = ES.Seed.ToString();
[78]134      muTextBox.Text = ES.Mu.ToString();
[86]135      rhoTextBox.Text = ES.Rho.ToString();
[78]136      lambdaTextBox.Text = ES.Lambda.ToString();
[97]137      learningRateTextBox.Text = ES.LearningRate.ToString();
[1091]138      generalLearningRateTextBox.Text = ES.GeneralLearningRate.ToString();
[78]139    }
140
[896]141    /// <summary>
142    /// Updates all controls with the latest data of the model.
143    /// </summary>
144    /// <remarks>Calls <see cref="ViewBase.UpdateControls"/> of base class <see cref="EditorBase"/>.</remarks>
[71]145    protected override void UpdateControls() {
146      base.UpdateControls();
147      if (ES == null) {
148        tabControl.Enabled = false;
149      } else {
150        tabControl.Enabled = true;
[2052]151        int index;
152        if (!((index = problemInitializers.FindIndex(op => op.Name.Equals(ES.ProblemInjector.Name))) >= 0)) {
153          index = AddOperator(problemInitializationComboBox, ES.ProblemInjector, problemInitializers);
154          if (index <= selectedProblemInitializer) selectedProblemInitializer++;
155        }
156        problemInitializationComboBox.SelectedIndex = index;
157        if (!((index = solutionGenerators.FindIndex(op => op.Name.Equals(ES.SolutionGenerator.Name))) >= 0)) {
158          index = AddOperator(solutionGenerationComboBox, ES.SolutionGenerator, solutionGenerators);
159          if (index <= selectedSolutionGenerator) selectedSolutionGenerator++;
160        }
161        solutionGenerationComboBox.SelectedIndex = index;
162        if (!((index = evaluators.FindIndex(op => op.Name.Equals(ES.Evaluator.Name))) >= 0)) {
163          index = AddOperator(evaluationComboBox, ES.Evaluator, evaluators);
164          if (index <= selectedEvaluator) selectedEvaluator++;
165        }
166        evaluationComboBox.SelectedIndex = index;
167        if (!((index = mutators.FindIndex(op => op.Name.Equals(ES.Mutator.Name))) >= 0)) {
168          index = AddOperator(mutationComboBox, ES.Mutator, mutators);
169          if (index <= selectedMutator) selectedMutator++;
170        }
171        mutationComboBox.SelectedIndex = index;
172        if (!((index = recombinators.FindIndex(op => op.Name.Equals(ES.Recombinator.Name))) >= 0)) {
173          index = AddOperator(recombinationComboBox, ES.Recombinator, recombinators);
174          if (index <= selectedRecombinator) selectedRecombinator++;
175        }
176        recombinationComboBox.SelectedIndex = index;
[1222]177        plusRadioButton.Checked = ES.PlusNotation;
178        commaRadioButton.Checked = !ES.PlusNotation;
[71]179      }
180    }
181
182    private void SetDataBinding() {
183      setRandomSeedRandomlyCheckBox.DataBindings.Add("Checked", ES, "SetSeedRandomly");
184      randomSeedTextBox.DataBindings.Add("Text", ES, "Seed");
185      muTextBox.DataBindings.Add("Text", ES, "Mu");
[86]186      rhoTextBox.DataBindings.Add("Text", ES, "Rho");
[71]187      lambdaTextBox.DataBindings.Add("Text", ES, "Lambda");
188      maximumGenerationsTextBox.DataBindings.Add("Text", ES, "MaximumGenerations");
[1222]189      shakingFactorsLowerBoundTextBox.DataBindings.Add("Text", ES, "ShakingFactorsMin");
190      shakingFactorsUpperBoundTextBox.DataBindings.Add("Text", ES, "ShakingFactorsMax");
191      problemDimensionTextBox.DataBindings.Add("Text", ES, "ProblemDimension");
[1091]192      generalLearningRateTextBox.DataBindings.Add("Text", ES, "GeneralLearningRate");
[97]193      learningRateTextBox.DataBindings.Add("Text", ES, "LearningRate");
[71]194    }
195
196    #region Button Events
197    private void viewProblemInitializationButton_Click(object sender, EventArgs e) {
198      IView view = ES.ProblemInjector.CreateView();
199      if (view != null)
200        PluginManager.ControlManager.ShowControl(view);
201    }
202    private void viewSolutionGenerationButton_Click(object sender, EventArgs e) {
203      IView view = ES.SolutionGenerator.CreateView();
204      if (view != null)
205        PluginManager.ControlManager.ShowControl(view);
206    }
207    private void viewMutationButton_Click(object sender, EventArgs e) {
208      IView view = ES.Mutator.CreateView();
209      if (view != null)
210        PluginManager.ControlManager.ShowControl(view);
211    }
212    private void viewEvaluationButton_Click(object sender, EventArgs e) {
213      IView view = ES.Evaluator.CreateView();
214      if (view != null)
215        PluginManager.ControlManager.ShowControl(view);
216    }
[86]217    private void viewRecombinationButton_Click(object sender, EventArgs e) {
218      IView view = ES.Recombinator.CreateView();
219      if (view != null)
220        PluginManager.ControlManager.ShowControl(view);
221    }
[71]222    private void setProblemInitializationButton_Click(object sender, EventArgs e) {
[2052]223     
[71]224    }
225    private void executeButton_Click(object sender, EventArgs e) {
226      executeButton.Enabled = false;
227      abortButton.Enabled = true;
228      ES.Engine.Execute();
229    }
230    private void abortButton_Click(object sender, EventArgs e) {
231      ES.Engine.Abort();
232    }
233    private void resetButton_Click(object sender, EventArgs e) {
234      ES.Engine.Reset();
235    }
236    private void cloneEngineButton_Click(object sender, EventArgs e) {
237      IEngine clone = (IEngine)ES.Engine.Clone();
238      IEditor editor = ((IEditable)clone).CreateEditor();
239      PluginManager.ControlManager.ShowControl(editor);
240    }
241    #endregion
242
243    #region Engine Events
244    private delegate void OnExceptionEventDelegate(object sender, ExceptionEventArgs e);
245    private void Engine_ExceptionOccurred(object sender, ExceptionEventArgs e) {
246      if (InvokeRequired)
247        Invoke(new OnExceptionEventDelegate(Engine_ExceptionOccurred), sender, e);
248      else
249        Auxiliary.ShowErrorMessageBox(e.Exception);
250    }
251    private void Engine_Finished(object sender, EventArgs e) {
252      scopeView.Refresh();
[81]253      if (executeButton.InvokeRequired) {
254        executeButton.Invoke(new MethodInvoker(EnableExecute));
255      } else {
256        executeButton.Enabled = true;
257        abortButton.Enabled = false;
258      }
259    }
260    private void EnableExecute() {
[71]261      executeButton.Enabled = true;
262      abortButton.Enabled = false;
263    }
264    #endregion
[81]265
[87]266    #region RadioButton Events
267    private void plusRadioButton_CheckedChanged(object sender, EventArgs e) {
268      if (plusRadioButton.Checked) ES.PlusNotation = true;
269    }
270
271    private void commaRadioButton_CheckedChanged(object sender, EventArgs e) {
272      if (commaRadioButton.Checked) ES.PlusNotation = false;
273    }
274    #endregion
[1091]275
[1222]276    private void problemDimensionTextBox_Validated(object sender, EventArgs e) {
277      UpdateLearningRates();
[1091]278      Refresh();
279    }
280
[1112]281    private void UpdateLearningRates() {
282      if (ES != null) {
283        int dimension = int.Parse(problemDimensionTextBox.Text);
284        ES.GeneralLearningRate = 1 / Math.Sqrt(2 * dimension);
285        ES.LearningRate = 1 / Math.Sqrt(2 * Math.Sqrt(dimension));
286      }
287    }
[2052]288
289    private void openOperatorLibraryButton_Click(object sender, EventArgs e) {
290      if (openOperatorLibraryFileDialog.ShowDialog() == DialogResult.OK) {
291
292        operatorLibrary = (PersistenceManager.Load(openOperatorLibraryFileDialog.FileName) as OperatorLibrary);
293        if (operatorLibrary == null) {
294          MessageBox.Show("The selected file is not an operator library");
295          return;
296        }
297        foreach (IOperatorGroup topLevelGroup in operatorLibrary.Group.SubGroups) {
298          if (topLevelGroup.Name.Equals("Problem")) {
299            foreach (IOperatorGroup group in topLevelGroup.SubGroups) {
300              if (group.Name.Equals("Initialization")) {
301                foreach (IOperator op in group.Operators) {
302                  int index = SetOperator(problemInitializationComboBox, (IOperator)op.Clone(), problemInitializers);
303                  if (index < 0) {
304                    index = AddOperator(problemInitializationComboBox, (IOperator)op.Clone(), problemInitializers);
305                    if (index <= selectedProblemInitializer) selectedProblemInitializer++;
306                  }
307                }
308              } else if (group.Name.Equals("Solution generation")) {
309                foreach (IOperator op in group.Operators) {
310                  int index = SetOperator(solutionGenerationComboBox, (IOperator)op.Clone(), solutionGenerators);
311                  if (index < 0) {
312                    index = AddOperator(solutionGenerationComboBox, (IOperator)op.Clone(), solutionGenerators);
313                    if (index <= selectedSolutionGenerator) selectedSolutionGenerator++;
314                  }
315                }
316              } else if (group.Name.Equals("Evaluation")) {
317                foreach (IOperator op in group.Operators) {
318                  int index = SetOperator(evaluationComboBox, (IOperator)op.Clone(), evaluators);
319                  if (index < 0) {
320                    index = AddOperator(evaluationComboBox, (IOperator)op.Clone(), evaluators);
321                    if (index <= selectedEvaluator) selectedEvaluator++;
322                  }
323                }
324              }
325            }
326          } else if (topLevelGroup.Name.Equals("ES")) {
327            foreach (IOperatorGroup group in topLevelGroup.SubGroups) {
328              if (group.Name.Equals("Mutation")) {
329                foreach (IOperator op in group.Operators) {
330                  int index = SetOperator(mutationComboBox, (IOperator)op.Clone(), mutators);
331                  if (index < 0) {
332                    index = AddOperator(mutationComboBox, (IOperator)op.Clone(), mutators);
333                    if (index <= selectedMutator) selectedMutator++;
334                  }
335                }
336              } else if (group.Name.Equals("Recombination")) {
337                foreach (IOperator op in group.Operators) {
338                  int index = SetOperator(recombinationComboBox, (IOperator)op.Clone(), recombinators);
339                  if (index < 0) {
340                    index = AddOperator(recombinationComboBox, (IOperator)op.Clone(), recombinators);
341                    if (index <= selectedRecombinator) selectedRecombinator++;
342                  }
343                }
344              }
345            }
346          }
347        }
348        problemInitializationComboBox.SelectedIndexChanged -= new EventHandler(problemInitializationComboBox_SelectedIndexChanged);
349        problemInitializationComboBox.SelectedIndex = selectedProblemInitializer;
350        problemInitializationComboBox.SelectedIndexChanged += new EventHandler(problemInitializationComboBox_SelectedIndexChanged);
351        solutionGenerationComboBox.SelectedIndexChanged -= new EventHandler(solutionGenerationComboBox_SelectedIndexChanged);
352        solutionGenerationComboBox.SelectedIndex = selectedSolutionGenerator;
353        solutionGenerationComboBox.SelectedIndexChanged += new EventHandler(solutionGenerationComboBox_SelectedIndexChanged);
354        evaluationComboBox.SelectedIndexChanged -= new EventHandler(evaluationComboBox_SelectedIndexChanged);
355        evaluationComboBox.SelectedIndex = selectedEvaluator;
356        evaluationComboBox.SelectedIndexChanged += new EventHandler(evaluationComboBox_SelectedIndexChanged);
357        mutationComboBox.SelectedIndexChanged -= new EventHandler(mutationComboBox_SelectedIndexChanged);
358        mutationComboBox.SelectedIndex = selectedMutator;
359        mutationComboBox.SelectedIndexChanged += new EventHandler(mutationComboBox_SelectedIndexChanged);
360        recombinationComboBox.SelectedIndexChanged -= new EventHandler(recombinationComboBox_SelectedIndexChanged);
361        recombinationComboBox.SelectedIndex = selectedRecombinator;
362        recombinationComboBox.SelectedIndexChanged += new EventHandler(recombinationComboBox_SelectedIndexChanged);
363      }
364    }
365
366    private void problemInitializationComboBox_SelectedIndexChanged(object sender, EventArgs e) {
367      int index = problemInitializationComboBox.SelectedIndex;
368      if (index != selectedProblemInitializer) {
369        if (index == problemInitializationComboBox.Items.Count - 1) {
370          if (chooseOperatorDialog == null) chooseOperatorDialog = new ChooseOperatorDialog();
371          if (chooseOperatorDialog.ShowDialog(this) == DialogResult.OK) {
372            selectedProblemInitializer = -1;
373            index = SetOperator(problemInitializationComboBox, chooseOperatorDialog.Operator, problemInitializers);
374            if (index < 0) index = AddOperator(problemInitializationComboBox, chooseOperatorDialog.Operator, problemInitializers);
375          } else {
376            problemInitializationComboBox.SelectedIndex = selectedProblemInitializer;
377            return;
378          }
379        }
380        if (index >= 0) {
381          ES.ProblemInjector = problemInitializers[index];
382          selectedProblemInitializer = index;
383          problemInitializationComboBox.SelectedIndex = index;
384        }
385      }
386    }
387
388    private void solutionGenerationComboBox_SelectedIndexChanged(object sender, EventArgs e) {
389      int index = solutionGenerationComboBox.SelectedIndex;
390      if (index != selectedSolutionGenerator) {
391        if (index == solutionGenerationComboBox.Items.Count - 1) {
392          if (chooseOperatorDialog == null) chooseOperatorDialog = new ChooseOperatorDialog();
393          if (chooseOperatorDialog.ShowDialog(this) == DialogResult.OK) {
394            selectedSolutionGenerator = -1;
395            index = SetOperator(solutionGenerationComboBox, chooseOperatorDialog.Operator, solutionGenerators);
396            if (index < 0) AddOperator(solutionGenerationComboBox, chooseOperatorDialog.Operator, solutionGenerators);
397          } else {
398            solutionGenerationComboBox.SelectedIndex = selectedSolutionGenerator;
399            return;
400          }
401        }
402        if (index >= 0) {
403          ES.SolutionGenerator = solutionGenerators[index];
404          selectedSolutionGenerator = index;
405          solutionGenerationComboBox.SelectedIndex = selectedSolutionGenerator;
406        }
407      }
408    }
409
410    private void evaluationComboBox_SelectedIndexChanged(object sender, EventArgs e) {
411      int index = evaluationComboBox.SelectedIndex;
412      if (index != selectedEvaluator) {
413        if (index == evaluationComboBox.Items.Count - 1) {
414          if (chooseOperatorDialog == null) chooseOperatorDialog = new ChooseOperatorDialog();
415          if (chooseOperatorDialog.ShowDialog(this) == DialogResult.OK) {
416            selectedEvaluator = -1;
417            index = SetOperator(evaluationComboBox, chooseOperatorDialog.Operator, evaluators);
418            if (index < 0) index = AddOperator(evaluationComboBox, chooseOperatorDialog.Operator, evaluators);
419          } else {
420            evaluationComboBox.SelectedIndex = selectedEvaluator;
421            return;
422          }
423        }
424        if (index >= 0) {
425          ES.Evaluator = evaluators[index];
426          selectedEvaluator = index;
427          evaluationComboBox.SelectedIndex = index;
428        }
429      }
430    }
431
432    private void mutationComboBox_SelectedIndexChanged(object sender, EventArgs e) {
433      int index = mutationComboBox.SelectedIndex;
434      if (index != selectedMutator) {
435        if (index == mutationComboBox.Items.Count - 1) {
436          if (chooseOperatorDialog == null) chooseOperatorDialog = new ChooseOperatorDialog();
437          if (chooseOperatorDialog.ShowDialog(this) == DialogResult.OK) {
438            selectedMutator = -1;
439            index = SetOperator(mutationComboBox, chooseOperatorDialog.Operator, mutators);
440            if (index < 0) index = AddOperator(mutationComboBox, chooseOperatorDialog.Operator, mutators);
441          } else {
442            mutationComboBox.SelectedIndex = selectedMutator;
443            return;
444          }
445        }
446        if (index >= 0) {
447          ES.Mutator = mutators[index];
448          selectedMutator = index;
449          mutationComboBox.SelectedIndex = index;
450        }
451      }
452    }
453
454    private void recombinationComboBox_SelectedIndexChanged(object sender, EventArgs e) {
455      int index = recombinationComboBox.SelectedIndex;
456      if (index != selectedRecombinator) {
457        if (index == recombinationComboBox.Items.Count - 1) {
458          if (chooseOperatorDialog == null) chooseOperatorDialog = new ChooseOperatorDialog();
459          if (chooseOperatorDialog.ShowDialog(this) == DialogResult.OK) {
460            selectedRecombinator = -1;
461            index = SetOperator(recombinationComboBox, chooseOperatorDialog.Operator, recombinators);
462            if (index < 0) index = AddOperator(recombinationComboBox, chooseOperatorDialog.Operator, recombinators);
463          } else {
464            recombinationComboBox.SelectedIndex = selectedRecombinator;
465            return;
466          }
467        }
468        if (index >= 0) {
469          ES.Recombinator = recombinators[index];
470          selectedRecombinator = index;
471          recombinationComboBox.SelectedIndex = index;
472        }
473      }
474    }
[71]475  }
476}
Note: See TracBrowser for help on using the repository browser.