Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/15/10 19:58:42 (14 years ago)
Author:
gkronber
Message:

Fixed bugs in ADF operators and added test classes for ADF operators. #290 (Implement ADFs)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/SubroutineDuplicater.cs

    r3338 r3360  
    4848      IntValue maxFunctionDefiningBranches, IntValue maxFunctionArguments,
    4949      out bool success) {
    50       success = DuplicateRandomSubroutine(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);
    5151    }
    5252
    53     public static bool DuplicateRandomSubroutine(
     53    public static bool DuplicateSubroutine(
    5454      IRandom random,
    5555      SymbolicExpressionTree symbolicExpressionTree,
     
    6666        return false;
    6767      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);
    8180        }
    8281      }
    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      }
    8495      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;
    9396    }
    9497
     
    9699      return from node in symbolicExpressionTree.IterateNodesPrefix()
    97100             where node.Symbol is Defun
    98              select ((DefunTreeNode)node).Name;
     101             select ((DefunTreeNode)node).FunctionName;
    99102    }
    100 
    101 
    102103  }
    103104}
Note: See TracChangeset for help on using the changeset viewer.