Changeset 181 for trunk/sources/HeuristicLab.StructureIdentification
- Timestamp:
- 04/24/08 14:39:48 (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.StructureIdentification/TreeGardener.cs
r180 r181 112 112 113 113 /// <summary> 114 /// selects a random function from allowedFunctions and creates a (un)balanced random tree with maximal size and height. 114 /// Selects a random function from allowedFunctions and creates a (un)balanced random tree with maximal size and height. 115 /// Max-size and max-height are not accepted as hard constraints, if all functions in the set of allowed functions would 116 /// lead to a bigger tree then the limits are automatically extended to guarantee that we can build a tree. 115 117 /// </summary> 116 118 /// <param name="allowedFunctions">Set of allowed functions.</param> … … 120 122 /// <returns>New random tree</returns> 121 123 internal IFunctionTree CreateRandomTree(ICollection<IFunction> allowedFunctions, int maxTreeSize, int maxTreeHeight, bool balanceTrees) { 124 // get the minimal needed height based on allowed functions and extend the max-height if necessary 122 125 int minTreeHeight = allowedFunctions.Select(f => ((IntData)f.GetVariable(GPOperatorLibrary.MIN_TREE_HEIGHT).Value).Data).Min(); 123 126 if(minTreeHeight > maxTreeHeight) 124 127 maxTreeHeight = minTreeHeight; 125 128 // get the minimal needed size based on allowed functions and extend the max-size if necessary 126 129 int minTreeSize = allowedFunctions.Select(f => ((IntData)f.GetVariable(GPOperatorLibrary.MIN_TREE_SIZE).Value).Data).Min(); 127 130 if(minTreeSize > maxTreeSize) 128 131 maxTreeSize = minTreeSize; 129 132 133 // select a random value for the size and height 130 134 int treeHeight = random.Next(minTreeHeight, maxTreeHeight + 1); 131 135 int treeSize = random.Next(minTreeSize, maxTreeSize + 1); 132 136 137 // filter the set of allowed functions and select only from those that fit into the given maximal size and height limits 133 138 IFunction[] possibleFunctions = allowedFunctions.Where(f => ((IntData)f.GetVariable(GPOperatorLibrary.MIN_TREE_HEIGHT).Value).Data <= treeHeight && 134 139 ((IntData)f.GetVariable(GPOperatorLibrary.MIN_TREE_SIZE).Value).Data <= treeSize).ToArray(); 135 140 IFunction selectedFunction = possibleFunctions[random.Next(possibleFunctions.Length)]; 136 141 142 // build the tree 137 143 IFunctionTree root = new FunctionTree(selectedFunction); 138 144 if(balanceTrees) { … … 416 422 for(int i = 0; i < actualArity; i++) { 417 423 if(maxTreeHeight == 1 || maxSubTreeSize == 1) { 418 IFunction[] possibleTerminals = GetAllowedSubFunctions(parent.Function, i).Where( 419 f => GetMinimalTreeHeight(f) <= maxTreeHeight && 420 GetMinimalTreeSize(f) <= maxSubTreeSize && 421 IsTerminal(f)).ToArray(); 424 IFunction[] possibleTerminals = GetAllowedSubFunctions(parent.Function, i).Where(IsTerminal(f)).ToArray(); 422 425 IFunction selectedTerminal = possibleTerminals[random.Next(possibleTerminals.Length)]; 423 426 IFunctionTree newTree = new FunctionTree(selectedTerminal);
Note: See TracChangeset
for help on using the changeset viewer.