public CNF Transform() { var res = Transform(logTree.ReturnRoot()); cnf = res.Item2; cnf.Add(res.Item1); return(cnf); }
private Tuple <Literal, CNF> Transform(INode node) { if (node.GetType() == typeof(Tree.AndNode)) { var myLit = new Literal(id); vars.Add(id); id--; var res1 = Transform(node.GetRight()); var res2 = Transform(node.GetLeft()); var cnfUnion = new CNF(); cnfUnion.Add(res1.Item2); cnfUnion.Add(res2.Item2); // ∆0 = ∆2 ∪ {¬p ∨ `1, ¬p ∨ `2, ¬`1 ∨ ¬`2 ∨ p} cnfUnion.Add(new Literal[] { myLit.Inversed(), res1.Item1 }); cnfUnion.Add(new Literal[] { myLit.Inversed(), res2.Item1 }); cnfUnion.Add(new Literal[] { myLit, res1.Item1.Inversed(), res2.Item1.Inversed() }); return(Tuple.Create(myLit, cnfUnion)); } else if (node.GetType() == typeof(Tree.OrNode)) { var myLit = new Literal(id); vars.Add(id); id--; var res1 = Transform(node.GetRight()); var res2 = Transform(node.GetLeft()); var cnfUnion = new CNF(); cnfUnion.Add(res1.Item2); cnfUnion.Add(res2.Item2); // ∆0 = ∆2 ∪ {¬p ∨ `1 ∨ `2, ¬`1 ∨ p, ¬`2 ∨ p} cnfUnion.Add(new Literal[] { myLit.Inversed(), res1.Item1, res2.Item1 }); cnfUnion.Add(new Literal[] { myLit, res2.Item1.Inversed() }); cnfUnion.Add(new Literal[] { myLit, res1.Item1.Inversed() }); return(Tuple.Create(myLit, cnfUnion)); } else if (node.GetType() == typeof(Tree.NotNode)) { if (node.GetLeft().GetType() == typeof(Tree.TermNode)) { int num = ((Tree.TermNode)node.GetLeft()).Num; vars.Add(num); return(Tuple.Create(new Literal(num, true), new CNF())); //not x_i } else { var cnfLoc = Transform(node.GetLeft()); return(Tuple.Create(cnfLoc.Item1.Inversed(), cnfLoc.Item2)); } } else if (node.GetType() == typeof(Tree.TermNode)) { int num = ((Tree.TermNode)node).Num; vars.Add(num); return(Tuple.Create(new Literal(num), new CNF())); // x_i } else { throw new ArgumentException(); } }
private bool DPLLAlgo(CNF f, List <int> unchLit, Dictionary <int, int> totalModel) { //Console.WriteLine(f); CNF newf = f; do { if (newf.IsEmpty()) { model = new Dictionary <int, int>(totalModel); return(true); } if (newf.HasEmptyConj()) { return(false); } var unionLit = newf.GetUnionDisj(); HashSet <Literal> newLit = new HashSet <Literal>(); foreach (var u in unionLit) { UnitPropagation(u, totalModel, newLit); } unchLit.RemoveAll(x => newLit.Select(y => y.Name).Contains(x)); newf = EliminatePureLiterals(newf, newLit); } while (newf.GetUnionDisj().Count > 0); if (newf.IsEmpty()) { model = new Dictionary <int, int>(totalModel); return(true); } if (newf.HasEmptyConj()) { return(false); } var l = ChooseLiteral(unchLit); if (l.Name == 0) // нет невыьранных литералов { Console.WriteLine("no Lit: " + newf.Count); return(DPLLAlgo(newf, unchLit, totalModel)); } else { newf.Add(l); if (DPLLAlgo(newf, new List <int>(unchLit), new Dictionary <int, int>(totalModel))) { return(true); } else { //Console.WriteLine("Back Track: " + l + " " + newf.Count + " UncLit:" + unchLit.Count); newf.AddInversed(l); return(DPLLAlgo(newf, unchLit, totalModel)); } } }