Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added first version of architecture altering operators for ADFs. #290 (Implement ADFs)

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