Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.GP/3.3/FunctionView.cs @ 5532

Last change on this file since 5532 was 2843, checked in by gkronber, 15 years ago

Removed max. and min. time offset constraints as algorithm parameters and from all engines. The time constraints were added to the relevant terminal symbols (variable & differential) instead. The time offset constraint can be changed by editing the symbols in the function library. #880 (Max and min time offsets for variable symbols are not set correctly by FunctionLibraryInjectors)

File size: 8.0 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.Core;
25using HeuristicLab.PluginInfrastructure;
26using HeuristicLab.GP.Interfaces;
27using System.Collections.Generic;
28using System.Linq;
29
30namespace HeuristicLab.GP {
31  public partial class FunctionView : ViewBase {
32
33    private const string ALL_SLOTS = "All";
34    private string selectedSlot = ALL_SLOTS;
35
36    public Function Function {
37      get {
38        return (Function)Item;
39      }
40      set {
41        Item = value;
42      }
43    }
44
45    public FunctionView()
46      : base() {
47      InitializeComponent();
48    }
49
50    public FunctionView(Function function)
51      : base() {
52      InitializeComponent();
53      Function = function;
54      UpdateControls();
55    }
56
57    protected override void UpdateControls() {
58      nameTextBox.Text = Function.Name;
59      minSubTreesTextBox.Text = Function.MinSubTrees.ToString();
60      maxSubTreesTextBox.Text = Function.MaxSubTrees.ToString();
61      ticketsTextBox.Text = Function.Tickets.ToString();
62      minTreeHeightTextBox.Text = Function.MinTreeHeight.ToString();
63      minTreeSizeTextBox.Text = Function.MinTreeSize.ToString();
64      if (Function.Initializer != null) {
65        initializerTextBox.Text = Function.Initializer.Name;
66      } else {
67        initializerTextBox.Enabled = false;
68        editInitializerButton.Enabled = false;
69      }
70      if (Function.Manipulator != null) {
71        manipulatorTextBox.Text = Function.Manipulator.Name;
72      } else {
73        manipulatorTextBox.Enabled = false;
74        editManipulatorButton.Enabled = false;
75      }
76
77      argumentComboBox.Items.Clear();
78      argumentComboBox.Items.Add(ALL_SLOTS);
79      for (int i = 0; i < Function.MaxSubTrees; i++) {
80        argumentComboBox.Items.Add(i.ToString());
81      }
82
83      UpdateAllowedSubFunctionsList();
84    }
85
86    private void UpdateAllowedSubFunctionsList() {
87      if (Function.MaxSubTrees > 0) {
88        subFunctionsListBox.Items.Clear();
89        if (selectedSlot == ALL_SLOTS) {
90          IEnumerable<IFunction> functionSet = Function.GetAllowedSubFunctions(0);
91          for (int i = 1; i < Function.MaxSubTrees; i++) {
92            functionSet = functionSet.Intersect(Function.GetAllowedSubFunctions(i));
93          }
94          foreach (var subFun in functionSet) {
95            subFunctionsListBox.Items.Add(subFun);
96          }
97        } else {
98          int slot = int.Parse(selectedSlot);
99          foreach (var subFun in Function.GetAllowedSubFunctions(slot)) {
100            subFunctionsListBox.Items.Add(subFun);
101          }
102        }
103      } else {
104        // no subfunctions allowed
105        subTreesGroupBox.Enabled = false;
106      }
107    }
108
109    private void nameTextBox_TextChanged(object sender, EventArgs e) {
110      string name = nameTextBox.Text;
111      if (!string.IsNullOrEmpty(name)) {
112        Function.Name = name;
113        functionPropertiesErrorProvider.SetError(nameTextBox, string.Empty);
114      } else {
115        functionPropertiesErrorProvider.SetError(nameTextBox, "Name can't be empty.");
116      }
117    }
118
119    private void minSubTreesTextBox_TextChanged(object sender, EventArgs e) {
120      int minSubTrees;
121      if (int.TryParse(minSubTreesTextBox.Text, out minSubTrees) && minSubTrees >= 0) {
122        Function.MinSubTrees = minSubTrees;
123        functionPropertiesErrorProvider.SetError(minSubTreesTextBox, string.Empty);
124      } else {
125        functionPropertiesErrorProvider.SetError(minSubTreesTextBox, "Min sub-trees must be 0 or a positive integer.");
126      }
127    }
128
129    private void maxSubTreesTextBox_TextChanged(object sender, EventArgs e) {
130      int maxSubTrees;
131      if (int.TryParse(maxSubTreesTextBox.Text, out maxSubTrees) && maxSubTrees >= 0) {
132        Function.MaxSubTrees = maxSubTrees;
133        functionPropertiesErrorProvider.SetError(maxSubTreesTextBox, string.Empty);
134      } else {
135        functionPropertiesErrorProvider.SetError(maxSubTreesTextBox, "Max sub-trees must be 0 or a positive integer and larger or equal min sub-trees.");
136      }
137    }
138
139    private void ticketsTextBox_TextChanged(object sender, EventArgs e) {
140      double tickets;
141      if (double.TryParse(ticketsTextBox.Text, out tickets) && tickets >= 0) {
142        Function.Tickets = tickets;
143        functionPropertiesErrorProvider.SetError(ticketsTextBox, string.Empty);
144      } else {
145        functionPropertiesErrorProvider.SetError(ticketsTextBox, "Number of tickets must be 0 or a positive real value.");
146      }
147    }
148
149    private void editInitializerButton_Click(object sender, EventArgs e) {
150      ControlManager.Manager.ShowControl(Function.Initializer.CreateView());
151    }
152
153    private void editManipulatorButton_Click(object sender, EventArgs e) {
154      ControlManager.Manager.ShowControl(Function.Manipulator.CreateView());
155    }
156
157    private void argumentComboBox_SelectedIndexChanged(object sender, EventArgs e) {
158      selectedSlot = argumentComboBox.Text;
159      UpdateAllowedSubFunctionsList();
160    }
161
162    private void subFunctionsListBox_DragEnter(object sender, DragEventArgs e) {
163      e.Effect = DragDropEffects.None;
164      if (e.Data.GetDataPresent("IFunction"))
165        e.Effect = DragDropEffects.Link;
166    }
167    private void subFunctionsListBox_DragOver(object sender, DragEventArgs e) {
168      e.Effect = DragDropEffects.None;
169      if (e.Data.GetDataPresent("IFunction"))
170        e.Effect = DragDropEffects.Link;
171    }
172
173    private void subFunctionsListBox_DragDrop(object sender, DragEventArgs e) {
174      if (e.Effect != DragDropEffects.None) {
175        if (e.Data.GetDataPresent("IFunction")) {
176          IFunction fun = (IFunction)e.Data.GetData("IFunction");
177          try {
178            Cursor = Cursors.WaitCursor;
179            if (selectedSlot == ALL_SLOTS) {
180              for (int slot = 0; slot < Function.MaxSubTrees; slot++)
181                Function.AddAllowedSubFunction(fun, slot);
182            } else {
183              int slot = int.Parse(selectedSlot);
184              Function.AddAllowedSubFunction(fun, slot);
185            }
186          }
187          finally {
188            Cursor = Cursors.Default;
189          }
190        }
191      }
192    }
193
194    private void subFunctionsListBox_KeyUp(object sender, KeyEventArgs e) {
195      try {
196        Cursor = Cursors.WaitCursor;
197        if (subFunctionsListBox.SelectedItems.Count > 0 && e.KeyCode == Keys.Delete) {
198          if (selectedSlot == ALL_SLOTS) {
199            List<IFunction> removedSubFunctions = new List<IFunction>(subFunctionsListBox.SelectedItems.Cast<IFunction>());
200            for (int slot = 0; slot < Function.MaxSubTrees; slot++) {
201              foreach (var subFun in removedSubFunctions) {
202                Function.RemoveAllowedSubFunction((IFunction)subFun, slot);
203              }
204            }
205          } else {
206            int slot = int.Parse(selectedSlot);
207            List<IFunction> removedSubFunctions = new List<IFunction>(subFunctionsListBox.SelectedItems.Cast<IFunction>());
208            foreach (var subFun in removedSubFunctions) {
209              Function.RemoveAllowedSubFunction(subFun, slot);
210            }
211          }
212
213        }
214      }
215      finally {
216        Cursor = Cursors.Default;
217      }
218    }
219  }
220}
Note: See TracBrowser for help on using the repository browser.