Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/17/15 17:22:58 (8 years ago)
Author:
mkommend
Message:

#2510: Added method for creating trees with a specific length to the PTC2 and adapted the according unit test.

File:
1 edited

Legend:

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

    r12422 r13227  
    7474    }
    7575
     76    public static ISymbolicExpressionTree CreateExpressionTree(IRandom random, ISymbolicExpressionGrammar grammar, int targetLength,
     77      int maxTreeDepth) {
     78      SymbolicExpressionTree tree = new SymbolicExpressionTree();
     79      var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode();
     80      if (rootNode.HasLocalParameters) rootNode.ResetLocalParameters(random);
     81      rootNode.SetGrammar(grammar.CreateExpressionTreeGrammar());
     82
     83      var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
     84      if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random);
     85      startNode.SetGrammar(grammar.CreateExpressionTreeGrammar());
     86
     87      rootNode.AddSubtree(startNode);
     88      bool success = TryCreateFullTreeFromSeed(random, startNode, targetLength - 2, maxTreeDepth - 1);
     89      if (!success) throw new InvalidOperationException(string.Format("Could not create a tree with target length {0} and max depth {1}", targetLength, maxTreeDepth));
     90
     91      tree.Root = rootNode;
     92      return tree;
     93
     94    }
     95
    7696    private class TreeExtensionPoint {
    7797      public ISymbolicExpressionTreeNode Parent { get; set; }
     
    107127        } else {
    108128          // clean seedNode
    109           while (seedNode.Subtrees.Count() > 0) seedNode.RemoveSubtree(0);
     129          while (seedNode.Subtrees.Any()) seedNode.RemoveSubtree(0);
    110130        }
    111131        // try a different length MAX_TRIES times
Note: See TracChangeset for help on using the changeset viewer.