Changeset 182
- Timestamp:
- 04/24/08 15:03:57 (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.StructureIdentification/TreeGardener.cs
r181 r182 138 138 IFunction[] possibleFunctions = allowedFunctions.Where(f => ((IntData)f.GetVariable(GPOperatorLibrary.MIN_TREE_HEIGHT).Value).Data <= treeHeight && 139 139 ((IntData)f.GetVariable(GPOperatorLibrary.MIN_TREE_SIZE).Value).Data <= treeSize).ToArray(); 140 IFunction selectedFunction = possibleFunctions[random.Next(possibleFunctions.Length)];140 IFunction selectedFunction = RandomSelect(possibleFunctions); 141 141 142 142 // build the tree … … 375 375 private IFunctionTree CreateRandomRoot(int maxTreeSize, int maxTreeHeight) { 376 376 if(maxTreeHeight == 1 || maxTreeSize == 1) { 377 IFunction selectedTerminal = terminals[random.Next(terminals.Count())];377 IFunction selectedTerminal = RandomSelect(terminals); 378 378 return new FunctionTree(selectedTerminal); 379 379 } else { 380 380 IFunction[] possibleFunctions = allFunctions.Where(f => GetMinimalTreeHeight(f) <= maxTreeHeight && 381 381 GetMinimalTreeSize(f) <= maxTreeSize).ToArray(); 382 IFunction selectedFunction = possibleFunctions[random.Next(possibleFunctions.Length)];382 IFunction selectedFunction = RandomSelect(possibleFunctions); 383 383 return new FunctionTree(selectedFunction); 384 384 } … … 399 399 IFunction[] possibleFunctions = GetAllowedSubFunctions(parent.Function, i).Where(f => GetMinimalTreeHeight(f) <= maxTreeHeight && 400 400 GetMinimalTreeSize(f) <= maxSubTreeSize).ToArray(); 401 IFunction selectedFunction = possibleFunctions[random.Next(possibleFunctions.Length)];401 IFunction selectedFunction = RandomSelect(possibleFunctions); 402 402 FunctionTree newSubTree = new FunctionTree(selectedFunction); 403 403 MakeUnbalancedTree(newSubTree, maxSubTreeSize - 1, maxTreeHeight - 1); … … 422 422 for(int i = 0; i < actualArity; i++) { 423 423 if(maxTreeHeight == 1 || maxSubTreeSize == 1) { 424 IFunction[] possibleTerminals = GetAllowedSubFunctions(parent.Function, i).Where( IsTerminal(f)).ToArray();425 IFunction selectedTerminal = possibleTerminals[random.Next(possibleTerminals.Length)];424 IFunction[] possibleTerminals = GetAllowedSubFunctions(parent.Function, i).Where(f => IsTerminal(f)).ToArray(); 425 IFunction selectedTerminal = RandomSelect(possibleTerminals); 426 426 IFunctionTree newTree = new FunctionTree(selectedTerminal); 427 427 parent.InsertSubTree(i, newTree); … … 431 431 GetMinimalTreeSize(f) <= maxSubTreeSize && 432 432 !IsTerminal(f)).ToArray(); 433 IFunction selectedFunction = possibleFunctions[random.Next(possibleFunctions.Length)];433 IFunction selectedFunction = RandomSelect(possibleFunctions); 434 434 FunctionTree newTree = new FunctionTree(selectedFunction); 435 435 parent.InsertSubTree(i, newTree); … … 465 465 } 466 466 467 private IFunction RandomSelect(IList<IFunction> functionSet) { 468 double[] accumulatedTickets = new double[functionSet.Count]; 469 double ticketAccumulator = 0; 470 int i = 0; 471 // precalculate the slot-sizes 472 foreach(IFunction function in functionSet) { 473 ticketAccumulator += ((DoubleData)function.GetVariable(GPOperatorLibrary.TICKETS).Value).Data; 474 accumulatedTickets[i] = ticketAccumulator; 475 i++; 476 } 477 // throw ball 478 double r = random.NextDouble() * ticketAccumulator; 479 // find the slot that has been hit 480 for(i = 0; i < accumulatedTickets.Length; i++) { 481 if(r < accumulatedTickets[i]) return functionSet[i]; 482 } 483 // sanity check 484 throw new InvalidProgramException(); // should never happen 485 } 467 486 468 487 #endregion
Note: See TracChangeset
for help on using the changeset viewer.