Changeset 10228 for branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/Mappers/DepthFirstMapper.cs
- Timestamp:
- 12/15/13 12:34:33 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/Mappers/DepthFirstMapper.cs
r10075 r10228 20 20 #endregion 21 21 22 using System; 23 using System.Collections.Generic; 22 24 using HeuristicLab.Common; 23 25 using HeuristicLab.Core; … … 63 65 tree.Root = rootNode; 64 66 65 int genotypeIndex = 0; 66 int currSubtreeCount = 1; 67 //int genotypeIndex = 0; 68 //int currSubtreeCount = 1; 69 //MapDepthFirstRecursively(startNode, genotype, 70 // grammar, genotype.Length, 71 // ref genotypeIndex, ref currSubtreeCount, 72 // new MersenneTwister()); 67 73 68 MapDepthFirstRecursively(startNode, genotype, 69 grammar, genotype.Length, 70 ref genotypeIndex, ref currSubtreeCount); 71 74 MapDepthFirstIteratively(startNode, genotype, grammar, 75 genotype.Length, new MersenneTwister()); 72 76 return tree; 73 77 } … … 85 89 /// <param name="currentNode">current parent node</param> 86 90 /// <param name="genotype">integer vector, which should be mapped to a tree</param> 87 /// <param name="grammar">grammar definitionto determine the allowed child symbols for currentNode </param>91 /// <param name="grammar">grammar to determine the allowed child symbols for currentNode </param> 88 92 /// <param name="maxSubtreeCount">maximum allowed subtrees (= number of used genomes)</param> 89 93 /// <param name="genotypeIndex">current index in integer vector</param> … … 94 98 int maxSubtreeCount, 95 99 ref int genotypeIndex, 96 ref int currSubtreeCount) { 100 ref int currSubtreeCount, 101 IRandom random) { 102 103 // TODO: check, if method calls of GetNewChildNode() and GetRandomTerminalNode() don't return null 97 104 if (currSubtreeCount < maxSubtreeCount) { 98 105 99 var newNode = GetNewChildNode(currentNode, genotype, grammar, genotypeIndex );106 var newNode = GetNewChildNode(currentNode, genotype, grammar, genotypeIndex, random); 100 107 101 108 if ((currSubtreeCount + newNode.Symbol.MinimumArity) > maxSubtreeCount) { 102 109 // TODO: maybe check, if there is any node, which fits in the tree yet 103 currentNode.AddSubtree(GetRandomTerminalNode(currentNode, grammar ));110 currentNode.AddSubtree(GetRandomTerminalNode(currentNode, grammar, random)); 104 111 } else { 105 112 currentNode.AddSubtree(newNode); … … 110 117 MapDepthFirstRecursively(newNode, genotype, 111 118 grammar, maxSubtreeCount, 112 ref genotypeIndex, ref currSubtreeCount );119 ref genotypeIndex, ref currSubtreeCount, random); 113 120 } 114 121 } … … 116 123 } else { 117 124 while (currentNode.Symbol.MinimumArity > currentNode.SubtreeCount) { 118 var newNode = GetNewChildNode(currentNode, genotype, grammar, genotypeIndex );125 var newNode = GetNewChildNode(currentNode, genotype, grammar, genotypeIndex, random); 119 126 currentNode.AddSubtree(newNode); 120 127 genotypeIndex++; 121 128 while (newNode.Symbol.MinimumArity > newNode.SubtreeCount) { 122 newNode.AddSubtree(GetRandomTerminalNode(newNode, grammar ));129 newNode.AddSubtree(GetRandomTerminalNode(newNode, grammar, random)); 123 130 } 124 131 } 125 132 } 126 133 } 134 135 136 /// <summary> 137 /// Genotype-to-Phenotype mapper (iterative depth-first approach). 138 /// </summary> 139 /// <param name="startNode">first node of the tree with arity 1</param> 140 /// <param name="genotype">integer vector, which should be mapped to a tree</param> 141 /// <param name="grammar">grammar to determine the allowed child symbols for each node</param> 142 /// <param name="maxSubtreeCount">maximum allowed subtrees (= number of used genomes)</param> 143 /// <param name="random">random number generator</param> 144 private void MapDepthFirstIteratively(ISymbolicExpressionTreeNode startNode, 145 IntegerVector genotype, 146 ISymbolicExpressionGrammar grammar, 147 int maxSubtreeCount, IRandom random) { 148 149 Stack<Tuple<ISymbolicExpressionTreeNode, int>> stack 150 = new Stack<Tuple<ISymbolicExpressionTreeNode, int>>(); // tuples of <node, arity> 151 152 int genotypeIndex = 0; 153 int currSubtreeCount = 1; 154 155 stack.Push(new Tuple<ISymbolicExpressionTreeNode, int>(startNode, 1)); 156 157 while ((currSubtreeCount < maxSubtreeCount) && (stack.Count > 0)) { 158 159 // get next node from stack and re-push it, if this node still has unhandled subtrees ... 160 Tuple<ISymbolicExpressionTreeNode, int> current = stack.Pop(); 161 if (current.Item2 > 1) { 162 stack.Push(new Tuple<ISymbolicExpressionTreeNode, int>(current.Item1, current.Item2 - 1)); 163 } 164 165 var newNode = GetNewChildNode(current.Item1, genotype, grammar, genotypeIndex, random); 166 int arity = SampleArity(random, newNode, maxSubtreeCount - currSubtreeCount); 167 168 if (arity < 0) { 169 current.Item1.AddSubtree(GetRandomTerminalNode(current.Item1, grammar, random)); 170 } else { 171 current.Item1.AddSubtree(newNode); 172 genotypeIndex++; 173 currSubtreeCount += arity; 174 if (arity > 0) { 175 // new node has subtrees so push it onto the stack 176 stack.Push(new Tuple<ISymbolicExpressionTreeNode, int>(newNode, arity)); 177 } 178 } 179 } 180 181 // maximum allowed subtree count was already reached, but there are still 182 // incomplete subtrees (non-terminal symbols) in the tree 183 // -> fill them with terminal symbols 184 while (stack.Count > 0) { 185 Tuple<ISymbolicExpressionTreeNode, int> current = stack.Pop(); 186 if (current.Item2 > 1) { 187 stack.Push(new Tuple<ISymbolicExpressionTreeNode, int>(current.Item1, current.Item2 - 1)); 188 } 189 current.Item1.AddSubtree(GetRandomTerminalNode(current.Item1, grammar, random)); 190 } 191 } 127 192 } 128 193 }
Note: See TracChangeset
for help on using the changeset viewer.