Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/ProbabilisticTreeCreaterTest.cs @ 3297

Last change on this file since 3297 was 3297, checked in by gkronber, 14 years ago

Added test classes for crossover and subroutine creater. #290 (Implement ADFs)

File size: 6.1 KB
Line 
1using System;
2using System.Text;
3using System.Collections.Generic;
4using System.Linq;
5using Microsoft.VisualStudio.TestTools.UnitTesting;
6using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
7using HeuristicLab.Random;
8using System.Diagnostics;
9
10namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._3.Tests {
11  [TestClass]
12  public class ProbabilisticTreeCreaterTest {
13    public ProbabilisticTreeCreaterTest() {
14      int populationSize = 1000;
15      randomTrees = new List<SymbolicExpressionTree>();
16      var grammar = new TestGrammar();
17      var random = new MersenneTwister();
18      for (int i = 0; i < populationSize; i++) {
19        randomTrees.Add(ProbabilisticTreeCreator.Create(random, grammar, 100, 10));
20      }
21      foreach (var tree in randomTrees)
22        Assert.IsTrue(grammar.IsValidExpression(tree));
23    }
24
25    private TestContext testContextInstance;
26
27    /// <summary>
28    ///Gets or sets the test context which provides
29    ///information about and functionality for the current test run.
30    ///</summary>
31    public TestContext TestContext {
32      get {
33        return testContextInstance;
34      }
35      set {
36        testContextInstance = value;
37      }
38    }
39
40    private List<SymbolicExpressionTree> randomTrees;
41
42
43    private class Addition : Symbol { }
44    private class Subtraction : Symbol { }
45    private class Multiplication : Symbol { }
46    private class Division : Symbol { }
47    private class Terminal : Symbol { }
48
49    private class TestGrammar : DefaultSymbolicExpressionGrammar {
50      public TestGrammar()
51        : base(0, 0, 0, 0) {
52        Initialize();
53      }
54
55      private void Initialize() {
56        var add = new Addition();
57        var sub = new Subtraction();
58        var mul = new Multiplication();
59        var div = new Division();
60        var terminal = new Terminal();
61
62        var allSymbols = new List<Symbol>() { add, sub, mul, div, terminal };
63        var functionSymbols = new List<Symbol>() { add, sub, mul, div };
64        allSymbols.ForEach(s => AddAllowedSymbols(StartSymbol, 0, s));
65
66        SetMinSubTreeCount(terminal, 0);
67        SetMaxSubTreeCount(terminal, 0);
68        int maxSubTrees = 3;
69        foreach (var functionSymbol in functionSymbols) {
70          SetMinSubTreeCount(functionSymbol, 1);
71          SetMaxSubTreeCount(functionSymbol, maxSubTrees);
72          foreach (var childSymbol in allSymbols) {
73            for (int argumentIndex = 0; argumentIndex < maxSubTrees; argumentIndex++) {
74              AddAllowedSymbols(functionSymbol, argumentIndex, childSymbol);
75            }
76          }
77        }
78      }
79    }
80
81    [TestMethod()]
82    public void ProbabilisticTreeCreaterSizeDistributionTest() {
83      int[] histogram = new int[105 / 5];
84      for (int i = 0; i < randomTrees.Count; i++) {
85        histogram[randomTrees[i].Size / 5]++;
86      }
87      StringBuilder strBuilder = new StringBuilder();
88      for (int i = 0; i < histogram.Length; i++) {
89        strBuilder.Append(Environment.NewLine);
90        strBuilder.Append("< "); strBuilder.Append((i + 1) * 5);
91        strBuilder.Append(": "); strBuilder.AppendFormat("{0:#0.00%}", histogram[i] / (double)randomTrees.Count);
92      }
93      Assert.Inconclusive("Size distribution of ProbabilisticTreeCreator: " + strBuilder);
94    }
95
96    [TestMethod()]
97    public void ProbabilisticTreeCreaterFunctionDistributionTest() {
98      Dictionary<Symbol, int> occurances = new Dictionary<Symbol, int>();
99      double n = 0.0;
100      for (int i = 0; i < randomTrees.Count; i++) {
101        foreach (var node in randomTrees[i].IterateNodesPrefix()) {
102          if (node.SubTrees.Count > 0) {
103            if (!occurances.ContainsKey(node.Symbol))
104              occurances[node.Symbol] = 0;
105            occurances[node.Symbol]++;
106            n++;
107          }
108        }
109      }
110      StringBuilder strBuilder = new StringBuilder();
111      foreach (var function in occurances.Keys) {
112        strBuilder.Append(Environment.NewLine);
113        strBuilder.Append(function.Name); strBuilder.Append(": ");
114        strBuilder.AppendFormat("{0:#0.00%}", occurances[function] / n);
115      }
116      Assert.Inconclusive("Function distribution of ProbabilisticTreeCreator: " + strBuilder);
117    }
118
119    [TestMethod()]
120    public void ProbabilisticTreeCreaterNumberOfSubTreesDistributionTest() {
121      Dictionary<int, int> occurances = new Dictionary<int, int>();
122      double n = 0.0;
123      for (int i = 0; i < randomTrees.Count; i++) {
124        foreach (var node in randomTrees[i].IterateNodesPrefix()) {
125          if (!occurances.ContainsKey(node.SubTrees.Count))
126            occurances[node.SubTrees.Count] = 0;
127          occurances[node.SubTrees.Count]++;
128          n++;
129        }
130      }
131      StringBuilder strBuilder = new StringBuilder();
132      foreach (var arity in occurances.Keys) {
133        strBuilder.Append(Environment.NewLine);
134        strBuilder.Append(arity); strBuilder.Append(": ");
135        strBuilder.AppendFormat("{0:#0.00%}", occurances[arity] / n);
136      }
137      Assert.Inconclusive("Distribution of function arities of ProbabilisticTreeCreator: " + strBuilder);
138    }
139
140
141    [TestMethod()]
142    public void ProbabilisticTreeCreaterTerminalDistributionTest() {
143      Dictionary<Symbol, int> occurances = new Dictionary<Symbol, int>();
144      double n = 0.0;
145      for (int i = 0; i < randomTrees.Count; i++) {
146        foreach (var node in randomTrees[i].IterateNodesPrefix()) {
147          if (node.SubTrees.Count == 0) {
148            if (!occurances.ContainsKey(node.Symbol))
149              occurances[node.Symbol] = 0;
150            occurances[node.Symbol]++;
151            n++;
152          }
153        }
154      }
155      StringBuilder strBuilder = new StringBuilder();
156      foreach (var function in occurances.Keys) {
157        strBuilder.Append(Environment.NewLine);
158        strBuilder.Append(function.Name); strBuilder.Append(": ");
159        strBuilder.AppendFormat("{0:#0.00%}", occurances[function] / n);
160      }
161      Assert.Inconclusive("Terminal distribution of ProbabilisticTreeCreator: " + strBuilder);
162    }
163  }
164}
Note: See TracBrowser for help on using the repository browser.