Changeset 12347 for branches/SymbolicExpressionTreeEncoding
- Timestamp:
- 04/25/15 00:53:07 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/SymbolicExpressionTreeEncoding/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/GrowTreeCreator.cs
r12313 r12347 89 89 throw new ArgumentException("Cannot grow tree. Seed node shouldn't have arity zero."); 90 90 91 var allowedSymbols = seedNode.Grammar.AllowedSymbols 92 .Where(s => s.InitialFrequency > 0.0) 93 .ToList(); 91 var allowedSymbols = seedNode.Grammar.AllowedSymbols.Where(s => s.InitialFrequency > 0.0).ToList(); 94 92 95 93 for (var i = 0; i < arity; i++) { 96 var possibleSymbols = allowedSymbols 97 .Where(s => seedNode.Grammar.IsAllowedChildSymbol(seedNode.Symbol, s, i)) 98 .ToList(); 94 var possibleSymbols = allowedSymbols.Where(s => seedNode.Grammar.IsAllowedChildSymbol(seedNode.Symbol, s, i)).ToList(); 99 95 var weights = possibleSymbols.Select(s => s.InitialFrequency).ToList(); 100 96 var selectedSymbol = possibleSymbols.SelectRandom(weights, random); … … 113 109 private static void RecursiveCreate(IRandom random, ISymbolicExpressionTreeNode root, int currentDepth, int maxDepth) { 114 110 var arity = SampleArity(random, root); 115 if (arity <= 0)116 throw new ArgumentException("Cannot grow node of arity zero. Expected a function node.");111 if (arity == 0) 112 return; 117 113 118 114 var allowedSymbols = root.Grammar.AllowedSymbols.Where(s => s.InitialFrequency > 0.0).ToList(); 119 115 120 116 for (var i = 0; i < arity; i++) { 121 var possibleSymbols = allowedSymbols 122 .Where(s => root.Grammar.IsAllowedChildSymbol(root.Symbol, s, i) && 123 root.Grammar.GetMinimumExpressionDepth(s) - 1 <= maxDepth - currentDepth) 124 .ToList(); 117 var possibleSymbols = allowedSymbols.Where(s => root.Grammar.IsAllowedChildSymbol(root.Symbol, s, i) && 118 root.Grammar.GetMinimumExpressionDepth(s) - 1 <= maxDepth - currentDepth).ToList(); 125 119 126 120 if (!possibleSymbols.Any()) 127 121 throw new InvalidOperationException("No symbols are available for the tree."); 122 128 123 var weights = possibleSymbols.Select(s => s.InitialFrequency).ToList(); 129 124 var selectedSymbol = possibleSymbols.SelectRandom(weights, random); … … 133 128 } 134 129 135 foreach (var subTree in root.Subtrees) 136 if (subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) != 0) 137 RecursiveCreate(random, subTree, currentDepth + 1, maxDepth); 130 if (maxDepth > currentDepth) 131 foreach (var subTree in root.Subtrees) 132 if (subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) != 0) 133 RecursiveCreate(random, subTree, currentDepth + 1, maxDepth); 138 134 } 139 135
Note: See TracChangeset
for help on using the changeset viewer.