Changeset 444 for trunk/sources
- Timestamp:
- 08/05/08 11:34:33 (16 years ago)
- Location:
- trunk/sources/HeuristicLab.StructureIdentification
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.StructureIdentification/ProbabilisticTreeCreator.cs
r324 r444 27 27 using HeuristicLab.Random; 28 28 using HeuristicLab.Functions; 29 using System.Diagnostics; 29 30 30 31 namespace HeuristicLab.StructureIdentification { … … 65 66 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeHeight"), new IntData(actualTreeHeight))); 66 67 67 if(!gardener.IsValidTree(root)) { throw new InvalidProgramException(); } 68 69 if(actualTreeHeight > maxTreeHeight) { 70 throw new InvalidProgramException(); 71 } 68 Debug.Assert(gardener.IsValidTree(root) && actualTreeHeight<=maxTreeHeight); 72 69 73 70 return gardener.CreateInitializationOperation(gardener.GetAllSubTrees(root), scope); -
trunk/sources/HeuristicLab.StructureIdentification/Recombination/SizeFairCrossOver.cs
r442 r444 96 96 97 97 // check if the new tree is valid and if the height of is still in the allowed bounds (we are not so strict for the max-size) 98 Debug.Assert(gardener.IsValidTree(newTree) && newTreeHeight <= maxTreeHeight );98 Debug.Assert(gardener.IsValidTree(newTree) && newTreeHeight <= maxTreeHeight && newTreeSize <= maxTreeSize); 99 99 return gardener.CreateInitializationOperation(newBranches, child); 100 100 } -
trunk/sources/HeuristicLab.StructureIdentification/TreeGardener.cs
r433 r444 100 100 101 101 internal IFunctionTree PTC2(IRandom random, int size, int maxDepth) { 102 if(size ==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(); 105 105 106 int currentSize = 1; 107 int totalListMinSize = 0; 106 108 int minArity; 107 109 int maxArity; … … 111 113 } 112 114 int actualArity = random.Next(minArity, maxArity + 1); 113 for(int i=0;i<actualArity;i++) { 115 totalListMinSize += GetMinimalTreeSize(root.Function) - 1; 116 for(int i = 0; i < actualArity; i++) { 114 117 // insert a dummy sub-tree and add the pending extension to the list 115 118 root.AddSubTree(null); 116 list.Add(new object[] { root, i, 2});117 } 118 119 while(list.Count > 0 && list.Count+ currentSize < size) {119 list.Add(new object[] { root, i, 2 }); 120 } 121 122 while(list.Count > 0 && totalListMinSize + currentSize < size) { 120 123 int randomIndex = random.Next(list.Count); 121 124 object[] nextExtension = list[randomIndex]; … … 126 129 if(d == maxDepth) { 127 130 parent.RemoveSubTree(a); 128 parent.InsertSubTree(a, RandomSelect(GetAllowedSubFunctions(parent.Function, a).Where(f => IsTerminal(f)).ToArray()).GetTreeNode()); 131 IFunctionTree branch = CreateRandomTree(GetAllowedSubFunctions(parent.Function, a), 1, 1); 132 parent.InsertSubTree(a, branch); // insert a smallest possible tree 133 currentSize += branch.Size; 134 totalListMinSize-=branch.Size; 129 135 } else { 130 IFunction selectedFunction = RandomSelect(GetAllowedSubFunctions(parent.Function, a).Where( 131 f => !IsTerminal(f) && GetMinimalTreeHeight(f) + (d -1) <= maxDepth).ToArray());136 IFunction selectedFunction = RandomSelect(GetAllowedSubFunctions(parent.Function, a).Where( 137 f => !IsTerminal(f) && GetMinimalTreeHeight(f) + (d - 1) <= maxDepth).ToArray()); 132 138 IFunctionTree newTree = selectedFunction.GetTreeNode(); 133 139 parent.RemoveSubTree(a); 134 140 parent.InsertSubTree(a, newTree); 141 currentSize++; 142 totalListMinSize--; 135 143 136 144 GetMinMaxArity(selectedFunction, out minArity, out maxArity); … … 144 152 list.Add(new object[] { newTree, i, d + 1 }); 145 153 } 146 }147 currentSize++;154 totalListMinSize += GetMinimalTreeSize(newTree.Function) - 1; 155 } 148 156 } 149 157 while(list.Count > 0) { … … 184 192 internal IFunctionTree CreateRandomTree(ICollection<IFunction> allowedFunctions, int maxTreeSize, int maxTreeHeight, bool balanceTrees) { 185 193 // get the minimal needed height based on allowed functions and extend the max-height if necessary 186 int minTreeHeight = allowedFunctions.Select(f => ((IntData)f.GetVariable(GPOperatorLibrary.MIN_TREE_HEIGHT).Value).Data).Min();194 int minTreeHeight = allowedFunctions.Select(f => GetMinimalTreeSize(f)).Min(); 187 195 if(minTreeHeight > maxTreeHeight) 188 196 maxTreeHeight = minTreeHeight; 189 197 // get the minimal needed size based on allowed functions and extend the max-size if necessary 190 int minTreeSize = allowedFunctions.Select(f => ((IntData)f.GetVariable(GPOperatorLibrary.MIN_TREE_SIZE).Value).Data).Min();198 int minTreeSize = allowedFunctions.Select(f => GetMinimalTreeSize(f)).Min(); 191 199 if(minTreeSize > maxTreeSize) 192 200 maxTreeSize = minTreeSize; … … 197 205 198 206 // filter the set of allowed functions and select only from those that fit into the given maximal size and height limits 199 IFunction[] possibleFunctions = allowedFunctions.Where(f => ((IntData)f.GetVariable(GPOperatorLibrary.MIN_TREE_HEIGHT).Value).Data<= treeHeight &&200 ((IntData)f.GetVariable(GPOperatorLibrary.MIN_TREE_SIZE).Value).Data<= treeSize).ToArray();207 IFunction[] possibleFunctions = allowedFunctions.Where(f => GetMinimalTreeHeight(f) <= treeHeight && 208 GetMinimalTreeSize(f) <= treeSize).ToArray(); 201 209 IFunction selectedFunction = RandomSelect(possibleFunctions); 202 210 … … 519 527 List<IFunctionTree> branches = new List<IFunctionTree>(); 520 528 foreach(IFunctionTree subTree in tree.SubTrees) { 521 if(subTree.Height >=level-1)522 branches.AddRange(GetBranchesAtLevel(subTree, level - 1));529 if(subTree.Height >= level - 1) 530 branches.AddRange(GetBranchesAtLevel(subTree, level - 1)); 523 531 } 524 532 return branches;
Note: See TracChangeset
for help on using the changeset viewer.