Free cookie consent management tool by TermsFeed Policy Generator

Changeset 181 for trunk


Ignore:
Timestamp:
04/24/08 14:39:48 (17 years ago)
Author:
gkronber
Message:

more comments and minor code improvements in TreeGardener

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.StructureIdentification/TreeGardener.cs

    r180 r181  
    112112
    113113    /// <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.
    115117    /// </summary>
    116118    /// <param name="allowedFunctions">Set of allowed functions.</param>
     
    120122    /// <returns>New random tree</returns>
    121123    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
    122125      int minTreeHeight = allowedFunctions.Select(f => ((IntData)f.GetVariable(GPOperatorLibrary.MIN_TREE_HEIGHT).Value).Data).Min();
    123126      if(minTreeHeight > maxTreeHeight)
    124127        maxTreeHeight = minTreeHeight;
    125 
     128      // get the minimal needed size based on allowed functions and extend the max-size if necessary
    126129      int minTreeSize = allowedFunctions.Select(f => ((IntData)f.GetVariable(GPOperatorLibrary.MIN_TREE_SIZE).Value).Data).Min();
    127130      if(minTreeSize > maxTreeSize)
    128131        maxTreeSize = minTreeSize;
    129132
     133      // select a random value for the size and height
    130134      int treeHeight = random.Next(minTreeHeight, maxTreeHeight + 1);
    131135      int treeSize = random.Next(minTreeSize, maxTreeSize + 1);
    132136
     137      // filter the set of allowed functions and select only from those that fit into the given maximal size and height limits
    133138      IFunction[] possibleFunctions = allowedFunctions.Where(f => ((IntData)f.GetVariable(GPOperatorLibrary.MIN_TREE_HEIGHT).Value).Data <= treeHeight &&
    134139        ((IntData)f.GetVariable(GPOperatorLibrary.MIN_TREE_SIZE).Value).Data <= treeSize).ToArray();
    135140      IFunction selectedFunction = possibleFunctions[random.Next(possibleFunctions.Length)];
    136141
     142      // build the tree
    137143      IFunctionTree root = new FunctionTree(selectedFunction);
    138144      if(balanceTrees) {
     
    416422        for(int i = 0; i < actualArity; i++) {
    417423          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();
    422425            IFunction selectedTerminal = possibleTerminals[random.Next(possibleTerminals.Length)];
    423426            IFunctionTree newTree = new FunctionTree(selectedTerminal);
Note: See TracChangeset for help on using the changeset viewer.