Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/01/10 08:52:32 (14 years ago)
Author:
gkronber
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.GP/3.3/FunctionLibraryEditor.cs

    r2701 r2728  
    2626using HeuristicLab.Core;
    2727using HeuristicLab.GP.Interfaces;
     28using System.Text;
     29using HeuristicLab.Random;
    2830
    2931namespace HeuristicLab.GP {
     
    3537    }
    3638
    37     public FunctionLibraryEditor(FunctionLibrary library)
     39    public FunctionLibraryEditor()
    3840      : base() {
    3941      InitializeComponent();
     42    }
     43
     44    public FunctionLibraryEditor(FunctionLibrary library)
     45      : this() {
    4046      FunctionLibrary = library;
    4147    }
     
    4854    protected override void UpdateControls() {
    4955      base.UpdateControls();
     56      mutationListView.Items.Clear();
     57      initListView.Items.Clear();
    5058      functionsListView.Clear();
    5159      functionsComboBox.Items.Clear();
     
    92100      // delete from the end of the list
    93101      List<int> removeIndices = functionsListView.SelectedIndices.OfType<int>().OrderBy(x => 1.0 / x).ToList();
    94       foreach (int selectedIndex in removeIndices) {
    95         FunctionLibrary.RemoveFunction((IFunction)functionsListView.Items[selectedIndex].Tag);       
     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;
    96110      }
    97111    }
     
    131145      }
    132146    }
     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
    133224  }
    134225}
Note: See TracChangeset for help on using the changeset viewer.