Пример #1
0
    internal static void AddInvokeSymbol(ISymbolicExpressionTreeGrammar grammar, string functionName, int nArgs, CutPoint startCutPoint, IEnumerable<CutPoint> argumentCutPoints) {
      if (!grammar.ContainsSymbol(startCutPoint.Child.Symbol)) return;

      var invokeSym = new InvokeFunction(functionName);
      grammar.AddSymbol(invokeSym);
      grammar.SetSubtreeCount(invokeSym, nArgs, nArgs);

      //allow invoke symbol everywhere, where the child of the startCutPoint was allowed
      SetAllowedParentSymbols(grammar, startCutPoint.Child.Symbol, invokeSym);

      if (nArgs > 0) {
        //set allowed child symbols of invoke symbol 
        foreach (ISymbol child in grammar.Symbols) {
          if (child.Name == invokeSym.Name) continue;
          int i = 0;
          foreach (CutPoint argumentCutPoint in argumentCutPoints) {
            if (grammar.IsAllowedChildSymbol(argumentCutPoint.Parent.Symbol, child, argumentCutPoint.ChildIndex))
              grammar.AddAllowedChildSymbol(invokeSym, child, i);
            i++;
          }
        }
      }
    }
Пример #2
0
        internal static void AddInvokeSymbol(ISymbolicExpressionTreeGrammar grammar, string functionName, int nArgs, CutPoint startCutPoint, IEnumerable <CutPoint> argumentCutPoints)
        {
            if (!grammar.ContainsSymbol(startCutPoint.Child.Symbol))
            {
                return;
            }

            var invokeSym = new InvokeFunction(functionName);

            grammar.AddSymbol(invokeSym);
            grammar.SetSubtreeCount(invokeSym, nArgs, nArgs);

            //allow invoke symbol everywhere, where the child of the startCutPoint was allowed
            SetAllowedParentSymbols(grammar, startCutPoint.Child.Symbol, invokeSym);

            if (nArgs > 0)
            {
                //set allowed child symbols of invoke symbol
                foreach (ISymbol child in grammar.Symbols)
                {
                    if (child.Name == invokeSym.Name)
                    {
                        continue;
                    }
                    int i = 0;
                    foreach (CutPoint argumentCutPoint in argumentCutPoints)
                    {
                        if (grammar.IsAllowedChildSymbol(argumentCutPoint.Parent.Symbol, child, argumentCutPoint.ChildIndex))
                        {
                            grammar.AddAllowedChildSymbol(invokeSym, child, i);
                        }
                        i++;
                    }
                }
            }
        }
