Changeset 5549 for branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/ProbabilisticTreeCreator.cs
- Timestamp:
- 02/22/11 19:04:54 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/ProbabilisticTreeCreator.cs
r5529 r5549 32 32 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { 33 33 [StorableClass] 34 [Item("ProbabilisticTreeCreator", "An operator that creates new symbolic expression trees with uniformly distributed size")]34 [Item("ProbabilisticTreeCreator", "An operator that creates new symbolic expression trees with uniformly distributed length")] 35 35 public sealed class ProbabilisticTreeCreator : SymbolicExpressionTreeCreator, 36 36 ISymbolicExpressionTreeSizeConstraintOperator, ISymbolicExpressionTreeGrammarBasedOperator, ISymbolicExpressionTreeArchitectureAlteringOperator { … … 98 98 99 99 public static ISymbolicExpressionTree Create(IRandom random, ISymbolicExpressionTreeGrammar grammar, 100 int maxTree Size, int maxTreeHeight,100 int maxTreeLength, int maxTreeDepth, 101 101 int maxFunctionDefinitions, int maxFunctionArguments 102 102 ) { … … 105 105 if (rootNode.HasLocalParameters) rootNode.ResetLocalParameters(random); 106 106 rootNode.SetGrammar(new SymbolicExpressionTreeGrammar(grammar)); 107 tree.Root = PTC2(random, rootNode, maxTree Size, maxTreeHeight, maxFunctionDefinitions, maxFunctionArguments);107 tree.Root = PTC2(random, rootNode, maxTreeLength, maxTreeDepth, maxFunctionDefinitions, maxFunctionArguments); 108 108 return tree; 109 109 } … … 116 116 117 117 public static ISymbolicExpressionTreeNode PTC2(IRandom random, ISymbolicExpressionTreeNode seedNode, 118 int max TreeSize, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) {119 // tree sizeis limited by the grammar and by the explicit size constraints120 int allowedMin Size= seedNode.Grammar.GetMinExpressionLength(seedNode.Symbol);121 int allowedMax Size = Math.Min(maxTreeSize, seedNode.Grammar.GetMaxExpressionLength(seedNode.Symbol));118 int maxLength, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) { 119 // tree length is limited by the grammar and by the explicit size constraints 120 int allowedMinLength = seedNode.Grammar.GetMinExpressionLength(seedNode.Symbol); 121 int allowedMaxLength = Math.Min(maxLength, seedNode.Grammar.GetMaxExpressionLength(seedNode.Symbol)); 122 122 int tries = 0; 123 123 while (tries++ < MAX_TRIES) { 124 // select a target tree sizeuniformly in the possible range (as determined by explicit limits and limits of the grammar)125 int t reeSize;126 t reeSize = random.Next(allowedMinSize, allowedMaxSize+ 1);127 if (t reeSize<= 1 || maxDepth <= 1) return seedNode;128 129 bool success = CreateFullTreeFromSeed(random, seedNode, seedNode.Grammar, t reeSize, maxDepth, maxFunctionDefinitions, maxFunctionArguments);124 // select a target tree length uniformly in the possible range (as determined by explicit limits and limits of the grammar) 125 int targetTreeLength; 126 targetTreeLength = random.Next(allowedMinLength, allowedMaxLength + 1); 127 if (targetTreeLength <= 1 || maxDepth <= 1) return seedNode; 128 129 bool success = CreateFullTreeFromSeed(random, seedNode, seedNode.Grammar, targetTreeLength, maxDepth, maxFunctionDefinitions, maxFunctionArguments); 130 130 131 131 // if successful => check constraints and return the tree if everything looks ok 132 if (success && seedNode.Get Size() <= maxTreeSize && seedNode.GetHeight() <= maxDepth) {132 if (success && seedNode.GetLength() <= maxLength && seedNode.GetDepth() <= maxDepth) { 133 133 return seedNode; 134 134 } else { … … 136 136 while (seedNode.SubTrees.Count() > 0) seedNode.RemoveSubTree(0); 137 137 } 138 // try a different sizeMAX_TRIES times138 // try a different length MAX_TRIES times 139 139 } 140 140 throw new ArgumentException("Couldn't create a random valid tree."); … … 142 142 143 143 private static bool CreateFullTreeFromSeed(IRandom random, ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeGrammar globalGrammar, 144 int size, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) {144 int targetLength, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) { 145 145 try { 146 TryCreateFullTreeFromSeed(random, root, globalGrammar, size, maxDepth, maxFunctionDefinitions, maxFunctionArguments);146 TryCreateFullTreeFromSeed(random, root, globalGrammar, targetLength, maxDepth, maxFunctionDefinitions, maxFunctionArguments); 147 147 return true; 148 148 } … … 151 151 152 152 private static void TryCreateFullTreeFromSeed(IRandom random, ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeGrammar globalGrammar, 153 int size, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) {153 int targetLength, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) { 154 154 List<TreeExtensionPoint> extensionPoints = new List<TreeExtensionPoint>(); 155 int current Size= 1;156 int totalListMin Size= globalGrammar.GetMinExpressionLength(root.Symbol) - 1;157 int actualArity = SampleArity(random, root, size);155 int currentLength = 1; 156 int totalListMinLength = globalGrammar.GetMinExpressionLength(root.Symbol) - 1; 157 int actualArity = SampleArity(random, root, targetLength); 158 158 for (int i = 0; i < actualArity; i++) { 159 159 // insert a dummy sub-tree and add the pending extension to the list … … 163 163 } 164 164 // while there are pending extension points and we have not reached the limit of adding new extension points 165 while (extensionPoints.Count > 0 && totalListMin Size + currentSize < size) {165 while (extensionPoints.Count > 0 && totalListMinLength + currentLength < targetLength) { 166 166 int randomIndex = random.Next(extensionPoints.Count); 167 167 TreeExtensionPoint nextExtension = extensionPoints[randomIndex]; … … 176 176 where parent.Grammar.IsAllowedChild(parent.Symbol, s, argumentIndex) 177 177 where parent.Grammar.GetMinExpressionDepth(s) + extensionDepth - 1 < maxDepth 178 where parent.Grammar.GetMaxExpressionLength(s) > size - totalListMinSize - currentSize178 where parent.Grammar.GetMaxExpressionLength(s) > targetLength - totalListMinLength - currentLength 179 179 select s) 180 180 .ToList(); … … 188 188 InitializeNewTreeNode(random, root, newTree, maxFunctionDefinitions, maxFunctionArguments); 189 189 190 current Size++;191 totalListMin Size--;192 193 actualArity = SampleArity(random, newTree, size - currentSize);190 currentLength++; 191 totalListMinLength--; 192 193 actualArity = SampleArity(random, newTree, targetLength - currentLength); 194 194 for (int i = 0; i < actualArity; i++) { 195 195 // insert a dummy sub-tree and add the pending extension to the list … … 198 198 extensionPoints.Add(new TreeExtensionPoint { Parent = newTree, ChildIndex = i, ExtensionPointDepth = extensionDepth + 1 }); 199 199 } 200 totalListMin Size+= newTree.Grammar.GetMinExpressionLength(newTree.Symbol);200 totalListMinLength += newTree.Grammar.GetMinExpressionLength(newTree.Symbol); 201 201 } 202 202 } … … 283 283 } 284 284 285 private static int SampleArity(IRandom random, ISymbolicExpressionTreeNode node, int target Size) {285 private static int SampleArity(IRandom random, ISymbolicExpressionTreeNode node, int targetLength) { 286 286 // select actualArity randomly with the constraint that the sub-trees in the minimal arity can become large enough 287 287 int minArity = node.Grammar.GetMinSubtreeCount(node.Symbol); 288 288 int maxArity = node.Grammar.GetMaxSubtreeCount(node.Symbol); 289 if (maxArity > target Size) {290 maxArity = target Size;291 } 292 // the min number of sub-trees has to be set to a value that is large enough so that the largest possible tree is at least tree size293 // if 1..3 trees are possible and the largest possible first sub-tree is smaller larger than the target sizethen minArity should be at least 2289 if (maxArity > targetLength) { 290 maxArity = targetLength; 291 } 292 // the min number of sub-trees has to be set to a value that is large enough so that the largest possible tree is at least tree length 293 // if 1..3 trees are possible and the largest possible first sub-tree is smaller larger than the target length then minArity should be at least 2 294 294 long aggregatedLongestExpressionLength = 0; 295 295 for (int i = 0; i < maxArity; i++) { 296 296 aggregatedLongestExpressionLength += (from s in node.Grammar.GetAllowedSymbols(node.Symbol, i) 297 297 select node.Grammar.GetMaxExpressionLength(s)).Max(); 298 if (aggregatedLongestExpressionLength < target Size) minArity = i;298 if (aggregatedLongestExpressionLength < targetLength) minArity = i; 299 299 else break; 300 300 } 301 301 302 // the max number of sub-trees has to be set to a value that is small enough so that the smallest possible tree is at most tree size303 // if 1..3 trees are possible and the smallest possible first sub-tree is already larger than the target sizethen maxArity should be at most 0302 // the max number of sub-trees has to be set to a value that is small enough so that the smallest possible tree is at most tree length 303 // if 1..3 trees are possible and the smallest possible first sub-tree is already larger than the target length then maxArity should be at most 0 304 304 long aggregatedShortestExpressionLength = 0; 305 305 for (int i = 0; i < maxArity; i++) { 306 306 aggregatedShortestExpressionLength += (from s in node.Grammar.GetAllowedSymbols(node.Symbol, i) 307 307 select node.Grammar.GetMinExpressionLength(s)).Min(); 308 if (aggregatedShortestExpressionLength > target Size) {308 if (aggregatedShortestExpressionLength > targetLength) { 309 309 maxArity = i; 310 310 break;
Note: See TracChangeset
for help on using the changeset viewer.