Exemple #1
0
        /// <summary>
        /// Create a symbolic expression tree using 'RampedHalfAndHalf' strategy.
        /// Half the trees are created with the 'Grow' method, and the other half are created with the 'Full' method.
        /// </summary>
        /// <param name="random">Random generator</param>
        /// <param name="grammar">Available tree grammar</param>
        /// <param name="maxTreeLength">Maximum tree length (this parameter is ignored)</param>
        /// <param name="maxTreeDepth">Maximum tree depth</param>
        /// <returns></returns>
        public static ISymbolicExpressionTree Create(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeLength, int maxTreeDepth)
        {
            var tree     = new SymbolicExpressionTree();
            var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode();

            if (rootNode.HasLocalParameters)
            {
                rootNode.ResetLocalParameters(random);
            }
            rootNode.SetGrammar(grammar.CreateExpressionTreeGrammar());

            var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();

            if (startNode.HasLocalParameters)
            {
                startNode.ResetLocalParameters(random);
            }
            startNode.SetGrammar(grammar.CreateExpressionTreeGrammar());

            rootNode.AddSubtree(startNode);

            double p = random.NextDouble();

            if (p < 0.5)
            {
                GrowTreeCreator.Create(random, startNode, maxTreeDepth - 2);
            }
            else
            {
                FullTreeCreator.Create(random, startNode, maxTreeDepth - 2);
            }

            tree.Root = rootNode;
            return(tree);
        }
        public static ISymbolicExpressionTree CreateExpressionTree(IRandom random, ISymbolicExpressionGrammar grammar, int targetLength,
                                                                   int maxTreeDepth)
        {
            SymbolicExpressionTree tree = new SymbolicExpressionTree();
            var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode();

            if (rootNode.HasLocalParameters)
            {
                rootNode.ResetLocalParameters(random);
            }
            rootNode.SetGrammar(grammar.CreateExpressionTreeGrammar());

            var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();

            if (startNode.HasLocalParameters)
            {
                startNode.ResetLocalParameters(random);
            }
            startNode.SetGrammar(grammar.CreateExpressionTreeGrammar());

            rootNode.AddSubtree(startNode);
            bool success = TryCreateFullTreeFromSeed(random, startNode, targetLength - 2, maxTreeDepth - 1);

            if (!success)
            {
                throw new InvalidOperationException(string.Format("Could not create a tree with target length {0} and max depth {1}", targetLength, maxTreeDepth));
            }

            tree.Root = rootNode;
            return(tree);
        }
    public static ISymbolicExpressionTree Create(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeLength, int maxTreeDepth) {
      SymbolicExpressionTree tree = new SymbolicExpressionTree();
      var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode();
      if (rootNode.HasLocalParameters) rootNode.ResetLocalParameters(random);
      rootNode.SetGrammar(grammar.CreateExpressionTreeGrammar());

      var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
      if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random);
      startNode.SetGrammar(grammar.CreateExpressionTreeGrammar());

      rootNode.AddSubtree(startNode);
      PTC2(random, startNode, maxTreeLength, maxTreeDepth);
      tree.Root = rootNode;
      return tree;
    }
    public static ISymbolicExpressionTree CreateExpressionTree(IRandom random, ISymbolicExpressionGrammar grammar, int targetLength,
      int maxTreeDepth) {
      SymbolicExpressionTree tree = new SymbolicExpressionTree();
      var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode();
      if (rootNode.HasLocalParameters) rootNode.ResetLocalParameters(random);
      rootNode.SetGrammar(grammar.CreateExpressionTreeGrammar());

      var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
      if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random);
      startNode.SetGrammar(grammar.CreateExpressionTreeGrammar());

      rootNode.AddSubtree(startNode);
      bool success = TryCreateFullTreeFromSeed(random, startNode, targetLength - 2, maxTreeDepth - 1);
      if (!success) throw new InvalidOperationException(string.Format("Could not create a tree with target length {0} and max depth {1}", targetLength, maxTreeDepth));

      tree.Root = rootNode;
      return tree;

    }
    /// <summary>
    /// Create a symbolic expression tree using 'RampedHalfAndHalf' strategy.
    /// Half the trees are created with the 'Grow' method, and the other half are created with the 'Full' method.
    /// </summary>
    /// <param name="random">Random generator</param>
    /// <param name="grammar">Available tree grammar</param>
    /// <param name="maxTreeLength">Maximum tree length (this parameter is ignored)</param>
    /// <param name="maxTreeDepth">Maximum tree depth</param>
    /// <returns></returns>
    public static ISymbolicExpressionTree Create(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeLength, int maxTreeDepth) {
      var tree = new SymbolicExpressionTree();
      var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode();
      if (rootNode.HasLocalParameters) rootNode.ResetLocalParameters(random);
      rootNode.SetGrammar(grammar.CreateExpressionTreeGrammar());

      var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
      if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random);
      startNode.SetGrammar(grammar.CreateExpressionTreeGrammar());

      rootNode.AddSubtree(startNode);

      double p = random.NextDouble();

      if (p < 0.5)
        GrowTreeCreator.Create(random, startNode, maxTreeDepth - 2);
      else
        FullTreeCreator.Create(random, startNode, maxTreeDepth - 2);

      tree.Root = rootNode;
      return tree;
    }
        public static ISymbolicExpressionTree Create(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeLength, int maxTreeDepth)
        {
            SymbolicExpressionTree tree = new SymbolicExpressionTree();
            var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode();

            if (rootNode.HasLocalParameters)
            {
                rootNode.ResetLocalParameters(random);
            }
            rootNode.SetGrammar(grammar.CreateExpressionTreeGrammar());

            var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();

            if (startNode.HasLocalParameters)
            {
                startNode.ResetLocalParameters(random);
            }
            startNode.SetGrammar(grammar.CreateExpressionTreeGrammar());

            rootNode.AddSubtree(startNode);
            PTC2(random, startNode, maxTreeLength, maxTreeDepth);
            tree.Root = rootNode;
            return(tree);
        }