Changeset 3360 for trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/SubroutineDuplicater.cs
- Timestamp:
- 04/15/10 19:58:42 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/SubroutineDuplicater.cs
r3338 r3360 48 48 IntValue maxFunctionDefiningBranches, IntValue maxFunctionArguments, 49 49 out bool success) { 50 success = Duplicate RandomSubroutine(random, symbolicExpressionTree, grammar, maxTreeSize.Value, maxTreeHeight.Value, maxFunctionDefiningBranches.Value, maxFunctionArguments.Value);50 success = DuplicateSubroutine(random, symbolicExpressionTree, grammar, maxTreeSize.Value, maxTreeHeight.Value, maxFunctionDefiningBranches.Value, maxFunctionArguments.Value); 51 51 } 52 52 53 public static bool Duplicate RandomSubroutine(53 public static bool DuplicateSubroutine( 54 54 IRandom random, 55 55 SymbolicExpressionTree symbolicExpressionTree, … … 66 66 return false; 67 67 var selectedBranch = functionDefiningBranches.SelectRandom(random); 68 var clonedBranch = (DefunTreeNode)selectedBranch.Clone(); 69 clonedBranch.Name = allowedFunctionNames.Except(UsedFunctionNames(symbolicExpressionTree)).First(); 70 foreach (var node in symbolicExpressionTree.IterateNodesPrefix()) { 71 var invokeFunctionNode = node as InvokeFunctionTreeNode; 72 // find all invokations of the old function 73 if (invokeFunctionNode != null && invokeFunctionNode.InvokedFunctionName == selectedBranch.Name) { 74 // add the new function name to the list of known functions in the branches that used the originating function 75 var branch = symbolicExpressionTree.GetTopLevelBranchOf(invokeFunctionNode); 76 branch.AddDynamicSymbol(clonedBranch.Name, clonedBranch.NumberOfArguments); 77 // flip coin wether to replace with newly defined function 78 if (random.NextDouble() < 0.5) { 79 invokeFunctionNode.InvokedFunctionName = clonedBranch.Name; 80 } 68 var duplicatedDefunBranch = (DefunTreeNode)selectedBranch.Clone(); 69 string newFunctionName = allowedFunctionNames.Except(UsedFunctionNames(symbolicExpressionTree)).First(); 70 duplicatedDefunBranch.FunctionName = newFunctionName; 71 symbolicExpressionTree.Root.SubTrees.Add(duplicatedDefunBranch); 72 duplicatedDefunBranch.Grammar = (ISymbolicExpressionGrammar)selectedBranch.Grammar.Clone(); 73 // add an invoke symbol for each branch that is allowed to invoke the original function 74 foreach (var subtree in symbolicExpressionTree.Root.SubTrees) { 75 var matchingInvokeSymbol = (from symb in subtree.Grammar.Symbols.OfType<InvokeFunction>() 76 where symb.FunctionName == selectedBranch.FunctionName 77 select symb).SingleOrDefault(); 78 if (matchingInvokeSymbol != null) { 79 GrammarModifier.AddDynamicSymbol(subtree.Grammar, subtree.Symbol, duplicatedDefunBranch.FunctionName, duplicatedDefunBranch.NumberOfArguments); 81 80 } 82 81 } 83 Debug.Assert(grammar.IsValidExpression(symbolicExpressionTree)); 82 // for all invoke nodes of the original function replace the invoke of the original function with an invoke of the new function randomly 83 var originalFunctionInvocations = from node in symbolicExpressionTree.IterateNodesPrefix().OfType<InvokeFunctionTreeNode>() 84 where node.Symbol.FunctionName == selectedBranch.FunctionName 85 select node; 86 foreach (var originalFunctionInvokeNode in originalFunctionInvocations) { 87 var newInvokeSymbol = (from symb in originalFunctionInvokeNode.Grammar.Symbols.OfType<InvokeFunction>() 88 where symb.FunctionName == duplicatedDefunBranch.FunctionName 89 select symb).Single(); 90 // flip coin wether to replace with newly defined function 91 if (random.NextDouble() < 0.5) { 92 originalFunctionInvokeNode.Symbol = newInvokeSymbol; 93 } 94 } 84 95 return true; 85 }86 87 private static bool ContainsNode(SymbolicExpressionTreeNode branch, SymbolicExpressionTreeNode node) {88 if (branch == node) return true;89 else foreach (var subtree in branch.SubTrees) {90 if (ContainsNode(subtree, node)) return true;91 }92 return false;93 96 } 94 97 … … 96 99 return from node in symbolicExpressionTree.IterateNodesPrefix() 97 100 where node.Symbol is Defun 98 select ((DefunTreeNode)node). Name;101 select ((DefunTreeNode)node).FunctionName; 99 102 } 100 101 102 103 } 103 104 }
Note: See TracChangeset
for help on using the changeset viewer.