- Timestamp:
- 08/05/08 15:23:43 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.StructureIdentification/TreeGardener.cs
r444 r448 82 82 internal IFunctionTree CreateBalancedRandomTree(int maxTreeSize, int maxTreeHeight) { 83 83 IFunction rootFunction = GetRandomRoot(maxTreeSize, maxTreeHeight); 84 IFunctionTree tree = MakeBalancedTree(rootFunction, maxTree Size - 1, maxTreeHeight - 1);84 IFunctionTree tree = MakeBalancedTree(rootFunction, maxTreeHeight - 1); 85 85 return tree; 86 86 } … … 95 95 internal IFunctionTree CreateUnbalancedRandomTree(int maxTreeSize, int maxTreeHeight) { 96 96 IFunction rootFunction = GetRandomRoot(maxTreeSize, maxTreeHeight); 97 IFunctionTree tree = MakeUnbalancedTree(rootFunction, maxTree Size - 1, maxTreeHeight - 1);97 IFunctionTree tree = MakeUnbalancedTree(rootFunction, maxTreeHeight - 1); 98 98 return tree; 99 99 } 100 100 101 101 internal IFunctionTree PTC2(IRandom random, int size, int maxDepth) { 102 if(size <= 1 || maxDepth <=1) return RandomSelect(terminals).GetTreeNode();102 if(size <= 1 || maxDepth <= 1) return RandomSelect(terminals).GetTreeNode(); 103 103 List<object[]> list = new List<object[]>(); 104 104 IFunctionTree root = GetRandomRoot(size, maxDepth).GetTreeNode(); … … 132 132 parent.InsertSubTree(a, branch); // insert a smallest possible tree 133 133 currentSize += branch.Size; 134 totalListMinSize -=branch.Size;134 totalListMinSize -= branch.Size; 135 135 } else { 136 136 IFunction selectedFunction = RandomSelect(GetAllowedSubFunctions(parent.Function, a).Where( … … 192 192 internal IFunctionTree CreateRandomTree(ICollection<IFunction> allowedFunctions, int maxTreeSize, int maxTreeHeight, bool balanceTrees) { 193 193 // get the minimal needed height based on allowed functions and extend the max-height if necessary 194 int minTreeHeight = allowedFunctions.Select(f => GetMinimalTree Size(f)).Min();194 int minTreeHeight = allowedFunctions.Select(f => GetMinimalTreeHeight(f)).Min(); 195 195 if(minTreeHeight > maxTreeHeight) 196 196 maxTreeHeight = minTreeHeight; … … 212 212 IFunctionTree root; 213 213 if(balanceTrees) { 214 root = MakeBalancedTree(selectedFunction, maxTree Size - 1, maxTreeHeight - 1);214 root = MakeBalancedTree(selectedFunction, maxTreeHeight - 1); 215 215 } else { 216 root = MakeUnbalancedTree(selectedFunction, maxTree Size - 1, maxTreeHeight - 1);216 root = MakeUnbalancedTree(selectedFunction, maxTreeHeight - 1); 217 217 } 218 218 return root; … … 446 446 } 447 447 448 private IFunctionTree MakeUnbalancedTree(IFunction parent, int maxTreeSize, int maxTreeHeight) { 449 if(maxTreeHeight == 0 || maxTreeSize == 0) return parent.GetTreeNode(); 448 449 private IFunctionTree MakeUnbalancedTree(IFunction parent, int maxTreeHeight) { 450 if(maxTreeHeight == 0) return parent.GetTreeNode(); 450 451 int minArity; 451 452 int maxArity; 452 453 GetMinMaxArity(parent, out minArity, out maxArity); 453 if(maxArity >= maxTreeSize) {454 maxArity = maxTreeSize;455 }456 454 int actualArity = random.Next(minArity, maxArity + 1); 457 455 if(actualArity > 0) { 458 456 IFunctionTree parentTree = parent.GetTreeNode(); 459 int maxSubTreeSize = maxTreeSize / actualArity;460 457 for(int i = 0; i < actualArity; i++) { 461 IFunction[] possibleFunctions = GetAllowedSubFunctions(parent, i).Where(f => GetMinimalTreeHeight(f) <= maxTreeHeight && 462 GetMinimalTreeSize(f) <= maxSubTreeSize).ToArray(); 458 IFunction[] possibleFunctions = GetAllowedSubFunctions(parent, i).Where(f => GetMinimalTreeHeight(f) <= maxTreeHeight).ToArray(); 463 459 IFunction selectedFunction = RandomSelect(possibleFunctions); 464 IFunctionTree newSubTree = MakeUnbalancedTree(selectedFunction, max SubTreeSize - 1, maxTreeHeight - 1);460 IFunctionTree newSubTree = MakeUnbalancedTree(selectedFunction, maxTreeHeight - 1); 465 461 parentTree.InsertSubTree(i, newSubTree); 466 462 } … … 469 465 return parent.GetTreeNode(); 470 466 } 467 471 468 472 469 // NOTE: this method doesn't build fully balanced trees because we have constraints on the 473 470 // types of possible sub-functions which can indirectly impose a limit for the depth of a given sub-tree 474 private IFunctionTree MakeBalancedTree(IFunction parent, int maxTree Size, int maxTreeHeight) {475 if(maxTreeHeight == 0 || maxTreeSize == 0) return parent.GetTreeNode();471 private IFunctionTree MakeBalancedTree(IFunction parent, int maxTreeHeight) { 472 if(maxTreeHeight == 0) return parent.GetTreeNode(); 476 473 int minArity; 477 474 int maxArity; 478 475 GetMinMaxArity(parent, out minArity, out maxArity); 479 if(maxArity >= maxTreeSize) {480 maxArity = maxTreeSize;481 }482 476 int actualArity = random.Next(minArity, maxArity + 1); 483 477 if(actualArity > 0) { 484 478 IFunctionTree parentTree = parent.GetTreeNode(); 485 int maxSubTreeSize = maxTreeSize / actualArity;486 479 for(int i = 0; i < actualArity; i++) { 487 // first try to find a function that fits into the maxHeight and maxSize limits 488 IFunction[] possibleFunctions = GetAllowedSubFunctions(parent, i).Where( 489 f => GetMinimalTreeHeight(f) <= maxTreeHeight && 490 GetMinimalTreeSize(f) <= maxSubTreeSize && 480 // first try to find a function that fits into the maxHeight limit 481 IFunction[] possibleFunctions = GetAllowedSubFunctions(parent, i).Where(f => GetMinimalTreeHeight(f) <= maxTreeHeight && 491 482 !IsTerminal(f)).ToArray(); 492 483 // no possible function found => extend function set to terminals … … 498 489 } else { 499 490 IFunction selectedFunction = RandomSelect(possibleFunctions); 500 IFunctionTree newTree = MakeBalancedTree(selectedFunction, max SubTreeSize - 1, maxTreeHeight - 1);491 IFunctionTree newTree = MakeBalancedTree(selectedFunction, maxTreeHeight - 1); 501 492 parentTree.InsertSubTree(i, newTree); 502 493 }
Note: See TracChangeset
for help on using the changeset viewer.