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/Creators/ProbabilisticTreeCreator.cs

    r3338 r3360  
    2929using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.GeneralSymbols;
    3030using System.Text;
     31using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureAlteringOperators;
    3132
    3233namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
     
    5152      int maxFunctionDefinitions, int maxFunctionArguments
    5253      ) {
    53       // tree size is limited by the grammar and by the explicit size constraints
    54       int allowedMinSize = grammar.GetMinExpressionLength(grammar.StartSymbol);
    55       int allowedMaxSize = Math.Min(maxTreeSize, grammar.GetMaxExpressionLength(grammar.StartSymbol));
    56       // select a target tree size uniformly in the possible range (as determined by explicit limits and limits of the grammar)
    57       int treeSize = random.Next(allowedMinSize, allowedMaxSize);
    5854      SymbolicExpressionTree tree = new SymbolicExpressionTree();
    59       do {
    60         try {
    61           tree.Root = PTC2(random, grammar, grammar.StartSymbol, treeSize + 1, maxTreeHeight + 1, maxFunctionDefinitions, maxFunctionArguments);
    62         }
    63         catch (ArgumentException) {
    64           // try a different size
    65           treeSize = random.Next(allowedMinSize, allowedMaxSize);
    66         }
    67       } while (tree.Root == null || tree.Size > maxTreeSize || tree.Height > maxTreeHeight);
     55      tree.Root = PTC2(random, grammar, grammar.StartSymbol, maxTreeSize, maxTreeHeight, maxFunctionDefinitions, maxFunctionArguments);
    6856      return tree;
    6957    }
     
    7462      public int ExtensionPointDepth { get; set; }
    7563    }
     64
     65    /// <summary>
     66    /// Creates a random tree with <paramref name="maxTreeSize"/> and <paramref name="maxDepth"/>.
     67    /// </summary>
     68    /// <param name="random"></param>
     69    /// <param name="grammar"></param>
     70    /// <param name="rootSymbol"></param>
     71    /// <param name="maxTreeSize"></param>
     72    /// <param name="maxDepth"></param>
     73    /// <param name="maxFunctionDefinitions"></param>
     74    /// <param name="maxFunctionArguments"></param>
     75    /// <returns></returns>
    7676    public static SymbolicExpressionTreeNode PTC2(IRandom random, ISymbolicExpressionGrammar grammar, Symbol rootSymbol,
    77       int size, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) {
    78       SymbolicExpressionTreeNode root = rootSymbol.CreateTreeNode();
    79       root.Grammar = grammar;
    80       if (size <= 1 || maxDepth <= 1) return root;
    81       CreateFullTreeFromSeed(random, root, size, maxDepth, maxFunctionDefinitions, maxFunctionArguments);
     77      int maxTreeSize, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) {
     78      // tree size is limited by the grammar and by the explicit size constraints
     79      int allowedMinSize = grammar.GetMinExpressionLength(rootSymbol);
     80      int allowedMaxSize = Math.Min(maxTreeSize, grammar.GetMaxExpressionLength(rootSymbol));
     81      // select a target tree size uniformly in the possible range (as determined by explicit limits and limits of the grammar)
     82      int treeSize = random.Next(allowedMinSize, allowedMaxSize + 1);
     83      SymbolicExpressionTreeNode root = null;
     84      do {
     85        try {
     86          root = rootSymbol.CreateTreeNode();
     87          root.Grammar = grammar;
     88          if (treeSize <= 1 || maxDepth <= 1) return root;
     89          CreateFullTreeFromSeed(random, root, treeSize, maxDepth, maxFunctionDefinitions, maxFunctionArguments);
     90        }
     91        catch (ArgumentException) {
     92          // try a different size
     93          root = null;
     94          treeSize = random.Next(allowedMinSize, allowedMaxSize);
     95        }
     96      } while (root == null || root.GetSize() > maxTreeSize || root.GetHeight() > maxDepth);
    8297      return root;
    8398    }
     
    105120        if (extensionDepth + parent.Grammar.GetMinExpressionDepth(parent.Symbol) >= maxDepth) {
    106121          ReplaceWithMinimalTree(random, root, parent, argumentIndex, maxFunctionDefinitions, maxFunctionArguments);
    107           //parent.RemoveSubTree(argumentIndex);
    108           //var allowedSymbols = from s in parent.Grammar.Symbols
    109           //                     where parent.Grammar.IsAllowedChild(parent.Symbol, s, argumentIndex)
    110           //                     select s;
    111           //SymbolicExpressionTreeNode branch = CreateMinimalTree(random, parent, allowedSymbols);
    112           //parent.InsertSubTree(argumentIndex, branch); // insert a smallest possible tree
    113           //currentSize += branch.GetSize();
    114           //totalListMinSize -= branch.GetSize();
    115122        } else {
    116123          var allowedSymbols = from s in parent.Grammar.Symbols
     
    118125                               where parent.Grammar.GetMinExpressionDepth(s) + extensionDepth - 1 < maxDepth
    119126                               where parent.Grammar.GetMaxExpressionLength(s) > size - totalListMinSize - currentSize
    120                                /*||
    121                                      totalListMinSize + currentSize >= size * 0.9 // if the necessary size is almost reached then also allow
    122                                // terminals or terminal-branches*/
    123                                // where !IsDynamicSymbol(s) || IsDynamicSymbolAllowed(grammar, root, parent, s)
    124127                               select s;
    125128          Symbol selectedSymbol = SelectRandomSymbol(random, allowedSymbols);
     
    172175        var dummy = new SymbolicExpressionTreeNode();
    173176        tree.AddSubTree(dummy);
    174         dummy.Grammar = (ISymbolicExpressionGrammar)dummy.Grammar.Clone(); 
     177        dummy.Grammar = (ISymbolicExpressionGrammar)dummy.Grammar.Clone();
    175178        // replace the just inserted dummy by recursive application
    176179        ReplaceWithMinimalTree(random, root, tree, i, maxFunctionDefinitions, maxFunctionArguments);
    177180      }
    178181    }
    179    
     182
    180183    private static void InitializeNewTreeNode(IRandom random, SymbolicExpressionTreeNode root, SymbolicExpressionTreeNode newTree, int maxFunctionDefinitions, int maxFunctionArguments) {
    181184      // NB it is assumed that defuns are only allowed as children of root and nowhere else
    182185      // also assumes that newTree is already attached to root somewhere
    183       if (IsTopLevelBranch(root, newTree))
     186      if (IsTopLevelBranch(root, newTree)) {
    184187        newTree.Grammar = (ISymbolicExpressionGrammar)newTree.Grammar.Clone();
     188
     189        // allow invokes of existing ADFs with higher index
     190        int argIndex = root.SubTrees.IndexOf(newTree);
     191        for (int i = argIndex + 1; i < root.SubTrees.Count; i++) {
     192          var otherDefunNode = root.SubTrees[i] as DefunTreeNode;
     193          if (otherDefunNode != null) {
     194            GrammarModifier.AddDynamicSymbol(newTree.Grammar, newTree.Symbol, otherDefunNode.FunctionName, otherDefunNode.NumberOfArguments);
     195          }
     196        }
     197      }
    185198      if (newTree.Symbol is Defun) {
    186199        var defunTree = newTree as DefunTreeNode;
     
    197210        defunTree.NumberOfArguments = nArgs;
    198211        if (nArgs > 0) {
    199           AddDynamicArguments(defunTree.Grammar, nArgs);
    200         }
     212          GrammarModifier.AddDynamicArguments(defunTree.Grammar, defunTree.Symbol, Enumerable.Range(0, nArgs));
     213        }
     214        // in existing branches with smaller index allow invoke of current function
    201215        int argIndex = root.SubTrees.IndexOf(newTree);
    202         // allow invokes of ADFs with higher index
    203         for (int i = argIndex + 1; i < root.SubTrees.Count; i++) {
    204           var otherDefunNode = root.SubTrees[i] as DefunTreeNode;
    205           if (otherDefunNode != null) {
    206             var allowedParents = from sym in defunTree.Grammar.Symbols
    207                                  where defunTree.Grammar.IsAllowedChild(defunTree.Symbol, sym, 0)
    208                                  select sym;
    209             var allowedChildren = allowedParents;
    210             AddDynamicSymbol(defunTree.Grammar, allowedParents, allowedChildren, otherDefunNode.FunctionName, otherDefunNode.NumberOfArguments);
    211           }
    212         }
    213         // make the ADF available as symbol for other branches (with smaller index to prevent recursions)
    214216        for (int i = 0; i < argIndex; i++) {
    215217          // if not dummy node
    216218          if (root.SubTrees[i].Symbol != null) {
    217             var topLevelGrammar = root.SubTrees[i].Grammar;
    218             var allowedParents = from sym in root.SubTrees[i].Grammar.Symbols
    219                                  where root.SubTrees[i].Grammar.IsAllowedChild(root.SubTrees[i].Symbol, sym, 0)
    220                                  select sym;
    221             var allowedChildren = allowedParents;
    222 
    223             AddDynamicSymbol(topLevelGrammar, allowedParents, allowedChildren, functionName, nArgs);
     219            var existingBranch = root.SubTrees[i];
     220            GrammarModifier.AddDynamicSymbol(existingBranch.Grammar, existingBranch.Symbol, functionName, nArgs);
    224221          }
    225222        }
    226       }
    227     }
    228 
    229     private static void AddDynamicSymbol(ISymbolicExpressionGrammar grammar, IEnumerable<Symbol> allowedParents, IEnumerable<Symbol> allowedChildren, string symbolName, int nArgs) {
    230       var invokeSym = new InvokeFunction(symbolName);
    231       grammar.AddSymbol(invokeSym);
    232       grammar.SetMinSubtreeCount(invokeSym, nArgs);
    233       grammar.SetMaxSubtreeCount(invokeSym, nArgs);
    234       foreach (var parent in allowedParents) {
    235         for (int arg = 0; arg < grammar.GetMaxSubtreeCount(parent); arg++) {
    236           grammar.SetAllowedChild(parent, invokeSym, arg);
    237         }
    238       }
    239       foreach (var child in allowedChildren) {
    240         for (int arg = 0; arg < grammar.GetMaxSubtreeCount(invokeSym); arg++) {
    241           grammar.SetAllowedChild(invokeSym, child, arg);
    242         }
    243       }
    244     }
    245 
    246     private static void AddDynamicArguments(ISymbolicExpressionGrammar grammar, int nArgs) {
    247       for (int argIndex = 0; argIndex < nArgs; argIndex++) {
    248         var argNode = new Argument(argIndex);
    249         grammar.AddSymbol(argNode);
    250         grammar.SetMinSubtreeCount(argNode, 0);
    251         grammar.SetMaxSubtreeCount(argNode, 0);
    252         // allow the argument as child of any other symbol
    253         foreach (var symb in grammar.Symbols)
    254           for (int i = 0; i < grammar.GetMaxSubtreeCount(symb); i++) {
    255             grammar.SetAllowedChild(symb, argNode, i);
    256           }
    257223      }
    258224    }
Note: See TracChangeset for help on using the changeset viewer.