Changeset 202 for trunk/sources/HeuristicLab.StructureIdentification
- Timestamp:
- 04/28/08 21:35:23 (17 years ago)
- Location:
- trunk/sources/HeuristicLab.StructureIdentification
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.StructureIdentification/Manipulation/ChangeNodeTypeManipulation.cs
r189 r202 156 156 int maxArity; 157 157 // create a new tree-node for a randomly selected function 158 IFunction Tree newTree = allowedFunctions[random.Next(allowedFunctions.Count)].GetTreeNode();159 gardener.GetMinMaxArity( newTree.Function, out minArity, out maxArity);158 IFunction selectedFunction = allowedFunctions[random.Next(allowedFunctions.Count)]; 159 gardener.GetMinMaxArity(selectedFunction, out minArity, out maxArity); 160 160 // if the old child had too many sub-trees then the new child should keep as many sub-trees as possible 161 161 if (actualArity > maxArity) … … 165 165 // create a list that holds old sub-trees that we can reuse in the new tree 166 166 List<IFunctionTree> availableSubTrees = new List<IFunctionTree>(child.SubTrees); 167 List<IFunctionTree> freshSubTrees = new List<IFunctionTree>() { newTree }; 167 List<IFunctionTree> freshSubTrees = new List<IFunctionTree>(); 168 IFunctionTree newTree = selectedFunction.GetTreeNode(); 168 169 // randomly select the sub-trees that we keep 169 170 for (int i = 0; i < actualArity; i++) { … … 172 173 // then use a random existing sub-tree. When there are no existing sub-trees 173 174 // that fit in the given slot then create a new random tree and use it for the slot 174 ICollection<IFunction> allowedSubFunctions = gardener.GetAllowedSubFunctions( newTree.Function, i);175 ICollection<IFunction> allowedSubFunctions = gardener.GetAllowedSubFunctions(selectedFunction, i); 175 176 var matchingSubTrees = availableSubTrees.Where(subTree => allowedSubFunctions.Contains(subTree.Function)); 176 177 if (matchingSubTrees.Count() > 0) { … … 186 187 } 187 188 } 189 freshSubTrees.Add(newTree); 188 190 uninitializedBranches = freshSubTrees; 189 191 return newTree; -
trunk/sources/HeuristicLab.StructureIdentification/TreeGardener.cs
r199 r202 81 81 /// <returns></returns> 82 82 internal IFunctionTree CreateBalancedRandomTree(int maxTreeSize, int maxTreeHeight) { 83 IFunction Tree root = CreateRandomRoot(maxTreeSize, maxTreeHeight);84 MakeBalancedTree(root, maxTreeSize - 1, maxTreeHeight - 1);85 return root;83 IFunction rootFunction = GetRandomRoot(maxTreeSize, maxTreeHeight); 84 IFunctionTree tree = MakeBalancedTree(rootFunction, maxTreeSize - 1, maxTreeHeight - 1); 85 return tree; 86 86 } 87 87 … … 94 94 /// <returns></returns> 95 95 internal IFunctionTree CreateUnbalancedRandomTree(int maxTreeSize, int maxTreeHeight) { 96 IFunction Tree root = CreateRandomRoot(maxTreeSize, maxTreeHeight);97 MakeUnbalancedTree(root, maxTreeSize - 1, maxTreeHeight - 1);98 return root;96 IFunction rootFunction = GetRandomRoot(maxTreeSize, maxTreeHeight); 97 IFunctionTree tree = MakeUnbalancedTree(rootFunction, maxTreeSize - 1, maxTreeHeight - 1); 98 return tree; 99 99 } 100 100 … … 141 141 142 142 // build the tree 143 IFunctionTree root = selectedFunction.GetTreeNode();143 IFunctionTree root; 144 144 if(balanceTrees) { 145 MakeBalancedTree(root, maxTreeSize - 1, maxTreeHeight - 1);145 root = MakeBalancedTree(selectedFunction, maxTreeSize - 1, maxTreeHeight - 1); 146 146 } else { 147 MakeUnbalancedTree(root, maxTreeSize - 1, maxTreeHeight - 1);147 root = MakeUnbalancedTree(selectedFunction, maxTreeSize - 1, maxTreeHeight - 1); 148 148 } 149 149 return root; … … 373 373 374 374 #region private utility methods 375 private IFunction Tree CreateRandomRoot(int maxTreeSize, int maxTreeHeight) {375 private IFunction GetRandomRoot(int maxTreeSize, int maxTreeHeight) { 376 376 if(maxTreeHeight == 1 || maxTreeSize == 1) { 377 377 IFunction selectedTerminal = RandomSelect(terminals); 378 return selectedTerminal .GetTreeNode();378 return selectedTerminal; 379 379 } else { 380 380 IFunction[] possibleFunctions = allFunctions.Where(f => GetMinimalTreeHeight(f) <= maxTreeHeight && 381 381 GetMinimalTreeSize(f) <= maxTreeSize).ToArray(); 382 382 IFunction selectedFunction = RandomSelect(possibleFunctions); 383 return selectedFunction .GetTreeNode();384 } 385 } 386 387 private void MakeUnbalancedTree(IFunctionTreeparent, int maxTreeSize, int maxTreeHeight) {388 if(maxTreeHeight == 0 || maxTreeSize == 0) return ;383 return selectedFunction; 384 } 385 } 386 387 private IFunctionTree MakeUnbalancedTree(IFunction parent, int maxTreeSize, int maxTreeHeight) { 388 if(maxTreeHeight == 0 || maxTreeSize == 0) return parent.GetTreeNode(); 389 389 int minArity; 390 390 int maxArity; 391 GetMinMaxArity(parent .Function, out minArity, out maxArity);391 GetMinMaxArity(parent, out minArity, out maxArity); 392 392 if(maxArity >= maxTreeSize) { 393 393 maxArity = maxTreeSize; … … 395 395 int actualArity = random.Next(minArity, maxArity + 1); 396 396 if(actualArity > 0) { 397 IFunctionTree parentTree = parent.GetTreeNode(); 397 398 int maxSubTreeSize = maxTreeSize / actualArity; 398 399 for(int i = 0; i < actualArity; i++) { 399 IFunction[] possibleFunctions = GetAllowedSubFunctions(parent .Function, i).Where(f => GetMinimalTreeHeight(f) <= maxTreeHeight &&400 IFunction[] possibleFunctions = GetAllowedSubFunctions(parent, i).Where(f => GetMinimalTreeHeight(f) <= maxTreeHeight && 400 401 GetMinimalTreeSize(f) <= maxSubTreeSize).ToArray(); 401 402 IFunction selectedFunction = RandomSelect(possibleFunctions); 402 IFunctionTree newSubTree = selectedFunction.GetTreeNode(); 403 MakeUnbalancedTree(newSubTree, maxSubTreeSize - 1, maxTreeHeight - 1); 404 parent.InsertSubTree(i, newSubTree); 405 } 406 } 403 IFunctionTree newSubTree = MakeUnbalancedTree(selectedFunction, maxSubTreeSize - 1, maxTreeHeight - 1); 404 parentTree.InsertSubTree(i, newSubTree); 405 } 406 return parentTree; 407 } 408 return parent.GetTreeNode(); 407 409 } 408 410 409 411 // NOTE: this method doesn't build fully balanced trees because we have constraints on the 410 412 // types of possible sub-functions which can indirectly impose a limit for the depth of a given sub-tree 411 private void MakeBalancedTree(IFunctionTreeparent, int maxTreeSize, int maxTreeHeight) {412 if(maxTreeHeight == 0 || maxTreeSize == 0) return ;413 private IFunctionTree MakeBalancedTree(IFunction parent, int maxTreeSize, int maxTreeHeight) { 414 if(maxTreeHeight == 0 || maxTreeSize == 0) return parent.GetTreeNode(); 413 415 int minArity; 414 416 int maxArity; 415 GetMinMaxArity(parent .Function, out minArity, out maxArity);417 GetMinMaxArity(parent, out minArity, out maxArity); 416 418 if(maxArity >= maxTreeSize) { 417 419 maxArity = maxTreeSize; … … 419 421 int actualArity = random.Next(minArity, maxArity + 1); 420 422 if(actualArity > 0) { 423 IFunctionTree parentTree = parent.GetTreeNode(); 421 424 int maxSubTreeSize = maxTreeSize / actualArity; 422 425 for(int i = 0; i < actualArity; i++) { 423 426 // first try to find a function that fits into the maxHeight and maxSize limits 424 IFunction[] possibleFunctions = GetAllowedSubFunctions(parent .Function, i).Where(427 IFunction[] possibleFunctions = GetAllowedSubFunctions(parent, i).Where( 425 428 f => GetMinimalTreeHeight(f) <= maxTreeHeight && 426 429 GetMinimalTreeSize(f) <= maxSubTreeSize && … … 428 431 // no possible function found => extend function set to terminals 429 432 if(possibleFunctions.Length == 0) { 430 possibleFunctions = GetAllowedSubFunctions(parent .Function, i).Where(f => IsTerminal(f)).ToArray();433 possibleFunctions = GetAllowedSubFunctions(parent, i).Where(f => IsTerminal(f)).ToArray(); 431 434 IFunction selectedTerminal = RandomSelect(possibleFunctions); 432 435 IFunctionTree newTree = selectedTerminal.GetTreeNode(); 433 parent .InsertSubTree(i, newTree);436 parentTree.InsertSubTree(i, newTree); 434 437 } else { 435 438 IFunction selectedFunction = RandomSelect(possibleFunctions); 436 IFunctionTree newTree = selectedFunction.GetTreeNode(); 437 parent.InsertSubTree(i, newTree); 438 MakeBalancedTree(newTree, maxSubTreeSize - 1, maxTreeHeight - 1); 439 IFunctionTree newTree = MakeBalancedTree(selectedFunction, maxSubTreeSize - 1, maxTreeHeight - 1); 440 parentTree.InsertSubTree(i, newTree); 439 441 } 440 442 } 441 } 443 return parentTree; 444 } 445 return parent.GetTreeNode(); 442 446 } 443 447
Note: See TracChangeset
for help on using the changeset viewer.