[2] | 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.SGA {
|
---|
[1153] | 33 | /// <summary>
|
---|
| 34 | /// Visual representation of the <see cref="SGA"/> class.
|
---|
| 35 | /// </summary>
|
---|
[2] | 36 | public partial class SGAEditor : EditorBase {
|
---|
| 37 | private ChooseOperatorDialog chooseOperatorDialog;
|
---|
| 38 |
|
---|
[1153] | 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>
|
---|
[2] | 44 | public SGA SGA {
|
---|
| 45 | get { return (SGA)Item; }
|
---|
| 46 | set { base.Item = value; }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[1153] | 49 | /// <summary>
|
---|
| 50 | /// Initializes a new instance of <see cref="SGAEditor"/>.
|
---|
| 51 | /// </summary>
|
---|
[2] | 52 | public SGAEditor() {
|
---|
| 53 | InitializeComponent();
|
---|
| 54 | }
|
---|
[1153] | 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>
|
---|
[2] | 59 | public SGAEditor(SGA sga)
|
---|
| 60 | : this() {
|
---|
| 61 | SGA = sga;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[1153] | 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>
|
---|
[2] | 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 | }
|
---|
[1153] | 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>
|
---|
[2] | 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 |
|
---|
[1153] | 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>
|
---|
[2] | 90 | protected override void UpdateControls() {
|
---|
| 91 | base.UpdateControls();
|
---|
| 92 | if (SGA == null) {
|
---|
| 93 | tabControl.Enabled = false;
|
---|
| 94 | } else {
|
---|
| 95 | tabControl.Enabled = true;
|
---|
[65] | 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;
|
---|
[2] | 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) {
|
---|
[65] | 116 | IView view = SGA.ProblemInjector.CreateView();
|
---|
[2] | 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) {
|
---|
[65] | 148 | SGA.ProblemInjector = chooseOperatorDialog.Operator;
|
---|
| 149 | problemInitializationTextBox.Text = SGA.ProblemInjector.GetType().Name;
|
---|
[2] | 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;
|
---|
[65] | 156 | solutionGenerationTextBox.Text = SGA.SolutionGenerator.GetType().Name;
|
---|
[2] | 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;
|
---|
[65] | 163 | selectionTextBox.Text = SGA.Selector.GetType().Name;
|
---|
[2] | 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;
|
---|
[65] | 170 | crossoverTextBox.Text = SGA.Crossover.GetType().Name;
|
---|
[2] | 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;
|
---|
[65] | 177 | mutationTextBox.Text = SGA.Mutator.GetType().Name;
|
---|
[2] | 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;
|
---|
[65] | 184 | evaluationTextBox.Text = SGA.Evaluator.GetType().Name;
|
---|
[2] | 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 | }
|
---|