// // PROTECTED: // // Note: Override this method if you wish to change the initial variable // ordering when dpllSatisfiable is called. protected ICollection <PropositionSymbol> getPropositionSymbolsInSentence(Sentence s) { ICollection <PropositionSymbol> result = CollectionFactory.CreateQueue <PropositionSymbol>( SymbolCollector.getSymbolsFrom(s)); return(result); }
/** * Adds the specified sentence to the knowledge base. * * @param aSentence * a fact to be added to the knowledge base. */ public void tell(Sentence aSentence) { if (!(sentences.Contains(aSentence))) { sentences.Add(aSentence); _asCNF = _asCNF.extend(ConvertToConjunctionOfClauses.convert(aSentence).getClauses()); symbols.AddAll(SymbolCollector.getSymbolsFrom(aSentence)); } }
protected IMap <PropositionSymbol, bool?> initializeInferred(KnowledgeBase kb) { // inferred <- a table, where inferred[s] is initially false for all // symbols IMap <PropositionSymbol, bool?> inferred = CollectionFactory.CreateInsertionOrderedMap <PropositionSymbol, bool?>(); foreach (PropositionSymbol p in SymbolCollector.getSymbolsFrom(kb.asSentence())) { inferred.Put(p, false); } return(inferred); }
public void testCollectSymbolsFromComplexSentence() { Sentence sentence = (Sentence)parser.parse("(~B11 | P12 | P21) & (B11 | ~P12) & (B11 | ~P21)"); ISet <PropositionSymbol> s = SymbolCollector.getSymbolsFrom(sentence); Assert.AreEqual(3, s.Size()); Sentence b11 = parser.parse("B11"); Sentence p21 = parser.parse("P21"); Sentence p12 = parser.parse("P12"); Assert.IsTrue(s.Contains(b11 as PropositionSymbol)); Assert.IsTrue(s.Contains(p21 as PropositionSymbol)); Assert.IsTrue(s.Contains(p12 as PropositionSymbol)); }
public void testDPLLReturnsFalseWhenOneClauseFalseInModel() { Model model = new Model(); model = model.union(new PropositionSymbol("A"), true).union( new PropositionSymbol("B"), false); Sentence sentence = parser.parse("(A | B) & (A => B)"); ISet <Clause> clauses = ConvertToConjunctionOfClauses.convert(sentence) .getClauses(); ICollection <PropositionSymbol> symbols = CollectionFactory.CreateQueue <PropositionSymbol>( SymbolCollector.getSymbolsFrom(sentence)); bool satisfiable = dpll.dpll(clauses, symbols, model); Assert.AreEqual(false, satisfiable); }
public void testIssue66() { // http://code.google.com/p/aima-java/issues/detail?id=66 Model model = new Model(); model = model.union(new PropositionSymbol("A"), false) .union(new PropositionSymbol("B"), false) .union(new PropositionSymbol("C"), true); Sentence sentence = parser.parse("((A | B) | C)"); ISet <Clause> clauses = ConvertToConjunctionOfClauses.convert(sentence) .getClauses(); ICollection <PropositionSymbol> symbols = CollectionFactory.CreateQueue <PropositionSymbol>( SymbolCollector.getSymbolsFrom(sentence)); bool satisfiable = dpll.dpll(clauses, symbols, model); Assert.AreEqual(true, satisfiable); }
// // SUPPORTING CODE // /** * Determine if KB |= α, i.e. alpha is entailed by KB. * * @param kb * a Knowledge Base in propositional logic. * @param alpha * a propositional sentence. * @return true, if α is entailed by KB, false otherwise. */ public bool isEntailed(KnowledgeBase kb, Sentence alpha) { // AIMA3e p.g. 260: kb |= alpha, can be done by testing // unsatisfiability of kb & ~alpha. ISet <Clause> kbAndNotAlpha = CollectionFactory.CreateSet <Clause>(); Sentence notQuery = new ComplexSentence(Connective.NOT, alpha); ISet <PropositionSymbol> symbols = CollectionFactory.CreateSet <PropositionSymbol>(); ICollection <PropositionSymbol> querySymbols = CollectionFactory.CreateQueue <PropositionSymbol>(SymbolCollector.getSymbolsFrom(notQuery)); kbAndNotAlpha.AddAll(kb.asCNF()); kbAndNotAlpha.AddAll(ConvertToConjunctionOfClauses.convert(notQuery).getClauses()); symbols.AddAll(querySymbols); symbols.AddAll(kb.getSymbols()); return(!dpll(kbAndNotAlpha, CollectionFactory.CreateQueue <PropositionSymbol>(symbols), new Model())); }
/** * function TT-ENTAILS?(KB, α) returns true or false. * * @param kb * KB, the knowledge base, a sentence in propositional logic * @param alpha * α, the query, a sentence in propositional logic * * @return true if KB entails α, false otherwise. */ public bool ttEntails(KnowledgeBase kb, Sentence alpha) { // symbols <- a list of proposition symbols in KB and &alpha ICollection <PropositionSymbol> symbols = CollectionFactory.CreateQueue <PropositionSymbol>(SymbolCollector.getSymbolsFrom(kb.asSentence(), alpha)); // return TT-CHECK-ALL(KB, α symbols, {}) return(ttCheckAll(kb, alpha, symbols, new Model())); }