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