Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.SGA/3.2/SGAEditor.cs @ 3098

Last change on this file since 3098 was 2591, checked in by gkronber, 15 years ago

Copied refactored plugin infrastructure from branch and merged changeset r2586:2589 from branch into the trunk. #799

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