Free cookie consent management tool by TermsFeed Policy Generator

Changeset 5569


Ignore:
Timestamp:
02/28/11 17:36:15 (13 years ago)
Author:
gkronber
Message:

#1418 fixed implementation of ReplaceBranchManipulation operator to make unit test succeed

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/ReplaceBranchManipulation.cs

    r5549 r5569  
    7171    public static void ReplaceRandomBranch(IRandom random, ISymbolicExpressionTree symbolicExpressionTree, int maxTreeLength, int maxTreeDepth) {
    7272      // select any node as parent (except the root node)
    73       var manipulationPoint = (from parent in symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1)
    74                                from subtree in parent.SubTrees
    75                                select new { Parent = parent, Node = subtree, Index = parent.IndexOfSubTree(subtree) })
    76                                .SelectRandom(random);
     73      var manipulationPoints = (from parent in symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1)
     74                                from subtree in parent.SubTrees
     75                                let subtreeIndex = parent.IndexOfSubTree(subtree)
     76                                let maxLength = maxTreeLength - symbolicExpressionTree.Length + subtree.GetLength()
     77                                let maxDepth = maxTreeDepth - symbolicExpressionTree.Depth + subtree.GetDepth()
     78                                // find possible symbols for the node (also considering the existing branches below it)
     79                                let allowedSymbols = (from symbol in parent.Grammar.GetAllowedSymbols(parent.Symbol, subtreeIndex)
     80                                                      // do not replace symbol with the same symbol
     81                                                      where symbol.Name != subtree.Symbol.Name
     82                                                      where parent.Grammar.GetMinExpressionDepth(symbol) <= maxDepth
     83                                                      where parent.Grammar.GetMinExpressionLength(symbol) <= maxLength
     84                                                      select symbol)
     85                                                      .ToList()
     86                                where allowedSymbols.Count > 0
     87                                select new {
     88                                  Parent = parent,
     89                                  Child = subtree,
     90                                  Index = subtreeIndex,
     91                                  AllowedSymbols = allowedSymbols,
     92                                  MaxLength = maxLength,
     93                                  MaxDepth = maxDepth
     94                                })
     95                               .ToList();
    7796
    78       int maxLength = maxTreeLength - symbolicExpressionTree.Length + manipulationPoint.Node.GetLength();
    79       int maxDepth = maxTreeDepth - symbolicExpressionTree.Depth + manipulationPoint.Node.GetDepth();
    80       // find possible symbols for the node (also considering the existing branches below it)
    81       var allowedSymbols = (from symbol in manipulationPoint.Parent.Grammar.GetAllowedSymbols(manipulationPoint.Parent.Symbol, manipulationPoint.Index)
    82                             where manipulationPoint.Node.Grammar.GetMinExpressionDepth(symbol) <= maxDepth
    83                             where manipulationPoint.Node.Grammar.GetMinExpressionLength(symbol) <= maxLength
    84                             select symbol).ToList();
    85       if (allowedSymbols.Count() == 0) return;
    86       var weights = allowedSymbols.Select(s => s.InitialFrequency).ToList();
    87       var seedSymbol = allowedSymbols.SelectRandom(weights, random);
     97      if (manipulationPoints.Count == 0) return;
     98      var selectedManipulationPoint = manipulationPoints.SelectRandom(random);
     99
     100      var weights = selectedManipulationPoint.AllowedSymbols.Select(s => s.InitialFrequency).ToList();
     101      var seedSymbol = selectedManipulationPoint.AllowedSymbols.SelectRandom(weights, random);
    88102      // replace the old node with the new node
    89103      var seedNode = seedSymbol.CreateTreeNode();
     
    91105        seedNode.ResetLocalParameters(random);
    92106
    93       manipulationPoint.Parent.RemoveSubTree(manipulationPoint.Index);
    94       manipulationPoint.Parent.InsertSubTree(manipulationPoint.Index, seedNode);
    95       seedNode = ProbabilisticTreeCreator.PTC2(random, seedNode, maxLength, maxDepth, 0, 0);
     107      selectedManipulationPoint.Parent.RemoveSubTree(selectedManipulationPoint.Index);
     108      selectedManipulationPoint.Parent.InsertSubTree(selectedManipulationPoint.Index, seedNode);
     109      seedNode = ProbabilisticTreeCreator.PTC2(random, seedNode, selectedManipulationPoint.MaxLength, selectedManipulationPoint.MaxDepth, 0, 0);
    96110    }
    97111  }
Note: See TracChangeset for help on using the changeset viewer.