Changeset 3338 for trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Crossovers
- Timestamp:
- 04/13/10 20:44:31 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Crossovers/SubtreeCrossover.cs
r3297 r3338 49 49 } 50 50 51 protected override SymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionGrammar grammar,51 protected override SymbolicExpressionTree Cross(IRandom random, 52 52 SymbolicExpressionTree parent0, SymbolicExpressionTree parent1, 53 53 IntValue maxTreeSize, IntValue maxTreeHeight, out bool success) { 54 return Cross(random, grammar,parent0, parent1, InternalCrossoverPointProbabilityParameter.ActualValue.Value, maxTreeSize.Value, maxTreeHeight.Value, out success);54 return Cross(random, parent0, parent1, InternalCrossoverPointProbabilityParameter.ActualValue.Value, maxTreeSize.Value, maxTreeHeight.Value, out success); 55 55 } 56 56 57 public static SymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionGrammar grammar,57 public static SymbolicExpressionTree Cross(IRandom random, 58 58 SymbolicExpressionTree parent0, SymbolicExpressionTree parent1, 59 59 double internalCrossoverPointProbability, int maxTreeSize, int maxTreeHeight, out bool success) { … … 67 67 int maxInsertedBranchHeight = maxTreeHeight - GetBranchLevel(parent0.Root, crossoverPoint0); 68 68 69 var allowedBranches = from branch in IterateNodes(parent1.Root)69 var allowedBranches = from branch in parent1.Root.IterateNodesPrefix() 70 70 where branch.GetSize() < maxInsertedBranchSize 71 71 where branch.GetHeight() < maxInsertedBranchHeight 72 where grammar.GetAllowedSymbols(crossoverPoint0.Symbol,replacedSubtreeIndex).Contains(branch.Symbol)73 where IsMatchingPointType( parent0, crossoverPoint0, branch)72 // where crossoverPoint0.GetAllowedSymbols(replacedSubtreeIndex).Contains(branch.Symbol) 73 where IsMatchingPointType(crossoverPoint0, replacedSubtreeIndex, branch) 74 74 select branch; 75 75 … … 89 89 } 90 90 91 private static bool IsMatchingPointType(SymbolicExpressionTree tree, SymbolicExpressionTreeNode parent, SymbolicExpressionTreeNode newBranch) { 92 var functionCalls = (from node in IterateNodes(newBranch) 93 let invokeNode = node as InvokeFunctionTreeNode 94 let argNode = node as ArgumentTreeNode 95 where invokeNode != null || argNode != null 96 let name = invokeNode != null ? invokeNode.InvokedFunctionName : "ARG" + argNode.ArgumentIndex 97 let argCount = invokeNode != null ? invokeNode.SubTrees.Count : 0 98 select new { FunctionName = name, FunctionArgumentCount = argCount }).Distinct(); 99 var definingBranch = GetDefiningBranch(tree, parent); 100 if (definingBranch == null) return false; 101 foreach (var functionCall in functionCalls) { 102 if (!definingBranch.DynamicSymbols.Contains(functionCall.FunctionName) || 103 definingBranch.GetDynamicSymbolArgumentCount(functionCall.FunctionName) != functionCall.FunctionArgumentCount) 104 return false; 91 private static bool IsMatchingPointType(SymbolicExpressionTreeNode parent, int replacedSubtreeIndex, SymbolicExpressionTreeNode branch) { 92 // cannot compare dynamic symbols by reference because new instances of Invoke/Argument are created on demand 93 // instead we must check equality by names and number of arguments 94 var branchSymbols = (from node in branch.IterateNodesPrefix() 95 select new { FunctionName = node.Symbol.Name, SubtreeCount = node.SubTrees.Count }).Distinct(); 96 97 // check syntax constraints of direct parent - child relation 98 var matchingSymbolInParentGrammar = (from sym in parent.GetAllowedSymbols(replacedSubtreeIndex) 99 where sym.Name == branch.Symbol.Name 100 select sym).SingleOrDefault(); 101 if (matchingSymbolInParentGrammar == null || 102 branch.SubTrees.Count < parent.Grammar.GetMinSubtreeCount(matchingSymbolInParentGrammar) || 103 branch.SubTrees.Count > parent.Grammar.GetMaxSubtreeCount(matchingSymbolInParentGrammar)) return false; 104 105 // check point type for the whole branch 106 foreach (var branchSymbol in branchSymbols) { 107 matchingSymbolInParentGrammar = (from sym in parent.Grammar.Symbols 108 where sym.Name == branchSymbol.FunctionName 109 select sym).SingleOrDefault(); 110 if (matchingSymbolInParentGrammar == null || 111 branchSymbol.SubtreeCount < parent.Grammar.GetMinSubtreeCount(matchingSymbolInParentGrammar) || 112 branchSymbol.SubtreeCount > parent.Grammar.GetMaxSubtreeCount(matchingSymbolInParentGrammar)) return false; 105 113 } 114 106 115 return true; 107 116 } 108 117 109 private static SymbolicExpressionTreeNode GetDefiningBranch(SymbolicExpressionTree tree, SymbolicExpressionTreeNode parent) {110 foreach (var treeNode in tree.Root.SubTrees) {111 if (IterateNodes(treeNode).Contains(parent)) return treeNode;112 }113 return null;114 }115 116 118 private static void SelectCrossoverPoint(IRandom random, SymbolicExpressionTree parent0, double internalNodeProbability, out SymbolicExpressionTreeNode crossoverPoint, out int subtreeIndex) { 117 var crossoverPoints = from branch in IterateNodes(parent0.Root)119 var crossoverPoints = from branch in parent0.Root.IterateNodesPrefix() 118 120 where branch.SubTrees.Count > 0 119 where !(branch.Symbol is ProgramRootSymbol)121 where branch != parent0.Root 120 122 from index in Enumerable.Range(0, branch.SubTrees.Count) 121 123 let p = new { CrossoverPoint = branch, SubtreeIndex = index, IsLeaf = branch.SubTrees[index].SubTrees.Count == 0 } … … 172 174 } 173 175 174 private static IEnumerable<SymbolicExpressionTreeNode> IterateNodes(SymbolicExpressionTreeNode root) {175 foreach (var subTree in root.SubTrees) {176 foreach (var branch in IterateNodes(subTree)) {177 yield return branch;178 }179 }180 yield return root;181 }182 183 176 private static int GetBranchLevel(SymbolicExpressionTreeNode root, SymbolicExpressionTreeNode point) { 184 177 if (root == point) return 0;
Note: See TracChangeset
for help on using the changeset viewer.