Changeset 3338 for trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/ProbabilisticTreeCreaterTest.cs
- Timestamp:
- 04/13/10 20:44:31 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/ProbabilisticTreeCreaterTest.cs
r3297 r3338 11 11 [TestClass] 12 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 13 private const int POPULATION_SIZE = 1000; 14 private const int MAX_TREE_SIZE = 100; 15 private const int MAX_TREE_HEIGHT = 10; 25 16 private TestContext testContextInstance; 26 17 … … 38 29 } 39 30 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(); 31 [TestMethod()] 32 public void ProbabilisticTreeCreaterDistributionsTest() { 33 var randomTrees = new List<SymbolicExpressionTree>(); 34 var grammar = Grammars.CreateSimpleArithmeticGrammar(); 35 var random = new MersenneTwister(); 36 for (int i = 0; i < POPULATION_SIZE; i++) { 37 randomTrees.Add(ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 0, 0)); 53 38 } 54 39 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); 40 foreach (var tree in randomTrees) 41 Assert.IsTrue(tree.IsValidExpression()); 42 Assert.Inconclusive("ProbabilisticTreeCreator: " + Environment.NewLine + 43 Util.GetSizeDistributionString(randomTrees, 105, 5) + Environment.NewLine + 44 Util.GetFunctionDistributionString(randomTrees) + Environment.NewLine + 45 Util.GetNumberOfSubTreesDistributionString(randomTrees) + Environment.NewLine + 46 Util.GetTerminalDistributionString(randomTrees) + Environment.NewLine 47 ); 138 48 } 139 49 140 50 141 51 [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 } 52 public void ProbabilisticTreeCreaterWithAdfDistributionsTest() { 53 var randomTrees = new List<SymbolicExpressionTree>(); 54 var grammar = Grammars.CreateArithmeticAndAdfGrammar(); 55 var random = new MersenneTwister(); 56 for (int i = 0; i < POPULATION_SIZE; i++) { 57 randomTrees.Add(ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3)); 154 58 } 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); 59 foreach (var tree in randomTrees) 60 Assert.IsTrue(tree.IsValidExpression()); 61 Assert.Inconclusive("ProbabilisticTreeCreator: " + Environment.NewLine + 62 Util.GetSizeDistributionString(randomTrees, 105, 5) + Environment.NewLine + 63 Util.GetFunctionDistributionString(randomTrees) + Environment.NewLine + 64 Util.GetNumberOfSubTreesDistributionString(randomTrees) + Environment.NewLine + 65 Util.GetTerminalDistributionString(randomTrees) + Environment.NewLine 66 ); 162 67 } 163 68 }
Note: See TracChangeset
for help on using the changeset viewer.