Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone2/sources/HeuristicLab.ES/3.2/ESEditor.cs @ 6319

Last change on this file since 6319 was 1529, checked in by gkronber, 16 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

File size: 10.5 KB
Line 
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 {
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    }
55    /// <summary>
56    /// Initializes a new instance of <see cref="ESEditor"/> with the given <paramref name="es"/>.
57    /// </summary>
58    /// <param name="es">The evolution strategy to display.</param>
59    public ESEditor(ES es)
60      : this() {
61      ES = es;
62    }
63
64    /// <summary>
65    /// Removes all event handlers from the underlying <see cref="ES"/>.
66    /// </summary>
67    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="EditorBase"/>.</remarks>
68    protected override void RemoveItemEvents() {
69      ES.Engine.ExceptionOccurred -= new EventHandler<ExceptionEventArgs>(Engine_ExceptionOccurred);
70      ES.Engine.Finished -= new EventHandler(Engine_Finished);
71      ES.Changed -= new EventHandler(ES_Changed);
72      scopeView.Scope = null;
73      base.RemoveItemEvents();
74    }
75    /// <summary>
76    /// Adds event handlers to the underlying <see cref="ES"/>.
77    /// </summary>
78    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="EditorBase"/>.</remarks>
79    protected override void AddItemEvents() {
80      base.AddItemEvents();
81      ES.Engine.ExceptionOccurred += new EventHandler<ExceptionEventArgs>(Engine_ExceptionOccurred);
82      ES.Engine.Finished += new EventHandler(Engine_Finished);
83      ES.Changed += new EventHandler(ES_Changed);
84      SetDataBinding();
85      scopeView.Scope = ES.Engine.GlobalScope;
86    }
87
88    void ES_Changed(object sender, EventArgs e) {
89      // neither Refresh() nor Update() work
90      randomSeedTextBox.Text = ES.Seed.ToString();
91      muTextBox.Text = ES.Mu.ToString();
92      rhoTextBox.Text = ES.Rho.ToString();
93      lambdaTextBox.Text = ES.Lambda.ToString();
94      learningRateTextBox.Text = ES.LearningRate.ToString();
95      generalLearningRateTextBox.Text = ES.GeneralLearningRate.ToString();
96    }
97
98    /// <summary>
99    /// Updates all controls with the latest data of the model.
100    /// </summary>
101    /// <remarks>Calls <see cref="ViewBase.UpdateControls"/> of base class <see cref="EditorBase"/>.</remarks>
102    protected override void UpdateControls() {
103      base.UpdateControls();
104      if (ES == null) {
105        tabControl.Enabled = false;
106      } else {
107        tabControl.Enabled = true;
108        problemInitializationTextBox.Text = ES.ProblemInjector.GetType().Name;
109        solutionGenerationTextBox.Text = ES.SolutionGenerator.GetType().Name;
110        mutationTextBox.Text = ES.Mutator.GetType().Name;
111        evaluationTextBox.Text = ES.Evaluator.GetType().Name;
112        recombinationTextBox.Text = ES.Recombinator.GetType().Name;
113        plusRadioButton.Checked = ES.PlusNotation;
114        commaRadioButton.Checked = !ES.PlusNotation;
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      shakingFactorsLowerBoundTextBox.DataBindings.Add("Text", ES, "ShakingFactorsMin");
126      shakingFactorsUpperBoundTextBox.DataBindings.Add("Text", ES, "ShakingFactorsMax");
127      problemDimensionTextBox.DataBindings.Add("Text", ES, "ProblemDimension");
128      generalLearningRateTextBox.DataBindings.Add("Text", ES, "GeneralLearningRate");
129      learningRateTextBox.DataBindings.Add("Text", ES, "LearningRate");
130    }
131
132    #region Button Events
133    private void viewProblemInitializationButton_Click(object sender, EventArgs e) {
134      IView view = ES.ProblemInjector.CreateView();
135      if (view != null)
136        PluginManager.ControlManager.ShowControl(view);
137    }
138    private void viewSolutionGenerationButton_Click(object sender, EventArgs e) {
139      IView view = ES.SolutionGenerator.CreateView();
140      if (view != null)
141        PluginManager.ControlManager.ShowControl(view);
142    }
143    private void viewMutationButton_Click(object sender, EventArgs e) {
144      IView view = ES.Mutator.CreateView();
145      if (view != null)
146        PluginManager.ControlManager.ShowControl(view);
147    }
148    private void viewEvaluationButton_Click(object sender, EventArgs e) {
149      IView view = ES.Evaluator.CreateView();
150      if (view != null)
151        PluginManager.ControlManager.ShowControl(view);
152    }
153    private void viewRecombinationButton_Click(object sender, EventArgs e) {
154      IView view = ES.Recombinator.CreateView();
155      if (view != null)
156        PluginManager.ControlManager.ShowControl(view);
157    }
158    private void setProblemInitializationButton_Click(object sender, EventArgs e) {
159      if (chooseOperatorDialog == null) chooseOperatorDialog = new ChooseOperatorDialog();
160      if (chooseOperatorDialog.ShowDialog(this) == DialogResult.OK) {
161        ES.ProblemInjector = chooseOperatorDialog.Operator;
162        problemInitializationTextBox.Text = ES.ProblemInjector.GetType().Name;
163      }
164    }
165    private void setSolutionGenerationButton_Click(object sender, EventArgs e) {
166      if (chooseOperatorDialog == null) chooseOperatorDialog = new ChooseOperatorDialog();
167      if (chooseOperatorDialog.ShowDialog(this) == DialogResult.OK) {
168        ES.SolutionGenerator= chooseOperatorDialog.Operator;
169        solutionGenerationTextBox.Text = ES.SolutionGenerator.GetType().Name;
170      }
171    }
172    private void setMutationButton_Click(object sender, EventArgs e) {
173      if (chooseOperatorDialog == null) chooseOperatorDialog = new ChooseOperatorDialog();
174      if (chooseOperatorDialog.ShowDialog(this) == DialogResult.OK) {
175        ES.Mutator = chooseOperatorDialog.Operator;
176        mutationTextBox.Text = ES.Mutator.GetType().Name;
177      }
178    }
179    private void setEvaluationButton_Click(object sender, EventArgs e) {
180      if (chooseOperatorDialog == null) chooseOperatorDialog = new ChooseOperatorDialog();
181      if (chooseOperatorDialog.ShowDialog(this) == DialogResult.OK) {
182        ES.Evaluator = chooseOperatorDialog.Operator;
183        evaluationTextBox.Text = ES.Evaluator.GetType().Name;
184      }
185    }
186    private void setRecombinationButton_Click(object sender, EventArgs e) {
187      if (chooseOperatorDialog == null) chooseOperatorDialog = new ChooseOperatorDialog();
188      if (chooseOperatorDialog.ShowDialog(this) == DialogResult.OK) {
189        ES.Recombinator = chooseOperatorDialog.Operator;
190        recombinationTextBox.Text = ES.Recombinator.GetType().Name;
191      }
192    }
193    private void executeButton_Click(object sender, EventArgs e) {
194      executeButton.Enabled = false;
195      abortButton.Enabled = true;
196      ES.Engine.Execute();
197    }
198    private void abortButton_Click(object sender, EventArgs e) {
199      ES.Engine.Abort();
200    }
201    private void resetButton_Click(object sender, EventArgs e) {
202      ES.Engine.Reset();
203    }
204    private void cloneEngineButton_Click(object sender, EventArgs e) {
205      IEngine clone = (IEngine)ES.Engine.Clone();
206      IEditor editor = ((IEditable)clone).CreateEditor();
207      PluginManager.ControlManager.ShowControl(editor);
208    }
209    #endregion
210
211    #region Engine Events
212    private delegate void OnExceptionEventDelegate(object sender, ExceptionEventArgs e);
213    private void Engine_ExceptionOccurred(object sender, ExceptionEventArgs e) {
214      if (InvokeRequired)
215        Invoke(new OnExceptionEventDelegate(Engine_ExceptionOccurred), sender, e);
216      else
217        Auxiliary.ShowErrorMessageBox(e.Exception);
218    }
219    private void Engine_Finished(object sender, EventArgs e) {
220      scopeView.Refresh();
221      if (executeButton.InvokeRequired) {
222        executeButton.Invoke(new MethodInvoker(EnableExecute));
223      } else {
224        executeButton.Enabled = true;
225        abortButton.Enabled = false;
226      }
227    }
228    private void EnableExecute() {
229      executeButton.Enabled = true;
230      abortButton.Enabled = false;
231    }
232    #endregion
233
234    #region RadioButton Events
235    private void plusRadioButton_CheckedChanged(object sender, EventArgs e) {
236      if (plusRadioButton.Checked) ES.PlusNotation = true;
237    }
238
239    private void commaRadioButton_CheckedChanged(object sender, EventArgs e) {
240      if (commaRadioButton.Checked) ES.PlusNotation = false;
241    }
242    #endregion
243
244    private void problemDimensionTextBox_Validated(object sender, EventArgs e) {
245      UpdateLearningRates();
246      Refresh();
247    }
248
249    private void UpdateLearningRates() {
250      if (ES != null) {
251        int dimension = int.Parse(problemDimensionTextBox.Text);
252        ES.GeneralLearningRate = 1 / Math.Sqrt(2 * dimension);
253        ES.LearningRate = 1 / Math.Sqrt(2 * Math.Sqrt(dimension));
254      }
255    }
256  }
257}
Note: See TracBrowser for help on using the repository browser.