// // START-InferenceProcedure public InferenceResult ask(FOLKnowledgeBase kb, Sentence aQuery) { // // Get the background knowledge - are assuming this is satisfiable // as using Set of Support strategy. List <Clause> bgClauses = new List <Clause>(kb.getAllClauses()); List <Clause> removeList = SubsumptionElimination.findSubsumedClauses(bgClauses); foreach (Clause c in removeList) { bgClauses.Remove(c); } List <Chain> background = createChainsFromClauses(bgClauses); // Collect the information necessary for constructing // an answer (supports use of answer literals). AnswerHandler ansHandler = new AnswerHandler(kb, aQuery, maxQueryTime, this); IndexedFarParents ifps = new IndexedFarParents(ansHandler .getSetOfSupport(), background); // Iterative deepening to be used for (int maxDepth = 1; maxDepth < int.MaxValue; maxDepth++) { // Track the depth actually reached ansHandler.resetMaxDepthReached(); if (null != tracer) { tracer.reset(); } foreach (Chain nearParent in ansHandler.getSetOfSupport()) { recursiveDLS(maxDepth, 0, nearParent, ifps, ansHandler); if (ansHandler.isComplete()) { return(ansHandler); } } // This means the search tree // has bottomed out (i.e. finite). // Return what I know based on exploring everything. if (ansHandler.getMaxDepthReached() < maxDepth) { return(ansHandler); } } return(ansHandler); }
// START-InferenceProcedure public InferenceResult ask(FOLKnowledgeBase KB, Sentence alpha) { List <Clause> sos = new List <Clause>(); List <Clause> usable = new List <Clause>(); // Usable set will be the set of clauses in the KB, // are assuming this is satisfiable as using the // Set of Support strategy. foreach (Clause c in KB.getAllClauses()) { Clause c2 = KB.standardizeApart(c); c2.setStandardizedApartCheckNotRequired(); usable.AddRange(c2.getFactors()); } // Ensure reflexivity axiom is added to usable if using paramodulation. if (isUseParamodulation()) { // Reflexivity Axiom: x = x TermEquality reflexivityAxiom = new TermEquality(new Variable("x"), new Variable("x")); Clause reflexivityClause = new Clause(); reflexivityClause.addLiteral(new Literal(reflexivityAxiom)); reflexivityClause = KB.standardizeApart(reflexivityClause); reflexivityClause.setStandardizedApartCheckNotRequired(); usable.Add(reflexivityClause); } Sentence notAlpha = new NotSentence(alpha); // Want to use an answer literal to pull // query variables where necessary Literal answerLiteral = KB.createAnswerLiteral(notAlpha); List <Variable> answerLiteralVariables = KB .collectAllVariables(answerLiteral.getAtomicSentence()); Clause answerClause = new Clause(); if (answerLiteralVariables.Count > 0) { Sentence notAlphaWithAnswer = new ConnectedSentence(Connectors.OR, notAlpha, answerLiteral.getAtomicSentence()); foreach (Clause c in KB.convertToClauses(notAlphaWithAnswer)) { Clause c2 = KB.standardizeApart(c); c2.setProofStep(new ProofStepGoal(c2)); c2.setStandardizedApartCheckNotRequired(); sos.AddRange(c2.getFactors()); } answerClause.addLiteral(answerLiteral); } else { foreach (Clause c in KB.convertToClauses(notAlpha)) { Clause c2 = KB.standardizeApart(c); c2.setProofStep(new ProofStepGoal(c2)); c2.setStandardizedApartCheckNotRequired(); sos.AddRange(c2.getFactors()); } } // Ensure all subsumed clauses are removed foreach (Clause c in SubsumptionElimination.findSubsumedClauses(usable)) { usable.Remove(c); } foreach (Clause c in SubsumptionElimination.findSubsumedClauses(sos)) { sos.Remove(c); } OTTERAnswerHandler ansHandler = new OTTERAnswerHandler(answerLiteral, answerLiteralVariables, answerClause, maxQueryTime); IndexedClauses idxdClauses = new IndexedClauses( getLightestClauseHeuristic(), sos, usable); return(otter(ansHandler, idxdClauses, sos, usable)); }
public void testFindSubsumedClauses() { // Taken from AIMA2e pg 679 FOLDomain domain = new FOLDomain(); domain.addPredicate("patrons"); domain.addPredicate("hungry"); domain.addPredicate("type"); domain.addPredicate("fri_sat"); domain.addPredicate("will_wait"); domain.addConstant("Some"); domain.addConstant("Full"); domain.addConstant("French"); domain.addConstant("Thai"); domain.addConstant("Burger"); FOLParser parser = new FOLParser(domain); String c1 = "patrons(v,Some)"; String c2 = "patrons(v,Full) AND (hungry(v) AND type(v,French))"; String c3 = "patrons(v,Full) AND (hungry(v) AND (type(v,Thai) AND fri_sat(v)))"; String c4 = "patrons(v,Full) AND (hungry(v) AND type(v,Burger))"; String sh = "FORALL v (will_wait(v) <=> (" + c1 + " OR (" + c2 + " OR (" + c3 + " OR (" + c4 + ")))))"; Sentence s = parser.parse(sh); CNFConverter cnfConv = new CNFConverter(parser); CNF cnf = cnfConv.convertToCNF(s); // Contains 9 duplicates Assert.AreEqual(40, cnf.getNumberOfClauses()); List <Clause> clauses = new List <Clause>(cnf.getConjunctionOfClauses()); // duplicates removed Assert.AreEqual(31, clauses.Count); foreach (Clause remove in SubsumptionElimination.findSubsumedClauses(clauses)) { clauses.Remove(remove); } // subsumed clauses removed Assert.AreEqual(8, clauses.Count); // Ensure only the 8 correct/expected clauses remain Clause cl1 = cnfConv .convertToCNF( parser .parse("(NOT(will_wait(v)) OR (patrons(v,Full) OR patrons(v,Some)))")) .getConjunctionOfClauses()[0]; Clause cl2 = cnfConv .convertToCNF( parser .parse("(NOT(will_wait(v)) OR (hungry(v) OR patrons(v,Some)))")) .getConjunctionOfClauses()[0]; Clause cl3 = cnfConv .convertToCNF( parser .parse("(NOT(will_wait(v)) OR (patrons(v,Some) OR (type(v,Burger) OR (type(v,French) OR type(v,Thai)))))")) .getConjunctionOfClauses()[0]; Clause cl4 = cnfConv .convertToCNF( parser .parse("(NOT(will_wait(v)) OR (fri_sat(v) OR (patrons(v,Some) OR (type(v,Burger) OR type(v,French)))))")) .getConjunctionOfClauses()[0]; Clause cl5 = cnfConv.convertToCNF( parser.parse("(NOT(patrons(v,Some)) OR will_wait(v))")) .getConjunctionOfClauses()[0]; Clause cl6 = cnfConv .convertToCNF( parser .parse("(NOT(hungry(v)) OR (NOT(patrons(v,Full)) OR (NOT(type(v,French)) OR will_wait(v))))")) .getConjunctionOfClauses()[0]; Clause cl7 = cnfConv .convertToCNF( parser .parse("(NOT(fri_sat(v)) OR (NOT(hungry(v)) OR (NOT(patrons(v,Full)) OR (NOT(type(v,Thai)) OR will_wait(v)))))")) .getConjunctionOfClauses()[0]; Clause cl8 = cnfConv .convertToCNF( parser .parse("(NOT(hungry(v)) OR (NOT(patrons(v,Full)) OR (NOT(type(v,Burger)) OR will_wait(v))))")) .getConjunctionOfClauses()[0]; Assert.IsTrue(clauses.Contains(cl1)); Assert.IsTrue(clauses.Contains(cl2)); Assert.IsTrue(clauses.Contains(cl3)); Assert.IsTrue(clauses.Contains(cl4)); Assert.IsTrue(clauses.Contains(cl5)); Assert.IsTrue(clauses.Contains(cl6)); Assert.IsTrue(clauses.Contains(cl7)); Assert.IsTrue(clauses.Contains(cl8)); }