Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.Tests/ProbabilisticTreeCreatorTest.cs @ 2447

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

Added until test project for HeuristicLab.GP plugins. #791 (Tests for GP operators and structure identification evaluators)

File size: 4.3 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.Append(histogram[i]);
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      for (int i = 0; i < randomTrees.Length; i++) {
79        foreach (var node in FunctionTreeIterator.IteratePrefix(randomTrees[i])) {
80          if (node.SubTrees.Count > 0) {
81            if (!occurances.ContainsKey(node.Function))
82              occurances[node.Function] = 0;
83            occurances[node.Function]++;
84          }
85        }
86      }
87      StringBuilder strBuilder = new StringBuilder();
88      foreach (var function in occurances.Keys) {
89        strBuilder.Append(Environment.NewLine);
90        strBuilder.Append(function.Name); strBuilder.Append(": ");
91        strBuilder.Append(occurances[function]);
92      }
93      Assert.Inconclusive("Function distribution of ProbabilisticTreeCreator: " + strBuilder);
94    }
95
96    [TestMethod()]
97    public void TerminalDistributionTest() {
98      Dictionary<IFunction, int> occurances = new Dictionary<IFunction, int>();
99      for (int i = 0; i < randomTrees.Length; i++) {
100        foreach (var node in FunctionTreeIterator.IteratePrefix(randomTrees[i])) {
101          if (node.SubTrees.Count == 0) {
102            if (!occurances.ContainsKey(node.Function))
103              occurances[node.Function] = 0;
104            occurances[node.Function]++;
105          }
106        }
107      }
108      StringBuilder strBuilder = new StringBuilder();
109      foreach (var function in occurances.Keys) {
110        strBuilder.Append(Environment.NewLine);
111        strBuilder.Append(function.Name); strBuilder.Append(": ");
112        strBuilder.Append(occurances[function]);
113      }
114      Assert.Inconclusive("Terminal distribution of ProbabilisticTreeCreator: " + strBuilder);
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.