Пример #3
0
 private InvokeFunction(InvokeFunction original, Cloner cloner)
     : base(original, cloner)
 {
     functionName = original.functionName;
     name         = "Invoke: " + original.functionName;
 }
    public static bool DuplicateSubroutine(
      IRandom random,
      ISymbolicExpressionTree symbolicExpressionTree,
      int maxFunctionDefinitions, int maxFunctionArguments) {
      var functionDefiningBranches = symbolicExpressionTree.IterateNodesPrefix().OfType<DefunTreeNode>().ToList();
      if (!functionDefiningBranches.Any() || functionDefiningBranches.Count() == maxFunctionDefinitions)
        // no function defining branches to duplicate or already reached the max number of ADFs
        return false;

      string formatString = new StringBuilder().Append('0', (int)Math.Log10(maxFunctionDefinitions) + 1).ToString(); // >= 100 functions => ###
      var allowedFunctionNames = from index in Enumerable.Range(0, maxFunctionDefinitions)
                                 select "ADF" + index.ToString(formatString);

      var selectedBranch = functionDefiningBranches.SampleRandom(random);
      var duplicatedDefunBranch = (DefunTreeNode)selectedBranch.Clone();
      string newFunctionName = allowedFunctionNames.Except(UsedFunctionNames(symbolicExpressionTree)).First();
      duplicatedDefunBranch.FunctionName = newFunctionName;
      symbolicExpressionTree.Root.AddSubtree(duplicatedDefunBranch);
      duplicatedDefunBranch.SetGrammar((ISymbolicExpressionTreeGrammar)selectedBranch.Grammar.Clone());

      var allowedChildSymbols = selectedBranch.Grammar.GetAllowedChildSymbols(selectedBranch.Symbol);
      foreach (var allowedChildSymbol in allowedChildSymbols)
        duplicatedDefunBranch.Grammar.AddAllowedChildSymbol(duplicatedDefunBranch.Symbol, allowedChildSymbol);
      var maxSubtrees = selectedBranch.Grammar.GetMaximumSubtreeCount(selectedBranch.Symbol);
      for (int i = 0; i < maxSubtrees; i++) {
        foreach (var allowedChildSymbol in selectedBranch.Grammar.GetAllowedChildSymbols(selectedBranch.Symbol, i))
          duplicatedDefunBranch.Grammar.AddAllowedChildSymbol(duplicatedDefunBranch.Symbol, allowedChildSymbol);
      }

      // add an invoke symbol for each branch that is allowed to invoke the original function
      foreach (var subtree in symbolicExpressionTree.Root.Subtrees.OfType<SymbolicExpressionTreeTopLevelNode>()) {
        var matchingInvokeSymbol = (from symb in subtree.Grammar.Symbols.OfType<InvokeFunction>()
                                    where symb.FunctionName == selectedBranch.FunctionName
                                    select symb).SingleOrDefault();
        if (matchingInvokeSymbol != null) {
          var invokeSymbol = new InvokeFunction(duplicatedDefunBranch.FunctionName);
          subtree.Grammar.AddSymbol(invokeSymbol);
          subtree.Grammar.SetSubtreeCount(invokeSymbol, duplicatedDefunBranch.NumberOfArguments, duplicatedDefunBranch.NumberOfArguments);

          foreach (ISymbol symbol in subtree.Grammar.Symbols) {
            if (subtree.Grammar.IsAllowedChildSymbol(symbol, matchingInvokeSymbol))
              subtree.Grammar.AddAllowedChildSymbol(symbol, invokeSymbol);
            else {
              for (int i = 0; i < subtree.Grammar.GetMaximumSubtreeCount(symbol); i++)
                if (subtree.Grammar.IsAllowedChildSymbol(symbol, matchingInvokeSymbol, i))
                  subtree.Grammar.AddAllowedChildSymbol(symbol, invokeSymbol, i);
            }
          }

          foreach (ISymbol symbol in subtree.Grammar.GetAllowedChildSymbols(matchingInvokeSymbol))
            if (symbol != invokeSymbol) //avoid duplicate entry invokesymbol / invokesymbol
              subtree.Grammar.AddAllowedChildSymbol(invokeSymbol, symbol);
          for (int i = 0; i < subtree.Grammar.GetMaximumSubtreeCount(matchingInvokeSymbol); i++) {
            foreach (ISymbol symbol in subtree.Grammar.GetAllowedChildSymbols(matchingInvokeSymbol, i).Except(subtree.Grammar.GetAllowedChildSymbols(matchingInvokeSymbol)))
              subtree.Grammar.AddAllowedChildSymbol(invokeSymbol, symbol, i);
          }
        }
        // in the current subtree:
        // for all invoke nodes of the original function replace the invoke of the original function with an invoke of the new function randomly
        var originalFunctionInvocations = from node in subtree.IterateNodesPrefix().OfType<InvokeFunctionTreeNode>()
                                          where node.Symbol.FunctionName == selectedBranch.FunctionName
                                          select node;
        foreach (var originalFunctionInvokeNode in originalFunctionInvocations) {
          var newInvokeSymbol = (from symb in subtree.Grammar.Symbols.OfType<InvokeFunction>()
                                 where symb.FunctionName == duplicatedDefunBranch.FunctionName
                                 select symb).Single();
          // flip coin wether to replace with newly defined function
          if (random.NextDouble() < 0.5) {
            originalFunctionInvokeNode.Symbol = newInvokeSymbol;
          }
        }
      }
      return true;
    }
 public InvokeFunctionTreeNode(InvokeFunction invokeSymbol) : base(invokeSymbol) { }
Пример #6
0
 public InvokeFunctionTreeNode(InvokeFunction invokeSymbol) : base(invokeSymbol)
 {
 }
