Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/22/11 15:07:20 (14 years ago)
Author:
mkommend
Message:

#1418: Corrected ADFs and adapted unit tests.

Location:
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/ArgumentCreater.cs

    r5733 r5792  
    124124      // the branch at the cut point is to be replaced by a new argument node
    125125      var cutPoints = (from node in defunBranch.IterateNodesPrefix()
    126                        where node.Subtrees.Count() > 0
     126                       where node.Subtrees.Count() > 0 &&
     127                             !node.IterateNodesPrefix().OfType<ArgumentTreeNode>().Any() &&
     128                             !node.IterateNodesPrefix().OfType<InvokeFunctionTreeNode>().Any()
    127129                       from subtree in node.Subtrees
    128130                       select new CutPoint(node, subtree)).ToList();
     
    169171      defunBranch.Grammar.SetSubtreeCount(newArgumentNode.Symbol, 0, 0);
    170172      // allow the argument as child of any other symbol
    171       foreach (var symb in defunBranch.Grammar.Symbols)
    172         for (int i = 0; i < defunBranch.Grammar.GetMaximumSubtreeCount(symb); i++) {
    173           defunBranch.Grammar.AddAllowedChildSymbol(symb, newArgumentNode.Symbol, i);
    174         }
     173      GrammarModifier.SetAllowedParentSymbols(defunBranch.Grammar, selectedCutPoint.Child.Symbol, newArgumentNode.Symbol);
     174
    175175      foreach (var subtree in tree.Root.Subtrees) {
    176176        // when the changed function is known in the branch then update the number of arguments
     
    178178        if (matchingSymbol != null) {
    179179          subtree.Grammar.SetSubtreeCount(matchingSymbol, defunBranch.NumberOfArguments, defunBranch.NumberOfArguments);
    180           foreach (var child in subtree.Grammar.GetAllowedChildSymbols(subtree.Symbol, 0)) {
    181             for (int i = 0; i < subtree.Grammar.GetMaximumSubtreeCount(matchingSymbol); i++) {
    182               subtree.Grammar.AddAllowedChildSymbol(matchingSymbol, child, i);
    183             }
     180          foreach (var symb in subtree.Grammar.Symbols) {
     181            if (symb is StartSymbol || symb is ProgramRootSymbol) continue;
     182            if (subtree.Grammar.IsAllowedChildSymbol(selectedCutPoint.Parent.Symbol, symb, selectedCutPoint.ChildIndex))
     183              subtree.Grammar.AddAllowedChildSymbol(matchingSymbol, symb, newArgumentNode.Symbol.ArgumentIndex);
    184184          }
    185185        }
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/ArgumentDuplicater.cs

    r5733 r5792  
    108108      selectedDefunBranch.Grammar.AddSymbol(newArgSymbol);
    109109      selectedDefunBranch.Grammar.SetSubtreeCount(newArgSymbol, 0, 0);
    110       // allow the argument as child of any other symbol
    111       foreach (var symb in selectedDefunBranch.Grammar.Symbols)
    112         for (int i = 0; i < selectedDefunBranch.Grammar.GetMaximumSubtreeCount(symb); i++) {
    113           selectedDefunBranch.Grammar.AddAllowedChildSymbol(symb, newArgSymbol, i);
    114         }
     110      // allow the duplicated argument as child of all other arguments where the orginal argument was allowed
     111      GrammarModifier.SetAllowedParentSymbols(selectedDefunBranch.Grammar, selectedArgumentSymbol, newArgSymbol);
    115112      selectedDefunBranch.NumberOfArguments++;
    116113
     
    122119        if (matchingInvokeSymbol != null) {
    123120          subtree.Grammar.SetSubtreeCount(matchingInvokeSymbol, selectedDefunBranch.NumberOfArguments, selectedDefunBranch.NumberOfArguments);
    124           foreach (var child in subtree.Grammar.GetAllowedChildSymbols(subtree.Symbol, 0)) {
    125             for (int i = 0; i < subtree.Grammar.GetMaximumSubtreeCount(matchingInvokeSymbol); i++) {
    126               subtree.Grammar.AddAllowedChildSymbol(matchingInvokeSymbol, child, i);
    127             }
     121          foreach (var symb in subtree.Grammar.Symbols) {
     122            if (symb is StartSymbol || symb is ProgramRootSymbol) continue;
     123            if (subtree.Grammar.IsAllowedChildSymbol(matchingInvokeSymbol, symb, selectedArgumentSymbol.ArgumentIndex))
     124              subtree.Grammar.AddAllowedChildSymbol(matchingInvokeSymbol, symb, newArgumentIndex);
    128125          }
    129126        }
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/GrammarModifier.cs

    r5686 r5792  
    2626  public static class GrammarModifier {
    2727    internal static void AddInvokeSymbol(ISymbolicExpressionTreeGrammar grammar, string functionName, int nArgs, CutPoint startCutPoint, IEnumerable<CutPoint> argumentCutPoints) {
     28      if (!grammar.ContainsSymbol(startCutPoint.Child.Symbol)) return;
     29
    2830      var invokeSym = new InvokeFunction(functionName);
    2931      grammar.AddSymbol(invokeSym);
     
    3133
    3234      //allow invoke symbol everywhere, where the child of the startCutPoint was allowed
    33       foreach (ISymbol parent in grammar.Symbols) {
    34         if (grammar.IsAllowedChildSymbol(parent, startCutPoint.Child.Symbol))
    35           grammar.AddAllowedChildSymbol(parent, invokeSym);
    36         else {
    37           for (int i = 0; i < grammar.GetMaximumSubtreeCount(parent); i++) {
    38             if (grammar.IsAllowedChildSymbol(parent, startCutPoint.Child.Symbol, i))
    39               grammar.AddAllowedChildSymbol(parent, invokeSym, i);
    40           }
    41         }
    42       }
     35      SetAllowedParentSymbols(grammar, startCutPoint.Child.Symbol, invokeSym);
    4336
    4437      if (nArgs > 0) {
    4538        //set allowed child symbols of invoke symbol
    4639        foreach (ISymbol child in grammar.Symbols) {
    47           if (argumentCutPoints.All(x => grammar.IsAllowedChildSymbol(x.Parent.Symbol, child)))
    48             grammar.AddAllowedChildSymbol(invokeSym, child);
    49           else {
    50             int i = 0;
    51             foreach (CutPoint argumentCutPoint in argumentCutPoints) {
    52               if (grammar.IsAllowedChildSymbol(argumentCutPoint.Parent.Symbol, child, argumentCutPoint.ChildIndex))
    53                 grammar.AddAllowedChildSymbol(invokeSym, child, i);
    54               i++;
    55             }
     40          int i = 0;
     41          foreach (CutPoint argumentCutPoint in argumentCutPoints) {
     42            if (grammar.IsAllowedChildSymbol(argumentCutPoint.Parent.Symbol, child, argumentCutPoint.ChildIndex))
     43              grammar.AddAllowedChildSymbol(invokeSym, child, i);
     44            i++;
    5645          }
    5746        }
    5847      }
    5948    }
     49
     50
    6051
    6152    internal static void AddArgumentSymbol(ISymbolicExpressionTreeGrammar originalGrammar, ISymbolicExpressionTreeGrammar grammar, IEnumerable<int> argumentIndexes, IEnumerable<CutPoint> argumentCutPoints) {
     
    6556        grammar.SetSubtreeCount(argSymbol, 0, 0);
    6657
    67         foreach (ISymbol parent in originalGrammar.Symbols) {
    68           if (parent is StartSymbol || parent is ProgramRootSymbol) continue;
    69           if (originalGrammar.IsAllowedChildSymbol(parent, pair.CutPoint.Child.Symbol))
    70             grammar.AddAllowedChildSymbol(parent, argSymbol);
     58        foreach (var symb in grammar.Symbols) {
     59          if (symb is ProgramRootSymbol || symb is StartSymbol) continue;
     60          if (originalGrammar.IsAllowedChildSymbol(symb, pair.CutPoint.Child.Symbol))
     61            grammar.AddAllowedChildSymbol(symb, argSymbol);
    7162          else {
    72             for (int i = 0; i < originalGrammar.GetMaximumSubtreeCount(parent); i++) {
    73               if (originalGrammar.IsAllowedChildSymbol(parent, pair.CutPoint.Child.Symbol, i))
    74                 grammar.AddAllowedChildSymbol(parent, argSymbol, i);
     63            for (int i = 0; i < grammar.GetMaximumSubtreeCount(symb); i++) {
     64              if (originalGrammar.IsAllowedChildSymbol(symb, pair.CutPoint.Child.Symbol, i))
     65                grammar.AddAllowedChildSymbol(symb, argSymbol, i);
    7566            }
     67          }
     68        }
     69      }
     70    }
     71
     72    internal static void SetAllowedParentSymbols(ISymbolicExpressionTreeGrammar grammar, ISymbol symbol, ISymbol newSymbol) {
     73      foreach (var symb in grammar.Symbols) {
     74        if (symb is ProgramRootSymbol) continue;
     75        if (newSymbol is Argument && symb is StartSymbol) continue;
     76        if (grammar.IsAllowedChildSymbol(symb, symbol))
     77          grammar.AddAllowedChildSymbol(symb, newSymbol);
     78        else {
     79          for (int i = 0; i < grammar.GetMaximumSubtreeCount(symb); i++) {
     80            if (grammar.IsAllowedChildSymbol(symb, symbol, i))
     81              grammar.AddAllowedChildSymbol(symb, newSymbol, i);
    7682          }
    7783        }
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/SubroutineDuplicater.cs

    r5733 r5792  
    8989              for (int i = 0; i < subtree.Grammar.GetMaximumSubtreeCount(symbol); i++)
    9090                if (subtree.Grammar.IsAllowedChildSymbol(symbol, matchingInvokeSymbol, i))
    91                   subtree.Grammar.AddAllowedChildSymbol(symbol, matchingInvokeSymbol, i);
     91                  subtree.Grammar.AddAllowedChildSymbol(symbol, invokeSymbol, i);
    9292            }
    9393          }
Note: See TracChangeset for help on using the changeset viewer.