- Timestamp:
- 06/03/08 16:21:56 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.StructureIdentification/TreeGardener.cs
r284 r286 97 97 IFunctionTree tree = MakeUnbalancedTree(rootFunction, maxTreeSize - 1, maxTreeHeight - 1); 98 98 return tree; 99 } 100 101 internal IFunctionTree PTC2(IRandom random, int size, int maxDepth) { 102 if(size == 1) return RandomSelect(terminals).GetTreeNode(); 103 List<object[]> list = new List<object[]>(); 104 IFunctionTree root = GetRandomRoot(size, maxDepth).GetTreeNode(); 105 int currentSize = 1; 106 int minArity; 107 int maxArity; 108 GetMinMaxArity(root.Function, out minArity, out maxArity); 109 if(maxArity >= size) { 110 maxArity = size; 111 } 112 int actualArity = random.Next(minArity, maxArity + 1); 113 for(int i=0;i<actualArity;i++) { 114 // insert a dummy sub-tree and add the pending extension to the list 115 root.AddSubTree(null); 116 list.Add(new object[] {root, i, 2}); 117 } 118 119 while(list.Count > 0 && list.Count + currentSize < size) { 120 int randomIndex = random.Next(list.Count); 121 object[] nextExtension = list[randomIndex]; 122 list.RemoveAt(randomIndex); 123 IFunctionTree parent = (IFunctionTree)nextExtension[0]; 124 int a = (int)nextExtension[1]; 125 int d = (int)nextExtension[2]; 126 if(d == maxDepth) { 127 parent.RemoveSubTree(a); 128 parent.InsertSubTree(a, RandomSelect(terminals).GetTreeNode()); 129 } else { 130 IFunction selectedFunction = RandomSelect(functions); 131 IFunctionTree newTree = selectedFunction.GetTreeNode(); 132 parent.RemoveSubTree(a); 133 parent.InsertSubTree(a, newTree); 134 135 GetMinMaxArity(selectedFunction, out minArity, out maxArity); 136 if(maxArity >= size) { 137 maxArity = size; 138 } 139 actualArity = random.Next(minArity, maxArity + 1); 140 for(int i = 0; i < actualArity; i++) { 141 // insert a dummy sub-tree and add the pending extension to the list 142 newTree.AddSubTree(null); 143 list.Add(new object[] { newTree, i, d + 1 }); 144 } 145 } 146 currentSize++; 147 } 148 while(list.Count > 0) { 149 int randomIndex = random.Next(list.Count); 150 object[] nextExtension = list[randomIndex]; 151 list.RemoveAt(randomIndex); 152 IFunctionTree parent = (IFunctionTree)nextExtension[0]; 153 int a = (int)nextExtension[1]; 154 int d = (int)nextExtension[2]; 155 IFunction selectedTerminal = RandomSelect(terminals); 156 parent.RemoveSubTree(a); 157 parent.InsertSubTree(a, selectedTerminal.GetTreeNode()); 158 } 159 return root; 99 160 } 100 161
Note: See TracChangeset
for help on using the changeset viewer.