Free cookie consent management tool by TermsFeed Policy Generator

Changeset 6944


Ignore:
Timestamp:
11/02/11 16:37:07 (12 years ago)
Author:
mkommend
Message:

#1654: Corrected bug in Full- and GrowTreeCreator.

Location:
trunk/sources
Files:
4 edited

Legend:

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

    r6888 r6944  
    2121
    2222using System;
    23 using System.Collections.Generic;
    24 using System.ComponentModel;
    2523using System.Linq;
    2624using HeuristicLab.Common;
     
    133131        throw new ArgumentException("Cannot create trees of depth " + maxDepth + " or smaller because of grammar constraints.", "maxDepth");
    134132
    135       var possibleSymbols = seedNode.Grammar.GetAllowedChildSymbols(seedNode.Symbol).Where(s => s.InitialFrequency > 0.0 && seedNode.Grammar.GetMaximumSubtreeCount(s) > 0).ToList();
    136133
    137134      int arity = seedNode.Grammar.GetMaximumSubtreeCount(seedNode.Symbol);
    138135      // Throw an exception if the seedNode happens to be a terminal, since in this case we cannot grow a tree.
    139136      if (arity <= 0)
    140         throw new ArgumentException("Cannot grow tree. Seed node shouldn't have arity zero."); 
     137        throw new ArgumentException("Cannot grow tree. Seed node shouldn't have arity zero.");
    141138
    142139      for (var i = 0; i != arity; ++i) {
     140        var possibleSymbols = seedNode.Grammar.GetAllowedChildSymbols(seedNode.Symbol,i).Where(s => s.InitialFrequency > 0.0 && seedNode.Grammar.GetMaximumSubtreeCount(s) > 0);
    143141        var selectedSymbol = possibleSymbols.SelectRandom(random);
    144142        var tree = selectedSymbol.CreateTreeNode();
     
    149147      // Only iterate over the non-terminal nodes (those which have arity > 0)
    150148      // Start from depth 2 since the first two levels are formed by the rootNode and the seedNode
    151       foreach (var subTree in seedNode.Subtrees.Where(subTree => subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) != 0)) 
     149      foreach (var subTree in seedNode.Subtrees.Where(subTree => subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) != 0))
    152150        RecursiveGrowFull(random, subTree, 2, maxDepth);
    153151    }
     
    159157        throw new ArgumentException("Cannot grow node of arity zero. Expected a function node.");
    160158
    161       var possibleSymbols = currentDepth < maxDepth ?
    162         root.Grammar.GetAllowedChildSymbols(root.Symbol).Where(s => s.InitialFrequency > 0.0 && root.Grammar.GetMaximumSubtreeCount(s) > 0).ToList() :
    163         root.Grammar.GetAllowedChildSymbols(root.Symbol).Where(s => s.InitialFrequency > 0.0 && root.Grammar.GetMaximumSubtreeCount(s) == 0).ToList();
    164159
    165160      for (var i = 0; i != arity; ++i) {
     161        var possibleSymbols = currentDepth < maxDepth ?
     162          root.Grammar.GetAllowedChildSymbols(root.Symbol,i).Where(s => s.InitialFrequency > 0.0 && root.Grammar.GetMaximumSubtreeCount(s) > 0) :
     163          root.Grammar.GetAllowedChildSymbols(root.Symbol,i).Where(s => s.InitialFrequency > 0.0 && root.Grammar.GetMaximumSubtreeCount(s) == 0);
    166164        var selectedSymbol = possibleSymbols.SelectRandom(random);
    167165        var tree = selectedSymbol.CreateTreeNode();
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/GrowTreeCreator.cs

    r6888 r6944  
    2121
    2222using System;
    23 using System.Collections.Generic;
    24 using System.ComponentModel;
    2523using System.Linq;
    2624using HeuristicLab.Common;
     
    134132      var arity = SampleArity(random, seedNode);
    135133      // throw an exception if the seedNode happens to be a terminal, since in this case we cannot grow a tree
    136       if (arity <= 0) 
    137         throw new ArgumentException("Cannot grow tree. Seed node shouldn't have arity zero."); 
     134      if (arity <= 0)
     135        throw new ArgumentException("Cannot grow tree. Seed node shouldn't have arity zero.");
    138136
    139       var possibleSymbols = seedNode.Grammar.GetAllowedChildSymbols(seedNode.Symbol).Where(s => s.InitialFrequency > 0.0).ToList();
    140137
    141138      for (var i = 0; i != arity; ++i) {
     139        var possibleSymbols = seedNode.Grammar.GetAllowedChildSymbols(seedNode.Symbol, i).Where(s => s.InitialFrequency > 0.0);
    142140        var selectedSymbol = possibleSymbols.SelectRandom(random);
    143141        var tree = selectedSymbol.CreateTreeNode();
     
    157155        throw new ArgumentException("Cannot grow node of arity zero. Expected a function node.");
    158156
    159       var possibleSymbols = currentDepth < maxDepth ?
    160         root.Grammar.GetAllowedChildSymbols(root.Symbol).Where(s => s.InitialFrequency > 0.0).ToList() :
    161         root.Grammar.GetAllowedChildSymbols(root.Symbol).Where(s => s.InitialFrequency > 0.0 && root.Grammar.GetMaximumSubtreeCount(s) == 0).ToList();
    162157
    163158      for (var i = 0; i != arity; ++i) {
     159        var possibleSymbols = currentDepth < maxDepth
     160                                ? root.Grammar.GetAllowedChildSymbols(root.Symbol,i).Where(s => s.InitialFrequency > 0.0)
     161                                : root.Grammar.GetAllowedChildSymbols(root.Symbol,i).Where(
     162                                  s => s.InitialFrequency > 0.0 && root.Grammar.GetMaximumSubtreeCount(s) == 0);
    164163        var selectedSymbol = possibleSymbols.SelectRandom(random);
    165164        var tree = selectedSymbol.CreateTreeNode();
  • trunk/sources/HeuristicLab.Tests/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4/FullTreeCreatorTest.cs

    r6888 r6944  
    7272        Util.GetTerminalDistributionString(randomTrees) + Environment.NewLine
    7373        );
    74       Assert.IsTrue(Math.Round(1000.0 / (msPerRandomTreeCreation)) > 300); // must achieve more than 500 random trees / s
     74      Assert.IsTrue(Math.Round(1000.0 / (msPerRandomTreeCreation)) > 300); // must achieve more than 300 random trees / s
    7575    }
    7676  }
  • trunk/sources/HeuristicLab.Tests/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4/GrowTreeCreatorTest.cs

    r6888 r6944  
    7171        Util.GetTerminalDistributionString(randomTrees) + Environment.NewLine
    7272        );
    73       Assert.IsTrue(Math.Round(1000.0 / (msPerRandomTreeCreation)) > 300); // must achieve more than 500 random trees / s
     73      Assert.IsTrue(Math.Round(1000.0 / (msPerRandomTreeCreation)) > 300); // must achieve more than 300 random trees / s
    7474    }
    7575  }
Note: See TracChangeset for help on using the changeset viewer.