Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.Algorithms/3.2/OffSpringSelectionGpEditor.cs @ 3042

Last change on this file since 3042 was 2728, checked in by gkronber, 15 years ago

Created a specialized view for function library injectors which allows full configuration of the function library. #748 (FunctionLibraryView is empty)

File size: 10.5 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.Windows.Forms;
24using HeuristicLab.PluginInfrastructure;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27
28namespace HeuristicLab.GP.Algorithms {
29  public partial class OffspringSelectionGpEditor : EditorBase {
30    private ChooseOperatorDialog chooseOperatorDialog;
31
32    public OffspringSelectionGP OffspringSelectionGP {
33      get { return (OffspringSelectionGP)Item; }
34      set { base.Item = value; }
35    }
36
37    public OffspringSelectionGpEditor() {
38      InitializeComponent();
39    }
40    public OffspringSelectionGpEditor(OffspringSelectionGP osgp)
41      : this() {
42      OffspringSelectionGP = osgp;
43    }
44
45    protected override void RemoveItemEvents() {
46      OffspringSelectionGP.Engine.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
47      OffspringSelectionGP.Engine.Finished -= new EventHandler(Engine_Finished);
48      scopeView.Scope = null;
49      base.RemoveItemEvents();
50    }
51
52    protected override void AddItemEvents() {
53      base.AddItemEvents();
54      OffspringSelectionGP.Engine.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
55      OffspringSelectionGP.Engine.Finished += new EventHandler(Engine_Finished);
56      SetDataBinding();
57      scopeView.Scope = OffspringSelectionGP.Engine.GlobalScope;
58    }
59
60    protected override void UpdateControls() {
61      base.UpdateControls();
62      if (OffspringSelectionGP == null) {
63        tabControl.Enabled = false;
64      } else {
65        tabControl.Enabled = true;
66        problemInitializationTextBox.Text = OffspringSelectionGP.ProblemInjector.Name;
67        functionLibraryInjectorTextBox.Text = OffspringSelectionGP.FunctionLibraryInjector.Name;
68      }
69    }
70
71    protected virtual void SetDataBinding() {
72      setRandomSeedRandomlyCheckBox.DataBindings.Add("Checked", OffspringSelectionGP, "SetSeedRandomly");
73      randomSeedTextBox.DataBindings.Add("Text", OffspringSelectionGP, "RandomSeed");
74      populationSizeTextBox.DataBindings.Add("Text", OffspringSelectionGP, "PopulationSize");
75      maximumEvaluatedSolutionsTextBox.DataBindings.Add("Text", OffspringSelectionGP, "MaxEvaluatedSolutions");
76      selectionPressureTextBox.DataBindings.Add("Text", OffspringSelectionGP, "SelectionPressureLimit");
77      mutationRateTextBox.DataBindings.Add("Text", OffspringSelectionGP, "MutationRate");
78      elitesTextBox.DataBindings.Add("Text", OffspringSelectionGP, "Elites");
79      comparisonFactorTextBox.DataBindings.Add("Text", OffspringSelectionGP, "ComparisonFactor");
80      successRatioLimitTextBox.DataBindings.Add("Text", OffspringSelectionGP, "SuccessRatioLimit");
81    }
82
83    #region Button Events
84    private void viewProblemInjectorButton_Click(object sender, EventArgs e) {
85      IView view = OffspringSelectionGP.ProblemInjector.CreateView();
86      if (view != null)
87        ControlManager.Manager.ShowControl(view);
88    }
89
90    private void setProblemInitializationButton_Click(object sender, EventArgs e) {
91      if (chooseOperatorDialog == null) chooseOperatorDialog = new ChooseOperatorDialog();
92      if (chooseOperatorDialog.ShowDialog(this) == DialogResult.OK) {
93        OffspringSelectionGP.ProblemInjector = chooseOperatorDialog.Operator;
94        problemInitializationTextBox.Text = OffspringSelectionGP.ProblemInjector.Name;
95      }
96    }
97
98    private void functionLibraryInjectorViewButton_Click(object sender, EventArgs e) {
99      IView view = OffspringSelectionGP.FunctionLibraryInjector.CreateView();
100      if (view != null)
101        ControlManager.Manager.ShowControl(view);
102    }
103
104    private void functionLibraryInjectorSetButton_Click(object sender, EventArgs e) {
105      if (chooseOperatorDialog == null) chooseOperatorDialog = new ChooseOperatorDialog();
106      if (chooseOperatorDialog.ShowDialog(this) == DialogResult.OK) {
107        OffspringSelectionGP.FunctionLibraryInjector = chooseOperatorDialog.Operator;
108        functionLibraryInjectorTextBox.Text = OffspringSelectionGP.FunctionLibraryInjector.Name;
109      }
110    }
111
112    private void executeButton_Click(object sender, EventArgs e) {
113      executeButton.Enabled = false;
114      abortButton.Enabled = true;
115      resetButton.Enabled = false;
116      OffspringSelectionGP.Engine.Execute();
117    }
118    private void abortButton_Click(object sender, EventArgs e) {
119      OffspringSelectionGP.Engine.Abort();
120    }
121    private void resetButton_Click(object sender, EventArgs e) {
122      OffspringSelectionGP.Engine.Reset();
123    }
124    private void cloneEngineButton_Click(object sender, EventArgs e) {
125      IEngine clone = (IEngine)OffspringSelectionGP.Engine.Clone();
126      IEditor editor = ((IEditable)clone).CreateEditor();
127      ControlManager.Manager.ShowControl(editor);
128    }
129    #endregion
130
131    #region Engine Events
132    private delegate void OnExceptionEventDelegate(object sender, EventArgs<Exception> e);
133    private void Engine_ExceptionOccurred(object sender, EventArgs<Exception> e) {
134      if (InvokeRequired)
135        Invoke(new OnExceptionEventDelegate(Engine_ExceptionOccurred), sender, e);
136      else
137        Auxiliary.ShowErrorMessageBox(e.Value);
138    }
139    private void Engine_Finished(object sender, EventArgs e) {
140      if (InvokeRequired)
141        Invoke((EventHandler)Engine_Finished, sender, e);
142      else {
143        scopeView.Refresh();
144        executeButton.Enabled = true;
145        abortButton.Enabled = false;
146        resetButton.Enabled = true;
147      }
148    }
149    #endregion
150
151    private void setRandomSeedRandomlyCheckBox_CheckedChanged(object sender, EventArgs e) {
152      randomSeedTextBox.Enabled = !setRandomSeedRandomlyCheckBox.Checked;
153      randomSeedLabel.Enabled = !setRandomSeedRandomlyCheckBox.Checked;
154    }
155
156    private void randomSeedTextBox_TextChanged(object sender, EventArgs e) {
157      int randomSeed;
158      if (int.TryParse(randomSeedTextBox.Text, out randomSeed)) {
159        //OffspringSelectionGP.RandomSeed = randomSeed;
160        errorProvider.SetError(randomSeedTextBox, string.Empty);
161      } else {
162        errorProvider.SetError(randomSeedTextBox, "Invalid value.");
163      }
164    }
165
166    private void populationSizeTextBox_TextChanged(object sender, EventArgs e) {
167      int popSize;
168      if (int.TryParse(populationSizeTextBox.Text, out popSize) && popSize > 1) {
169        //OffspringSelectionGP.PopulationSize = popSize;
170        errorProvider.SetError(populationSizeTextBox, string.Empty);
171      } else {
172        errorProvider.SetError(populationSizeTextBox, "Population size must be a positive integer > 0.");
173      }
174    }
175
176    private void elitesTextBox_TextChanged(object sender, EventArgs e) {
177      int elites;
178      if (int.TryParse(elitesTextBox.Text, out elites) && elites >= 0 && elites < OffspringSelectionGP.PopulationSize) {
179        //OffspringSelectionGP.Elites = elites;
180        errorProvider.SetError(elitesTextBox, string.Empty);
181      } else {
182        errorProvider.SetError(elitesTextBox, "Invalid value for number of elites. Allowed range [0.." + OffspringSelectionGP.PopulationSize + "[.");
183      }
184    }
185
186    private void mutationRateTextBox_TextChanged(object sender, EventArgs e) {
187      double mutationRate;
188      if (double.TryParse(mutationRateTextBox.Text, out mutationRate) && mutationRate >= 0.0 && mutationRate <= 1.0) {
189        //OffspringSelectionGP.MutationRate = mutationRate;
190        errorProvider.SetError(mutationRateTextBox, string.Empty);
191      } else {
192        errorProvider.SetError(mutationRateTextBox, "Invalid value for mutation rate. Allowed range = [0..1].");
193      }
194    }
195
196    private void comparisonFactorTextBox_TextChanged(object sender, EventArgs e) {
197      double comparisonFactor;
198      if (double.TryParse(comparisonFactorTextBox.Text, out comparisonFactor) && comparisonFactor >= 0.0 && comparisonFactor <= 1.0) {
199        //OffspringSelectionGP.ComparisonFactor = comparisonFactor;
200        errorProvider.SetError(comparisonFactorTextBox, string.Empty);
201      } else {
202        errorProvider.SetError(comparisonFactorTextBox, "Invalid value for comparison factor. Allowed range = [0..1].");
203      }
204    }
205
206    private void successRatioLimitTextBox_TextChanged(object sender, EventArgs e) {
207      double successRatioLimit;
208      if (double.TryParse(successRatioLimitTextBox.Text, out successRatioLimit) && successRatioLimit >= 0.0 && successRatioLimit <= 1.0) {
209        //OffspringSelectionGP.SuccessRatioLimit = successRatioLimit;
210        errorProvider.SetError(successRatioLimitTextBox, string.Empty);
211      } else {
212        errorProvider.SetError(successRatioLimitTextBox, "Invalid value for success ratio limit. Allowed range = [0..1].");
213      }
214    }
215
216    private void maximumEvaluatedSolutionsTextBox_TextChanged(object sender, EventArgs e) {
217      int maxEvaluatedSolutions;
218      if (int.TryParse(maximumEvaluatedSolutionsTextBox.Text, out maxEvaluatedSolutions) && maxEvaluatedSolutions > 0) {
219        //OffspringSelectionGP.MaxEvaluatedSolutions = maxEvaluatedSolutions;
220        errorProvider.SetError(maximumEvaluatedSolutionsTextBox, string.Empty);
221      } else {
222        errorProvider.SetError(maximumEvaluatedSolutionsTextBox, "Max. evaluated solutions must be a positive integer > 0.");
223      }
224    }
225
226    private void selectionPressureTextBox_TextChanged(object sender, EventArgs e) {
227      double maxSelPres;
228      if (double.TryParse(selectionPressureTextBox.Text, out maxSelPres) && maxSelPres > 1.0) {
229        //OffspringSelectionGP.SelectionPressureLimit = maxSelPres;
230        errorProvider.SetError(selectionPressureTextBox, string.Empty);
231      } else {
232        errorProvider.SetError(selectionPressureTextBox, "Selection pressure limit must be > 1.0.");
233      }
234    }
235  }
236}
Note: See TracBrowser for help on using the repository browser.