Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/16/10 12:12:29 (15 years ago)
Author:
gkronber
Message:

Changed way the grammar is stored in tree nodes to make it more efficient and fixed bugs in symbolic expression tree operators. #290 (Implement ADFs)

Location:
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators
Files:
2 edited

Legend:

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

    r3360 r3369  
    6868
    6969      // remove references to deleted function
    70       foreach (var subtree in symbolicExpressionTree.Root.SubTrees) {
     70      foreach (var subtree in symbolicExpressionTree.Root.SubTrees.OfType<SymbolicExpressionTreeTopLevelNode>()) {
    7171        var matchingInvokeSymbol = (from symb in subtree.Grammar.Symbols.OfType<InvokeFunction>()
    7272                                    where symb.FunctionName == selectedDefunBranch.FunctionName
     
    9999        int minPossibleHeight = invocationCutPoint.Parent.Grammar.GetMinExpressionDepth(selectedSymbol);
    100100        int maxHeight = Math.Max(minPossibleHeight, invocationCutPoint.ReplacedChild.GetHeight());
    101 
    102         replacementTree = ProbabilisticTreeCreator.PTC2(random, invocationCutPoint.Parent.Grammar, selectedSymbol, maxSize, maxHeight, 0, 0);
     101        replacementTree = selectedSymbol.CreateTreeNode();
    103102        invocationCutPoint.Parent.RemoveSubTree(invocationCutPoint.ReplacedChildIndex);
    104103        invocationCutPoint.Parent.InsertSubTree(invocationCutPoint.ReplacedChildIndex, replacementTree);
     104
     105        ProbabilisticTreeCreator.PTC2(random, replacementTree, maxSize, maxHeight, 0, 0);
    105106
    106107        invocationCutPoint = (from node in symbolicExpressionTree.IterateNodesPrefix()
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/SubroutineDuplicater.cs

    r3360 r3369  
    7272      duplicatedDefunBranch.Grammar = (ISymbolicExpressionGrammar)selectedBranch.Grammar.Clone();
    7373      // add an invoke symbol for each branch that is allowed to invoke the original function
    74       foreach (var subtree in symbolicExpressionTree.Root.SubTrees) {
     74      foreach (var subtree in symbolicExpressionTree.Root.SubTrees.OfType<SymbolicExpressionTreeTopLevelNode>()) {
    7575        var matchingInvokeSymbol = (from symb in subtree.Grammar.Symbols.OfType<InvokeFunction>()
    7676                                    where symb.FunctionName == selectedBranch.FunctionName
     
    7979          GrammarModifier.AddDynamicSymbol(subtree.Grammar, subtree.Symbol, duplicatedDefunBranch.FunctionName, duplicatedDefunBranch.NumberOfArguments);
    8080        }
    81       }
    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;
     81        // in the current subtree:
     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 subtree.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 subtree.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          }
    9394        }
    9495      }
Note: See TracChangeset for help on using the changeset viewer.