public ProbabilityTable pointwiseProductPOS(ProbabilityTable multiplier, params IRandomVariable[] prodVarOrder)
        {
            ProbabilityTable product = new ProbabilityTable(prodVarOrder);

            if (!product.randomVarInfo.GetKeys()
                .SequenceEqual(
                    SetOps.union(CollectionFactory.CreateSet <IRandomVariable>(randomVarInfo.GetKeys()), CollectionFactory.CreateSet <IRandomVariable>(multiplier.randomVarInfo.GetKeys()))))
            {
                throw new IllegalArgumentException("Specified list deatailing order of mulitplier is inconsistent.");
            }

            // If no variables in the product
            if (1 == product.getValues().Length)
            {
                product.getValues()[0] = getValues()[0] * multiplier.getValues()[0];
            }
            else
            {
                // Otherwise need to iterate through the product
                // to calculate its values based on the terms.
                object[] term1Values        = new object[randomVarInfo.Size()];
                object[] term2Values        = new object[multiplier.randomVarInfo.Size()];
                ProbabilityTableIterator di = new ProbabilityTablecIteratorImpl3(term1Values,
                                                                                 term2Values, product, multiplier, this);
                product.iterateOverTable(di);
            }

            return(product);
        }
Exemple #2
0
        public List <Symbol> getPureSymbolsIn(Sentence sentence)
        {
            List <Symbol> allPureNegatives = getPureNegativeSymbolsIn(sentence);
            List <Symbol> allPurePositives = getPurePositiveSymbolsIn(sentence);

            return(SetOps.union(allPurePositives, allPureNegatives));
        }
        public ProbabilityTable pointwiseProduct(ProbabilityTable multiplier)
        {
            ISet <IRandomVariable> prodVars = SetOps.union(CollectionFactory.CreateSet <IRandomVariable>(randomVarInfo.GetKeys()),
                                                           CollectionFactory.CreateSet <IRandomVariable>(multiplier.randomVarInfo.GetKeys()));

            return(pointwiseProductPOS(multiplier, prodVars.ToArray()));
        }
        public ProbabilityTable pointwiseProductPOS(
            ProbabilityTable multiplier, params RandomVariable[] prodVarOrder)
        {
            ProbabilityTable product = new ProbabilityTable(prodVarOrder);

            if (!product.randomVarInfo.keySet().Equals(
                    SetOps.union(new List <RandomVariable>(randomVarInfo.keySet()),
                                 new List <RandomVariable>(multiplier.randomVarInfo
                                                           .keySet()))))
            {
                if (1 == product.getValues().Length)
                {
                    product.getValues()[0] = getValues()[0] * multiplier.getValues()[0];
                }
                else
                {
                    // Otherwise need to iterate through the product
                    // to calculate its values based on the terms.
                    Object[] term1Values = new Object[randomVarInfo.size()];
                    Object[] term2Values = new Object[multiplier.randomVarInfo
                                                      .size()];
                    //ProbabilityTable.Iterator di = new ProbabilityTable.Iterator() {
                    //    private int idx = 0;

                    //    public void iterate(Map<RandomVariable, Object> possibleWorld,
                    //            double probability) {
                    //        int term1Idx = termIdx(term1Values, ProbabilityTable.this,
                    //                possibleWorld);
                    //        int term2Idx = termIdx(term2Values, multiplier,
                    //                possibleWorld);

                    //        product.getValues()[idx] = getValues()[term1Idx]
                    //                * multiplier.getValues()[term2Idx];

                    //        idx++;
                    //    }

                    //    private int termIdx(Object[] termValues, ProbabilityTable d,
                    //            Map<RandomVariable, Object> possibleWorld) {
                    //        if (0 == termValues.Length) {
                    //            // The term has no variables so always position 0.
                    //            return 0;
                    //        }

                    //        int i = 0;
                    //        for (RandomVariable rv : d.randomVarInfo.keySet()) {
                    //            termValues[i] = possibleWorld.get(rv);
                    //            i++;
                    //        }

                    //        return d.getIndex(termValues);
                    //    }
                    //};
                    //product.iterateOverTable(di);
                    // TODO
                }
            }
            return(product);
        }
        public ProbabilityTable pointwiseProduct(ProbabilityTable multiplier)
        {
            List <RandomVariable> prodVars = SetOps.union(new List <RandomVariable>(randomVarInfo.Keys),
                                                          new List <RandomVariable>(multiplier.randomVarInfo.Keys));

            return(pointwiseProductPOS(multiplier, prodVars
                                       .ToArray()));
        }
Exemple #6
0
        public virtual ISet <T> visitBinarySentence(ComplexSentence s, ISet <T> arg)
        {
            ISet <T> termunion = SetOps.union(
                s.getSimplerSentence(0).accept(this, arg), s
                .getSimplerSentence(1).accept(this, arg));

            return(SetOps.union(arg, termunion));
        }
Exemple #7
0
        public bool ttEntails(KnowledgeBase kb, String alpha)
        {
            Sentence        kbSentence    = kb.asSentence();
            Sentence        querySentence = (Sentence) new PEParser().parse(alpha);
            SymbolCollector collector     = new SymbolCollector();
            List <Symbol>   kbSymbols     = collector.getSymbolsIn(kbSentence);
            List <Symbol>   querySymbols  = collector.getSymbolsIn(querySentence);
            List <Symbol>   symbols       = SetOps.union(kbSymbols, querySymbols);
            List <Symbol>   symbolList    = symbols;

            return(ttCheckAll(kbSentence, querySentence, symbolList, new Model()));
        }
