private void ResetCachedValues()
        {
            length = 0; depth = 0;
            SymbolicExpressionTreeNode parentNode = parent as SymbolicExpressionTreeNode;

            if (parentNode != null)
            {
                parentNode.ResetCachedValues();
            }
        }
 protected SymbolicExpressionTreeNode(SymbolicExpressionTreeNode original, Cloner cloner)
   : base(original, cloner) {
   symbol = original.symbol; // symbols are reused
   length = original.length;
   depth = original.depth;
   if (original.subtrees != null) {
     subtrees = new List<ISymbolicExpressionTreeNode>(original.subtrees.Count);
     foreach (var subtree in original.subtrees) {
       var clonedSubtree = cloner.Clone(subtree);
       subtrees.Add(clonedSubtree);
       clonedSubtree.Parent = this;
     }
   }
 }
 protected SymbolicExpressionTreeNode(SymbolicExpressionTreeNode original, Cloner cloner)
     : base(original, cloner)
 {
     symbol = original.symbol; // symbols are reused
     length = original.length;
     depth  = original.depth;
     if (original.subtrees != null)
     {
         subtrees = new List <ISymbolicExpressionTreeNode>(original.subtrees.Count);
         foreach (var subtree in original.subtrees)
         {
             var clonedSubtree = cloner.Clone(subtree);
             subtrees.Add(clonedSubtree);
             clonedSubtree.Parent = this;
         }
     }
 }
        private static void ReplaceWithMinimalTree(IRandom random, ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode parent,
                                                   int childIndex)
        {
            // determine possible symbols that will lead to the smallest possible tree
            var possibleSymbols = (from s in parent.Grammar.GetAllowedChildSymbols(parent.Symbol, childIndex)
                                   where s.InitialFrequency > 0.0
                                   group s by parent.Grammar.GetMinimumExpressionLength(s) into g
                                   orderby g.Key
                                   select g).First().ToList();
            var weights = possibleSymbols.Select(x => x.InitialFrequency).ToList();

#pragma warning disable 612, 618
            var selectedSymbol = possibleSymbols.SelectRandom(weights, random);
#pragma warning restore 612, 618

            var tree = selectedSymbol.CreateTreeNode();
            if (tree.HasLocalParameters)
            {
                tree.ResetLocalParameters(random);
            }
            parent.RemoveSubtree(childIndex);
            parent.InsertSubtree(childIndex, tree);

            var topLevelNode = tree as SymbolicExpressionTreeTopLevelNode;
            if (topLevelNode != null)
            {
                topLevelNode.SetGrammar((ISymbolicExpressionTreeGrammar)root.Grammar.Clone());
            }

            for (int i = 0; i < tree.Grammar.GetMinimumSubtreeCount(tree.Symbol); i++)
            {
                // insert a dummy sub-tree and add the pending extension to the list
                var dummy = new SymbolicExpressionTreeNode();
                tree.AddSubtree(dummy);
                // replace the just inserted dummy by recursive application
                ReplaceWithMinimalTree(random, root, tree, i);
            }
        }
    private static void ReplaceWithMinimalTree(IRandom random, ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode parent, int childIndex) {
      // determine possible symbols that will lead to the smallest possible tree
      var possibleSymbols = (from s in parent.Grammar.GetAllowedChildSymbols(parent.Symbol, childIndex)
                             where s.InitialFrequency > 0.0
                             group s by parent.Grammar.GetMinimumExpressionLength(s) into g
                             orderby g.Key
                             select g).First().ToList();
      var weights = possibleSymbols.Select(x => x.InitialFrequency).ToList();

#pragma warning disable 612, 618
      var selectedSymbol = possibleSymbols.SelectRandom(weights, random);
#pragma warning restore 612, 618

      var newTreeNode = selectedSymbol.CreateTreeNode();
      if (newTreeNode.HasLocalParameters) newTreeNode.ResetLocalParameters(random);
      parent.RemoveSubtree(childIndex);
      parent.InsertSubtree(childIndex, newTreeNode);

      var topLevelNode = newTreeNode as SymbolicExpressionTreeTopLevelNode;
      if (topLevelNode != null)
        topLevelNode.SetGrammar((ISymbolicExpressionTreeGrammar)root.Grammar.Clone());

      for (int i = 0; i < newTreeNode.Grammar.GetMinimumSubtreeCount(newTreeNode.Symbol); i++) {
        // insert a dummy sub-tree and add the pending extension to the list
        var dummy = new SymbolicExpressionTreeNode();
        newTreeNode.AddSubtree(dummy);
        // replace the just inserted dummy by recursive application
        ReplaceWithMinimalTree(random, root, newTreeNode, i);
      }
    }
    private static bool TryCreateFullTreeFromSeed(IRandom random, ISymbolicExpressionTreeNode root,
      int targetLength, int maxDepth) {
      List<TreeExtensionPoint> extensionPoints = new List<TreeExtensionPoint>();
      int currentLength = 0;
      int actualArity = SampleArity(random, root, targetLength, maxDepth);
      if (actualArity < 0) return false;

      for (int i = 0; i < actualArity; i++) {
        // insert a dummy sub-tree and add the pending extension to the list
        var dummy = new SymbolicExpressionTreeNode();
        root.AddSubtree(dummy);
        var x = new TreeExtensionPoint { Parent = root, ChildIndex = i, ExtensionPointDepth = 0 };
        FillExtensionLengths(x, maxDepth);
        extensionPoints.Add(x);
      }
      //necessary to use long data type as the extension point length could be int.MaxValue
      long minExtensionPointsLength = extensionPoints.Select(x => (long)x.MinimumExtensionLength).Sum();
      long maxExtensionPointsLength = extensionPoints.Select(x => (long)x.MaximumExtensionLength).Sum();

      // while there are pending extension points and we have not reached the limit of adding new extension points
      while (extensionPoints.Count > 0 && minExtensionPointsLength + currentLength <= targetLength) {
        int randomIndex = random.Next(extensionPoints.Count);
        TreeExtensionPoint nextExtension = extensionPoints[randomIndex];
        extensionPoints.RemoveAt(randomIndex);
        ISymbolicExpressionTreeNode parent = nextExtension.Parent;
        int argumentIndex = nextExtension.ChildIndex;
        int extensionDepth = nextExtension.ExtensionPointDepth;

        if (parent.Grammar.GetMinimumExpressionDepth(parent.Symbol) > maxDepth - extensionDepth) {
          ReplaceWithMinimalTree(random, root, parent, argumentIndex);
          int insertedTreeLength = parent.GetSubtree(argumentIndex).GetLength();
          currentLength += insertedTreeLength;
          minExtensionPointsLength -= insertedTreeLength;
          maxExtensionPointsLength -= insertedTreeLength;
        } else {
          //remove currently chosen extension point from calculation
          minExtensionPointsLength -= nextExtension.MinimumExtensionLength;
          maxExtensionPointsLength -= nextExtension.MaximumExtensionLength;

          var symbols = from s in parent.Grammar.GetAllowedChildSymbols(parent.Symbol, argumentIndex)
                        where s.InitialFrequency > 0.0
                        where parent.Grammar.GetMinimumExpressionDepth(s) <= maxDepth - extensionDepth
                        where parent.Grammar.GetMinimumExpressionLength(s) <= targetLength - currentLength - minExtensionPointsLength
                        select s;
          if (maxExtensionPointsLength < targetLength - currentLength)
            symbols = from s in symbols
                      where parent.Grammar.GetMaximumExpressionLength(s, maxDepth - extensionDepth) >= targetLength - currentLength - maxExtensionPointsLength
                      select s;
          var allowedSymbols = symbols.ToList();

          if (allowedSymbols.Count == 0) return false;
          var weights = allowedSymbols.Select(x => x.InitialFrequency).ToList();

#pragma warning disable 612, 618
          var selectedSymbol = allowedSymbols.SelectRandom(weights, random);
#pragma warning restore 612, 618

          ISymbolicExpressionTreeNode newTree = selectedSymbol.CreateTreeNode();
          if (newTree.HasLocalParameters) newTree.ResetLocalParameters(random);
          parent.RemoveSubtree(argumentIndex);
          parent.InsertSubtree(argumentIndex, newTree);

          var topLevelNode = newTree as SymbolicExpressionTreeTopLevelNode;
          if (topLevelNode != null)
            topLevelNode.SetGrammar((ISymbolicExpressionTreeGrammar)root.Grammar.Clone());

          currentLength++;
          actualArity = SampleArity(random, newTree, targetLength - currentLength, maxDepth - extensionDepth);
          if (actualArity < 0) return false;
          for (int i = 0; i < actualArity; i++) {
            // insert a dummy sub-tree and add the pending extension to the list
            var dummy = new SymbolicExpressionTreeNode();
            newTree.AddSubtree(dummy);
            var x = new TreeExtensionPoint { Parent = newTree, ChildIndex = i, ExtensionPointDepth = extensionDepth + 1 };
            FillExtensionLengths(x, maxDepth);
            extensionPoints.Add(x);
            maxExtensionPointsLength += x.MaximumExtensionLength;
            minExtensionPointsLength += x.MinimumExtensionLength;
          }
        }
      }
      // fill all pending extension points
      while (extensionPoints.Count > 0) {
        int randomIndex = random.Next(extensionPoints.Count);
        TreeExtensionPoint nextExtension = extensionPoints[randomIndex];
        extensionPoints.RemoveAt(randomIndex);
        ISymbolicExpressionTreeNode parent = nextExtension.Parent;
        int a = nextExtension.ChildIndex;
        ReplaceWithMinimalTree(random, root, parent, a);
      }
      return true;
    }
 private ISymbolicExpressionTreeNode CreateNodeFromWeight(ISymbolicExpressionTreeNode transformationTree, VariableTreeNode variableNode) {
   var multiplicationNode = new SymbolicExpressionTreeNode(new Multiplication());
   multiplicationNode.AddSubtree(new ConstantTreeNode(new Constant()) { Value = variableNode.Weight });
   multiplicationNode.AddSubtree(transformationTree);
   return multiplicationNode;
 }
        private static bool TryCreateFullTreeFromSeed(IRandom random, ISymbolicExpressionTreeNode root,
                                                      int targetLength, int maxDepth)
        {
            List <TreeExtensionPoint> extensionPoints = new List <TreeExtensionPoint>();
            int currentLength = 0;
            int actualArity   = SampleArity(random, root, targetLength, maxDepth);

            if (actualArity < 0)
            {
                return(false);
            }

            for (int i = 0; i < actualArity; i++)
            {
                // insert a dummy sub-tree and add the pending extension to the list
                var dummy = new SymbolicExpressionTreeNode();
                root.AddSubtree(dummy);
                var x = new TreeExtensionPoint {
                    Parent = root, ChildIndex = i, ExtensionPointDepth = 0
                };
                FillExtensionLengths(x, maxDepth);
                extensionPoints.Add(x);
            }
            //necessary to use long data type as the extension point length could be int.MaxValue
            long minExtensionPointsLength = extensionPoints.Select(x => (long)x.MinimumExtensionLength).Sum();
            long maxExtensionPointsLength = extensionPoints.Select(x => (long)x.MaximumExtensionLength).Sum();

            // while there are pending extension points and we have not reached the limit of adding new extension points
            while (extensionPoints.Count > 0 && minExtensionPointsLength + currentLength <= targetLength)
            {
                int randomIndex = random.Next(extensionPoints.Count);
                TreeExtensionPoint nextExtension = extensionPoints[randomIndex];
                extensionPoints.RemoveAt(randomIndex);
                ISymbolicExpressionTreeNode parent = nextExtension.Parent;
                int argumentIndex  = nextExtension.ChildIndex;
                int extensionDepth = nextExtension.ExtensionPointDepth;

                if (parent.Grammar.GetMinimumExpressionDepth(parent.Symbol) > maxDepth - extensionDepth)
                {
                    ReplaceWithMinimalTree(random, root, parent, argumentIndex);
                    int insertedTreeLength = parent.GetSubtree(argumentIndex).GetLength();
                    currentLength            += insertedTreeLength;
                    minExtensionPointsLength -= insertedTreeLength;
                    maxExtensionPointsLength -= insertedTreeLength;
                }
                else
                {
                    //remove currently chosen extension point from calculation
                    minExtensionPointsLength -= nextExtension.MinimumExtensionLength;
                    maxExtensionPointsLength -= nextExtension.MaximumExtensionLength;

                    var symbols = from s in parent.Grammar.GetAllowedChildSymbols(parent.Symbol, argumentIndex)
                                  where s.InitialFrequency > 0.0
                                  where parent.Grammar.GetMinimumExpressionDepth(s) <= maxDepth - extensionDepth
                                  where parent.Grammar.GetMinimumExpressionLength(s) <= targetLength - currentLength - minExtensionPointsLength
                                  select s;
                    if (maxExtensionPointsLength < targetLength - currentLength)
                    {
                        symbols = from s in symbols
                                  where parent.Grammar.GetMaximumExpressionLength(s, maxDepth - extensionDepth) >= targetLength - currentLength - maxExtensionPointsLength
                                  select s;
                    }
                    var allowedSymbols = symbols.ToList();

                    if (allowedSymbols.Count == 0)
                    {
                        return(false);
                    }
                    var weights = allowedSymbols.Select(x => x.InitialFrequency).ToList();

#pragma warning disable 612, 618
                    var selectedSymbol = allowedSymbols.SelectRandom(weights, random);
#pragma warning restore 612, 618

                    ISymbolicExpressionTreeNode newTree = selectedSymbol.CreateTreeNode();
                    if (newTree.HasLocalParameters)
                    {
                        newTree.ResetLocalParameters(random);
                    }
                    parent.RemoveSubtree(argumentIndex);
                    parent.InsertSubtree(argumentIndex, newTree);

                    var topLevelNode = newTree as SymbolicExpressionTreeTopLevelNode;
                    if (topLevelNode != null)
                    {
                        topLevelNode.SetGrammar((ISymbolicExpressionTreeGrammar)root.Grammar.Clone());
                    }

                    currentLength++;
                    actualArity = SampleArity(random, newTree, targetLength - currentLength, maxDepth - extensionDepth);
                    if (actualArity < 0)
                    {
                        return(false);
                    }
                    for (int i = 0; i < actualArity; i++)
                    {
                        // insert a dummy sub-tree and add the pending extension to the list
                        var dummy = new SymbolicExpressionTreeNode();
                        newTree.AddSubtree(dummy);
                        var x = new TreeExtensionPoint {
                            Parent = newTree, ChildIndex = i, ExtensionPointDepth = extensionDepth + 1
                        };
                        FillExtensionLengths(x, maxDepth);
                        extensionPoints.Add(x);
                        maxExtensionPointsLength += x.MaximumExtensionLength;
                        minExtensionPointsLength += x.MinimumExtensionLength;
                    }
                }
            }
            // fill all pending extension points
            while (extensionPoints.Count > 0)
            {
                int randomIndex = random.Next(extensionPoints.Count);
                TreeExtensionPoint nextExtension = extensionPoints[randomIndex];
                extensionPoints.RemoveAt(randomIndex);
                ISymbolicExpressionTreeNode parent = nextExtension.Parent;
                int a = nextExtension.ChildIndex;
                ReplaceWithMinimalTree(random, root, parent, a);
            }
            return(true);
        }