public static FOLKnowledgeBase createLovesAnimalKnowledgeBase(InferenceProcedure infp) { FOLKnowledgeBase kb = new FOLKnowledgeBase(DomainFactory.lovesAnimalDomain(), infp); kb.tell("FORALL x (FORALL y (Animal(y) => Loves(x, y)) => EXISTS y Loves(y, x))"); kb.tell("FORALL x (EXISTS y (Animal(y) AND Kills(x, y)) => FORALL z NOT(Loves(z, x)))"); kb.tell("FORALL x (Animal(x) => Loves(Jack, x))"); kb.tell("(Kills(Jack, Tuna) OR Kills(Curiosity, Tuna))"); kb.tell("Cat(Tuna)"); kb.tell("FORALL x (Cat(x) => Animal(x))"); return(kb); }
public void testExamplePg296AIMA2e() { FOLDomain domain = DomainFactory.lovesAnimalDomain(); FOLParser parser = new FOLParser(domain); Sentence origSentence = parser .parse("FORALL x (FORALL y (Animal(y) => Loves(x, y)) => EXISTS y Loves(y, x))"); CNFConverter cnfConv = new CNFConverter(parser); CNF cnf = cnfConv.convertToCNF(origSentence); Assert.AreEqual( "[Animal(SF0(x)), Loves(SF1(x),x)],[~Loves(x,SF0(x)), Loves(SF1(x),x)]", cnf.ToString()); }
static void fOL_CNFConversion() { System.Console.WriteLine("-------------------------------------------------"); System.Console.WriteLine("Conjuctive Normal Form for First Order Logic Demo"); System.Console.WriteLine("-------------------------------------------------"); FOLDomain domain = DomainFactory.lovesAnimalDomain(); FOLParser parser = new FOLParser(domain); Sentence origSentence = parser.parse("FORALL x (FORALL y (Animal(y) => Loves(x, y)) => EXISTS y Loves(y, x))"); CNFConverter cnfConv = new CNFConverter(parser); CNF cnf = cnfConv.convertToCNF(origSentence); System.Console.WriteLine("Convert '" + origSentence + "' to CNF."); System.Console.WriteLine("CNF=" + cnf.ToString()); System.Console.WriteLine(""); }
public void testExamplesPg299AIMA2e() { FOLDomain domain = DomainFactory.lovesAnimalDomain(); FOLParser parser = new FOLParser(domain); // FOL A. Sentence origSentence = parser .parse("FORALL x (FORALL y (Animal(y) => Loves(x, y)) => EXISTS y Loves(y, x))"); CNFConverter cnfConv = new CNFConverter(parser); CNF cnf = cnfConv.convertToCNF(origSentence); // CNF A1. and A2. Assert.AreEqual( "[Animal(SF0(x)), Loves(SF1(x),x)],[~Loves(x,SF0(x)), Loves(SF1(x),x)]", cnf.ToString()); // FOL B. origSentence = parser .parse("FORALL x (EXISTS y (Animal(y) AND Kills(x, y)) => FORALL z NOT(Loves(z, x)))"); cnf = cnfConv.convertToCNF(origSentence); // CNF B. Assert.AreEqual("[~Animal(y), ~Kills(x,y), ~Loves(z,x)]", cnf.ToString()); // FOL C. origSentence = parser.parse("FORALL x (Animal(x) => Loves(Jack, x))"); cnf = cnfConv.convertToCNF(origSentence); // CNF C. Assert.AreEqual("[~Animal(x), Loves(Jack,x)]", cnf.ToString()); // FOL D. origSentence = parser .parse("(Kills(Jack, Tuna) OR Kills(Curiosity, Tuna))"); cnf = cnfConv.convertToCNF(origSentence); // CNF D. Assert.AreEqual("[Kills(Curiosity,Tuna), Kills(Jack,Tuna)]", cnf.ToString()); // FOL E. origSentence = parser.parse("Cat(Tuna)"); cnf = cnfConv.convertToCNF(origSentence); // CNF E. Assert.AreEqual("[Cat(Tuna)]", cnf.ToString()); // FOL F. origSentence = parser.parse("FORALL x (Cat(x) => Animal(x))"); cnf = cnfConv.convertToCNF(origSentence); // CNF F. Assert.AreEqual("[~Cat(x), Animal(x)]", cnf.ToString()); // FOL G. origSentence = parser.parse("NOT(Kills(Curiosity, Tuna))"); cnf = cnfConv.convertToCNF(origSentence); // CNF G. Assert.AreEqual("[~Kills(Curiosity,Tuna)]", cnf.ToString()); }
public void testBinaryResolventsOrderDoesNotMatter() { // This is a regression test, to ensure // the ordering of resolvents does not matter. // If the order ends up mattering, then likely // a problem was introduced in the Clause class // unifier, or related class. // Set up the initial set of clauses based on the // loves animal domain as it contains functions // new clauses will always be created (i.e. is an // infinite universe of discourse). FOLKnowledgeBase kb = new FOLKnowledgeBase(DomainFactory.lovesAnimalDomain()); kb.tell("FORALL x (FORALL y (Animal(y) => Loves(x, y)) => EXISTS y Loves(y, x))"); kb.tell("FORALL x (EXISTS y (Animal(y) AND Kills(x, y)) => FORALL z NOT(Loves(z, x)))"); kb.tell("FORALL x (Animal(x) => Loves(Jack, x))"); kb.tell("(Kills(Jack, Tuna) OR Kills(Curiosity, Tuna))"); kb.tell("Cat(Tuna)"); kb.tell("FORALL x (Cat(x) => Animal(x))"); ISet <Clause> clauses = CollectionFactory.CreateSet <Clause>(); clauses.AddAll(kb.getAllClauses()); ISet <Clause> newClauses = CollectionFactory.CreateSet <Clause>(); long maxRunTime = 30 * 1000; // 30 seconds IDateTime finishTime = CommonFactory.Now().AddMilliseconds(maxRunTime); do { clauses.AddAll(newClauses); newClauses.Clear(); Clause[] clausesA = clauses.ToArray(); for (int i = 0; i < clausesA.Length; ++i) { Clause cI = clausesA[i]; for (int j = 0; j < clausesA.Length; j++) { Clause cJ = clausesA[j]; newClauses.AddAll(cI.getFactors()); newClauses.AddAll(cJ.getFactors()); ISet <Clause> cIresolvents = cI.binaryResolvents(cJ); ISet <Clause> cJresolvents = cJ.binaryResolvents(cI); if (!cIresolvents.SequenceEqual(cJresolvents)) { System.Console.WriteLine("cI=" + cI); System.Console.WriteLine("cJ=" + cJ); System.Console.WriteLine("cIR=" + cIresolvents); System.Console.WriteLine("cJR=" + cJresolvents); Assert.Fail("Ordering of binary resolvents has become important, which should not be the case"); } foreach (Clause r in cIresolvents) { newClauses.AddAll(r.getFactors()); } if (CommonFactory.Now().BiggerThan(finishTime)) { break; } } if (CommonFactory.Now().BiggerThan(finishTime)) { break; } } } while (CommonFactory.Now().SmallerThan(finishTime)); }