Free cookie consent management tool by TermsFeed Policy Generator

Changeset 6888 for trunk/sources


Ignore:
Timestamp:
10/07/11 21:25:02 (13 years ago)
Author:
bburlacu
Message:

#1654: Small adjustments to the Grow and Full tree creators. Added corresponding classes and unit tests.

Location:
trunk/sources
Files:
5 added
4 edited

Legend:

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

    r6887 r6888  
    5151
    5252    public IValueLookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionTreeGrammarParameter {
    53       get {
    54         return
    55             (IValueLookupParameter<ISymbolicExpressionGrammar>)
    56             Parameters[SymbolicExpressionTreeGrammarParameterName];
    57       }
     53      get { return (IValueLookupParameter<ISymbolicExpressionGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
    5854    }
    5955
    6056    public ILookupParameter<ISymbolicExpressionGrammar> ClonedSymbolicExpressionTreeGrammarParameter {
    61       get {
    62         return
    63             (ILookupParameter<ISymbolicExpressionGrammar>)
    64             Parameters[ClonedSymbolicExpressionTreeGrammarParameterName];
    65       }
     57      get { return (ILookupParameter<ISymbolicExpressionGrammar>)Parameters[ClonedSymbolicExpressionTreeGrammarParameterName]; }
    6658    }
    6759
     
    144136
    145137      int arity = seedNode.Grammar.GetMaximumSubtreeCount(seedNode.Symbol);
    146       if (arity <= 0) return; // should never happen
     138      // Throw an exception if the seedNode happens to be a terminal, since in this case we cannot grow a tree.
     139      if (arity <= 0)
     140        throw new ArgumentException("Cannot grow tree. Seed node shouldn't have arity zero.");
    147141
    148142      for (var i = 0; i != arity; ++i) {
     
    153147      }
    154148
    155       foreach (var subTree in seedNode.Subtrees) {
    156         RecursiveGrowFull(random, subTree, 0, maxDepth);
    157       }
     149      // Only iterate over the non-terminal nodes (those which have arity > 0)
     150      // 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))
     152        RecursiveGrowFull(random, subTree, 2, maxDepth);
    158153    }
    159154
    160155    public static void RecursiveGrowFull(IRandom random, ISymbolicExpressionTreeNode root, int currentDepth, int maxDepth) {
    161156      var arity = root.Grammar.GetMaximumSubtreeCount(root.Symbol);
    162       if (arity <= 0) return;
     157      // In the 'Full' grow method, we cannot have terminals on the intermediate tree levels.
     158      if (arity <= 0)
     159        throw new ArgumentException("Cannot grow node of arity zero. Expected a function node.");
    163160
    164161      var possibleSymbols = currentDepth < maxDepth ?
     
    173170      }
    174171
    175       foreach (var tree in root.Subtrees)
    176         RecursiveGrowFull(random, tree, currentDepth + 1, maxDepth);
     172      foreach (var subTree in root.Subtrees.Where(subTree => subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) != 0))
     173        RecursiveGrowFull(random, subTree, currentDepth + 1, maxDepth);
    177174    }
    178175  }
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/GrowTreeCreator.cs

    r6887 r6888  
    5555
    5656    public ILookupParameter<ISymbolicExpressionGrammar> ClonedSymbolicExpressionTreeGrammarParameter {
    57       get {
    58         return
    59             (ILookupParameter<ISymbolicExpressionGrammar>)
    60             Parameters[ClonedSymbolicExpressionTreeGrammarParameterName];
    61       }
     57      get { return (ILookupParameter<ISymbolicExpressionGrammar>)Parameters[ClonedSymbolicExpressionTreeGrammarParameterName]; }
    6258    }
    6359
     
    137133
    138134      var arity = SampleArity(random, seedNode);
    139       if (arity <= 0) return; // maybe throw an exception here? But this should never happen.
     135      // 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.");
    140138
    141139      var possibleSymbols = seedNode.Grammar.GetAllowedChildSymbols(seedNode.Symbol).Where(s => s.InitialFrequency > 0.0).ToList();
     
    148146      }
    149147
    150       foreach (var subTree in seedNode.Subtrees) {
    151         RecursiveGrow(random, subTree, 0, maxDepth);
    152       }
     148      // Only iterate over the non-terminal nodes (those which have arity > 0)
     149      // Start from depth 2 since the first two levels are formed by the rootNode and the seedNode
     150      foreach (var subTree in seedNode.Subtrees.Where(subTree => subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) != 0))
     151        RecursiveGrow(random, subTree, 2, maxDepth);
    153152    }
    154153
    155154    public static void RecursiveGrow(IRandom random, ISymbolicExpressionTreeNode root, int currentDepth, int maxDepth) {
    156155      var arity = SampleArity(random, root);
    157       if (arity <= 0) return;
     156      if (arity <= 0)
     157        throw new ArgumentException("Cannot grow node of arity zero. Expected a function node.");
    158158
    159159      var possibleSymbols = currentDepth < maxDepth ?
     
    168168      }
    169169
    170       foreach (var subTree in root.Subtrees) {
     170      foreach (var subTree in root.Subtrees.Where(subTree => subTree.Grammar.GetMaximumSubtreeCount(subTree.Symbol) != 0))
    171171        RecursiveGrow(random, subTree, currentDepth + 1, maxDepth);
    172       }
    173172    }
    174173
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj

    r6866 r6888  
    121121    <Compile Include="Analyzers\SymbolicDataAnalysisVariableFrequencyAnalyzer.cs" />
    122122    <Compile Include="Analyzers\SymbolicDataAnalysisAlleleFrequencyAnalyzer.cs" />
     123    <Compile Include="SymbolicDataAnalysisExpressionFullTreeCreator.cs" />
    123124    <Compile Include="Plugin.cs" />
     125    <Compile Include="SymbolicDataAnalysisExpressionGrowTreeCreator.cs" />
     126    <Compile Include="SymbolicDataAnalysisExpressionRampedHalfAndHalfTreeCreator.cs" />
    124127    <Compile Include="SymbolicDataAnalysisExpressionTreeILEmittingInterpreter.cs" />
    125128    <Compile Include="Formatters\SymbolicDataAnalysisExpressionLatexFormatter.cs" />
  • trunk/sources/HeuristicLab.Tests/HeuristicLab.Tests.csproj

    r6882 r6888  
    236236    <Compile Include="HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4\ArgumentDuplicaterTest.cs" />
    237237    <Compile Include="HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4\ChangeNodeTypeManipulationTest.cs" />
     238    <Compile Include="HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4\FullTreeCreatorTest.cs" />
    238239    <Compile Include="HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4\Grammars.cs" />
     240    <Compile Include="HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4\GrowTreeCreatorTest.cs" />
    239241    <Compile Include="HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4\ProbabilisticTreeCreaterTest.cs" />
    240242    <Compile Include="HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4\ReplaceBranchManipulationTest.cs" />
Note: See TracChangeset for help on using the changeset viewer.