Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.SA/SAEditor.cs @ 1289

Last change on this file since 1289 was 1289, checked in by abeham, 15 years ago

Added simulated annealing (SA) user interface (#18)

File size: 8.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2009 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.SA {
33  /// <summary>
34  /// Visual representation of the <see cref="SA"/> class.
35  /// </summary>
36  public partial class SAEditor : EditorBase {
37    private ChooseOperatorDialog chooseOperatorDialog;
38
39    /// <summary>
40    /// Gets or sets the <see cref="SA"/> 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 SA SA {
45      get { return (SA)Item; }
46      set { base.Item = value; }
47    }
48
49    /// <summary>
50    /// Initializes a new instance of <see cref="SAEditor"/>.
51    /// </summary>
52    public SAEditor() {
53      InitializeComponent();
54    }
55    /// <summary>
56    /// Initializes a new instance of <see cref="SAEditor"/> with the given <paramref name="sga"/>.
57    /// </summary>
58    /// <param name="sa">The simulated annealing algorithm to represent visually.</param>
59    public SAEditor(SA sa)
60      : this() {
61      SA = sa;
62    }
63
64    /// <summary>
65    /// Removes the eventhandlers from the underlying <see cref="IEngine"/> of the <see cref="SA"/>.
66    /// </summary>
67    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="EditorBase"/>.</remarks>
68    protected override void RemoveItemEvents() {
69      SA.Engine.ExceptionOccurred -= new EventHandler<ExceptionEventArgs>(Engine_ExceptionOccurred);
70      SA.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="SA"/>.
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      SA.Engine.ExceptionOccurred += new EventHandler<ExceptionEventArgs>(Engine_ExceptionOccurred);
81      SA.Engine.Finished += new EventHandler(Engine_Finished);
82      SetDataBinding();
83      scopeView.Scope = SA.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 (SA == null) {
93        tabControl.Enabled = false;
94      } else {
95        tabControl.Enabled = true;
96        problemInitializationTextBox.Text = SA.ProblemInjector.GetType().Name;
97        solutionGenerationTextBox.Text = SA.SolutionGenerator.GetType().Name;
98        annealingSchemaTextBox.Text = SA.AnnealingScheme.GetType().Name;
99        mutationTextBox.Text = SA.Mutator.GetType().Name;
100        evaluationTextBox.Text = SA.Evaluator.GetType().Name;
101      }
102    }
103
104    private void SetDataBinding() {
105      setRandomSeedRandomlyCheckBox.DataBindings.Add("Checked", SA, "SetSeedRandomly");
106      randomSeedTextBox.DataBindings.Add("Text", SA, "Seed");
107      maximumIterationsTextBox.DataBindings.Add("Text", SA, "MaximumIterations");
108      maximumIterationEffortTextBox.DataBindings.Add("Text", SA, "MaximumIterationEffort");
109      temperatureTextBox.DataBindings.Add("Text", SA, "Temperature");
110      annealingParameterTextBox.DataBindings.Add("Text", SA, "AnnealingParameter");
111    }
112
113    #region Button Events
114    private void viewProblemInitializationButton_Click(object sender, EventArgs e) {
115      IView view = SA.ProblemInjector.CreateView();
116      if (view != null)
117        PluginManager.ControlManager.ShowControl(view);
118    }
119    private void viewSolutionGenerationButton_Click(object sender, EventArgs e) {
120      IView view = SA.SolutionGenerator.CreateView();
121      if (view != null)
122        PluginManager.ControlManager.ShowControl(view);
123    }
124    private void viewAnnealingSchemeButton_Click(object sender, EventArgs e) {
125      IView view = SA.AnnealingScheme.CreateView();
126      if (view != null)
127        PluginManager.ControlManager.ShowControl(view);
128    }
129    private void viewMutationButton_Click(object sender, EventArgs e) {
130      IView view = SA.Mutator.CreateView();
131      if (view != null)
132        PluginManager.ControlManager.ShowControl(view);
133    }
134    private void viewEvaluationButton_Click(object sender, EventArgs e) {
135      IView view = SA.Evaluator.CreateView();
136      if (view != null)
137        PluginManager.ControlManager.ShowControl(view);
138    }
139    private void setProblemInitializationButton_Click(object sender, EventArgs e) {
140      if (chooseOperatorDialog == null) chooseOperatorDialog = new ChooseOperatorDialog();
141      if (chooseOperatorDialog.ShowDialog(this) == DialogResult.OK) {
142        SA.ProblemInjector = chooseOperatorDialog.Operator;
143        problemInitializationTextBox.Text = SA.ProblemInjector.GetType().Name;
144      }
145    }
146    private void setSolutionGenerationButton_Click(object sender, EventArgs e) {
147      if (chooseOperatorDialog == null) chooseOperatorDialog = new ChooseOperatorDialog();
148      if (chooseOperatorDialog.ShowDialog(this) == DialogResult.OK) {
149        SA.SolutionGenerator= chooseOperatorDialog.Operator;
150        solutionGenerationTextBox.Text = SA.SolutionGenerator.GetType().Name;
151      }
152    }
153    private void setAnnealingSchemeButton_Click(object sender, EventArgs e) {
154      if (chooseOperatorDialog == null) chooseOperatorDialog = new ChooseOperatorDialog();
155      if (chooseOperatorDialog.ShowDialog(this) == DialogResult.OK) {
156        SA.AnnealingScheme = chooseOperatorDialog.Operator;
157        annealingSchemaTextBox.Text = SA.AnnealingScheme.GetType().Name;
158      }
159    }
160    private void setMutationButton_Click(object sender, EventArgs e) {
161      if (chooseOperatorDialog == null) chooseOperatorDialog = new ChooseOperatorDialog();
162      if (chooseOperatorDialog.ShowDialog(this) == DialogResult.OK) {
163        SA.Mutator = chooseOperatorDialog.Operator;
164        mutationTextBox.Text = SA.Mutator.GetType().Name;
165      }
166    }
167    private void setEvaluationButton_Click(object sender, EventArgs e) {
168      if (chooseOperatorDialog == null) chooseOperatorDialog = new ChooseOperatorDialog();
169      if (chooseOperatorDialog.ShowDialog(this) == DialogResult.OK) {
170        SA.Evaluator = chooseOperatorDialog.Operator;
171        evaluationTextBox.Text = SA.Evaluator.GetType().Name;
172      }
173    }
174    private void executeButton_Click(object sender, EventArgs e) {
175      executeButton.Enabled = false;
176      abortButton.Enabled = true;
177      SA.Engine.Execute();
178    }
179    private void abortButton_Click(object sender, EventArgs e) {
180      SA.Engine.Abort();
181    }
182    private void resetButton_Click(object sender, EventArgs e) {
183      SA.Engine.Reset();
184    }
185    private void cloneEngineButton_Click(object sender, EventArgs e) {
186      IEngine clone = (IEngine)SA.Engine.Clone();
187      IEditor editor = ((IEditable)clone).CreateEditor();
188      PluginManager.ControlManager.ShowControl(editor);
189    }
190    #endregion
191
192    #region Engine Events
193    private delegate void OnExceptionEventDelegate(object sender, ExceptionEventArgs e);
194    private void Engine_ExceptionOccurred(object sender, ExceptionEventArgs e) {
195      if (InvokeRequired)
196        Invoke(new OnExceptionEventDelegate(Engine_ExceptionOccurred), sender, e);
197      else
198        Auxiliary.ShowErrorMessageBox(e.Exception);
199    }
200    private void Engine_Finished(object sender, EventArgs e) {
201      scopeView.Refresh();
202      executeButton.Enabled = true;
203      abortButton.Enabled = false;
204    }
205    #endregion
206  }
207}
Note: See TracBrowser for help on using the repository browser.