Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.Tests/NetworkFunctionLibraryTest.cs @ 2636

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

Initial commit of plugin for GP based network or equation modeling. #833

File size: 6.8 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 HeuristicLab.GP.StructureIdentification.Networks;
32using System.Diagnostics;
33namespace HeuristicLab.GP.Test {
34
35
36  [TestClass()]
37  public class NetworkFunctionLibraryTest {
38    private const int N = 1000;
39    private TestContext testContextInstance;
40    private static IFunctionTree[] randomTrees;
41
42    /// <summary>
43    ///Gets or sets the test context which provides
44    ///information about and functionality for the current test run.
45    ///</summary>
46    public TestContext TestContext {
47      get {
48        return testContextInstance;
49      }
50      set {
51        testContextInstance = value;
52      }
53    }
54
55    [ClassInitialize()]
56    public static void CreateRandomNetworks(TestContext testContext) {
57      MersenneTwister twister = new MersenneTwister();
58      Dataset ds = Util.CreateRandomDataset(twister, 1, 20);
59      randomTrees = Util.CreateRandomTrees(twister, ds, FunctionLibraryInjector.Create(true, -3, 0), N, 1, 100);
60    }
61
62    [TestMethod()]
63    public void NetworkFunctionLibrarySizeDistributionTest() {
64      int[] histogram = new int[105 / 5];
65      for (int i = 0; i < randomTrees.Length; i++) {
66        histogram[randomTrees[i].GetSize() / 5]++;
67      }
68      StringBuilder strBuilder = new StringBuilder();
69      for (int i = 0; i < histogram.Length; i++) {
70        strBuilder.Append(Environment.NewLine);
71        strBuilder.Append("< "); strBuilder.Append((i + 1) * 5);
72        strBuilder.Append(": "); strBuilder.AppendFormat("{0:#0.00%}", histogram[i] / (double)randomTrees.Length);
73      }
74      Assert.Inconclusive("Size distribution of ProbabilisticTreeCreator: " + strBuilder);
75    }
76
77    [TestMethod()]
78    public void NetworkFunctionLibraryFunctionDistributionTest() {
79      Dictionary<IFunction, int> occurances = new Dictionary<IFunction, int>();
80      double n = 0.0;
81      for (int i = 0; i < randomTrees.Length; i++) {
82        foreach (var node in FunctionTreeIterator.IteratePrefix(randomTrees[i])) {
83          if (node.SubTrees.Count > 0) {
84            if (!occurances.ContainsKey(node.Function))
85              occurances[node.Function] = 0;
86            occurances[node.Function]++;
87            n++;
88          }
89        }
90      }
91      StringBuilder strBuilder = new StringBuilder();
92      foreach (var function in occurances.Keys) {
93        strBuilder.Append(Environment.NewLine);
94        strBuilder.Append(function.Name); strBuilder.Append(": ");
95        strBuilder.AppendFormat("{0:#0.00%}", occurances[function] / n);
96      }
97      Assert.Inconclusive("Function distribution of ProbabilisticTreeCreator: " + strBuilder);
98    }
99
100    [TestMethod()]
101    public void NetworkFunctionLibraryNumberOfSubTreesDistributionTest() {
102      Dictionary<int, int> occurances = new Dictionary<int, int>();
103      double n = 0.0;
104      for (int i = 0; i < randomTrees.Length; i++) {
105        foreach (var node in FunctionTreeIterator.IteratePrefix(randomTrees[i])) {
106          if (!occurances.ContainsKey(node.SubTrees.Count))
107            occurances[node.SubTrees.Count] = 0;
108          occurances[node.SubTrees.Count]++;
109          n++;
110        }
111      }
112      StringBuilder strBuilder = new StringBuilder();
113      foreach (var arity in occurances.Keys) {
114        strBuilder.Append(Environment.NewLine);
115        strBuilder.Append(arity); strBuilder.Append(": ");
116        strBuilder.AppendFormat("{0:#0.00%}", occurances[arity] / n);
117      }
118      Assert.Inconclusive("Distribution of function arities of ProbabilisticTreeCreator: " + strBuilder);
119    }
120
121
122    [TestMethod()]
123    public void NetworkFunctionLibraryTerminalDistributionTest() {
124      Dictionary<IFunction, int> occurances = new Dictionary<IFunction, int>();
125      double n = 0.0;
126      for (int i = 0; i < randomTrees.Length; i++) {
127        foreach (var node in FunctionTreeIterator.IteratePrefix(randomTrees[i])) {
128          if (node.SubTrees.Count == 0) {
129            if (!occurances.ContainsKey(node.Function))
130              occurances[node.Function] = 0;
131            occurances[node.Function]++;
132            n++;
133          }
134        }
135      }
136      StringBuilder strBuilder = new StringBuilder();
137      foreach (var function in occurances.Keys) {
138        strBuilder.Append(Environment.NewLine);
139        strBuilder.Append(function.Name); strBuilder.Append(": ");
140        strBuilder.AppendFormat("{0:#0.00%}", occurances[function] / n);
141      }
142      Assert.Inconclusive("Terminal distribution of ProbabilisticTreeCreator: " + strBuilder);
143    }
144
145    [TestMethod()]
146    public void NetworkFunctionLibraryOpenParametersDistributionTest() {
147      Dictionary<int, int> occurances = new Dictionary<int, int>();
148
149      for (int i = 0; i < randomTrees.Length; i++) {
150        int nParameters = CountParameters(randomTrees[i]);
151
152        if (!occurances.ContainsKey(nParameters))
153          occurances[nParameters] = 1;
154        occurances[nParameters]++;
155      }
156      StringBuilder strBuilder = new StringBuilder();
157      foreach (var nParameters in occurances.Keys) {
158        strBuilder.Append(Environment.NewLine);
159        strBuilder.Append(nParameters); strBuilder.Append(": ");
160        strBuilder.AppendFormat("{0:#0.00%}", occurances[nParameters] / (double)randomTrees.Length);
161      }
162      Assert.Inconclusive("Number of parameters distribution of ProbabilisticTreeCreator: " + strBuilder);
163    }
164
165    private int CountParameters(IFunctionTree tree) {
166      if (tree.SubTrees.Count == 0) {
167        if (tree.Function is OpenParameter) return 1;
168        else return 0;
169      } else {
170        int n = 0;
171        foreach (var subTree in tree.SubTrees) {
172          n += CountParameters(subTree);
173        }
174        return n;
175      }
176    }
177  }
178}
Note: See TracBrowser for help on using the repository browser.