Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP/3.3/FunctionLibraryEditor.cs @ 2728

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

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

File size: 8.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.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Core;
27using HeuristicLab.GP.Interfaces;
28using System.Text;
29using HeuristicLab.Random;
30
31namespace HeuristicLab.GP {
32  public partial class FunctionLibraryEditor : EditorBase {
33    private ChooseItemDialog chooseFunctionDialog;
34    public FunctionLibrary FunctionLibrary {
35      get { return (FunctionLibrary)Item; }
36      set { base.Item = value; }
37    }
38
39    public FunctionLibraryEditor()
40      : base() {
41      InitializeComponent();
42    }
43
44    public FunctionLibraryEditor(FunctionLibrary library)
45      : this() {
46      FunctionLibrary = library;
47    }
48
49    protected override void AddItemEvents() {
50      base.AddItemEvents();
51      base.Item.Changed += (sender, args) => UpdateControls();
52    }
53
54    protected override void UpdateControls() {
55      base.UpdateControls();
56      mutationListView.Items.Clear();
57      initListView.Items.Clear();
58      functionsListView.Clear();
59      functionsComboBox.Items.Clear();
60      foreach (IFunction fun in FunctionLibrary.Functions) {
61        functionsListView.Items.Add(CreateListViewItem(fun));
62        functionsComboBox.Items.Add(fun);
63        if (fun.Manipulator != null) {
64          mutationListView.Items.Add(CreateListViewItem(fun));
65        }
66        if (fun.Initializer != null) {
67          initListView.Items.Add(CreateListViewItem(fun));
68        }
69      }
70    }
71
72    private void mutationListView_SelectedIndexChanged(object sender, EventArgs e) {
73      if (mutationListView.SelectedItems.Count > 0 && mutationListView.SelectedItems[0].Tag != null) {
74        IOperator manipulator = ((IFunction)mutationListView.SelectedItems[0].Tag).Manipulator;
75        mutationVariableView.Enabled = true;
76        mutationVariableView.Variable = new Variable("Manipulator", manipulator);
77      } else {
78        mutationVariableView.Enabled = false;
79      }
80    }
81
82    private void initListView_SelectedIndexChanged(object sender, EventArgs e) {
83      if (initListView.SelectedItems.Count > 0 && initListView.SelectedItems[0].Tag != null) {
84        IOperator initializer = ((IFunction)initListView.SelectedItems[0].Tag).Initializer;
85        initVariableView.Enabled = true;
86        initVariableView.Variable = new Variable("Initializer", initializer);
87      } else {
88        initVariableView.Enabled = false;
89      }
90    }
91
92    private void addButton_Click(object sender, EventArgs e) {
93      if (chooseFunctionDialog == null) chooseFunctionDialog = new ChooseItemDialog(typeof(IFunction));
94      if (chooseFunctionDialog.ShowDialog(this) == DialogResult.OK) {
95        FunctionLibrary.AddFunction((IFunction)chooseFunctionDialog.Item);
96      }
97    }
98
99    private void removeButton_Click(object sender, EventArgs e) {
100      // delete from the end of the list
101      List<int> removeIndices = functionsListView.SelectedIndices.OfType<int>().OrderBy(x => 1.0 / x).ToList();
102      try {
103        Cursor = Cursors.WaitCursor;
104        foreach (int selectedIndex in removeIndices) {
105          FunctionLibrary.RemoveFunction((IFunction)functionsListView.Items[selectedIndex].Tag);
106        }
107      }
108      finally {
109        Cursor = Cursors.Default;
110      }
111    }
112
113    private void functionsListView_SelectedIndexChanged(object sender, EventArgs e) {
114      if (functionsListView.SelectedIndices.Count > 0) {
115        removeButton.Enabled = true;
116      } else {
117        removeButton.Enabled = false;
118      }
119    }
120
121    private ListViewItem CreateListViewItem(IFunction function) {
122      ListViewItem item = new ListViewItem();
123      item.Name = function.Name;
124      item.Text = function.Name;
125      item.Tag = function;
126      return item;
127    }
128
129    private void functionsListView_ItemDrag(object sender, ItemDragEventArgs e) {
130      ListViewItem item = (ListViewItem)e.Item;
131      IFunction fun = (IFunction)item.Tag;
132      DataObject data = new DataObject();
133      data.SetData("IFunction", fun);
134      data.SetData("DragSource", functionsListView);
135      DoDragDrop(data, DragDropEffects.Link);
136    }
137
138    private void functionsComboBox_SelectedIndexChanged(object sender, EventArgs e) {
139      if (functionsComboBox.SelectedItem != null) {
140        IFunction selectedFun = (IFunction)functionsComboBox.SelectedItem;
141        Control funView = (Control)selectedFun.CreateView();
142        funView.Dock = DockStyle.Fill;
143        functionDetailsPanel.Controls.Clear();
144        functionDetailsPanel.Controls.Add(funView);
145      }
146    }
147
148    private void functionsListView_KeyUp(object sender, KeyEventArgs e) {
149      if (e.KeyCode == Keys.Delete && functionsListView.SelectedItems.Count > 0) {
150        List<IFunction> removedFunctions = new List<IFunction>(from x in functionsListView.SelectedItems.OfType<ListViewItem>()
151                                                               select (IFunction)x.Tag);
152        try {
153          Cursor = Cursors.WaitCursor;
154          foreach (var fun in removedFunctions) {
155            FunctionLibrary.RemoveFunction(fun);
156          }
157        }
158        finally {
159          Cursor = Cursors.Default;
160        }
161      }
162    }
163
164    private void tabControl_Selected(object sender, TabControlEventArgs e) {
165      if (e.TabPage == testTabPage) {
166        outputTextBox.Text = TestFunctionLibrary();
167      }
168    }
169
170    private string TestFunctionLibrary() {
171      int n = 1000;
172      IFunctionTree[] randomTrees = CreateRandomTrees(n, 1, 100);
173
174      StringBuilder builder = new StringBuilder();
175      builder.AppendLine(CalculateFunctionFrequencies(randomTrees));
176      return builder.ToString();
177    }
178
179    private string CalculateFunctionFrequencies(IFunctionTree[] randomTrees) {
180      Dictionary<IFunction, int> occurances = new Dictionary<IFunction, int>();
181      double n = 0.0;
182      for (int i = 0; i < randomTrees.Length; i++) {
183        foreach (var node in FunctionTreeIterator.IteratePrefix(randomTrees[i])) {
184          if (node.SubTrees.Count > 0) {
185            if (!occurances.ContainsKey(node.Function))
186              occurances[node.Function] = 0;
187            occurances[node.Function]++;
188            n++;
189          }
190        }
191      }
192      StringBuilder strBuilder = new StringBuilder();
193      foreach (var function in occurances.Keys) {
194        strBuilder.Append(Environment.NewLine);
195        strBuilder.Append(function.Name); strBuilder.Append(": ");
196        strBuilder.AppendFormat("{0:#0.00%}", occurances[function] / n);
197      }
198      return strBuilder.ToString();
199    }
200
201    private IFunctionTree[] CreateRandomTrees(int popSize, int minSize, int maxSize) {
202      int maxHeight = 10;
203      int maxTries = 100;
204      IFunctionTree[] randomTrees = new IFunctionTree[popSize];
205      MersenneTwister twister = new MersenneTwister();
206      for (int i = 0; i < randomTrees.Length; i++) {
207        int treeSize = twister.Next(minSize, maxSize);
208        IFunctionTree root;
209        int tries = 0;
210        TreeGardener gardener = new TreeGardener(twister, FunctionLibrary);
211        do {
212          root = gardener.PTC2(treeSize, maxSize);
213          if (tries++ >= maxTries) {
214            // try a different size
215            treeSize = twister.Next(minSize, maxSize);
216            tries = 0;
217          }
218        } while (root.GetSize() > maxSize || root.GetHeight() > maxHeight);
219        randomTrees[i] = root;
220      }
221      return randomTrees;
222    }
223
224  }
225}
Note: See TracBrowser for help on using the repository browser.