Index: /branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/ReplaceBranchManipulation.cs
===================================================================
--- /branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/ReplaceBranchManipulation.cs	(revision 5568)
+++ /branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/ReplaceBranchManipulation.cs	(revision 5569)
@@ -71,19 +71,33 @@
     public static void ReplaceRandomBranch(IRandom random, ISymbolicExpressionTree symbolicExpressionTree, int maxTreeLength, int maxTreeDepth) {
       // select any node as parent (except the root node)
-      var manipulationPoint = (from parent in symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1)
-                               from subtree in parent.SubTrees
-                               select new { Parent = parent, Node = subtree, Index = parent.IndexOfSubTree(subtree) })
-                               .SelectRandom(random);
+      var manipulationPoints = (from parent in symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1)
+                                from subtree in parent.SubTrees
+                                let subtreeIndex = parent.IndexOfSubTree(subtree)
+                                let maxLength = maxTreeLength - symbolicExpressionTree.Length + subtree.GetLength()
+                                let maxDepth = maxTreeDepth - symbolicExpressionTree.Depth + subtree.GetDepth()
+                                // find possible symbols for the node (also considering the existing branches below it)
+                                let allowedSymbols = (from symbol in parent.Grammar.GetAllowedSymbols(parent.Symbol, subtreeIndex)
+                                                      // do not replace symbol with the same symbol
+                                                      where symbol.Name != subtree.Symbol.Name
+                                                      where parent.Grammar.GetMinExpressionDepth(symbol) <= maxDepth
+                                                      where parent.Grammar.GetMinExpressionLength(symbol) <= maxLength
+                                                      select symbol)
+                                                      .ToList()
+                                where allowedSymbols.Count > 0
+                                select new {
+                                  Parent = parent,
+                                  Child = subtree,
+                                  Index = subtreeIndex,
+                                  AllowedSymbols = allowedSymbols,
+                                  MaxLength = maxLength,
+                                  MaxDepth = maxDepth
+                                })
+                               .ToList();
 
-      int maxLength = maxTreeLength - symbolicExpressionTree.Length + manipulationPoint.Node.GetLength();
-      int maxDepth = maxTreeDepth - symbolicExpressionTree.Depth + manipulationPoint.Node.GetDepth();
-      // find possible symbols for the node (also considering the existing branches below it)
-      var allowedSymbols = (from symbol in manipulationPoint.Parent.Grammar.GetAllowedSymbols(manipulationPoint.Parent.Symbol, manipulationPoint.Index)
-                            where manipulationPoint.Node.Grammar.GetMinExpressionDepth(symbol) <= maxDepth
-                            where manipulationPoint.Node.Grammar.GetMinExpressionLength(symbol) <= maxLength
-                            select symbol).ToList();
-      if (allowedSymbols.Count() == 0) return;
-      var weights = allowedSymbols.Select(s => s.InitialFrequency).ToList();
-      var seedSymbol = allowedSymbols.SelectRandom(weights, random);
+      if (manipulationPoints.Count == 0) return;
+      var selectedManipulationPoint = manipulationPoints.SelectRandom(random);
+
+      var weights = selectedManipulationPoint.AllowedSymbols.Select(s => s.InitialFrequency).ToList();
+      var seedSymbol = selectedManipulationPoint.AllowedSymbols.SelectRandom(weights, random);
       // replace the old node with the new node
       var seedNode = seedSymbol.CreateTreeNode();
@@ -91,7 +105,7 @@
         seedNode.ResetLocalParameters(random);
 
-      manipulationPoint.Parent.RemoveSubTree(manipulationPoint.Index);
-      manipulationPoint.Parent.InsertSubTree(manipulationPoint.Index, seedNode);
-      seedNode = ProbabilisticTreeCreator.PTC2(random, seedNode, maxLength, maxDepth, 0, 0);
+      selectedManipulationPoint.Parent.RemoveSubTree(selectedManipulationPoint.Index);
+      selectedManipulationPoint.Parent.InsertSubTree(selectedManipulationPoint.Index, seedNode);
+      seedNode = ProbabilisticTreeCreator.PTC2(random, seedNode, selectedManipulationPoint.MaxLength, selectedManipulationPoint.MaxDepth, 0, 0);
     }
   }