Пример #7
0
    public static void IsValid(ISymbolicExpressionTree tree) {
      int reportedSize = tree.Length;
      int actualSize = tree.IterateNodesPostfix().Count();
      Assert.AreEqual(actualSize, reportedSize);

      foreach (var defunTreeNode in tree.Root.Subtrees.OfType<DefunTreeNode>()) {
        int arity = defunTreeNode.NumberOfArguments;

        foreach (var argTreenode in defunTreeNode.IterateNodesPrefix().OfType<ArgumentTreeNode>()) {
          Assert.IsTrue(argTreenode.SubtreeCount == 0);
          Assert.IsTrue(((Argument)argTreenode.Symbol).ArgumentIndex < arity);
        }

        foreach (var argSymbol in Enumerable.Range(0, defunTreeNode.NumberOfArguments).Select(x => new Argument(x))) {
          Assert.IsTrue(defunTreeNode.Grammar.ContainsSymbol(argSymbol));
          Assert.IsTrue(defunTreeNode.Grammar.GetMaximumSubtreeCount(argSymbol) == 0);
          Assert.IsTrue(defunTreeNode.Grammar.GetMinimumSubtreeCount(argSymbol) == 0);
        }

        var invoke = new InvokeFunction(defunTreeNode.FunctionName);
        foreach (var otherRootNode in tree.Root.Subtrees) {
          if (otherRootNode.Grammar.ContainsSymbol(invoke)) {
            Assert.IsTrue(otherRootNode.Grammar.GetMinimumSubtreeCount(invoke) == arity);
            Assert.IsTrue(otherRootNode.Grammar.GetMaximumSubtreeCount(invoke) == arity);
            Assert.IsFalse(otherRootNode.Grammar.IsAllowedChildSymbol(invoke, invoke));
            for (int i = 0; i < arity; i++) {
              Assert.IsFalse(otherRootNode.Grammar.IsAllowedChildSymbol(invoke, invoke, i));
            }
          }
        }

      }

      foreach (var subtree in tree.Root.Subtrees) {
        if (tree.Root.Grammar.GetType().Name != "EmptySymbolicExpressionTreeGrammar")
          Assert.AreNotSame(subtree.Grammar, tree.Root.Grammar);
        IsValid(subtree.Grammar);
      }

      IsValid(tree.Root.Grammar);
      IsValid(tree.Root);
    }
