public void testDefaultClauseSimplifier() { FOLDomain domain = new FOLDomain(); domain.addConstant("ZERO"); domain.addConstant("ONE"); domain.addPredicate("P"); domain.addFunction("Plus"); domain.addFunction("Power"); FOLParser parser = new FOLParser(domain); ICollection <TermEquality> rewrites = CollectionFactory.CreateQueue <TermEquality>(); rewrites.Add((TermEquality)parser.parse("Plus(x, ZERO) = x")); rewrites.Add((TermEquality)parser.parse("Plus(ZERO, x) = x")); rewrites.Add((TermEquality)parser.parse("Power(x, ONE) = x")); rewrites.Add((TermEquality)parser.parse("Power(x, ZERO) = ONE")); DefaultClauseSimplifier simplifier = new DefaultClauseSimplifier(rewrites); Sentence s1 = parser.parse("((P(Plus(y,ZERO),Plus(ZERO,y)) OR P(Power(y, ONE),Power(y,ZERO))) OR P(Power(y,ZERO),Plus(y,ZERO)))"); CNFConverter cnfConverter = new CNFConverter(parser); CNF cnf = cnfConverter.convertToCNF(s1); Assert.AreEqual(1, cnf.getNumberOfClauses()); Clause simplified = simplifier.simplify(cnf.getConjunctionOfClauses().Get(0)); Assert.AreEqual("[P(y,y), P(y,ONE), P(ONE,y)]", simplified.ToString()); }
private static void test06() //****************************************************************************80 // // Purpose: // // TEST06 calls CNF_DATA_READ to read the data of a small CNF example file. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 09 June 2008 // // Author: // // John Burkardt // { int c_num = 0; const string cnf_file_name = "cnf_io_v16_c18.cnf"; int l_num = 0; int v_num = 0; Console.WriteLine(""); Console.WriteLine("TEST06"); Console.WriteLine(" CNF_DATA_READ reads the data of a CNF file."); Console.WriteLine(""); Console.WriteLine(" Read the header of \"" + cnf_file_name + "\"."); bool error = CNF.cnf_header_read(cnf_file_name, ref v_num, ref c_num, ref l_num); switch (error) { case true: Console.WriteLine(""); Console.WriteLine(" The header information could not be read."); return; } Console.WriteLine(""); Console.WriteLine(" The number of variables V_NUM = " + v_num + ""); Console.WriteLine(" The number of clauses C_NUM = " + c_num + ""); Console.WriteLine(" The number of signed literals L_NUM = " + l_num + ""); int[] l_c_num = new int[c_num]; int[] l_val = new int[l_num]; CNF.cnf_data_read(cnf_file_name, v_num, c_num, l_num, ref l_c_num, ref l_val); Console.WriteLine(""); Console.WriteLine(" Here is the data as read from the file:"); CNF.cnf_print(v_num, c_num, l_num, l_c_num, l_val); }
public DPLLResultHolder ParallelSAT() { CNF cnf = new CNF(); cnf.ReadFormula(new StreamReader(file)); DPLL dpll = new DPLL(); return(dpll.SatisfiableParallel(cnf)); }
public void testImplicationsAndExtendedAndsOrs() { FOLDomain domain = new FOLDomain(); domain.addPredicate("Cheat"); domain.addPredicate("Extra"); domain.addPredicate("Knows"); domain.addPredicate("Diff"); domain.addPredicate("F"); domain.addPredicate("A"); domain.addPredicate("Probation"); domain.addPredicate("Award"); FOLParser parser = new FOLParser(domain); CNFConverter cnfConv = new CNFConverter(parser); // cheat(x,y) => f(x,y) Sentence def1 = parser.parse("(Cheat(x,y) => F(x,y))"); CNF cnfDef1 = cnfConv.convertToCNF(def1); Assert.AreEqual("[~Cheat(x,y), F(x,y)]", cnfDef1.ToString()); // extra(x,y) | knows(x) => a(x,y) Sentence def2 = parser.parse("((Extra(x,y) OR Knows(x)) => A(x,y))"); CNF cnfDef2 = cnfConv.convertToCNF(def2); Assert.AreEqual("[~Extra(x,y), A(x,y)],[~Knows(x), A(x,y)]", cnfDef2.ToString()); // f(x,y) & f(x,z) & diff(y,z) <=> probation(x) Sentence def3 = parser .parse("(((NOT(((F(x,y) AND F(x,z)) AND Diff(y,z)))) OR Probation(x)) AND (((F(x,y) AND F(x,z)) AND Diff(y,z)) OR NOT(Probation(x))))"); CNF cnfDef3 = cnfConv.convertToCNF(def3); Assert .AreEqual( "[~Diff(y,z), ~F(x,y), ~F(x,z), Probation(x)],[~Probation(x), F(x,y)],[~Probation(x), F(x,z)],[~Probation(x), Diff(y,z)]", cnfDef3.ToString()); // a(x,y) & a(x,z) & diff(y,z) <=> award(x) Sentence def4 = parser .parse("(((NOT(((A(x,y) AND A(x,z)) AND Diff(y,z)))) OR Award(x)) AND (((A(x,y) AND A(x,z)) AND Diff(y,z)) OR NOT(Award(x))))"); CNF cnfDef4 = cnfConv.convertToCNF(def4); Assert .AreEqual( "[~A(x,y), ~A(x,z), ~Diff(y,z), Award(x)],[~Award(x), A(x,y)],[~Award(x), A(x,z)],[~Award(x), Diff(y,z)]", cnfDef4.ToString()); // f(x,y) <=> ~a(x,y) Sentence def5 = parser .parse("( ( NOT(F(x,y)) OR NOT(A(x,y))) AND ( F(x,y) OR NOT(NOT(A(x,y))) ) )"); CNF cnfDef5 = cnfConv.convertToCNF(def5); Assert.AreEqual("[~A(x,y), ~F(x,y)],[A(x,y), F(x,y)]", cnfDef5 .ToString()); }
public void testNestedExistsAndOrs() { FOLDomain domain = new FOLDomain(); domain.addPredicate("P"); domain.addPredicate("R"); domain.addPredicate("Q"); FOLParser parser = new FOLParser(domain); Sentence origSentence = parser .parse("EXISTS w (FORALL x ( (EXISTS z (Q(w, z))) => (EXISTS y (NOT(P(x, y)) AND R(y))) ) )"); CNFConverter cnfConv = new CNFConverter(parser); CNF cnf = cnfConv.convertToCNF(origSentence); Assert.AreEqual("[~P(x,SF0(x)), ~Q(SC0,z)],[~Q(SC0,z), R(SF0(x))]", cnf.ToString()); // Ax.Ay.(p(x,y) => Ez.(q(x,y,z))) origSentence = parser .parse("FORALL x1 (FORALL y1 (P(x1, y1) => EXISTS z1 (Q(x1, y1, z1))))"); cnf = cnfConv.convertToCNF(origSentence); Assert.AreEqual("[~P(x1,y1), Q(x1,y1,SF1(x1,y1))]", cnf.ToString()); // Ex.Ay.Az.(r(y,z) <=> q(x,y,z)) origSentence = parser .parse("EXISTS x2 (FORALL y2 (FORALL z2 (R(y2, z2) <=> Q(x2, y2, z2))))"); cnf = cnfConv.convertToCNF(origSentence); Assert.AreEqual( "[~R(y2,z2), Q(SC1,y2,z2)],[~Q(SC1,y2,z2), R(y2,z2)]", cnf .ToString()); // Ax.Ey.(~p(x,y) => Az.(q(x,y,z))) origSentence = parser .parse("FORALL x3 (EXISTS y3 (NOT(P(x3, y3)) => FORALL z3 (Q(x3, y3, z3))))"); cnf = cnfConv.convertToCNF(origSentence); Assert .AreEqual("[P(x3,SF2(x3)), Q(x3,SF2(x3),z3)]", cnf .ToString()); // Ew.Ex.Ey.Ez.(r(x,y) & q(x,w,z)) origSentence = parser .parse("NOT(EXISTS w4 (EXISTS x4 (EXISTS y4 ( EXISTS z4 (R(x4, y4) AND Q(x4, w4, z4))))))"); cnf = cnfConv.convertToCNF(origSentence); Assert.AreEqual("[~Q(x4,w4,z4), ~R(x4,y4)]", cnf.ToString()); }
private static void test01() //****************************************************************************80 // // Purpose: // // TEST01 calls CNF_WRITE to write a small CNF example to a CNF file. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 06 June 2008 // // Author: // // John Burkardt // { const int C_NUM = 2; const int L_NUM = 5; const string cnf_file_name = "cnf_io_v3_c2.cnf"; int[] l_c_num = { 2, 3 } ; int[] l_val = { 1, -3, 2, 3, -1 } ; const int v_num = 3; Console.WriteLine(""); Console.WriteLine("TEST01"); Console.WriteLine(" CNF_WRITE can write CNF data to a CNF file."); Console.WriteLine(""); Console.WriteLine(" Here is the data:"); CNF.cnf_print(v_num, C_NUM, L_NUM, l_c_num, l_val); Console.WriteLine(""); Console.WriteLine(" Now we call CNF_WRITE to store this information"); Console.WriteLine(" in the file \"" + cnf_file_name + "\"."); CNF.cnf_write(v_num, C_NUM, L_NUM, l_c_num, l_val, cnf_file_name); }
public void testInductionAxiomSchema() { FOLDomain domain = new FOLDomain(); domain.addPredicate("Equal"); domain.addFunction("Plus"); domain.addConstant("A"); domain.addConstant("B"); domain.addConstant("N"); domain.addConstant("ONE"); domain.addConstant("ZERO"); FOLParser parser = new FOLParser(domain); CNFConverter cnfConv = new CNFConverter(parser); // Base Case: Sentence sent = parser .parse("NOT(FORALL x (FORALL y (Equal(Plus(Plus(x,y),ZERO), Plus(x,Plus(y,ZERO))))))"); CNF cnf = cnfConv.convertToCNF(sent); Assert.AreEqual( "[~Equal(Plus(Plus(SC0,SC1),ZERO),Plus(SC0,Plus(SC1,ZERO)))]", cnf.ToString()); // Instance of Induction Axion Scmema sent = parser .parse("((" + "Equal(Plus(Plus(A,B),ZERO), Plus(A,Plus(B,ZERO)))" + " AND " + "(FORALL x (FORALL y (FORALL z(" + "Equal(Plus(Plus(x,y),z), Plus(x,Plus(y,z)))" + " => " + "Equal(Plus(Plus(x,y),Plus(z,ONE)), Plus(x,Plus(y,Plus(z,ONE))))" + "))))" + ")" + " => " + "FORALL x (FORALL y (FORALL z(" + "Equal(Plus(Plus(x,y),z), Plus(x,Plus(y,z)))" + "))))"); cnf = cnfConv.convertToCNF(sent); Assert .AreEqual( "[~Equal(Plus(Plus(A,B),ZERO),Plus(A,Plus(B,ZERO))), Equal(Plus(Plus(q0,q1),q2),Plus(q0,Plus(q1,q2))), Equal(Plus(Plus(SC2,SC3),SC4),Plus(SC2,Plus(SC3,SC4)))],[~Equal(Plus(Plus(A,B),ZERO),Plus(A,Plus(B,ZERO))), ~Equal(Plus(Plus(SC2,SC3),Plus(SC4,ONE)),Plus(SC2,Plus(SC3,Plus(SC4,ONE)))), Equal(Plus(Plus(q0,q1),q2),Plus(q0,Plus(q1,q2)))]", cnf.ToString()); // Goal sent = parser .parse("NOT(FORALL x (FORALL y (FORALL z (Equal(Plus(Plus(x,y),z), Plus(x,Plus(y,z)))))))"); cnf = cnfConv.convertToCNF(sent); Assert.AreEqual( "[~Equal(Plus(Plus(SC5,SC6),SC7),Plus(SC5,Plus(SC6,SC7)))]", cnf.ToString()); }
public void Check() { try { if (CNF.DoWeHaveAllNeededFiles()) { ffxiprocess.GetProcess(); } } catch (Exception ex) { Logger.AddDebugText(CheckedItemsRTB, ex.ToString()); } }
private static void test05() //****************************************************************************80 // // Purpose: // // TEST05 calls CNF_HEADER_READ to read the header of a small CNF example file. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 09 June 2008 // // Author: // // John Burkardt // { int c_num = 0; const string cnf_file_name = "cnf_io_v16_c18.cnf"; int l_num = 0; int v_num = 0; Console.WriteLine(""); Console.WriteLine("TEST05"); Console.WriteLine(" CNF_HEADER_READ reads the header of a CNF file."); Console.WriteLine(""); Console.WriteLine(" Read the header of \"" + cnf_file_name + "\"."); bool error = CNF.cnf_header_read(cnf_file_name, ref v_num, ref c_num, ref l_num); switch (error) { case true: Console.WriteLine(""); Console.WriteLine(" The header information could not be read."); break; default: Console.WriteLine(""); Console.WriteLine(" The number of variables V_NUM = " + v_num + ""); Console.WriteLine(" The number of clauses C_NUM = " + c_num + ""); Console.WriteLine(" The number of signed literals L_NUM = " + l_num + ""); break; } }
public void testExamplePg295AIMA2e() { FOLDomain domain = DomainFactory.weaponsDomain(); FOLParser parser = new FOLParser(domain); Sentence origSentence = parser .parse("FORALL x ((((American(x) AND Weapon(y)) AND Sells(x, y, z)) AND Hostile(z)) => Criminal(x))"); CNFConverter cnfConv = new CNFConverter(parser); CNF cnf = cnfConv.convertToCNF(origSentence); Assert.AreEqual( "[~American(x), ~Hostile(z), ~Sells(x,y,z), ~Weapon(y), Criminal(x)]", cnf.ToString()); }
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()); }
// PRIVATE METHODS // Note: pg 278, STORE(s) concept. private /*lock*/ void store(Sentence aSentence) { originalSentences.Add(aSentence); // Convert the sentence to CNF CNF cnfOfOrig = cnfConverter.convertToCNF(aSentence); List <Clause> conjunctionOfClauses = cnfOfOrig.getConjunctionOfClauses(); foreach (Clause c in conjunctionOfClauses) { c.setProofStep(new ProofStepClauseClausifySentence(c, aSentence)); if (c.isEmpty()) { // This should not happen, if so the user // is trying to add an unsatisfiable sentence // to the KB. throw new ArgumentException( "Attempted to add unsatisfiable sentence to KB, orig=[" + aSentence + "] CNF=" + cnfOfOrig); } // Ensure all clauses added to the KB are Standardized Apart. Clause c2 = _standardizeApart.standardizeApart(c, variableIndexical); // Will make all clauses immutable // so that they cannot be modified externally. c2.setImmutable(); if (!clauses.Contains(c2)) { clauses.Add(c2); // If added keep track of special types of // clauses, as useful for query purposes if (c2.isDefiniteClause()) { allDefiniteClauses.Add(c2); } if (c2.isImplicationDefiniteClause()) { implicationDefiniteClauses.Add(c2); } if (c2.isUnitClause()) { indexFact(c2.getLiterals().First <Literal>()); } } } }
private void Store(ISentence aSentence) { originalSentences.Add(aSentence); // Convert the sentence to CNF CNF cnfOfOrig = cnfConverter.ConvertToCNF(aSentence); foreach (Clause c in cnfOfOrig.GetConjunctionOfClauses()) { c.SetProofStep(new ProofStepClauseClausifySentence(c, aSentence)); if (c.IsEmpty()) { // This should not happen, if so the user // is trying to add an unsatisfiable sentence // to the KB. throw new InvalidOperationException( "Attempted to add unsatisfiable sentence to KB, orig=[" + aSentence + "] CNF=" + cnfOfOrig); } // Ensure all clauses added to the KB are Standardized Apart. var standardizedC = standardizeApart.GetStandardizeApartResult(c, variableIndexical); // Will make all clauses immutable // so that they cannot be modified externally. standardizedC.Immutable = true; if (clauses.Add(standardizedC)) { // If added keep track of special types of // clauses, as useful for query purposes if (standardizedC.IsDefiniteClause()) { allDefiniteClauses.Add(standardizedC); } if (standardizedC.IsImplicationDefiniteClause()) { implicationDefiniteClauses.Add(standardizedC); } if (standardizedC.IsUnitClause()) { this.IndexFact(standardizedC.GetLiterals().First()); } } } }
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(""); }
/// <summary> /// Extract the CNF formulas. /// </summary> /// <returns>An enumerable of formulas in CNF form.</returns> private void ExtractCNFs(TreeNode rootNode, ref List <MultipleDisjunction> data, bool andProve = false) { string expr = textBoxInput.Text; // expression input. tabControl1.SelectedIndex = 2; // Creates a new analyzer for the expression. Analyzer asl = new Analyzer(expr); // Tokenization phase. if (!Tokenize(asl)) { return; } // Parsing phase. AST ast = CNF.Convert(asl.Parse()); CNFProposition cnf = new CNFProposition(ast); string fnc = ast.ToString(); if (andProve) { atp.Theorem = cnf; bool proved = atp.ProveIt(new ATP.ReportDelegate(Report)); string msg = proved ? "TEOREMA PROVADO!" : "NÃO CONSEGUI PROVAR A TEORIA"; MessageBoxIcon icon = proved ? MessageBoxIcon.Asterisk : MessageBoxIcon.Warning; MessageBox.Show(msg, "Resultado", MessageBoxButtons.OK, icon); } else { atp.AddPremisse(cnf); } //IEnumerable<CnfOr> orClauses = CNF.Separate(ast, true); IEnumerable <MultipleDisjunction> orClauses = cnf.Props; // Add the formula to the node and data. data.AddRange(orClauses); foreach (var clause in orClauses) { TreeNode node = new TreeNode(clause.ToString()); rootNode.Nodes.Add(node); } rootNode.ExpandAll(); }
public void testNegationsAndNestedImplications() { FOLDomain domain = new FOLDomain(); domain.addPredicate("P"); domain.addPredicate("Q"); domain.addPredicate("R"); domain.addConstant("A"); FOLParser parser = new FOLParser(domain); CNFConverter cnfConv = new CNFConverter(parser); // ~(((~p or ~q) => ~(p or q)) => r) Sentence sent = parser .parse("NOT(((((NOT(P(A)) OR NOT(Q(A)))) => NOT((P(A) OR Q(A)))) => R(A)))"); CNF cnf = cnfConv.convertToCNF(sent); Assert.AreEqual( "[~P(A), P(A)],[~P(A), Q(A)],[~Q(A), P(A)],[~Q(A), Q(A)],[~R(A)]", cnf.ToString()); }
// Convert the formula to Conjunctive Normal Form. private void buttonFNC_Click(object sender, EventArgs e) { string expr = textBoxInput.Text; // expression input. tabControl1.SelectedIndex = 1; // Creates a new analyzer for the expression. Analyzer asl = new Analyzer(expr); // Tokenization phase. if (!Tokenize(asl)) { return; } // Parsing phase. AST ast = CNF.Convert(asl.Parse()); string fnc = ASTFormat.Format(ast, ASTFormat.FormatType.PLAIN); CNFProposition cnf = new CNFProposition(ast); //IEnumerable<CnfOr> orClauses = CNF.Separate(ast, true); // Add this formula to the FNC list. RichTextTool rtt = new RichTextTool(ref richTextBoxCNF); rtt.AppendText(expr + " - "); richTextBoxCNF.SelectionBackColor = Color.Aquamarine; rtt.ToggleBold(); rtt.AppendText(fnc); richTextBoxCNF.SelectionBackColor = Color.White; rtt.ToggleBold(); rtt.AppendText(" - "); rtt.ToggleBold(); richTextBoxCNF.SelectionBackColor = Color.LawnGreen; rtt.AppendText(cnf.ToString()); rtt.Eol(); // Add the tree to the image. /* * if (m_treeFiller == null) m_treeFiller = new TreeFiller(pictureBoxTree); * m_treeFiller.Draw(ast); */ }
public ISet <Clause> ConvertToClauses(ISentence aSentence) { CNF cnf = cnfConverter.ConvertToCNF(aSentence); return(new HashedSet <Clause>(cnf.GetConjunctionOfClauses())); }
private static void test04() //*****************************************************************************80 // // Purpose: // // TEST04 calls CNF_WRITE to write a CNF example to a CNF file. // // Discussion: // // This formula is used as an example in the Quinn reference. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 06 June 2008 // // Author: // // John Burkardt // { const int C_NUM = 18; const int L_NUM = 36; const string cnf_file_name = "cnf_io_v16_c18.cnf"; int[] l_c_num = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 } ; int[] l_val = { 1, 2, -2, -4, 3, 4, -4, -5, 5, -6, 6, -7, 6, 7, 7, -16, 8, -9, -8, -14, 9, 10, 9, -10, -10, -11, 10, 12, 11, 12, 13, 14, 14, -15, 15, 16 } ; const int v_num = 16; Console.WriteLine(""); Console.WriteLine("TEST04"); Console.WriteLine(" CNF_WRITE can write CNF data to a CNF file."); Console.WriteLine(""); Console.WriteLine(" Here is the data to be written to the file:"); CNF.cnf_print(v_num, C_NUM, L_NUM, l_c_num, l_val); Console.WriteLine(""); Console.WriteLine(" Now we call CNF_WRITE to store this information"); Console.WriteLine(" in the file \"" + cnf_file_name + "\"."); CNF.cnf_write(v_num, C_NUM, L_NUM, l_c_num, l_val, cnf_file_name); }
private void checkAPIForUpdatesToolStripMenuItem1_Click(object sender, EventArgs e) { CNF.CheckAPI(); }
private static void test07() //****************************************************************************80 // // Purpose: // // TEST07 calls CNF_EVALUATE to evaluate a formula. // // Discussion: // // This formula is used as an example in the Quinn reference. // Here, we seek the logical inputs that make the formula true. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 06 June 2008 // // Author: // // John Burkardt // { const int C_NUM = 18; const int L_NUM = 36; const int V_NUM = 16; int i; int j; int[] l_c_num = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 } ; int[] l_val = { 1, 2, -2, -4, 3, 4, -4, -5, 5, -6, 6, -7, 6, 7, 7, -16, 8, -9, -8, -14, 9, 10, 9, -10, -10, -11, 10, 12, 11, 12, 13, 14, 14, -15, 15, 16 } ; bool[] v_val = new bool[V_NUM]; Console.WriteLine(""); Console.WriteLine("TEST07"); Console.WriteLine(" Seek inputs to a circuit that produce a 1 (TRUE) output."); Console.WriteLine(""); Console.WriteLine(" Here is the CNF data defining the formula:"); CNF.cnf_print(V_NUM, C_NUM, L_NUM, l_c_num, l_val); // // Initialize the logical vector. // for (j = 0; j < V_NUM; j++) { v_val[j] = false; } // // Compute the number of binary vectors to check. // int ihi = (int)Math.Pow(2, V_NUM); Console.WriteLine(""); Console.WriteLine(" Number of input vectors to check is " + ihi + ""); Console.WriteLine(""); Console.WriteLine(" # Index ---------Input Values----------"); Console.WriteLine(""); // // Check every possible input vector. // int solution_num = 0; for (i = 0; i < ihi; i++) { bool f_val = CNF.cnf_evaluate(V_NUM, C_NUM, L_NUM, l_c_num, l_val, v_val); switch (f_val) { case true: { solution_num += 1; string cout = " " + solution_num.ToString().PadLeft(2) + " " + i.ToString().PadLeft(10); for (j = 0; j < V_NUM; j++) { cout += v_val[j] ? 1 : 0; // v_val[j]; } Console.WriteLine(cout); break; } } typeMethods.lvec_next(V_NUM, ref v_val); } // // Report. // Console.WriteLine(""); Console.WriteLine(" Number of solutions found was " + solution_num + ""); }
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 testTermEquality() { FOLDomain domain = new FOLDomain(); domain.addPredicate("P"); domain.addPredicate("Q"); domain.addPredicate("R"); domain.addConstant("A"); domain.addConstant("B"); domain.addConstant("C"); domain.addConstant("D"); domain.addFunction("Plus"); domain.addConstant("ONE"); domain.addConstant("ZERO"); FOLParser parser = new FOLParser(domain); CNFConverter cnfConv = new CNFConverter(parser); // x=y Sentence sent = parser.parse("x = y"); CNF cnf = cnfConv.convertToCNF(sent); Assert.AreEqual("[x = y]", cnf.ToString()); // x!=y sent = parser.parse("NOT(x = y)"); cnf = cnfConv.convertToCNF(sent); Assert.AreEqual("[~x = y]", cnf.ToString()); // A=B sent = parser.parse("A = B"); cnf = cnfConv.convertToCNF(sent); Assert.AreEqual("[A = B]", cnf.ToString()); // A!=B sent = parser.parse("NOT(A = B)"); cnf = cnfConv.convertToCNF(sent); Assert.AreEqual("[~A = B]", cnf.ToString()); // ~(((~A=B or ~D=C) => ~(A=B or D=C)) => A=D) sent = parser .parse("NOT(((((NOT(A = B) OR NOT(D = C))) => NOT((A = B OR D = C))) => A = D))"); cnf = cnfConv.convertToCNF(sent); Assert.AreEqual( "[~A = B, A = B],[~A = B, D = C],[~D = C, A = B],[~D = C, D = C],[~A = D]", cnf.ToString()); // // Induction Axiom Schema using Term Equality // Base Case: sent = parser .parse("NOT(FORALL x (FORALL y (Plus(Plus(x,y),ZERO) = Plus(x,Plus(y,ZERO)))))"); cnf = cnfConv.convertToCNF(sent); Assert.AreEqual( "[~Plus(Plus(SC0,SC1),ZERO) = Plus(SC0,Plus(SC1,ZERO))]", cnf.ToString()); // Instance of Induction Axion Scmema sent = parser.parse("((" + "Plus(Plus(A,B),ZERO) = Plus(A,Plus(B,ZERO))" + " AND " + "(FORALL x (FORALL y (FORALL z(" + "Plus(Plus(x,y),z) = Plus(x,Plus(y,z))" + " => " + "Plus(Plus(x,y),Plus(z,ONE)) = Plus(x,Plus(y,Plus(z,ONE)))" + "))))" + ")" + " => " + "FORALL x (FORALL y (FORALL z(" + "Plus(Plus(x,y),z) = Plus(x,Plus(y,z))" + "))))"); cnf = cnfConv.convertToCNF(sent); Assert.AreEqual( "[~Plus(Plus(A,B),ZERO) = Plus(A,Plus(B,ZERO)), Plus(Plus(q0,q1),q2) = Plus(q0,Plus(q1,q2)), Plus(Plus(SC2,SC3),SC4) = Plus(SC2,Plus(SC3,SC4))],[~Plus(Plus(A,B),ZERO) = Plus(A,Plus(B,ZERO)), ~Plus(Plus(SC2,SC3),Plus(SC4,ONE)) = Plus(SC2,Plus(SC3,Plus(SC4,ONE))), Plus(Plus(q0,q1),q2) = Plus(q0,Plus(q1,q2))]", cnf.ToString()); // Goal sent = parser .parse("NOT(FORALL x (FORALL y (FORALL z (Plus(Plus(x,y),z) = Plus(x,Plus(y,z))))))"); cnf = cnfConv.convertToCNF(sent); Assert.AreEqual( "[~Plus(Plus(SC5,SC6),SC7) = Plus(SC5,Plus(SC6,SC7))]", cnf.ToString()); }
public List <Clause> convertToClauses(Sentence aSentence) { CNF cnf = cnfConverter.convertToCNF(aSentence); return(new List <Clause>(cnf.getConjunctionOfClauses())); }
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)); }
private void BuildNormalFormsClick(object sender, EventArgs e) { DNF.Clear(); CNF.Clear(); if (n == 0) { textBox2.Text = "Для начала постройте таблицу истинности!"; } else { bool tavt = true, otr = true; for (var i = 1; i < h; i++) { if (table[i, w - 1] == "1") { if (DNF.Text != "") { DNF.Text += " ⋁ "; } for (var j = 1; j <= varNames.Count; j++) { if (table[i, j] == "0") { DNF.Text += "¬" + table[0, j]; } else { DNF.Text += table[0, j]; } } tavt = false; } } if (tavt) { textBox2.Text = "Данное логическое выражение является отрицанием. Поэтому построить СДНФ невозможно"; } for (var i = 1; i < h; i++) { if (table[i, w - 1] == "0") { if (CNF.Text != "") { CNF.Text += " ⋀ ("; } else { CNF.Text += "("; } var f = true; for (var j = 1; j <= varNames.Count; j++) { if (!f) { CNF.Text += " ⋁ "; } if (table[i, j] == "1") { CNF.Text += "¬" + table[0, j]; } else { CNF.Text += table[0, j]; } f = false; } CNF.Text += ")"; otr = false; } } if (otr) { textBox2.Text = "Данное логическое выражение является тавтологией. Поэтому построить СКНФ невозможно"; } if (!otr && !tavt) { textBox2.Text = "СДНФ и СКНФ успешно построены!"; } } }
public void testComplexEquals() { FOLDomain domain = new FOLDomain(); domain.addConstant("A"); domain.addConstant("B"); domain.addConstant("C"); domain.addConstant("D"); domain.addPredicate("P"); domain.addPredicate("Animal"); domain.addPredicate("Kills"); domain.addFunction("F"); domain.addFunction("SF0"); FOLParser parser = new FOLParser(domain); CNFConverter cnfConverter = new CNFConverter(parser); Sentence s1 = parser.parse("((x1 = y1 AND y1 = z1) => x1 = z1)"); Sentence s2 = parser.parse("((x2 = y2 AND F(y2) = z2) => F(x2) = z2)"); CNF cnf1 = cnfConverter.convertToCNF(s1); CNF cnf2 = cnfConverter.convertToCNF(s2); Clause c1 = cnf1.getConjunctionOfClauses().Get(0); Clause c2 = cnf2.getConjunctionOfClauses().Get(0); Assert.IsFalse(c1.Equals(c2)); s1 = parser.parse("((x1 = y1 AND y1 = z1) => x1 = z1)"); s2 = parser.parse("((x2 = y2 AND y2 = z2) => x2 = z2)"); cnf1 = cnfConverter.convertToCNF(s1); cnf2 = cnfConverter.convertToCNF(s2); c1 = cnf1.getConjunctionOfClauses().Get(0); c2 = cnf2.getConjunctionOfClauses().Get(0); Assert.IsTrue(c1.Equals(c2)); s1 = parser.parse("((x1 = y1 AND y1 = z1) => x1 = z1)"); s2 = parser.parse("((y2 = z2 AND x2 = y2) => x2 = z2)"); cnf1 = cnfConverter.convertToCNF(s1); cnf2 = cnfConverter.convertToCNF(s2); c1 = cnf1.getConjunctionOfClauses().Get(0); c2 = cnf2.getConjunctionOfClauses().Get(0); Assert.IsTrue(c1.Equals(c2)); s1 = parser.parse("(((x1 = y1 AND y1 = z1) AND z1 = r1) => x1 = r1)"); s2 = parser.parse("(((x2 = y2 AND y2 = z2) AND z2 = r2) => x2 = r2)"); cnf1 = cnfConverter.convertToCNF(s1); cnf2 = cnfConverter.convertToCNF(s2); c1 = cnf1.getConjunctionOfClauses().Get(0); c2 = cnf2.getConjunctionOfClauses().Get(0); Assert.IsTrue(c1.Equals(c2)); s1 = parser.parse("(((x1 = y1 AND y1 = z1) AND z1 = r1) => x1 = r1)"); s2 = parser.parse("(((z2 = r2 AND y2 = z2) AND x2 = y2) => x2 = r2)"); cnf1 = cnfConverter.convertToCNF(s1); cnf2 = cnfConverter.convertToCNF(s2); c1 = cnf1.getConjunctionOfClauses().Get(0); c2 = cnf2.getConjunctionOfClauses().Get(0); Assert.IsTrue(c1.Equals(c2)); s1 = parser.parse("(((x1 = y1 AND y1 = z1) AND z1 = r1) => x1 = r1)"); s2 = parser.parse("(((x2 = y2 AND y2 = z2) AND z2 = y2) => x2 = r2)"); cnf1 = cnfConverter.convertToCNF(s1); cnf2 = cnfConverter.convertToCNF(s2); c1 = cnf1.getConjunctionOfClauses().Get(0); c2 = cnf2.getConjunctionOfClauses().Get(0); Assert.IsFalse(c1.Equals(c2)); s1 = parser .parse("(((((x1 = y1 AND y1 = z1) AND z1 = r1) AND r1 = q1) AND q1 = s1) => x1 = r1)"); s2 = parser .parse("(((((x2 = y2 AND y2 = z2) AND z2 = r2) AND r2 = q2) AND q2 = s2) => x2 = r2)"); cnf1 = cnfConverter.convertToCNF(s1); cnf2 = cnfConverter.convertToCNF(s2); c1 = cnf1.getConjunctionOfClauses().Get(0); c2 = cnf2.getConjunctionOfClauses().Get(0); Assert.IsTrue(c1.Equals(c2)); s1 = parser .parse("((((NOT(Animal(c1920)) OR NOT(Animal(c1921))) OR NOT(Kills(c1922,c1920))) OR NOT(Kills(c1919,c1921))) OR NOT(Kills(SF0(c1922),SF0(c1919))))"); s2 = parser .parse("((((NOT(Animal(c1929)) OR NOT(Animal(c1928))) OR NOT(Kills(c1927,c1929))) OR NOT(Kills(c1930,c1928))) OR NOT(Kills(SF0(c1930),SF0(c1927))))"); cnf1 = cnfConverter.convertToCNF(s1); cnf2 = cnfConverter.convertToCNF(s2); c1 = cnf1.getConjunctionOfClauses().Get(0); c2 = cnf2.getConjunctionOfClauses().Get(0); Assert.IsTrue(c1.Equals(c2)); }
public ISet <Clause> convertToClauses(Sentence sentence) { CNF cnf = cnfConverter.convertToCNF(sentence); return(CollectionFactory.CreateSet <Clause>(cnf.getConjunctionOfClauses())); }