Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.SGA/3.3/SGAView.cs @ 2601

Last change on this file since 2601 was 2546, checked in by swagner, 15 years ago

Continued work on Optimizer and on adapting all views to the new MainForm concept (#770)

File size: 9.4 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;
31using HeuristicLab.Common;
32using HeuristicLab.Core.Views;
33using HeuristicLab.MainForm;
34
35namespace HeuristicLab.SGA {
36  /// <summary>
37  /// Visual representation of the <see cref="SGA"/> class.
38  /// </summary>
39  [Content(typeof(SGA), true)]
40  public partial class SGAView : ItemViewBase {
41    private ChooseOperatorDialog chooseOperatorDialog;
42
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>
48    public SGA SGA {
49      get { return (SGA)Item; }
50      set { base.Item = value; }
51    }
52
53    /// <summary>
54    /// Initializes a new instance of <see cref="SGAEditor"/>.
55    /// </summary>
56    public SGAView() {
57      InitializeComponent();
58    }
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>
63    public SGAView(SGA sga)
64      : this() {
65      SGA = sga;
66    }
67
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>
72    protected override void RemoveItemEvents() {
73      SGA.Engine.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
74      SGA.Engine.Finished -= new EventHandler(Engine_Finished);
75      scopeView.Scope = null;
76      base.RemoveItemEvents();
77    }
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>
82    protected override void AddItemEvents() {
83      base.AddItemEvents();
84      SGA.Engine.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
85      SGA.Engine.Finished += new EventHandler(Engine_Finished);
86      SetDataBinding();
87      scopeView.Scope = SGA.Engine.GlobalScope;
88    }
89
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>
94    protected override void UpdateControls() {
95      base.UpdateControls();
96      if (SGA == null) {
97        tabControl.Enabled = false;
98      } else {
99        tabControl.Enabled = true;
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;
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) {
120      IView view = MainFormManager.CreateDefaultView(SGA.ProblemInjector);
121      if (view != null) MainFormManager.MainForm.ShowView(view);
122    }
123    private void viewSolutionGenerationButton_Click(object sender, EventArgs e) {
124      IView view = MainFormManager.CreateDefaultView(SGA.SolutionGenerator);
125      if (view != null) MainFormManager.MainForm.ShowView(view);
126    }
127    private void viewSelectionButton_Click(object sender, EventArgs e) {
128      IView view = MainFormManager.CreateDefaultView(SGA.Selector);
129      if (view != null) MainFormManager.MainForm.ShowView(view);
130    }
131    private void viewCrossoverButton_Click(object sender, EventArgs e) {
132      IView view = MainFormManager.CreateDefaultView(SGA.Crossover);
133      if (view != null) MainFormManager.MainForm.ShowView(view);
134    }
135    private void viewMutationButton_Click(object sender, EventArgs e) {
136      IView view = MainFormManager.CreateDefaultView(SGA.Mutator);
137      if (view != null) MainFormManager.MainForm.ShowView(view);
138    }
139    private void viewEvaluationButton_Click(object sender, EventArgs e) {
140      IView view = MainFormManager.CreateDefaultView(SGA.Evaluator);
141      if (view != null) MainFormManager.MainForm.ShowView(view);
142    }
143    private void setProblemInitializationButton_Click(object sender, EventArgs e) {
144      if (chooseOperatorDialog == null) chooseOperatorDialog = new ChooseOperatorDialog();
145      if (chooseOperatorDialog.ShowDialog(this) == DialogResult.OK) {
146        SGA.ProblemInjector = chooseOperatorDialog.Operator;
147        problemInitializationTextBox.Text = SGA.ProblemInjector.GetType().Name;
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;
154        solutionGenerationTextBox.Text = SGA.SolutionGenerator.GetType().Name;
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;
161        selectionTextBox.Text = SGA.Selector.GetType().Name;
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;
168        crossoverTextBox.Text = SGA.Crossover.GetType().Name;
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;
175        mutationTextBox.Text = SGA.Mutator.GetType().Name;
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;
182        evaluationTextBox.Text = SGA.Evaluator.GetType().Name;
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();
198      IView view = MainFormManager.CreateDefaultView(clone);
199      if (view != null) MainFormManager.MainForm.ShowView(view);
200    }
201    #endregion
202
203    #region Engine Events
204    private delegate void OnExceptionEventDelegate(object sender, EventArgs<Exception> e);
205    private void Engine_ExceptionOccurred(object sender, EventArgs<Exception> e) {
206      if (InvokeRequired)
207        Invoke(new OnExceptionEventDelegate(Engine_ExceptionOccurred), sender, e);
208      else
209        HeuristicLab.Core.Views.Auxiliary.ShowErrorMessageBox(e.Value);
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}
Note: See TracBrowser for help on using the repository browser.