Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/30/10 15:26:36 (14 years ago)
Author:
gkronber
Message:

Improved LINQ statements in SubtreeCrossover to make them more efficient. #938

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Crossovers/SubtreeCrossover.cs

    r3462 r3989  
    6767      int maxInsertedBranchHeight = maxTreeHeight - GetBranchLevel(parent0.Root, crossoverPoint0);
    6868
    69       var allowedBranches = (from branch in parent1.Root.IterateNodesPrefix()
     69      var allowedBranches = (from branch in parent1.Root.IterateNodesPostfix()
    7070                             where branch.GetSize() < maxInsertedBranchSize
    7171                             where branch.GetHeight() < maxInsertedBranchHeight
     
    103103
    104104    private static void SelectCrossoverPoint(IRandom random, SymbolicExpressionTree parent0, double internalNodeProbability, int maxBranchSize, int maxBranchHeight, out SymbolicExpressionTreeNode crossoverPoint, out int subtreeIndex) {
    105       var crossoverPoints = from branch in parent0.Root.IterateNodesPrefix()
    106                             where branch.SubTrees.Count > 0
    107                             where branch != parent0.Root
    108                             where branch.GetSize() < maxBranchSize
    109                             where branch.GetHeight() < maxBranchHeight
    110                             from index in Enumerable.Range(0, branch.SubTrees.Count)
    111                             let p = new { CrossoverPoint = branch, SubtreeIndex = index, IsLeaf = branch.SubTrees[index].SubTrees.Count == 0 }
    112                             select p;
     105      var crossoverPoints = (from branch in parent0.Root.IterateNodesPostfix()
     106                             where branch.SubTrees.Count > 0
     107                             where branch != parent0.Root
     108                             where branch.GetSize() < maxBranchSize
     109                             where branch.GetHeight() < maxBranchHeight
     110                             from index in Enumerable.Range(0, branch.SubTrees.Count)
     111                             let p = new { CrossoverPoint = branch, SubtreeIndex = index, IsLeaf = branch.SubTrees[index].SubTrees.Count == 0 }
     112                             select p).ToList();
    113113      var internalCrossoverPoints = (from p in crossoverPoints
    114114                                     where !p.IsLeaf
     
    139139    private static SymbolicExpressionTreeNode SelectRandomBranch(IRandom random, IEnumerable<SymbolicExpressionTreeNode> branches, double internalNodeProbability) {
    140140      if (internalNodeProbability < 0.0 || internalNodeProbability > 1.0) throw new ArgumentException("internalNodeProbability");
    141       var groupedBranches = (from branch in branches
    142                              group branch by branch.SubTrees.Count into g
    143                              select g).ToList();
    144       var allowedInternalBranches = (from g in groupedBranches
    145                                      where g.Key > 0
    146                                      from branch in g
     141      var allowedInternalBranches = (from branch in branches
     142                                     where branch.SubTrees.Count > 0
    147143                                     select branch).ToList();
    148       var allowedLeafBranches = (from g in groupedBranches
    149                                  where g.Key == 0
    150                                  from leaf in g
    151                                  select leaf).ToList();
     144      var allowedLeafBranches = (from branch in branches
     145                                 where branch.SubTrees.Count == 0
     146                                 select branch).ToList();
    152147      if (allowedInternalBranches.Count == 0) {
    153148        return allowedLeafBranches.SelectRandom(random);
Note: See TracChangeset for help on using the changeset viewer.