[645] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Windows.Forms;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
[2210] | 27 | using HeuristicLab.GP.Interfaces;
|
---|
[2728] | 28 | using System.Text;
|
---|
| 29 | using HeuristicLab.Random;
|
---|
[645] | 30 |
|
---|
| 31 | namespace HeuristicLab.GP {
|
---|
[2202] | 32 | public partial class FunctionLibraryEditor : EditorBase {
|
---|
[2212] | 33 | private ChooseItemDialog chooseFunctionDialog;
|
---|
[2202] | 34 | public FunctionLibrary FunctionLibrary {
|
---|
| 35 | get { return (FunctionLibrary)Item; }
|
---|
[645] | 36 | set { base.Item = value; }
|
---|
| 37 | }
|
---|
| 38 |
|
---|
[2728] | 39 | public FunctionLibraryEditor()
|
---|
[645] | 40 | : base() {
|
---|
| 41 | InitializeComponent();
|
---|
[2728] | 42 | }
|
---|
| 43 |
|
---|
| 44 | public FunctionLibraryEditor(FunctionLibrary library)
|
---|
| 45 | : this() {
|
---|
[2202] | 46 | FunctionLibrary = library;
|
---|
[2212] | 47 | }
|
---|
[645] | 48 |
|
---|
[2212] | 49 | protected override void AddItemEvents() {
|
---|
| 50 | base.AddItemEvents();
|
---|
| 51 | base.Item.Changed += (sender, args) => UpdateControls();
|
---|
| 52 | }
|
---|
[645] | 53 |
|
---|
[2212] | 54 | protected override void UpdateControls() {
|
---|
| 55 | base.UpdateControls();
|
---|
[2728] | 56 | mutationListView.Items.Clear();
|
---|
| 57 | initListView.Items.Clear();
|
---|
[2701] | 58 | functionsListView.Clear();
|
---|
[2212] | 59 | foreach (IFunction fun in FunctionLibrary.Functions) {
|
---|
[2701] | 60 | functionsListView.Items.Add(CreateListViewItem(fun));
|
---|
[2202] | 61 | if (fun.Manipulator != null) {
|
---|
[2212] | 62 | mutationListView.Items.Add(CreateListViewItem(fun));
|
---|
[645] | 63 | }
|
---|
[2202] | 64 | if (fun.Initializer != null) {
|
---|
[2212] | 65 | initListView.Items.Add(CreateListViewItem(fun));
|
---|
[645] | 66 | }
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[2212] | 70 | private void mutationListView_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
| 71 | if (mutationListView.SelectedItems.Count > 0 && mutationListView.SelectedItems[0].Tag != null) {
|
---|
| 72 | IOperator manipulator = ((IFunction)mutationListView.SelectedItems[0].Tag).Manipulator;
|
---|
| 73 | mutationVariableView.Enabled = true;
|
---|
| 74 | mutationVariableView.Variable = new Variable("Manipulator", manipulator);
|
---|
| 75 | } else {
|
---|
| 76 | mutationVariableView.Enabled = false;
|
---|
| 77 | }
|
---|
[2202] | 78 | }
|
---|
[645] | 79 |
|
---|
[2212] | 80 | private void initListView_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
| 81 | if (initListView.SelectedItems.Count > 0 && initListView.SelectedItems[0].Tag != null) {
|
---|
| 82 | IOperator initializer = ((IFunction)initListView.SelectedItems[0].Tag).Initializer;
|
---|
| 83 | initVariableView.Enabled = true;
|
---|
| 84 | initVariableView.Variable = new Variable("Initializer", initializer);
|
---|
| 85 | } else {
|
---|
| 86 | initVariableView.Enabled = false;
|
---|
| 87 | }
|
---|
[2202] | 88 | }
|
---|
[645] | 89 |
|
---|
[2212] | 90 | private void addButton_Click(object sender, EventArgs e) {
|
---|
| 91 | if (chooseFunctionDialog == null) chooseFunctionDialog = new ChooseItemDialog(typeof(IFunction));
|
---|
| 92 | if (chooseFunctionDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
| 93 | FunctionLibrary.AddFunction((IFunction)chooseFunctionDialog.Item);
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
[645] | 96 |
|
---|
[2212] | 97 | private void removeButton_Click(object sender, EventArgs e) {
|
---|
| 98 | // delete from the end of the list
|
---|
[2701] | 99 | List<int> removeIndices = functionsListView.SelectedIndices.OfType<int>().OrderBy(x => 1.0 / x).ToList();
|
---|
[2728] | 100 | try {
|
---|
| 101 | Cursor = Cursors.WaitCursor;
|
---|
| 102 | foreach (int selectedIndex in removeIndices) {
|
---|
| 103 | FunctionLibrary.RemoveFunction((IFunction)functionsListView.Items[selectedIndex].Tag);
|
---|
| 104 | }
|
---|
[2212] | 105 | }
|
---|
[2728] | 106 | finally {
|
---|
| 107 | Cursor = Cursors.Default;
|
---|
| 108 | }
|
---|
[2212] | 109 | }
|
---|
[645] | 110 |
|
---|
[2212] | 111 | private void functionsListView_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
[2701] | 112 | if (functionsListView.SelectedIndices.Count > 0) {
|
---|
| 113 | removeButton.Enabled = true;
|
---|
| 114 | } else {
|
---|
| 115 | removeButton.Enabled = false;
|
---|
| 116 | }
|
---|
[645] | 117 | }
|
---|
| 118 |
|
---|
[2212] | 119 | private ListViewItem CreateListViewItem(IFunction function) {
|
---|
| 120 | ListViewItem item = new ListViewItem();
|
---|
| 121 | item.Name = function.Name;
|
---|
| 122 | item.Text = function.Name;
|
---|
| 123 | item.Tag = function;
|
---|
| 124 | return item;
|
---|
[645] | 125 | }
|
---|
[2701] | 126 |
|
---|
| 127 | private void functionsListView_ItemDrag(object sender, ItemDragEventArgs e) {
|
---|
| 128 | ListViewItem item = (ListViewItem)e.Item;
|
---|
| 129 | IFunction fun = (IFunction)item.Tag;
|
---|
| 130 | DataObject data = new DataObject();
|
---|
| 131 | data.SetData("IFunction", fun);
|
---|
| 132 | data.SetData("DragSource", functionsListView);
|
---|
| 133 | DoDragDrop(data, DragDropEffects.Link);
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[2728] | 136 | private void functionsListView_KeyUp(object sender, KeyEventArgs e) {
|
---|
| 137 | if (e.KeyCode == Keys.Delete && functionsListView.SelectedItems.Count > 0) {
|
---|
| 138 | List<IFunction> removedFunctions = new List<IFunction>(from x in functionsListView.SelectedItems.OfType<ListViewItem>()
|
---|
| 139 | select (IFunction)x.Tag);
|
---|
| 140 | try {
|
---|
| 141 | Cursor = Cursors.WaitCursor;
|
---|
| 142 | foreach (var fun in removedFunctions) {
|
---|
| 143 | FunctionLibrary.RemoveFunction(fun);
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 | finally {
|
---|
| 147 | Cursor = Cursors.Default;
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
[2729] | 151 | #region fun lib test
|
---|
| 152 | private string TestFunctionLibrary() {
|
---|
| 153 | int n = 1000;
|
---|
| 154 | try {
|
---|
| 155 | IFunctionTree[] randomTrees = CreateRandomTrees(n, 1, 100);
|
---|
| 156 | StringBuilder builder = new StringBuilder();
|
---|
| 157 | builder.AppendLine("Function symbol frequencies:");
|
---|
| 158 | builder.AppendLine(CalculateFunctionFrequencies(randomTrees));
|
---|
| 159 | builder.AppendLine("-----------------------------------------");
|
---|
| 160 | builder.AppendLine("Terminal symbol frequencies:");
|
---|
| 161 | builder.AppendLine(CalculateTerminalFrequencies(randomTrees));
|
---|
| 162 | builder.AppendLine("-----------------------------------------");
|
---|
| 163 | builder.AppendLine("Function arity frequencies:");
|
---|
| 164 | builder.AppendLine(CalculateFunctionArityFrequencies(randomTrees));
|
---|
| 165 | builder.AppendLine("-----------------------------------------");
|
---|
| 166 | builder.AppendLine("Tree size frequencies:");
|
---|
| 167 | builder.AppendLine(CalculateTreeSizeFrequencies(randomTrees));
|
---|
| 168 | builder.AppendLine("-----------------------------------------");
|
---|
| 169 | builder.AppendLine("Tree height frequencies:");
|
---|
| 170 | builder.AppendLine(CalculateTreeHeightFrequencies(randomTrees));
|
---|
| 171 | return builder.ToString();
|
---|
| 172 | }
|
---|
| 173 | catch (ArgumentException ex) {
|
---|
| 174 | return "Could not create random trees:" + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace;
|
---|
| 175 | }
|
---|
| 176 | }
|
---|
[2728] | 177 |
|
---|
[2729] | 178 | private string CalculateFunctionFrequencies(IFunctionTree[] randomTrees) {
|
---|
| 179 | Dictionary<IFunction, int> occurances = new Dictionary<IFunction, int>();
|
---|
| 180 | double n = 0.0;
|
---|
| 181 | for (int i = 0; i < randomTrees.Length; i++) {
|
---|
| 182 | foreach (var node in FunctionTreeIterator.IteratePrefix(randomTrees[i])) {
|
---|
| 183 | if (node.SubTrees.Count > 0) {
|
---|
| 184 | if (!occurances.ContainsKey(node.Function))
|
---|
| 185 | occurances[node.Function] = 0;
|
---|
| 186 | occurances[node.Function]++;
|
---|
| 187 | n++;
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
[2728] | 190 | }
|
---|
[2729] | 191 | StringBuilder strBuilder = new StringBuilder();
|
---|
| 192 | foreach (var function in occurances.Keys) {
|
---|
| 193 | strBuilder.Append(Environment.NewLine);
|
---|
| 194 | strBuilder.Append(function.Name); strBuilder.Append(": ");
|
---|
| 195 | strBuilder.AppendFormat("{0:#0.00%}", occurances[function] / n);
|
---|
| 196 | }
|
---|
| 197 | return strBuilder.ToString();
|
---|
[2728] | 198 | }
|
---|
| 199 |
|
---|
[2729] | 200 | public string CalculateTreeSizeFrequencies(IFunctionTree[] randomTrees) {
|
---|
| 201 | int[] histogram = new int[105 / 5];
|
---|
| 202 | for (int i = 0; i < randomTrees.Length; i++) {
|
---|
| 203 | histogram[randomTrees[i].GetSize() / 5]++;
|
---|
| 204 | }
|
---|
| 205 | StringBuilder strBuilder = new StringBuilder();
|
---|
| 206 | for (int i = 0; i < histogram.Length; i++) {
|
---|
| 207 | strBuilder.Append(Environment.NewLine);
|
---|
| 208 | strBuilder.Append("< "); strBuilder.Append((i + 1) * 5);
|
---|
| 209 | strBuilder.Append(": "); strBuilder.AppendFormat("{0:#0.00%}", histogram[i] / (double)randomTrees.Length);
|
---|
| 210 | }
|
---|
| 211 | return strBuilder.ToString();
|
---|
| 212 | }
|
---|
[2728] | 213 |
|
---|
[2729] | 214 | public string CalculateTreeHeightFrequencies(IFunctionTree[] randomTrees) {
|
---|
| 215 | int[] histogram = new int[100];
|
---|
| 216 | for (int i = 0; i < randomTrees.Length; i++) {
|
---|
| 217 | histogram[randomTrees[i].GetHeight()]++;
|
---|
| 218 | }
|
---|
| 219 | StringBuilder strBuilder = new StringBuilder();
|
---|
| 220 | for (int i = 0; i < histogram.Length; i++) {
|
---|
| 221 | strBuilder.Append(Environment.NewLine);
|
---|
| 222 | strBuilder.Append("< "); strBuilder.Append((i + 1));
|
---|
| 223 | strBuilder.Append(": "); strBuilder.AppendFormat("{0:#0.00%}", histogram[i] / (double)randomTrees.Length);
|
---|
| 224 | }
|
---|
| 225 | return strBuilder.ToString();
|
---|
[2728] | 226 | }
|
---|
| 227 |
|
---|
[2729] | 228 | public string CalculateFunctionArityFrequencies(IFunctionTree[] randomTrees) {
|
---|
| 229 | Dictionary<int, int> occurances = new Dictionary<int, int>();
|
---|
| 230 | double n = 0.0;
|
---|
| 231 | for (int i = 0; i < randomTrees.Length; i++) {
|
---|
| 232 | foreach (var node in FunctionTreeIterator.IteratePrefix(randomTrees[i])) {
|
---|
| 233 | if (!occurances.ContainsKey(node.SubTrees.Count))
|
---|
| 234 | occurances[node.SubTrees.Count] = 0;
|
---|
| 235 | occurances[node.SubTrees.Count]++;
|
---|
| 236 | n++;
|
---|
| 237 | }
|
---|
| 238 | }
|
---|
| 239 | StringBuilder strBuilder = new StringBuilder();
|
---|
| 240 | foreach (var arity in occurances.Keys) {
|
---|
| 241 | strBuilder.Append(Environment.NewLine);
|
---|
| 242 | strBuilder.Append(arity); strBuilder.Append(": ");
|
---|
| 243 | strBuilder.AppendFormat("{0:#0.00%}", occurances[arity] / n);
|
---|
| 244 | }
|
---|
| 245 | return strBuilder.ToString();
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | public string CalculateTerminalFrequencies(IFunctionTree[] randomTrees) {
|
---|
[2728] | 249 | Dictionary<IFunction, int> occurances = new Dictionary<IFunction, int>();
|
---|
| 250 | double n = 0.0;
|
---|
| 251 | for (int i = 0; i < randomTrees.Length; i++) {
|
---|
| 252 | foreach (var node in FunctionTreeIterator.IteratePrefix(randomTrees[i])) {
|
---|
[2729] | 253 | if (node.SubTrees.Count == 0) {
|
---|
[2728] | 254 | if (!occurances.ContainsKey(node.Function))
|
---|
| 255 | occurances[node.Function] = 0;
|
---|
| 256 | occurances[node.Function]++;
|
---|
| 257 | n++;
|
---|
| 258 | }
|
---|
| 259 | }
|
---|
| 260 | }
|
---|
| 261 | StringBuilder strBuilder = new StringBuilder();
|
---|
| 262 | foreach (var function in occurances.Keys) {
|
---|
| 263 | strBuilder.Append(Environment.NewLine);
|
---|
| 264 | strBuilder.Append(function.Name); strBuilder.Append(": ");
|
---|
| 265 | strBuilder.AppendFormat("{0:#0.00%}", occurances[function] / n);
|
---|
| 266 | }
|
---|
| 267 | return strBuilder.ToString();
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | private IFunctionTree[] CreateRandomTrees(int popSize, int minSize, int maxSize) {
|
---|
| 271 | int maxHeight = 10;
|
---|
| 272 | int maxTries = 100;
|
---|
| 273 | IFunctionTree[] randomTrees = new IFunctionTree[popSize];
|
---|
| 274 | MersenneTwister twister = new MersenneTwister();
|
---|
| 275 | for (int i = 0; i < randomTrees.Length; i++) {
|
---|
| 276 | int treeSize = twister.Next(minSize, maxSize);
|
---|
[2729] | 277 | IFunctionTree root = null;
|
---|
[2728] | 278 | int tries = 0;
|
---|
| 279 | TreeGardener gardener = new TreeGardener(twister, FunctionLibrary);
|
---|
| 280 | do {
|
---|
[2729] | 281 | try {
|
---|
| 282 | root = gardener.PTC2(treeSize, maxSize);
|
---|
| 283 | }
|
---|
| 284 | catch (ArgumentException) {
|
---|
| 285 | // try a different size
|
---|
| 286 | treeSize = twister.Next(minSize, maxSize);
|
---|
| 287 | tries = 0;
|
---|
| 288 | }
|
---|
[2728] | 289 | if (tries++ >= maxTries) {
|
---|
| 290 | // try a different size
|
---|
| 291 | treeSize = twister.Next(minSize, maxSize);
|
---|
| 292 | tries = 0;
|
---|
| 293 | }
|
---|
[2729] | 294 | } while (root == null || root.GetSize() > maxSize || root.GetHeight() > maxHeight);
|
---|
[2728] | 295 | randomTrees[i] = root;
|
---|
| 296 | }
|
---|
| 297 | return randomTrees;
|
---|
| 298 | }
|
---|
| 299 |
|
---|
[2729] | 300 | private void testButton_Click(object sender, EventArgs e) {
|
---|
| 301 | try {
|
---|
| 302 | Cursor = Cursors.WaitCursor;
|
---|
| 303 | outputTextBox.Text = TestFunctionLibrary();
|
---|
| 304 | }
|
---|
| 305 | finally {
|
---|
| 306 | Cursor = Cursors.Default;
|
---|
| 307 | }
|
---|
| 308 | }
|
---|
| 309 | #endregion
|
---|
[2788] | 310 |
|
---|
| 311 | private void functionsListView_MouseUp(object sender, MouseEventArgs e) {
|
---|
| 312 | if (functionsListView.SelectedItems.Count > 0) {
|
---|
| 313 | IFunction selectedFun = (IFunction)functionsListView.SelectedItems[0].Tag;
|
---|
| 314 | Control funView = (Control)selectedFun.CreateView();
|
---|
| 315 | funView.Dock = DockStyle.Fill;
|
---|
| 316 | functionDetailsPanel.Controls.Clear();
|
---|
| 317 | functionDetailsPanel.Controls.Add(funView);
|
---|
| 318 | }
|
---|
| 319 | }
|
---|
[645] | 320 | }
|
---|
| 321 | }
|
---|