- Timestamp:
- 12/15/13 15:56:42 (11 years ago)
- Location:
- branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/Mappers
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/Mappers/BreathFirstMapper.cs
r10068 r10229 20 20 #endregion 21 21 22 using System; 23 using System.Collections.Generic; 22 24 using HeuristicLab.Common; 23 25 using HeuristicLab.Core; … … 25 27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 26 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 29 using HeuristicLab.Random; 27 30 28 31 namespace HeuristicLab.Problems.GrammaticalEvolution { … … 30 33 /// BreathFirstMapper 31 34 /// </summary> 32 [Item("BreathFirstMapper", " ")]35 [Item("BreathFirstMapper", "Resolves the non-terminal symbols of the resulting phenotypic syntax tree in a breath-first manner.")] 33 36 [StorableClass] 34 37 public class BreathFirstMapper : GenotypeToPhenotypeMapper { … … 60 63 tree.Root = rootNode; 61 64 62 // TODO 65 MapBreathFirstIteratively(startNode, genotype, grammar, 66 genotype.Length, new MersenneTwister()); 63 67 64 68 return tree; 65 69 } 70 71 /// <summary> 72 /// Genotype-to-Phenotype mapper (iterative breath-first approach, by using a queue -> FIFO). 73 /// </summary> 74 /// <param name="startNode">first node of the tree with arity 1</param> 75 /// <param name="genotype">integer vector, which should be mapped to a tree</param> 76 /// <param name="grammar">grammar to determine the allowed child symbols for each node</param> 77 /// <param name="maxSubtreeCount">maximum allowed subtrees (= number of used genomes)</param> 78 /// <param name="random">random number generator</param> 79 private void MapBreathFirstIteratively(ISymbolicExpressionTreeNode startNode, 80 IntegerVector genotype, 81 ISymbolicExpressionGrammar grammar, 82 int maxSubtreeCount, IRandom random) { 83 84 Queue<Tuple<ISymbolicExpressionTreeNode, int>> queue 85 = new Queue<Tuple<ISymbolicExpressionTreeNode, int>>(); // tuples of <node, arity> 86 87 int genotypeIndex = 0; 88 int currSubtreeCount = 1; 89 90 queue.Enqueue(new Tuple<ISymbolicExpressionTreeNode, int>(startNode, 1)); 91 92 while ((currSubtreeCount < maxSubtreeCount) && (queue.Count > 0)) { 93 94 Tuple<ISymbolicExpressionTreeNode, int> current = queue.Dequeue(); 95 96 // foreach subtree of the current node, create a new node and enqueue it, if it is no terminal node 97 for (int i = 0; i < current.Item2; ++i) { 98 99 var newNode = GetNewChildNode(current.Item1, genotype, grammar, genotypeIndex, random); 100 int arity = SampleArity(random, newNode, maxSubtreeCount - currSubtreeCount); 101 102 if (arity < 0) { 103 current.Item1.AddSubtree(GetRandomTerminalNode(current.Item1, grammar, random)); 104 } else { 105 current.Item1.AddSubtree(newNode); 106 genotypeIndex++; 107 currSubtreeCount += arity; 108 if (arity > 0) { 109 // new node has subtrees so enqueue the node 110 queue.Enqueue(new Tuple<ISymbolicExpressionTreeNode, int>(newNode, arity)); 111 } 112 } 113 } 114 } 115 116 // maximum allowed subtree count was already reached, but there are still 117 // incomplete subtrees (non-terminal symbols) in the tree 118 // -> fill them with terminal symbols 119 while (queue.Count > 0) { 120 Tuple<ISymbolicExpressionTreeNode, int> current = queue.Dequeue(); 121 for (int i = 0; i < current.Item2; ++i) { 122 current.Item1.AddSubtree(GetRandomTerminalNode(current.Item1, grammar, random)); 123 } 124 } 125 } 66 126 } 67 127 } -
branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/Mappers/DepthFirstMapper.cs
r10228 r10229 33 33 /// DepthFirstMapper 34 34 /// </summary> 35 [Item("DepthFirstMapper", " ")]35 [Item("DepthFirstMapper", "Resolves the non-terminal symbols of the resulting phenotypic syntax tree in a depth-first manner.")] 36 36 [StorableClass] 37 37 public class DepthFirstMapper : GenotypeToPhenotypeMapper { … … 135 135 136 136 /// <summary> 137 /// Genotype-to-Phenotype mapper (iterative depth-first approach ).137 /// Genotype-to-Phenotype mapper (iterative depth-first approach, by using a stack -> LIFO). 138 138 /// </summary> 139 139 /// <param name="startNode">first node of the tree with arity 1</param>
Note: See TracChangeset
for help on using the changeset viewer.