コード例 #1
0
        internal static void AddArgumentSymbol(ISymbolicExpressionTreeGrammar originalGrammar, ISymbolicExpressionTreeGrammar grammar, IEnumerable <int> argumentIndexes, IEnumerable <CutPoint> argumentCutPoints)
        {
            foreach (var pair in argumentIndexes.Zip(argumentCutPoints, (a, b) => new { Index = a, CutPoint = b }))
            {
                var argSymbol = new Argument(pair.Index);
                grammar.AddSymbol(argSymbol);
                grammar.SetSubtreeCount(argSymbol, 0, 0);

                foreach (var symb in grammar.Symbols)
                {
                    if (symb is ProgramRootSymbol || symb is StartSymbol)
                    {
                        continue;
                    }
                    if (originalGrammar.IsAllowedChildSymbol(symb, pair.CutPoint.Child.Symbol))
                    {
                        grammar.AddAllowedChildSymbol(symb, argSymbol);
                    }
                    else
                    {
                        for (int i = 0; i < grammar.GetMaximumSubtreeCount(symb); i++)
                        {
                            if (originalGrammar.IsAllowedChildSymbol(symb, pair.CutPoint.Child.Symbol, i))
                            {
                                grammar.AddAllowedChildSymbol(symb, argSymbol, i);
                            }
                        }
                    }
                }
            }
        }
コード例 #2
0
 internal static void SetAllowedParentSymbols(ISymbolicExpressionTreeGrammar grammar, ISymbol symbol, ISymbol newSymbol)
 {
     foreach (var symb in grammar.Symbols)
     {
         if (symb is ProgramRootSymbol)
         {
             continue;
         }
         if (newSymbol is Argument && symb is StartSymbol)
         {
             continue;
         }
         if (grammar.IsAllowedChildSymbol(symb, symbol))
         {
             grammar.AddAllowedChildSymbol(symb, newSymbol);
         }
         else
         {
             for (int i = 0; i < grammar.GetMaximumSubtreeCount(symb); i++)
             {
                 if (grammar.IsAllowedChildSymbol(symb, symbol, i))
                 {
                     grammar.AddAllowedChildSymbol(symb, newSymbol, i);
                 }
             }
         }
     }
 }
コード例 #3
0
ファイル: CutPoint.cs プロジェクト: YChuan1115/HeuristicLab
        public bool IsMatchingPointType(ISymbolicExpressionTreeNode newChild)
        {
            var parent = this.Parent;

            if (newChild == null)
            {
                // make sure that one subtree can be removed and that only the last subtree is removed
                return(grammar.GetMinimumSubtreeCount(parent.Symbol) < parent.SubtreeCount &&
                       this.ChildIndex == parent.SubtreeCount - 1);
            }
            else
            {
                // check syntax constraints of direct parent - child relation
                if (!grammar.ContainsSymbol(newChild.Symbol) ||
                    !grammar.IsAllowedChildSymbol(parent.Symbol, newChild.Symbol, this.ChildIndex))
                {
                    return(false);
                }

                bool result = true;
                // check point type for the whole branch
                newChild.ForEachNodePostfix((n) => {
                    result =
                        result &&
                        grammar.ContainsSymbol(n.Symbol) &&
                        n.SubtreeCount >= grammar.GetMinimumSubtreeCount(n.Symbol) &&
                        n.SubtreeCount <= grammar.GetMaximumSubtreeCount(n.Symbol);
                });
                return(result);
            }
        }
コード例 #4
0
        public static void IsValid(ISymbolicExpressionTreeGrammar grammar)
        {
            Assert.IsTrue(grammar.Symbols.Count() == grammar.Symbols.Distinct().Count());
            foreach (ISymbol symbol in grammar.AllowedSymbols)
            {
                Assert.IsTrue(grammar.GetAllowedChildSymbols(symbol).Count() == grammar.GetAllowedChildSymbols(symbol).Distinct().Count());
                for (int i = 0; i < grammar.GetMaximumSubtreeCount(symbol); i++)
                {
                    Assert.IsTrue(grammar.GetAllowedChildSymbols(symbol, i).Count() == grammar.GetAllowedChildSymbols(symbol, i).Distinct().Count());
                }
            }

            foreach (var symbol in grammar.ModifyableSymbols)
            {
                //check if every symbol has at least one allowed child
                for (int i = 0; i < grammar.GetMaximumSubtreeCount(symbol); i++)
                {
                    Assert.IsTrue(grammar.GetAllowedChildSymbols(symbol, i).Any());
                }
            }
        }
コード例 #5
0
 internal static void SetAllowedParentSymbols(ISymbolicExpressionTreeGrammar grammar, ISymbol symbol, ISymbol newSymbol) {
   foreach (var symb in grammar.Symbols) {
     if (symb is ProgramRootSymbol) continue;
     if (newSymbol is Argument && symb is StartSymbol) continue;
     if (grammar.IsAllowedChildSymbol(symb, symbol))
       grammar.AddAllowedChildSymbol(symb, newSymbol);
     else {
       for (int i = 0; i < grammar.GetMaximumSubtreeCount(symb); i++) {
         if (grammar.IsAllowedChildSymbol(symb, symbol, i))
           grammar.AddAllowedChildSymbol(symb, newSymbol, i);
       }
     }
   }
 }
コード例 #6
0
    internal static void AddArgumentSymbol(ISymbolicExpressionTreeGrammar originalGrammar, ISymbolicExpressionTreeGrammar grammar, IEnumerable<int> argumentIndexes, IEnumerable<CutPoint> argumentCutPoints) {
      foreach (var pair in argumentIndexes.Zip(argumentCutPoints, (a, b) => new { Index = a, CutPoint = b })) {
        var argSymbol = new Argument(pair.Index);
        grammar.AddSymbol(argSymbol);
        grammar.SetSubtreeCount(argSymbol, 0, 0);

        foreach (var symb in grammar.Symbols) {
          if (symb is ProgramRootSymbol || symb is StartSymbol) continue;
          if (originalGrammar.IsAllowedChildSymbol(symb, pair.CutPoint.Child.Symbol))
            grammar.AddAllowedChildSymbol(symb, argSymbol);
          else {
            for (int i = 0; i < grammar.GetMaximumSubtreeCount(symb); i++) {
              if (originalGrammar.IsAllowedChildSymbol(symb, pair.CutPoint.Child.Symbol, i))
                grammar.AddAllowedChildSymbol(symb, argSymbol, i);
            }
          }
        }
      }
    }
コード例 #7
0
ファイル: Util.cs プロジェクト: t-h-e/HeuristicLab
    public static void IsValid(ISymbolicExpressionTreeGrammar grammar) {
      Assert.IsTrue(grammar.Symbols.Count() == grammar.Symbols.Distinct().Count());
      foreach (ISymbol symbol in grammar.AllowedSymbols) {
        Assert.IsTrue(grammar.GetAllowedChildSymbols(symbol).Count() == grammar.GetAllowedChildSymbols(symbol).Distinct().Count());
        for (int i = 0; i < grammar.GetMaximumSubtreeCount(symbol); i++) {
          Assert.IsTrue(grammar.GetAllowedChildSymbols(symbol, i).Count() == grammar.GetAllowedChildSymbols(symbol, i).Distinct().Count());
        }
      }

      foreach (var symbol in grammar.ModifyableSymbols) {
        //check if every symbol has at least one allowed child
        for (int i = 0; i < grammar.GetMaximumSubtreeCount(symbol); i++)
          Assert.IsTrue(grammar.GetAllowedChildSymbols(symbol, i).Any());
      }
    }