Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.SGA/SGAEditor.cs @ 1308

Last change on this file since 1308 was 1153, checked in by vdorfer, 16 years ago

Created API documentation for HeuristicLab.Random, HeuristicLab.SGA and HeuristicLab.Selection.OffspringSelection namespace (#331)

File size: 9.2 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.SGA {
33  /// <summary>
34  /// Visual representation of the <see cref="SGA"/> class.
35  /// </summary>
36  public partial class SGAEditor : EditorBase {
37    private ChooseOperatorDialog chooseOperatorDialog;
38
39    /// <summary>
40    /// Gets or sets the <see cref="SGA"/> item to represent visually.
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 SGA SGA {
45      get { return (SGA)Item; }
46      set { base.Item = value; }
47    }
48
49    /// <summary>
50    /// Initializes a new instance of <see cref="SGAEditor"/>.
51    /// </summary>
52    public SGAEditor() {
53      InitializeComponent();
54    }
55    /// <summary>
56    /// Initializes a new instance of <see cref="SGAEditor"/> with the given <paramref name="sga"/>.
57    /// </summary>
58    /// <param name="sga">The simple genetic algorithm to represent visually.</param>
59    public SGAEditor(SGA sga)
60      : this() {
61      SGA = sga;
62    }
63
64    /// <summary>
65    /// Removes the eventhandlers from the underlying <see cref="IEngine"/> of the <see cref="SGA"/>.
66    /// </summary>
67    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="EditorBase"/>.</remarks>
68    protected override void RemoveItemEvents() {
69      SGA.Engine.ExceptionOccurred -= new EventHandler<ExceptionEventArgs>(Engine_ExceptionOccurred);
70      SGA.Engine.Finished -= new EventHandler(Engine_Finished);
71      scopeView.Scope = null;
72      base.RemoveItemEvents();
73    }
74    /// <summary>
75    /// Adds eventhandlers to the underlying <see cref="IEngine"/> of the <see cref="SGA"/>.
76    /// </summary>
77    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="EditorBase"/>.</remarks>
78    protected override void AddItemEvents() {
79      base.AddItemEvents();
80      SGA.Engine.ExceptionOccurred += new EventHandler<ExceptionEventArgs>(Engine_ExceptionOccurred);
81      SGA.Engine.Finished += new EventHandler(Engine_Finished);
82      SetDataBinding();
83      scopeView.Scope = SGA.Engine.GlobalScope;
84    }
85
86    /// <summary>
87    /// Updates all controls with the latest data of the model.
88    /// </summary>
89    /// <remarks>Calls <see cref="EditorBase.UpdateControls"/> of base class <see cref="EditorBase"/>.</remarks>
90    protected override void UpdateControls() {
91      base.UpdateControls();
92      if (SGA == null) {
93        tabControl.Enabled = false;
94      } else {
95        tabControl.Enabled = true;
96        problemInitializationTextBox.Text = SGA.ProblemInjector.GetType().Name;
97        solutionGenerationTextBox.Text = SGA.SolutionGenerator.GetType().Name;
98        selectionTextBox.Text = SGA.Selector.GetType().Name;
99        crossoverTextBox.Text = SGA.Crossover.GetType().Name;
100        mutationTextBox.Text = SGA.Mutator.GetType().Name;
101        evaluationTextBox.Text = SGA.Evaluator.GetType().Name;
102      }
103    }
104
105    private void SetDataBinding() {
106      setRandomSeedRandomlyCheckBox.DataBindings.Add("Checked", SGA, "SetSeedRandomly");
107      randomSeedTextBox.DataBindings.Add("Text", SGA, "Seed");
108      populationSizeTextBox.DataBindings.Add("Text", SGA, "PopulationSize");
109      maximumGenerationsTextBox.DataBindings.Add("Text", SGA, "MaximumGenerations");
110      mutationRateTextBox.DataBindings.Add("Text", SGA, "MutationRate");
111      elitesTextBox.DataBindings.Add("Text", SGA, "Elites");
112    }
113
114    #region Button Events
115    private void viewProblemInitializationButton_Click(object sender, EventArgs e) {
116      IView view = SGA.ProblemInjector.CreateView();
117      if (view != null)
118        PluginManager.ControlManager.ShowControl(view);
119    }
120    private void viewSolutionGenerationButton_Click(object sender, EventArgs e) {
121      IView view = SGA.SolutionGenerator.CreateView();
122      if (view != null)
123        PluginManager.ControlManager.ShowControl(view);
124    }
125    private void viewSelectionButton_Click(object sender, EventArgs e) {
126      IView view = SGA.Selector.CreateView();
127      if (view != null)
128        PluginManager.ControlManager.ShowControl(view);
129    }
130    private void viewCrossoverButton_Click(object sender, EventArgs e) {
131      IView view = SGA.Crossover.CreateView();
132      if (view != null)
133        PluginManager.ControlManager.ShowControl(view);
134    }
135    private void viewMutationButton_Click(object sender, EventArgs e) {
136      IView view = SGA.Mutator.CreateView();
137      if (view != null)
138        PluginManager.ControlManager.ShowControl(view);
139    }
140    private void viewEvaluationButton_Click(object sender, EventArgs e) {
141      IView view = SGA.Evaluator.CreateView();
142      if (view != null)
143        PluginManager.ControlManager.ShowControl(view);
144    }
145    private void setProblemInitializationButton_Click(object sender, EventArgs e) {
146      if (chooseOperatorDialog == null) chooseOperatorDialog = new ChooseOperatorDialog();
147      if (chooseOperatorDialog.ShowDialog(this) == DialogResult.OK) {
148        SGA.ProblemInjector = chooseOperatorDialog.Operator;
149        problemInitializationTextBox.Text = SGA.ProblemInjector.GetType().Name;
150      }
151    }
152    private void setSolutionGenerationButton_Click(object sender, EventArgs e) {
153      if (chooseOperatorDialog == null) chooseOperatorDialog = new ChooseOperatorDialog();
154      if (chooseOperatorDialog.ShowDialog(this) == DialogResult.OK) {
155        SGA.SolutionGenerator= chooseOperatorDialog.Operator;
156        solutionGenerationTextBox.Text = SGA.SolutionGenerator.GetType().Name;
157      }
158    }
159    private void setSelectionButton_Click(object sender, EventArgs e) {
160      if (chooseOperatorDialog == null) chooseOperatorDialog = new ChooseOperatorDialog();
161      if (chooseOperatorDialog.ShowDialog(this) == DialogResult.OK) {
162        SGA.Selector = chooseOperatorDialog.Operator;
163        selectionTextBox.Text = SGA.Selector.GetType().Name;
164      }
165    }
166    private void setCrossoverButton_Click(object sender, EventArgs e) {
167      if (chooseOperatorDialog == null) chooseOperatorDialog = new ChooseOperatorDialog();
168      if (chooseOperatorDialog.ShowDialog(this) == DialogResult.OK) {
169        SGA.Crossover = chooseOperatorDialog.Operator;
170        crossoverTextBox.Text = SGA.Crossover.GetType().Name;
171      }
172    }
173    private void setMutationButton_Click(object sender, EventArgs e) {
174      if (chooseOperatorDialog == null) chooseOperatorDialog = new ChooseOperatorDialog();
175      if (chooseOperatorDialog.ShowDialog(this) == DialogResult.OK) {
176        SGA.Mutator = chooseOperatorDialog.Operator;
177        mutationTextBox.Text = SGA.Mutator.GetType().Name;
178      }
179    }
180    private void setEvaluationButton_Click(object sender, EventArgs e) {
181      if (chooseOperatorDialog == null) chooseOperatorDialog = new ChooseOperatorDialog();
182      if (chooseOperatorDialog.ShowDialog(this) == DialogResult.OK) {
183        SGA.Evaluator = chooseOperatorDialog.Operator;
184        evaluationTextBox.Text = SGA.Evaluator.GetType().Name;
185      }
186    }
187    private void executeButton_Click(object sender, EventArgs e) {
188      executeButton.Enabled = false;
189      abortButton.Enabled = true;
190      SGA.Engine.Execute();
191    }
192    private void abortButton_Click(object sender, EventArgs e) {
193      SGA.Engine.Abort();
194    }
195    private void resetButton_Click(object sender, EventArgs e) {
196      SGA.Engine.Reset();
197    }
198    private void cloneEngineButton_Click(object sender, EventArgs e) {
199      IEngine clone = (IEngine)SGA.Engine.Clone();
200      IEditor editor = ((IEditable)clone).CreateEditor();
201      PluginManager.ControlManager.ShowControl(editor);
202    }
203    #endregion
204
205    #region Engine Events
206    private delegate void OnExceptionEventDelegate(object sender, ExceptionEventArgs e);
207    private void Engine_ExceptionOccurred(object sender, ExceptionEventArgs e) {
208      if (InvokeRequired)
209        Invoke(new OnExceptionEventDelegate(Engine_ExceptionOccurred), sender, e);
210      else
211        Auxiliary.ShowErrorMessageBox(e.Exception);
212    }
213    private void Engine_Finished(object sender, EventArgs e) {
214      scopeView.Refresh();
215      executeButton.Enabled = true;
216      abortButton.Enabled = false;
217    }
218    #endregion
219  }
220}
Note: See TracBrowser for help on using the repository browser.