Changeset 7108


Ignore:
Timestamp:
12/01/11 16:53:12 (8 years ago)
Author:
mkommend
Message:

#1654: Added performance improvements for Full- and GrowTreeCreator.

Location:
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/FullTreeCreator.cs

    r7076 r7108  
    148148        throw new ArgumentException("Cannot grow tree. Seed node shouldn't have arity zero.");
    149149
     150      var allowedSymbols = seedNode.Grammar.AllowedSymbols.Where(s => s.InitialFrequency > 0.0 && seedNode.Grammar.GetMaximumSubtreeCount(s) > 0).ToList();
     151
    150152      for (var i = 0; i != arity; ++i) {
    151         var possibleSymbols = seedNode.Grammar.GetAllowedChildSymbols(seedNode.Symbol, i).Where(s => s.InitialFrequency > 0.0 && seedNode.Grammar.GetMaximumSubtreeCount(s) > 0);
     153        var possibleSymbols = allowedSymbols.Where(s => seedNode.Grammar.IsAllowedChildSymbol(seedNode.Symbol, s, i)).ToList();
    152154        var selectedSymbol = possibleSymbols.SelectRandom(random);
    153155        var tree = selectedSymbol.CreateTreeNode();
     
    158160      // Only iterate over the non-terminal nodes (those which have arity > 0)
    159161      // Start from depth 2 since the first two levels are formed by the rootNode and the seedNode
    160       foreach (var subTree in seedNode.Subtrees.Where(subTree => subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) != 0))
     162      foreach (var subTree in seedNode.Subtrees.Where(subTree => subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) > 0))
    161163        RecursiveGrowFull(random, subTree, 2, maxDepth);
    162164    }
     
    168170        throw new ArgumentException("Cannot grow node of arity zero. Expected a function node.");
    169171
     172      var allowedSymbols = root.Grammar.AllowedSymbols.Where(s => s.InitialFrequency > 0.0).ToList(); ;
     173
    170174      for (var i = 0; i != arity; ++i) {
    171         var possibleSymbols = root.Grammar.GetAllowedChildSymbols(root.Symbol, i);
    172         possibleSymbols = possibleSymbols.Where(s => s.InitialFrequency > 0.0 &&
    173                                           root.Grammar.GetMinimumExpressionDepth(s) - 1 <= maxDepth - currentDepth &&
    174                                           root.Grammar.GetMaximumExpressionDepth(s) > maxDepth - currentDepth);
     175        var possibleSymbols = allowedSymbols.Where(s => root.Grammar.IsAllowedChildSymbol(root.Symbol, s, i) &&
     176                                                   root.Grammar.GetMinimumExpressionDepth(s) - 1 <= maxDepth - currentDepth &&
     177                                                   root.Grammar.GetMaximumExpressionDepth(s) > maxDepth - currentDepth).ToList();
     178        if (!possibleSymbols.Any())
     179          throw new InvalidOperationException("No symbols are available for the tree.");
    175180
    176 
    177         if (!possibleSymbols.Any()) throw new InvalidOperationException("No symbols are available for the tree.");
    178181        var selectedSymbol = possibleSymbols.SelectRandom(random);
    179182        var tree = selectedSymbol.CreateTreeNode();
     
    182185      }
    183186
    184       foreach (var subTree in root.Subtrees.Where(subTree => subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) != 0))
     187      foreach (var subTree in root.Subtrees.Where(subTree => subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) > 0))
    185188        RecursiveGrowFull(random, subTree, currentDepth + 1, maxDepth);
    186189    }
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/GrowTreeCreator.cs

    r7076 r7108  
    146146        throw new ArgumentException("Cannot grow tree. Seed node shouldn't have arity zero.");
    147147
     148      var allowedSymbols = seedNode.Grammar.AllowedSymbols.Where(s => s.InitialFrequency > 0.0).ToList();
    148149
    149150      for (var i = 0; i != arity; ++i) {
    150         var possibleSymbols = seedNode.Grammar.GetAllowedChildSymbols(seedNode.Symbol, i).Where(s => s.InitialFrequency > 0.0);
     151        var possibleSymbols = allowedSymbols.Where(s => seedNode.Grammar.IsAllowedChildSymbol(seedNode.Symbol, s, i)).ToList();
    151152        var selectedSymbol = possibleSymbols.SelectRandom(random);
    152153        var tree = selectedSymbol.CreateTreeNode();
     
    157158      // Only iterate over the non-terminal nodes (those which have arity > 0)
    158159      // Start from depth 2 since the first two levels are formed by the rootNode and the seedNode
    159       foreach (var subTree in seedNode.Subtrees.Where(subTree => subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) != 0))
     160      foreach (var subTree in seedNode.Subtrees.Where(subTree => subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) > 0))
    160161        RecursiveGrow(random, subTree, 2, maxDepth);
    161162    }
     
    166167        throw new ArgumentException("Cannot grow node of arity zero. Expected a function node.");
    167168
     169      var allowedSymbols = root.Grammar.AllowedSymbols.Where(s => s.InitialFrequency > 0.0).ToList();
     170
    168171      for (var i = 0; i != arity; ++i) {
    169         var possibleSymbols = root.Grammar.GetAllowedChildSymbols(root.Symbol, i);
    170         possibleSymbols = possibleSymbols.Where(s => s.InitialFrequency > 0.0 &&
    171                                           root.Grammar.GetMinimumExpressionDepth(s) - 1 <= maxDepth - currentDepth);
    172         if (!possibleSymbols.Any()) throw new InvalidOperationException("No symbols are available for the tree.");
     172        var possibleSymbols = allowedSymbols.Where(s => root.Grammar.IsAllowedChildSymbol(root.Symbol, s, i) && root.Grammar.GetMinimumExpressionDepth(s) - 1 <= maxDepth - currentDepth).ToList();
     173        if (!possibleSymbols.Any())
     174          throw new InvalidOperationException("No symbols are available for the tree.");
    173175        var selectedSymbol = possibleSymbols.SelectRandom(random);
    174176        var tree = selectedSymbol.CreateTreeNode();
Note: See TracChangeset for help on using the changeset viewer.