Changeset 10290 for branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/Mappers/DepthFirstMapper.cs
- Timestamp:
- 01/06/14 20:41:57 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/Mappers/DepthFirstMapper.cs
r10280 r10290 24 24 using HeuristicLab.Common; 25 25 using HeuristicLab.Core; 26 using HeuristicLab.Data; 26 27 using HeuristicLab.Encodings.IntegerVectorEncoding; 27 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; … … 54 55 /// <param name="genotype">integer vector, which should be mapped to a tree</param> 55 56 /// <returns>phenotype (a symbolic expression tree)</returns> 56 public override SymbolicExpressionTree Map(IRandom random, 57 public override SymbolicExpressionTree Map(IRandom random, IntMatrix bounds, int length, 57 58 ISymbolicExpressionGrammar grammar, 58 59 IntegerVector genotype) { … … 89 90 90 91 int genotypeIndex = 0; 91 int currSubtreeCount = 1;92 93 92 stack.Push(new Tuple<ISymbolicExpressionTreeNode, int>(startNode, 1)); 94 93 95 while ( (currSubtreeCount < maxSubtreeCount) && (stack.Count > 0)) {94 while (stack.Count > 0) { 96 95 97 96 // get next node from stack and re-push it, if this node still has unhandled subtrees ... … … 101 100 } 102 101 103 var newNode = GetNewChildNode(current.Item1, genotype, grammar, genotypeIndex, random); 104 int arity = SampleArity(random, newNode, maxSubtreeCount - currSubtreeCount, grammar); 105 106 if (arity < 0) { 102 if (genotypeIndex >= maxSubtreeCount) { 103 // if all genomes were used, only add terminal nodes to the remaining subtrees 107 104 current.Item1.AddSubtree(GetRandomTerminalNode(current.Item1, grammar, random)); 108 105 } else { 106 var newNode = GetNewChildNode(current.Item1, genotype, grammar, genotypeIndex, random); 107 int arity = SampleArity(random, newNode, grammar); 108 109 109 current.Item1.AddSubtree(newNode); 110 110 genotypeIndex++; 111 currSubtreeCount += arity;112 111 if (arity > 0) { 113 112 // new node has subtrees so push it onto the stack … … 116 115 } 117 116 } 118 119 // maximum allowed subtree count was already reached, but there are still120 // incomplete subtrees (non-terminal symbols) in the tree121 // -> fill them with terminal symbols122 while (stack.Count > 0) {123 Tuple<ISymbolicExpressionTreeNode, int> current = stack.Pop();124 if (current.Item2 > 1) {125 stack.Push(new Tuple<ISymbolicExpressionTreeNode, int>(current.Item1, current.Item2 - 1));126 }127 current.Item1.AddSubtree(GetRandomTerminalNode(current.Item1, grammar, random));128 }129 117 } 130 118 }
Note: See TracChangeset
for help on using the changeset viewer.