Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.GP.Tests/ProbabilisticTreeCreatorTest.cs @ 16755

Last change on this file since 16755 was 2455, checked in by gkronber, 15 years ago

Added test for ProbabilisticTreeCreator. #791 (Tests for GP operators and structure identification evaluators)

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