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)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Creators/ProbabilisticTreeCreator.cs

    r3360 r3369  
    3535  [Item("ProbabilisticTreeCreator", "An operator that creates new symbolic expression trees with uniformly distributed size")]
    3636  public class ProbabilisticTreeCreator : SymbolicExpressionTreeCreator {
     37    private const int MAX_TRIES = 100;
    3738
    3839    public ProbabilisticTreeCreator()
     
    5354      ) {
    5455      SymbolicExpressionTree tree = new SymbolicExpressionTree();
    55       tree.Root = PTC2(random, grammar, grammar.StartSymbol, maxTreeSize, maxTreeHeight, maxFunctionDefinitions, maxFunctionArguments);
     56      var rootNode = grammar.StartSymbol.CreateTreeNode();
     57      rootNode.Grammar = grammar;
     58      tree.Root = PTC2(random, rootNode, maxTreeSize, maxTreeHeight, maxFunctionDefinitions, maxFunctionArguments);
    5659      return tree;
    5760    }
     
    6366    }
    6467
    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>
    76     public static SymbolicExpressionTreeNode PTC2(IRandom random, ISymbolicExpressionGrammar grammar, Symbol rootSymbol,
     68    public static SymbolicExpressionTreeNode PTC2(IRandom random, SymbolicExpressionTreeNode seedNode,
    7769      int maxTreeSize, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) {
    7870      // 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);
    97       return root;
    98     }
    99 
    100     private static void CreateFullTreeFromSeed(IRandom random, SymbolicExpressionTreeNode root, int size, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) {
     71      int allowedMinSize = seedNode.Grammar.GetMinExpressionLength(seedNode.Symbol);
     72      int allowedMaxSize = Math.Min(maxTreeSize, seedNode.Grammar.GetMaxExpressionLength(seedNode.Symbol));
     73      int tries = 0;
     74      while (tries++ < MAX_TRIES) {
     75        // select a target tree size uniformly in the possible range (as determined by explicit limits and limits of the grammar)
     76        int treeSize = random.Next(allowedMinSize, allowedMaxSize + 1);
     77        if (treeSize <= 1 || maxDepth <= 1) return seedNode;
     78
     79        bool success = CreateFullTreeFromSeed(random, seedNode, seedNode.Grammar, treeSize, maxDepth, maxFunctionDefinitions, maxFunctionArguments);
     80
     81        // if successfull => check constraints and return the tree if everything looks ok       
     82        if (success && seedNode.GetSize() <= maxTreeSize && seedNode.GetHeight() <= maxDepth) {
     83          return seedNode;
     84        } else {
     85          // clean seedNode
     86          while (seedNode.SubTrees.Count > 0) seedNode.RemoveSubTree(0);
     87        }
     88        // try a different size MAX_TRIES times
     89      }
     90      throw new ArgumentException("Couldn't create a valid tree with the specified constraints.");
     91    }
     92
     93    private static bool CreateFullTreeFromSeed(IRandom random, SymbolicExpressionTreeNode root, ISymbolicExpressionGrammar globalGrammar,
     94      int size, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) {
     95      try {
     96        TryCreateFullTreeFromSeed(random, root, globalGrammar, size, maxDepth, maxFunctionDefinitions, maxFunctionArguments);
     97        return true;
     98      }
     99      catch (ArgumentException) { return false; }
     100    }
     101
     102    private static void TryCreateFullTreeFromSeed(IRandom random, SymbolicExpressionTreeNode root, ISymbolicExpressionGrammar globalGrammar,
     103      int size, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) {
    101104      List<TreeExtensionPoint> extensionPoints = new List<TreeExtensionPoint>();
    102105      int currentSize = 1;
    103       int totalListMinSize = root.Grammar.GetMinExpressionLength(root.Symbol) - 1;
     106      int totalListMinSize = globalGrammar.GetMinExpressionLength(root.Symbol) - 1;
    104107      int actualArity = SampleArity(random, root, size);
    105108      for (int i = 0; i < actualArity; i++) {
     
    107110        var dummy = new SymbolicExpressionTreeNode();
    108111        root.AddSubTree(dummy);
    109         dummy.Grammar = (ISymbolicExpressionGrammar)dummy.Grammar.Clone();
     112        // dummy.Grammar = (ISymbolicExpressionGrammar)dummy.Grammar.Clone();
    110113        extensionPoints.Add(new TreeExtensionPoint { Parent = root, ChildIndex = i, ExtensionPointDepth = 2 });
    111114      }
     
    141144            var dummy = new SymbolicExpressionTreeNode();
    142145            newTree.AddSubTree(dummy);
    143             if (IsTopLevelBranch(root, dummy))
    144               dummy.Grammar = (ISymbolicExpressionGrammar)dummy.Grammar.Clone();
     146            //if (IsTopLevelBranch(root, dummy))
     147            //  dummy.Grammar = (ISymbolicExpressionGrammar)dummy.Grammar.Clone();
    145148            extensionPoints.Add(new TreeExtensionPoint { Parent = newTree, ChildIndex = i, ExtensionPointDepth = extensionDepth + 1 });
    146149          }
     
    175178        var dummy = new SymbolicExpressionTreeNode();
    176179        tree.AddSubTree(dummy);
    177         dummy.Grammar = (ISymbolicExpressionGrammar)dummy.Grammar.Clone();
     180        // dummy.Grammar = (ISymbolicExpressionGrammar)dummy.Grammar.Clone();
    178181        // replace the just inserted dummy by recursive application
    179182        ReplaceWithMinimalTree(random, root, tree, i, maxFunctionDefinitions, maxFunctionArguments);
     
    185188      // also assumes that newTree is already attached to root somewhere
    186189      if (IsTopLevelBranch(root, newTree)) {
    187         newTree.Grammar = (ISymbolicExpressionGrammar)newTree.Grammar.Clone();
     190        newTree.Grammar = (ISymbolicExpressionGrammar)root.Grammar.Clone();
    188191
    189192        // allow invokes of existing ADFs with higher index
     
    225228
    226229    private static bool IsTopLevelBranch(SymbolicExpressionTreeNode root, SymbolicExpressionTreeNode branch) {
    227       return root.SubTrees.IndexOf(branch) > -1;
     230      //return root.SubTrees.IndexOf(branch) > -1;
     231      return branch is SymbolicExpressionTreeTopLevelNode;
    228232    }
    229233
Note: See TracChangeset for help on using the changeset viewer.