Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/01/10 16:40:24 (15 years ago)
Author:
gkronber
Message:

Fixed problems in persistence and cloning of functions and function libraries. Added test-functionality to editor for function libraries. Fixed bugs in editor for function libraries. #748 (FunctionLibraryView is empty)

File:
1 edited

Legend:

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

    r2728 r2729  
    161161      }
    162162    }
    163 
    164     private void tabControl_Selected(object sender, TabControlEventArgs e) {
    165       if (e.TabPage == testTabPage) {
    166         outputTextBox.Text = TestFunctionLibrary();
    167       }
    168     }
    169 
     163    #region fun lib test
    170164    private string TestFunctionLibrary() {
    171165      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();
     166      try {
     167        IFunctionTree[] randomTrees = CreateRandomTrees(n, 1, 100);
     168        StringBuilder builder = new StringBuilder();
     169        builder.AppendLine("Function symbol frequencies:");
     170        builder.AppendLine(CalculateFunctionFrequencies(randomTrees));
     171        builder.AppendLine("-----------------------------------------");
     172        builder.AppendLine("Terminal symbol frequencies:");
     173        builder.AppendLine(CalculateTerminalFrequencies(randomTrees));
     174        builder.AppendLine("-----------------------------------------");
     175        builder.AppendLine("Function arity frequencies:");
     176        builder.AppendLine(CalculateFunctionArityFrequencies(randomTrees));
     177        builder.AppendLine("-----------------------------------------");
     178        builder.AppendLine("Tree size frequencies:");
     179        builder.AppendLine(CalculateTreeSizeFrequencies(randomTrees));
     180        builder.AppendLine("-----------------------------------------");
     181        builder.AppendLine("Tree height frequencies:");
     182        builder.AppendLine(CalculateTreeHeightFrequencies(randomTrees));
     183        return builder.ToString();
     184      }
     185      catch (ArgumentException ex) {
     186        return "Could not create random trees:" + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace;
     187      }
    177188    }
    178189
     
    199210    }
    200211
     212    public string CalculateTreeSizeFrequencies(IFunctionTree[] randomTrees) {
     213      int[] histogram = new int[105 / 5];
     214      for (int i = 0; i < randomTrees.Length; i++) {
     215        histogram[randomTrees[i].GetSize() / 5]++;
     216      }
     217      StringBuilder strBuilder = new StringBuilder();
     218      for (int i = 0; i < histogram.Length; i++) {
     219        strBuilder.Append(Environment.NewLine);
     220        strBuilder.Append("< "); strBuilder.Append((i + 1) * 5);
     221        strBuilder.Append(": "); strBuilder.AppendFormat("{0:#0.00%}", histogram[i] / (double)randomTrees.Length);
     222      }
     223      return strBuilder.ToString();
     224    }
     225
     226    public string CalculateTreeHeightFrequencies(IFunctionTree[] randomTrees) {
     227      int[] histogram = new int[100];
     228      for (int i = 0; i < randomTrees.Length; i++) {
     229        histogram[randomTrees[i].GetHeight()]++;
     230      }
     231      StringBuilder strBuilder = new StringBuilder();
     232      for (int i = 0; i < histogram.Length; i++) {
     233        strBuilder.Append(Environment.NewLine);
     234        strBuilder.Append("< "); strBuilder.Append((i + 1));
     235        strBuilder.Append(": "); strBuilder.AppendFormat("{0:#0.00%}", histogram[i] / (double)randomTrees.Length);
     236      }
     237      return strBuilder.ToString();
     238    }
     239
     240    public string CalculateFunctionArityFrequencies(IFunctionTree[] randomTrees) {
     241      Dictionary<int, int> occurances = new Dictionary<int, int>();
     242      double n = 0.0;
     243      for (int i = 0; i < randomTrees.Length; i++) {
     244        foreach (var node in FunctionTreeIterator.IteratePrefix(randomTrees[i])) {
     245          if (!occurances.ContainsKey(node.SubTrees.Count))
     246            occurances[node.SubTrees.Count] = 0;
     247          occurances[node.SubTrees.Count]++;
     248          n++;
     249        }
     250      }
     251      StringBuilder strBuilder = new StringBuilder();
     252      foreach (var arity in occurances.Keys) {
     253        strBuilder.Append(Environment.NewLine);
     254        strBuilder.Append(arity); strBuilder.Append(": ");
     255        strBuilder.AppendFormat("{0:#0.00%}", occurances[arity] / n);
     256      }
     257      return strBuilder.ToString();
     258    }
     259
     260    public string CalculateTerminalFrequencies(IFunctionTree[] randomTrees) {
     261      Dictionary<IFunction, int> occurances = new Dictionary<IFunction, int>();
     262      double n = 0.0;
     263      for (int i = 0; i < randomTrees.Length; i++) {
     264        foreach (var node in FunctionTreeIterator.IteratePrefix(randomTrees[i])) {
     265          if (node.SubTrees.Count == 0) {
     266            if (!occurances.ContainsKey(node.Function))
     267              occurances[node.Function] = 0;
     268            occurances[node.Function]++;
     269            n++;
     270          }
     271        }
     272      }
     273      StringBuilder strBuilder = new StringBuilder();
     274      foreach (var function in occurances.Keys) {
     275        strBuilder.Append(Environment.NewLine);
     276        strBuilder.Append(function.Name); strBuilder.Append(": ");
     277        strBuilder.AppendFormat("{0:#0.00%}", occurances[function] / n);
     278      }
     279      return strBuilder.ToString();
     280    }
     281
    201282    private IFunctionTree[] CreateRandomTrees(int popSize, int minSize, int maxSize) {
    202283      int maxHeight = 10;
     
    206287      for (int i = 0; i < randomTrees.Length; i++) {
    207288        int treeSize = twister.Next(minSize, maxSize);
    208         IFunctionTree root;
     289        IFunctionTree root = null;
    209290        int tries = 0;
    210291        TreeGardener gardener = new TreeGardener(twister, FunctionLibrary);
    211292        do {
    212           root = gardener.PTC2(treeSize, maxSize);
     293          try {
     294            root = gardener.PTC2(treeSize, maxSize);
     295          }
     296          catch (ArgumentException) {
     297            // try a different size
     298            treeSize = twister.Next(minSize, maxSize);
     299            tries = 0;
     300          }
    213301          if (tries++ >= maxTries) {
    214302            // try a different size
     
    216304            tries = 0;
    217305          }
    218         } while (root.GetSize() > maxSize || root.GetHeight() > maxHeight);
     306        } while (root == null || root.GetSize() > maxSize || root.GetHeight() > maxHeight);
    219307        randomTrees[i] = root;
    220308      }
     
    222310    }
    223311
     312    private void testButton_Click(object sender, EventArgs e) {
     313      try {
     314        Cursor = Cursors.WaitCursor;
     315        outputTextBox.Text = TestFunctionLibrary();
     316      }
     317      finally {
     318        Cursor = Cursors.Default;
     319      }
     320    }
     321    #endregion
    224322  }
    225323}
Note: See TracChangeset for help on using the changeset viewer.