Пример #8
0
        public static bool DuplicateSubroutine(
            IRandom random,
            ISymbolicExpressionTree symbolicExpressionTree,
            int maxFunctionDefinitions, int maxFunctionArguments)
        {
            var functionDefiningBranches = symbolicExpressionTree.IterateNodesPrefix().OfType <DefunTreeNode>().ToList();

            if (!functionDefiningBranches.Any() || functionDefiningBranches.Count() == maxFunctionDefinitions)
            {
                // no function defining branches to duplicate or already reached the max number of ADFs
                return(false);
            }

            string formatString         = new StringBuilder().Append('0', (int)Math.Log10(maxFunctionDefinitions) + 1).ToString(); // >= 100 functions => ###
            var    allowedFunctionNames = from index in Enumerable.Range(0, maxFunctionDefinitions)
                                          select "ADF" + index.ToString(formatString);

            var    selectedBranch        = functionDefiningBranches.SampleRandom(random);
            var    duplicatedDefunBranch = (DefunTreeNode)selectedBranch.Clone();
            string newFunctionName       = allowedFunctionNames.Except(UsedFunctionNames(symbolicExpressionTree)).First();

            duplicatedDefunBranch.FunctionName = newFunctionName;
            symbolicExpressionTree.Root.AddSubtree(duplicatedDefunBranch);
            duplicatedDefunBranch.SetGrammar((ISymbolicExpressionTreeGrammar)selectedBranch.Grammar.Clone());

            var allowedChildSymbols = selectedBranch.Grammar.GetAllowedChildSymbols(selectedBranch.Symbol);

            foreach (var allowedChildSymbol in allowedChildSymbols)
            {
                duplicatedDefunBranch.Grammar.AddAllowedChildSymbol(duplicatedDefunBranch.Symbol, allowedChildSymbol);
            }
            var maxSubtrees = selectedBranch.Grammar.GetMaximumSubtreeCount(selectedBranch.Symbol);

            for (int i = 0; i < maxSubtrees; i++)
            {
                foreach (var allowedChildSymbol in selectedBranch.Grammar.GetAllowedChildSymbols(selectedBranch.Symbol, i))
                {
                    duplicatedDefunBranch.Grammar.AddAllowedChildSymbol(duplicatedDefunBranch.Symbol, allowedChildSymbol);
                }
            }

            // add an invoke symbol for each branch that is allowed to invoke the original function
            foreach (var subtree in symbolicExpressionTree.Root.Subtrees.OfType <SymbolicExpressionTreeTopLevelNode>())
            {
                var matchingInvokeSymbol = (from symb in subtree.Grammar.Symbols.OfType <InvokeFunction>()
                                            where symb.FunctionName == selectedBranch.FunctionName
                                            select symb).SingleOrDefault();
                if (matchingInvokeSymbol != null)
                {
                    var invokeSymbol = new InvokeFunction(duplicatedDefunBranch.FunctionName);
                    subtree.Grammar.AddSymbol(invokeSymbol);
                    subtree.Grammar.SetSubtreeCount(invokeSymbol, duplicatedDefunBranch.NumberOfArguments, duplicatedDefunBranch.NumberOfArguments);

                    foreach (ISymbol symbol in subtree.Grammar.Symbols)
                    {
                        if (subtree.Grammar.IsAllowedChildSymbol(symbol, matchingInvokeSymbol))
                        {
                            subtree.Grammar.AddAllowedChildSymbol(symbol, invokeSymbol);
                        }
                        else
                        {
                            for (int i = 0; i < subtree.Grammar.GetMaximumSubtreeCount(symbol); i++)
                            {
                                if (subtree.Grammar.IsAllowedChildSymbol(symbol, matchingInvokeSymbol, i))
                                {
                                    subtree.Grammar.AddAllowedChildSymbol(symbol, invokeSymbol, i);
                                }
                            }
                        }
                    }

                    foreach (ISymbol symbol in subtree.Grammar.GetAllowedChildSymbols(matchingInvokeSymbol))
                    {
                        if (symbol != invokeSymbol) //avoid duplicate entry invokesymbol / invokesymbol
                        {
                            subtree.Grammar.AddAllowedChildSymbol(invokeSymbol, symbol);
                        }
                    }
                    for (int i = 0; i < subtree.Grammar.GetMaximumSubtreeCount(matchingInvokeSymbol); i++)
                    {
                        foreach (ISymbol symbol in subtree.Grammar.GetAllowedChildSymbols(matchingInvokeSymbol, i).Except(subtree.Grammar.GetAllowedChildSymbols(matchingInvokeSymbol)))
                        {
                            subtree.Grammar.AddAllowedChildSymbol(invokeSymbol, symbol, i);
                        }
                    }
                }
                // in the current subtree:
                // for all invoke nodes of the original function replace the invoke of the original function with an invoke of the new function randomly
                var originalFunctionInvocations = from node in subtree.IterateNodesPrefix().OfType <InvokeFunctionTreeNode>()
                                                  where node.Symbol.FunctionName == selectedBranch.FunctionName
                                                  select node;
                foreach (var originalFunctionInvokeNode in originalFunctionInvocations)
                {
                    var newInvokeSymbol = (from symb in subtree.Grammar.Symbols.OfType <InvokeFunction>()
                                           where symb.FunctionName == duplicatedDefunBranch.FunctionName
                                           select symb).Single();
                    // flip coin wether to replace with newly defined function
                    if (random.NextDouble() < 0.5)
                    {
                        originalFunctionInvokeNode.Symbol = newInvokeSymbol;
                    }
                }
            }
            return(true);
        }
 private ISymbolicExpressionTreeNode ParseInvoke(Queue<Token> tokens) {
   Token invokeTok = tokens.Dequeue();
   Debug.Assert(invokeTok.StringValue == "CALL");
   InvokeFunction invokeSym = new InvokeFunction(tokens.Dequeue().StringValue);
   ISymbolicExpressionTreeNode invokeNode = invokeSym.CreateTreeNode();
   return invokeNode;
 }
Пример #10
0
 private InvokeFunction(InvokeFunction original, Cloner cloner)
   : base(original, cloner) {
   functionName = original.functionName;
   name = "Invoke: " + original.functionName;
 }