// Artificial Intelligence A Modern Approach (2nd Edition): page 288. // COMPOSE(delta, tau) is the substitution whose effect is identical to // the effect of applying each substitution in turn. That is, // SUBST(COMPOSE(theta1, theta2), p) = SUBST(theta2, SUBST(theta1, p)) private IDictionary <Variable, ITerm> Compose(FOLKnowledgeBase KB, IDictionary <Variable, ITerm> theta1, IDictionary <Variable, ITerm> theta2) { IDictionary <Variable, ITerm> composed = new Dictionary <Variable, ITerm>(); // So that it behaves like: // SUBST(theta2, SUBST(theta1, p)) // There are two steps involved here. // See: http://logic.stanford.edu/classes/cs157/2008/notes/chap09.pdf // for a detailed discussion: // 1. Apply theta2 to the range of theta1. foreach (Variable v in theta1.Keys) { composed[v] = KB.Subst(theta2, theta1[v]); } // 2. Adjoin to delta all pairs from tau with different // domain variables. foreach (Variable v in theta2.Keys) { if (!theta1.ContainsKey(v)) { composed[v] = theta2[v]; } } return(this.CascadeSubstitutions(KB, composed)); }
// See: // http://logic.stanford.edu/classes/cs157/2008/miscellaneous/faq.html#jump165 // for need for this. private IDictionary <Variable, ITerm> CascadeSubstitutions(FOLKnowledgeBase kb, IDictionary <Variable, ITerm> theta) { foreach (Variable v in theta.Keys) { ITerm t = theta[v]; theta[v] = kb.Subst(theta, t); } return(theta); }
/// <summary> /// <code> /// function FOL-BC-ASK(KB, goals, theta) returns a set of substitutions /// input: KB, a knowledge base /// goals, a list of conjuncts forming a query (theta already applied) /// theta, the current substitution, initially the empty substitution {} /// </code> /// </summary> /// <param name="KB"></param> /// <param name="ansHandler"></param> /// <param name="goals"></param> /// <param name="theta"></param> /// <returns></returns> private IList <IList <ProofStepBwChGoal> > Folbcask(FOLKnowledgeBase KB, BCAskAnswerHandler ansHandler, IList <Literal> goals, IDictionary <Variable, ITerm> theta) { var thisLevelProofSteps = new List <IList <ProofStepBwChGoal> >(); // local variables: answers, a set of substitutions, initially empty // if goals is empty then return {theta} if (goals.Count == 0) { thisLevelProofSteps.Add(new List <ProofStepBwChGoal>()); return(thisLevelProofSteps); } // qDelta <- SUBST(theta, FIRST(goals)) Literal qDelta = KB.Subst(theta, goals[0]); // for each sentence r in KB where // STANDARDIZE-APART(r) = (p1 ^ ... ^ pn => q) foreach (Clause r in KB.GetAllDefiniteClauses()) { var standardizedR = KB.StandardizeApart(r); // and thetaDelta <- UNIFY(q, qDelta) succeeds IDictionary <Variable, ITerm> thetaDelta = KB.Unify(standardizedR.GetPositiveLiterals()[0].AtomicSentence, qDelta.AtomicSentence); if (null != thetaDelta) { // new_goals <- [p1,...,pn|REST(goals)] var newGoals = new List <Literal>(standardizedR.GetNegativeLiterals()); newGoals.AddRange(goals.Skip(1)); // answers <- FOL-BC-ASK(KB, new_goals, COMPOSE(thetaDelta, // theta)) U answers IDictionary <Variable, ITerm> composed = this.Compose(KB, thetaDelta, theta); IList <IList <ProofStepBwChGoal> > lowerLevelProofSteps = this.Folbcask( KB, ansHandler, newGoals, composed); ansHandler.AddProofStep(lowerLevelProofSteps, standardizedR, qDelta, composed); thisLevelProofSteps.AddRange(lowerLevelProofSteps); } } // return answers return(thisLevelProofSteps); }
/// <summary> /// <code> /// function FOL-FC-ASK(KB, alpha) returns a substitution or false /// inputs: KB, the knowledge base, a set of first order definite clauses /// alpha, the query, an atomic sentence /// </code> /// </summary> /// <param name="KB"></param> /// <param name="query"></param> /// <returns></returns> public IInferenceResult Ask(FOLKnowledgeBase KB, ISentence query) { // Assertions on the type of queries this Inference procedure // supports if (!(query is IAtomicSentence)) { throw new ArgumentOutOfRangeException("query", "Only Atomic Queries are supported."); } var ansHandler = new FCAskAnswerHandler(); var alpha = new Literal((IAtomicSentence)query); // local variables: new, the new sentences inferred on each iteration IList <Literal> newSentences = new List <Literal>(); // Ensure query is not already a know fact before // attempting forward chaining. ISet <IDictionary <Variable, ITerm> > answers = KB.Fetch(alpha); if (answers.Count > 0) { ansHandler.AddProofStep(new ProofStepFoChAlreadyAFact(alpha)); ansHandler.SetAnswers(answers); return(ansHandler); } // repeat until new is empty do { // new <- {} newSentences.Clear(); // for each rule in KB do // (p1 ^ ... ^ pn => q) <-STANDARDIZE-VARIABLES(rule) foreach (Clause impl in KB.GetAllDefiniteClauseImplications()) { var standardizedImpl = KB.StandardizeApart(impl); // for each theta such that SUBST(theta, p1 ^ ... ^ pn) = // SUBST(theta, p'1 ^ ... ^ p'n) // --- for some p'1,...,p'n in KB foreach (IDictionary <Variable, ITerm> theta in KB.Fetch(this.Invert(standardizedImpl.GetNegativeLiterals()))) { // q' <- SUBST(theta, q) Literal qPrime = KB.Subst(theta, standardizedImpl.GetPositiveLiterals()[0]); // if q' does not unify with some sentence already in KB or // new then do if (!KB.IsRenaming(qPrime) && !KB.IsRenaming(qPrime, newSentences)) { // add q' to new newSentences.Add(qPrime); ansHandler.AddProofStep(standardizedImpl, qPrime, theta); // theta <- UNIFY(q', alpha) // if theta is not fail then return theta if (KB.Unify(qPrime.AtomicSentence, alpha.AtomicSentence) != null) { foreach (Literal l in newSentences) { ISentence s; if (l.IsPositiveLiteral()) { s = l.AtomicSentence; } else { s = new NotSentence(l.AtomicSentence); } KB.tell(s); } ansHandler.SetAnswers(KB.Fetch(alpha)); return(ansHandler); } } } } // add new to KB foreach (Literal l in newSentences) { ISentence s; if (l.IsPositiveLiteral()) { s = l.AtomicSentence; } else { s = new NotSentence(l.AtomicSentence); } KB.tell(s); } } while (newSentences.Count > 0); // return false return(ansHandler); }