- Timestamp:
- 07/28/09 19:24:23 (15 years ago)
- Location:
- branches/GP-Refactoring-713
- Files:
-
- 1 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/TreeGardener.cs
r1529 r2202 36 36 internal class TreeGardener { 37 37 private IRandom random; 38 private GPOperatorLibrary funLibrary;38 private FunctionLibrary funLibrary; 39 39 private List<IFunction> functions; 40 40 … … 50 50 51 51 #region constructors 52 internal TreeGardener(IRandom random, GPOperatorLibrary funLibrary) {52 internal TreeGardener(IRandom random, FunctionLibrary funLibrary) { 53 53 this.random = random; 54 54 this.funLibrary = funLibrary; … … 57 57 functions = new List<IFunction>(); 58 58 // init functions and terminals based on constraints 59 foreach(IFunction fun in funLibrary. Group.Operators) {59 foreach(IFunction fun in funLibrary.Functions) { 60 60 if(fun.MaxArity == 0) { 61 61 terminals.Add(fun); … … 112 112 } 113 113 int actualArity = random.Next(minArity, maxArity + 1); 114 totalListMinSize += GetMinimalTreeSize(root.Function)- 1;114 totalListMinSize += root.Function.MinTreeSize - 1; 115 115 for(int i = 0; i < actualArity; i++) { 116 116 // insert a dummy sub-tree and add the pending extension to the list … … 134 134 } else { 135 135 IFunction selectedFunction = RandomSelect(GetAllowedSubFunctions(parent.Function, a).Where( 136 f => !IsTerminal(f) && GetMinimalTreeHeight(f)+ (d - 1) <= maxDepth).ToArray());136 f => !IsTerminal(f) && f.MinTreeHeight + (d - 1) <= maxDepth).ToArray()); 137 137 IFunctionTree newTree = selectedFunction.GetTreeNode(); 138 138 parent.RemoveSubTree(a); … … 152 152 list.Add(new object[] { newTree, i, d + 1 }); 153 153 } 154 totalListMinSize += GetMinimalTreeSize(newTree.Function)- 1;154 totalListMinSize += newTree.Function.MinTreeSize - 1; 155 155 } 156 156 } … … 178 178 internal IFunctionTree CreateRandomTree(ICollection<IFunction> allowedFunctions, int maxTreeSize, int maxTreeHeight) { 179 179 // get the minimal needed height based on allowed functions and extend the max-height if necessary 180 int minTreeHeight = allowedFunctions.Select(f => GetMinimalTreeHeight(f)).Min();180 int minTreeHeight = allowedFunctions.Select(f => f.MinTreeHeight).Min(); 181 181 if(minTreeHeight > maxTreeHeight) 182 182 maxTreeHeight = minTreeHeight; 183 183 // get the minimal needed size based on allowed functions and extend the max-size if necessary 184 int minTreeSize = allowedFunctions.Select(f => GetMinimalTreeSize(f)).Min();184 int minTreeSize = allowedFunctions.Select(f => f.MinTreeSize).Min(); 185 185 if(minTreeSize > maxTreeSize) 186 186 maxTreeSize = minTreeSize; … … 191 191 192 192 // filter the set of allowed functions and select only from those that fit into the given maximal size and height limits 193 IFunction[] possibleFunctions = allowedFunctions.Where(f => GetMinimalTreeHeight(f)<= treeHeight &&194 GetMinimalTreeSize(f)<= treeSize).ToArray();193 IFunction[] possibleFunctions = allowedFunctions.Where(f => f.MinTreeHeight <= treeHeight && 194 f.MinTreeSize <= treeSize).ToArray(); 195 195 IFunction selectedFunction = RandomSelect(possibleFunctions); 196 196 … … 206 206 Scope tempScope = new Scope("Temp. initialization scope"); 207 207 208 var parametricTrees = trees.Where(t => t. Function.GetVariable(FunctionBase.INITIALIZATION)!= null);208 var parametricTrees = trees.Where(t => t.Initializer != null); 209 209 foreach(IFunctionTree tree in parametricTrees) { 210 210 // enqueue an initialization operation for each operator with local variables 211 IOperator initialization = (IOperator)tree.Function.GetVariable(FunctionBase.INITIALIZATION).Value;211 IOperator initialization = tree.Initializer; 212 212 Scope initScope = new Scope(); 213 213 // copy the local variables into a temporary scope used for initialization … … 280 280 internal bool IsValidTree(IFunctionTree tree) { 281 281 for(int i = 0; i < tree.SubTrees.Count; i++) { 282 if(!tree.Function. AllowedSubFunctions(i).Contains(tree.SubTrees[i].Function)) return false;282 if(!tree.Function.GetAllowedSubFunctions(i).Contains(tree.SubTrees[i].Function)) return false; 283 283 } 284 284 … … 329 329 // we only count those slots that can hold at least one of the children that we should combine 330 330 for(int slot = 0; slot < nSlots; slot++) { 331 HashSet<IFunction> functionSet = new HashSet<IFunction>(f. AllowedSubFunctions(slot));331 HashSet<IFunction> functionSet = new HashSet<IFunction>(f.GetAllowedSubFunctions(slot)); 332 332 if(functionSet.Count() > 0) { 333 333 slotSets.Add(functionSet); … … 378 378 return f.MinArity == 0 && f.MaxArity == 0; 379 379 } 380 internal I List<IFunction> GetAllowedSubFunctions(IFunction f, int index) {380 internal ICollection<IFunction> GetAllowedSubFunctions(IFunction f, int index) { 381 381 if(f == null) { 382 382 return allFunctions; 383 383 } else { 384 return f. AllowedSubFunctions(index);384 return f.GetAllowedSubFunctions(index); 385 385 } 386 386 } … … 393 393 return selectedTerminal; 394 394 } else { 395 IFunction[] possibleFunctions = functions.Where(f => GetMinimalTreeHeight(f)<= maxTreeHeight &&396 GetMinimalTreeSize(f)<= maxTreeSize).ToArray();395 IFunction[] possibleFunctions = functions.Where(f => f.MinTreeHeight <= maxTreeHeight && 396 f.MinTreeSize <= maxTreeSize).ToArray(); 397 397 IFunction selectedFunction = RandomSelect(possibleFunctions); 398 398 return selectedFunction; … … 409 409 IFunctionTree parentTree = parent.GetTreeNode(); 410 410 for(int i = 0; i < actualArity; i++) { 411 IFunction[] possibleFunctions = GetAllowedSubFunctions(parent, i).Where(f => GetMinimalTreeHeight(f)<= maxTreeHeight).ToArray();411 IFunction[] possibleFunctions = GetAllowedSubFunctions(parent, i).Where(f => f.MinTreeHeight <= maxTreeHeight).ToArray(); 412 412 IFunction selectedFunction = RandomSelect(possibleFunctions); 413 413 IFunctionTree newSubTree = MakeUnbalancedTree(selectedFunction, maxTreeHeight - 1); … … 431 431 for(int i = 0; i < actualArity; i++) { 432 432 // first try to find a function that fits into the maxHeight limit 433 IFunction[] possibleFunctions = GetAllowedSubFunctions(parent, i).Where(f => GetMinimalTreeHeight(f)<= maxTreeHeight &&433 IFunction[] possibleFunctions = GetAllowedSubFunctions(parent, i).Where(f => f.MinTreeHeight <= maxTreeHeight && 434 434 !IsTerminal(f)).ToArray(); 435 435 // no possible function found => extend function set to terminals … … 450 450 } 451 451 452 private int GetMinimalTreeHeight(IOperator op) {453 return ((IntData)op.GetVariable(GPOperatorLibrary.MIN_TREE_HEIGHT).Value).Data;454 }455 456 private int GetMinimalTreeSize(IOperator op) {457 return ((IntData)op.GetVariable(GPOperatorLibrary.MIN_TREE_SIZE).Value).Data;458 }459 460 452 private void TreeForEach(IFunctionTree tree, Action<IFunctionTree> action) { 461 453 action(tree); … … 479 471 // precalculate the slot-sizes 480 472 foreach(IFunction function in functionSet) { 481 ticketAccumulator += ((DoubleData)function.GetVariable(GPOperatorLibrary.TICKETS).Value).Data;473 ticketAccumulator += function.Tickets; 482 474 accumulatedTickets[i] = ticketAccumulator; 483 475 i++;
Note: See TracChangeset
for help on using the changeset viewer.