Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/28/08 21:35:23 (17 years ago)
Author:
gkronber
Message:

bug fixes in struct-id operators

Location:
trunk/sources/HeuristicLab.StructureIdentification
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.StructureIdentification/Manipulation/ChangeNodeTypeManipulation.cs

    r189 r202  
    156156      int maxArity;
    157157      // create a new tree-node for a randomly selected function
    158       IFunctionTree newTree = allowedFunctions[random.Next(allowedFunctions.Count)].GetTreeNode();
    159       gardener.GetMinMaxArity(newTree.Function, out minArity, out maxArity);
     158      IFunction selectedFunction = allowedFunctions[random.Next(allowedFunctions.Count)];
     159      gardener.GetMinMaxArity(selectedFunction, out minArity, out maxArity);
    160160      // if the old child had too many sub-trees then the new child should keep as many sub-trees as possible
    161161      if (actualArity > maxArity)
     
    165165      // create a list that holds old sub-trees that we can reuse in the new tree
    166166      List<IFunctionTree> availableSubTrees = new List<IFunctionTree>(child.SubTrees);
    167       List<IFunctionTree> freshSubTrees = new List<IFunctionTree>() { newTree };
     167      List<IFunctionTree> freshSubTrees = new List<IFunctionTree>();
     168      IFunctionTree newTree = selectedFunction.GetTreeNode();
    168169      // randomly select the sub-trees that we keep
    169170      for (int i = 0; i < actualArity; i++) {
     
    172173        // then use a random existing sub-tree. When there are no existing sub-trees
    173174        // that fit in the given slot then create a new random tree and use it for the slot
    174         ICollection<IFunction> allowedSubFunctions = gardener.GetAllowedSubFunctions(newTree.Function, i);
     175        ICollection<IFunction> allowedSubFunctions = gardener.GetAllowedSubFunctions(selectedFunction, i);
    175176        var matchingSubTrees = availableSubTrees.Where(subTree => allowedSubFunctions.Contains(subTree.Function));
    176177        if (matchingSubTrees.Count() > 0) {
     
    186187        }
    187188      }
     189      freshSubTrees.Add(newTree);
    188190      uninitializedBranches = freshSubTrees;
    189191      return newTree;
  • trunk/sources/HeuristicLab.StructureIdentification/TreeGardener.cs

    r199 r202  
    8181    /// <returns></returns>
    8282    internal IFunctionTree CreateBalancedRandomTree(int maxTreeSize, int maxTreeHeight) {
    83       IFunctionTree root = CreateRandomRoot(maxTreeSize, maxTreeHeight);
    84       MakeBalancedTree(root, maxTreeSize - 1, maxTreeHeight - 1);
    85       return root;
     83      IFunction rootFunction = GetRandomRoot(maxTreeSize, maxTreeHeight);
     84      IFunctionTree tree = MakeBalancedTree(rootFunction, maxTreeSize - 1, maxTreeHeight - 1);
     85      return tree;
    8686    }
    8787
     
    9494    /// <returns></returns>
    9595    internal IFunctionTree CreateUnbalancedRandomTree(int maxTreeSize, int maxTreeHeight) {
    96       IFunctionTree root = CreateRandomRoot(maxTreeSize, maxTreeHeight);
    97       MakeUnbalancedTree(root, maxTreeSize - 1, maxTreeHeight - 1);
    98       return root;
     96      IFunction rootFunction = GetRandomRoot(maxTreeSize, maxTreeHeight);
     97      IFunctionTree tree = MakeUnbalancedTree(rootFunction, maxTreeSize - 1, maxTreeHeight - 1);
     98      return tree;
    9999    }
    100100
     
    141141
    142142      // build the tree
    143       IFunctionTree root = selectedFunction.GetTreeNode();
     143      IFunctionTree root;
    144144      if(balanceTrees) {
    145         MakeBalancedTree(root, maxTreeSize - 1, maxTreeHeight - 1);
     145        root = MakeBalancedTree(selectedFunction, maxTreeSize - 1, maxTreeHeight - 1);
    146146      } else {
    147         MakeUnbalancedTree(root, maxTreeSize - 1, maxTreeHeight - 1);
     147        root = MakeUnbalancedTree(selectedFunction, maxTreeSize - 1, maxTreeHeight - 1);
    148148      }
    149149      return root;
     
    373373
    374374    #region private utility methods
    375     private IFunctionTree CreateRandomRoot(int maxTreeSize, int maxTreeHeight) {
     375    private IFunction GetRandomRoot(int maxTreeSize, int maxTreeHeight) {
    376376      if(maxTreeHeight == 1 || maxTreeSize == 1) {
    377377        IFunction selectedTerminal = RandomSelect(terminals);
    378         return selectedTerminal.GetTreeNode();
     378        return selectedTerminal;
    379379      } else {
    380380        IFunction[] possibleFunctions = allFunctions.Where(f => GetMinimalTreeHeight(f) <= maxTreeHeight &&
    381381          GetMinimalTreeSize(f) <= maxTreeSize).ToArray();
    382382        IFunction selectedFunction = RandomSelect(possibleFunctions);
    383         return selectedFunction.GetTreeNode();
    384       }
    385     }
    386 
    387     private void MakeUnbalancedTree(IFunctionTree parent, int maxTreeSize, int maxTreeHeight) {
    388       if(maxTreeHeight == 0 || maxTreeSize == 0) return;
     383        return selectedFunction;
     384      }
     385    }
     386
     387    private IFunctionTree MakeUnbalancedTree(IFunction parent, int maxTreeSize, int maxTreeHeight) {
     388      if(maxTreeHeight == 0 || maxTreeSize == 0) return parent.GetTreeNode();
    389389      int minArity;
    390390      int maxArity;
    391       GetMinMaxArity(parent.Function, out minArity, out maxArity);
     391      GetMinMaxArity(parent, out minArity, out maxArity);
    392392      if(maxArity >= maxTreeSize) {
    393393        maxArity = maxTreeSize;
     
    395395      int actualArity = random.Next(minArity, maxArity + 1);
    396396      if(actualArity > 0) {
     397        IFunctionTree parentTree = parent.GetTreeNode();
    397398        int maxSubTreeSize = maxTreeSize / actualArity;
    398399        for(int i = 0; i < actualArity; i++) {
    399           IFunction[] possibleFunctions = GetAllowedSubFunctions(parent.Function, i).Where(f => GetMinimalTreeHeight(f) <= maxTreeHeight &&
     400          IFunction[] possibleFunctions = GetAllowedSubFunctions(parent, i).Where(f => GetMinimalTreeHeight(f) <= maxTreeHeight &&
    400401            GetMinimalTreeSize(f) <= maxSubTreeSize).ToArray();
    401402          IFunction selectedFunction = RandomSelect(possibleFunctions);
    402           IFunctionTree newSubTree = selectedFunction.GetTreeNode();
    403           MakeUnbalancedTree(newSubTree, maxSubTreeSize - 1, maxTreeHeight - 1);
    404           parent.InsertSubTree(i, newSubTree);
    405         }
    406       }
     403          IFunctionTree newSubTree = MakeUnbalancedTree(selectedFunction, maxSubTreeSize - 1, maxTreeHeight - 1);
     404          parentTree.InsertSubTree(i, newSubTree);
     405        }
     406        return parentTree;
     407      }
     408      return parent.GetTreeNode();
    407409    }
    408410
    409411    // NOTE: this method doesn't build fully balanced trees because we have constraints on the
    410412    // types of possible sub-functions which can indirectly impose a limit for the depth of a given sub-tree
    411     private void MakeBalancedTree(IFunctionTree parent, int maxTreeSize, int maxTreeHeight) {
    412       if(maxTreeHeight == 0 || maxTreeSize == 0) return;
     413    private IFunctionTree MakeBalancedTree(IFunction parent, int maxTreeSize, int maxTreeHeight) {
     414      if(maxTreeHeight == 0 || maxTreeSize == 0) return parent.GetTreeNode();
    413415      int minArity;
    414416      int maxArity;
    415       GetMinMaxArity(parent.Function, out minArity, out maxArity);
     417      GetMinMaxArity(parent, out minArity, out maxArity);
    416418      if(maxArity >= maxTreeSize) {
    417419        maxArity = maxTreeSize;
     
    419421      int actualArity = random.Next(minArity, maxArity + 1);
    420422      if(actualArity > 0) {
     423        IFunctionTree parentTree = parent.GetTreeNode();
    421424        int maxSubTreeSize = maxTreeSize / actualArity;
    422425        for(int i = 0; i < actualArity; i++) {
    423426          // first try to find a function that fits into the maxHeight and maxSize limits
    424           IFunction[] possibleFunctions = GetAllowedSubFunctions(parent.Function, i).Where(
     427          IFunction[] possibleFunctions = GetAllowedSubFunctions(parent, i).Where(
    425428            f => GetMinimalTreeHeight(f) <= maxTreeHeight &&
    426429            GetMinimalTreeSize(f) <= maxSubTreeSize &&
     
    428431          // no possible function found => extend function set to terminals
    429432          if(possibleFunctions.Length == 0) {
    430             possibleFunctions = GetAllowedSubFunctions(parent.Function, i).Where(f => IsTerminal(f)).ToArray();
     433            possibleFunctions = GetAllowedSubFunctions(parent, i).Where(f => IsTerminal(f)).ToArray();
    431434            IFunction selectedTerminal = RandomSelect(possibleFunctions);
    432435            IFunctionTree newTree = selectedTerminal.GetTreeNode();
    433             parent.InsertSubTree(i, newTree);
     436            parentTree.InsertSubTree(i, newTree);
    434437          } else {
    435438            IFunction selectedFunction = RandomSelect(possibleFunctions);
    436             IFunctionTree newTree = selectedFunction.GetTreeNode();
    437             parent.InsertSubTree(i, newTree);
    438             MakeBalancedTree(newTree, maxSubTreeSize - 1, maxTreeHeight - 1);
     439            IFunctionTree newTree = MakeBalancedTree(selectedFunction, maxSubTreeSize - 1, maxTreeHeight - 1);
     440            parentTree.InsertSubTree(i, newTree);
    439441          }
    440442        }
    441       }
     443        return parentTree;
     444      }
     445      return parent.GetTreeNode();
    442446    }
    443447
Note: See TracChangeset for help on using the changeset viewer.