Changeset 12347


Ignore:
Timestamp:
04/25/15 00:53:07 (4 years ago)
Author:
bburlacu
Message:

#2320: Bugfix to allow nodes that can appear both internally or as leafs

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/SymbolicExpressionTreeEncoding/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/GrowTreeCreator.cs

    r12313 r12347  
    8989        throw new ArgumentException("Cannot grow tree. Seed node shouldn't have arity zero.");
    9090
    91       var allowedSymbols = seedNode.Grammar.AllowedSymbols
    92         .Where(s => s.InitialFrequency > 0.0)
    93         .ToList();
     91      var allowedSymbols = seedNode.Grammar.AllowedSymbols.Where(s => s.InitialFrequency > 0.0).ToList();
    9492
    9593      for (var i = 0; i < arity; i++) {
    96         var possibleSymbols = allowedSymbols
    97           .Where(s => seedNode.Grammar.IsAllowedChildSymbol(seedNode.Symbol, s, i))
    98           .ToList();
     94        var possibleSymbols = allowedSymbols.Where(s => seedNode.Grammar.IsAllowedChildSymbol(seedNode.Symbol, s, i)).ToList();
    9995        var weights = possibleSymbols.Select(s => s.InitialFrequency).ToList();
    10096        var selectedSymbol = possibleSymbols.SelectRandom(weights, random);
     
    113109    private static void RecursiveCreate(IRandom random, ISymbolicExpressionTreeNode root, int currentDepth, int maxDepth) {
    114110      var arity = SampleArity(random, root);
    115       if (arity <= 0)
    116         throw new ArgumentException("Cannot grow node of arity zero. Expected a function node.");
     111      if (arity == 0)
     112        return;
    117113
    118114      var allowedSymbols = root.Grammar.AllowedSymbols.Where(s => s.InitialFrequency > 0.0).ToList();
    119115
    120116      for (var i = 0; i < arity; i++) {
    121         var possibleSymbols = allowedSymbols
    122           .Where(s => root.Grammar.IsAllowedChildSymbol(root.Symbol, s, i) &&
    123             root.Grammar.GetMinimumExpressionDepth(s) - 1 <= maxDepth - currentDepth)
    124           .ToList();
     117        var possibleSymbols = allowedSymbols.Where(s => root.Grammar.IsAllowedChildSymbol(root.Symbol, s, i) &&
     118                                                        root.Grammar.GetMinimumExpressionDepth(s) - 1 <= maxDepth - currentDepth).ToList();
    125119
    126120        if (!possibleSymbols.Any())
    127121          throw new InvalidOperationException("No symbols are available for the tree.");
     122
    128123        var weights = possibleSymbols.Select(s => s.InitialFrequency).ToList();
    129124        var selectedSymbol = possibleSymbols.SelectRandom(weights, random);
     
    133128      }
    134129
    135       foreach (var subTree in root.Subtrees)
    136         if (subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) != 0)
    137           RecursiveCreate(random, subTree, currentDepth + 1, maxDepth);
     130      if (maxDepth > currentDepth)
     131        foreach (var subTree in root.Subtrees)
     132          if (subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) != 0)
     133            RecursiveCreate(random, subTree, currentDepth + 1, maxDepth);
    138134    }
    139135
Note: See TracChangeset for help on using the changeset viewer.