Exemple #8
0
        public void testUnion()
        {
            ISet <int> union = SetOps.union(s1, s2);

            Assert.AreEqual(6, union.Size());
            Assert.AreEqual(4, s1.Size());
            Assert.AreEqual(3, s2.Size());

            s1.Remove(1);
            Assert.AreEqual(6, union.Size());
            Assert.AreEqual(3, s1.Size());
            Assert.AreEqual(3, s2.Size());
        }
Exemple #9
0
        public void testUnion()
        {
            List <int> union = SetOps.union(s1, s2);

            Assert.AreEqual(6, union.Count);
            Assert.AreEqual(4, s1.Count);
            Assert.AreEqual(3, s2.Count);

            s1.Remove(1);
            Assert.AreEqual(6, union.Count);
            Assert.AreEqual(3, s1.Count);
            Assert.AreEqual(3, s2.Count);
        }
Exemple #10
0
        public override Object visitNotSentence(UnarySentence ns, Object arg)
        {
            List <Symbol> s = (List <Symbol>)arg;

            if (ns.getNegated() is Symbol)
            {
                s.Add((Symbol)ns.getNegated());
            }
            else
            {
                s = SetOps
                    .union(s, (List <Symbol>)ns.getNegated().accept(this, arg));
            }
            return(s);
        }
Exemple #11
0
        public override Object visitNotSentence(UnarySentence ns, Object arg)
        {
            List <Symbol> s = (List <Symbol>)arg;

            if (ns.getNegated() is Symbol)
            {
                // do nothing .do NOT add a negated Symbol
            }
            else
            {
                s = SetOps
                    .union(s, (List <Symbol>)ns.getNegated().accept(this, arg));
            }
            return(s);
        }
Exemple #12
0
        public virtual Object visitBinarySentence(BinarySentence bs, Object arg)
        {
            List <Sentence> s = new List <Sentence>();

            if (arg is List <Symbol> )
            {
                List <Symbol> symbols = ((List <Symbol>)arg);
                foreach (Symbol symbol in symbols)
                {
                    s.Add((Sentence)symbol);
                }
            }
            else
            {
                throw new ArgumentException("Could not cast arg to List<Sentence>");
            }
            List <Sentence> termunion = SetOps.union((List <Sentence>)bs.getFirst().accept(this, arg),
                                                     (List <Sentence>)bs.getSecond().accept(this, arg));

            return(SetOps.union(s, termunion));
        }
Exemple #13
0
        private Sentence createResolventClause(ClauseSymbols cs, Symbol toRemove)
        {
            List <Symbol> positiveSymbols = SetOps
                                            .union(cs.clause1PositiveSymbols, cs.clause2PositiveSymbols);
            List <Symbol> negativeSymbols = SetOps
                                            .union(cs.clause1NegativeSymbols, cs.clause2NegativeSymbols);

            if (positiveSymbols.Contains(toRemove))
            {
                positiveSymbols.Remove(toRemove);
            }
            if (negativeSymbols.Contains(toRemove))
            {
                negativeSymbols.Remove(toRemove);
            }

            positiveSymbols.Sort(new SymbolComparator());
            negativeSymbols.Sort(new SymbolComparator());

            List <Sentence> sentences = new List <Sentence>();

            for (int i = 0; i < positiveSymbols.Count; i++)
            {
                sentences.Add(positiveSymbols[i]);
            }
            for (int i = 0; i < negativeSymbols.Count; i++)
            {
                sentences.Add(new UnarySentence(negativeSymbols[i]));
            }
            if (sentences.Count == 0)
            {
                return(new Symbol("EMPTY_CLAUSE")); // == empty clause
            }
            else
            {
                return(LogicUtils.chainWith("OR", sentences));
            }
        }
Exemple #14
0
        public bool plResolution(KnowledgeBase kb, Sentence alpha)
        {
            Sentence kBAndNotAlpha = new BinarySentence("AND", kb.asSentence(),
                                                        new UnarySentence(alpha));
            List <Sentence> clauses = new CNFClauseGatherer()
                                      .getClausesFrom(new CNFTransformer().transform(kBAndNotAlpha));

            clauses = filterOutClausesWithTwoComplementaryLiterals(clauses);
            List <Sentence> newClauses = new List <Sentence>();

            while (true)
            {
                List <List <Sentence> > pairs = getCombinationPairs(clauses);

                for (int i = 0; i < pairs.Count; i++)
                {
                    List <Sentence> pair = pairs[i];
                    // System.Console.WriteLine("pair number" + i+" of "+pairs.Count);
                    List <Sentence> resolvents = plResolve(pair[0], pair[1]);
                    resolvents = filterOutClausesWithTwoComplementaryLiterals(resolvents);

                    if (resolvents.Contains(new Symbol("EMPTY_CLAUSE")))
                    {
                        return(true);
                    }
                    newClauses = SetOps.union(newClauses, resolvents);
                    // System.Console.WriteLine("clauseslist size = " +clauses.Count);
                }
                if (SetOps.intersection(newClauses, clauses).Count == newClauses
                    .Count)
                {// subset test
                    return(false);
                }
                clauses = SetOps.union(newClauses, clauses);
                clauses = filterOutClausesWithTwoComplementaryLiterals(clauses);
            }
        }
Exemple #15
0
 public List <Symbol> getComplementedSymbols()
 {
     return(SetOps.union(positiveInClause1NegativeInClause2,
                         negativeInClause1PositiveInClause2));
 }
Exemple #16
0
        public virtual Object visitNotSentence(UnarySentence ns, Object arg)
        {
            List <Sentence> s = (List <Sentence>)arg;

            return(SetOps.union(s, (List <Sentence>)ns.getNegated().accept(this, arg)));
        }
Exemple #17
0
 public virtual ISet <T> visitUnarySentence(ComplexSentence s, ISet <T> arg)
 {
     return(SetOps.union(arg, s.getSimplerSentence(0).accept(this, arg)));
 }