Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.Tests/SimpleFunctionLibraryTest.cs @ 2727

Last change on this file since 2727 was 2675, checked in by gkronber, 15 years ago

Implemented a fix in TreeGardener for #836 (Initial population doesn't contain all functions in combination with SimpleFunctionLibraryInjector). Added test cases for SimpleFunctionLibraryInjector. Changed code for calculation of minTreeHeight and minTreeSize in FunctionBase

File size: 5.5 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 HeuristicLab.GP.StructureIdentification;
23using Microsoft.VisualStudio.TestTools.UnitTesting;
24using HeuristicLab.DataAnalysis;
25using System;
26using HeuristicLab.GP.Interfaces;
27using HeuristicLab.Random;
28using HeuristicLab.GP.Operators;
29using System.Collections.Generic;
30using System.Text;
31using System.Diagnostics;
32namespace HeuristicLab.GP.Test {
33
34
35  [TestClass()]
36  public class SimpleFunctionLibraryTest {
37    private const int N = 1000;
38    private TestContext testContextInstance;
39    private static IFunctionTree[] randomTrees;
40
41    /// <summary>
42    ///Gets or sets the test context which provides
43    ///information about and functionality for the current test run.
44    ///</summary>
45    public TestContext TestContext {
46      get {
47        return testContextInstance;
48      }
49      set {
50        testContextInstance = value;
51      }
52    }
53
54    [ClassInitialize()]
55    public static void CreateRandomNetworks(TestContext testContext) {
56      MersenneTwister twister = new MersenneTwister();
57      Dataset ds = Util.CreateRandomDataset(twister, 1, 20);
58      randomTrees = Util.CreateRandomTrees(twister, ds, SimpleFunctionLibraryInjector.Create(true, -3, 0), N, 1, 100);
59    }
60
61    [TestMethod()]
62    public void SimpleFunctionLibrarySizeDistributionTest() {
63      int[] histogram = new int[105 / 5];
64      for (int i = 0; i < randomTrees.Length; i++) {
65        histogram[randomTrees[i].GetSize() / 5]++;
66      }
67      StringBuilder strBuilder = new StringBuilder();
68      for (int i = 0; i < histogram.Length; i++) {
69        strBuilder.Append(Environment.NewLine);
70        strBuilder.Append("< "); strBuilder.Append((i + 1) * 5);
71        strBuilder.Append(": "); strBuilder.AppendFormat("{0:#0.00%}", histogram[i] / (double)randomTrees.Length);
72      }
73      Assert.Inconclusive("Size distribution of ProbabilisticTreeCreator: " + strBuilder);
74    }
75
76    [TestMethod()]
77    public void SimpleFunctionLibraryFunctionDistributionTest() {
78      Dictionary<IFunction, int> occurances = new Dictionary<IFunction, int>();
79      double n = 0.0;
80      for (int i = 0; i < randomTrees.Length; i++) {
81        foreach (var node in FunctionTreeIterator.IteratePrefix(randomTrees[i])) {
82          if (node.SubTrees.Count > 0) {
83            if (!occurances.ContainsKey(node.Function))
84              occurances[node.Function] = 0;
85            occurances[node.Function]++;
86            n++;
87          }
88        }
89      }
90      StringBuilder strBuilder = new StringBuilder();
91      foreach (var function in occurances.Keys) {
92        strBuilder.Append(Environment.NewLine);
93        strBuilder.Append(function.Name); strBuilder.Append(": ");
94        strBuilder.AppendFormat("{0:#0.00%}", occurances[function] / n);
95      }
96      Assert.Inconclusive("Function distribution of ProbabilisticTreeCreator: " + strBuilder);
97    }
98
99    [TestMethod()]
100    public void SimpleFunctionLibraryNumberOfSubTreesDistributionTest() {
101      Dictionary<int, int> occurances = new Dictionary<int, int>();
102      double n = 0.0;
103      for (int i = 0; i < randomTrees.Length; i++) {
104        foreach (var node in FunctionTreeIterator.IteratePrefix(randomTrees[i])) {
105          if (!occurances.ContainsKey(node.SubTrees.Count))
106            occurances[node.SubTrees.Count] = 0;
107          occurances[node.SubTrees.Count]++;
108          n++;
109        }
110      }
111      StringBuilder strBuilder = new StringBuilder();
112      foreach (var arity in occurances.Keys) {
113        strBuilder.Append(Environment.NewLine);
114        strBuilder.Append(arity); strBuilder.Append(": ");
115        strBuilder.AppendFormat("{0:#0.00%}", occurances[arity] / n);
116      }
117      Assert.Inconclusive("Distribution of function arities of ProbabilisticTreeCreator: " + strBuilder);
118    }
119
120
121    [TestMethod()]
122    public void SimpleFunctionLibraryTerminalDistributionTest() {
123      Dictionary<IFunction, int> occurances = new Dictionary<IFunction, int>();
124      double n = 0.0;
125      for (int i = 0; i < randomTrees.Length; i++) {
126        foreach (var node in FunctionTreeIterator.IteratePrefix(randomTrees[i])) {
127          if (node.SubTrees.Count == 0) {
128            if (!occurances.ContainsKey(node.Function))
129              occurances[node.Function] = 0;
130            occurances[node.Function]++;
131            n++;
132          }
133        }
134      }
135      StringBuilder strBuilder = new StringBuilder();
136      foreach (var function in occurances.Keys) {
137        strBuilder.Append(Environment.NewLine);
138        strBuilder.Append(function.Name); strBuilder.Append(": ");
139        strBuilder.AppendFormat("{0:#0.00%}", occurances[function] / n);
140      }
141      Assert.Inconclusive("Terminal distribution of ProbabilisticTreeCreator: " + strBuilder);
142    }
143  }
144}
Note: See TracBrowser for help on using the repository browser.