// this demonstrate the structure interface static void test_structure_solver(string[] args) { SATSolver solver = new SATSolver(); int a = solver.CreatePI(); int b = solver.CreatePI(); int c = solver.CreatePI(); //int d = solver.CreatePI(); int xor_a_b = solver.Xor(a, b); int not_a = solver.Not(a); int not_b = solver.Not(b); int and_a_not_b = solver.And(a, not_b); int and_b_not_a = solver.And(b, not_a); int xor_a_b_p = solver.Or(and_a_not_b, and_b_not_a); int miter = solver.Xor(xor_a_b, xor_a_b_p); SATStatus r = solver.TestSAT(miter); WriteLine(String.Format("Test Right {0}", r.ToString())); int and_a_b = solver.And(a, b); int xor_a_b_wrong = solver.Or(and_a_not_b, and_a_b); int miter1 = solver.Xor(xor_a_b, xor_a_b_wrong); r = solver.TestSAT(miter1); WriteLine(String.Format("Test Wrong {0}", r.ToString())); int and_a_b_c = solver.And(solver.And(a, b), c); int flag = solver.AllocFlag(); solver.MarkTransitiveFanins(and_a_b_c, flag); for (int i = 0, sz = solver.NumPI(); i < sz; ++i) { int node_id = solver.NthPI(i); bool flagset = solver.IsNodeFlagSet(node_id, flag); if (flagset) { WriteLine(String.Format("PI {0} is in the transitive fanin", i)); } else { WriteLine(String.Format("PI {0} is NOT in the transitive fanin", i)); } } solver.ClearFlag(flag); MyHashtable nameMap = new MyHashtable(); nameMap.Add(0, "PrimaryIn 0"); WriteLine(solver.TreeToString(miter1, nameMap)); WriteLine(solver.TreeToString(xor_a_b, nameMap)); WriteLine("Press Return..."); System.Console.ReadLine(); }
public static bool UnitTest() { SATSolver solver = new SATSolver(); int ab = solver.NthPI(0); int bc = solver.NthPI(1); int ac = solver.NthPI(2); int not_ab = solver.Not(ab); int not_bc = solver.Not(bc); solver.Not(ac); int fafb = solver.NthPI(3); int not_fafb = solver.Not(fafb); int fbfc = solver.NthPI(4); int not_fbfc = solver.Not(fbfc); // ((a=b && b != c) || (a!=b && b=c) || (a!=b && a=c)) && f(a)!=f(b) && f(b)!=f(c) int d1 = solver.And(ab, not_bc); int d2 = solver.And(bc, not_ab); int d3 = solver.And(ac, not_ab); int c1 = solver.Or(d1, d2); int c2 = solver.Or(c1, d3); int c3 = solver.And(c2, not_fafb); int c4 = solver.And(c3, not_fbfc); int formula = c4; int[] ret = solver.FindSatAssignment(formula); if (ret == null) { return(false); } int[] clause = new int[2]; clause[0] = solver.Not(ab); clause[1] = fafb; solver.AddClause(clause); ret = solver.FindSatAssignment(formula); if (ret == null) { return(false); } return(true); }
static int build_irregular_struct(SATSolver solver, int [][] clauses, int randseed) { Random rand = new Random(randseed); int sig = solver.one(); for (int i = 0; i < clauses.Length; ++i) { int s = solver.zero(); foreach (int lit in clauses[i]) { if (rand.NextDouble() > 0.2) { s = solver.Or(s, lit); } else { s = solver.Xor(s, lit); } } if (rand.NextDouble() > 0.15) { sig = solver.And(sig, s); } else { sig = solver.Xor(sig, s); } } return(sig); }
public static bool test_small_model() { SATSolver solver = new SATSolver(); int a = solver.NthPI(0); int b = solver.NthPI(1); int c = solver.NthPI(2); int d = solver.NthPI(3); int ab = solver.And(a, b); int cd = solver.And(c, d); int abcd = solver.And(ab, cd); int constraint = solver.Constraint(abcd); solver.Solve(); int [] model = solver.FindSmallModel(); print_model(model); solver.ReleaseConstraint(constraint); int not_abcd = solver.Not(abcd); constraint = solver.Constraint(not_abcd); solver.Solve(); model = solver.FindSmallModel(); print_model(model); solver.ReleaseConstraint(constraint); int e = solver.CreatePI(); int f = solver.CreatePI(); int exf = solver.Xor(e, f); int and_exf_abcd = solver.And(exf, abcd); constraint = solver.Constraint(and_exf_abcd); solver.Solve(); model = solver.FindSmallModel(); print_model(model); solver.ReleaseConstraint(constraint); int not_and_exf_abcd = solver.Not(and_exf_abcd); constraint = solver.Constraint(not_and_exf_abcd); solver.Solve(); model = solver.FindSmallModel(); print_model(model); solver.ReleaseConstraint(constraint); return(true); }
public static void test_quantification_edge_case() { int n_cls = 100; int n_vars = 50; int n_qvar = 10; rand = new Random(10); int [] [] clauses = new int [n_cls][]; for (int i = 0; i < n_cls; ++i) { clauses[i] = randclause(1, n_vars + 1); } int [] qvars = new int [n_qvar]; for (int i = 1; i <= n_qvar; ++i) { qvars[i - 1] = i + i; } SATSolver solver = new SATSolver(); solver.SetNumVariables(n_vars); solver.ConvertVarsToPI(); build_irregular_struct(solver, clauses, 1); int t = solver.GetOne(); int f = solver.GetZero(); int s1 = solver.And(t, 10); int s2 = solver.And(f, 10); int [] bounded = new int[2]; bounded[0] = 2; bounded[1] = 3; int result = solver.EnumExistQuantify(s1, bounded); result = solver.EnumExistQuantify(s2, bounded); WriteLine(result.ToString()); }
static int build_struct(SATSolver solver, int [][] clauses) { int sig = solver.one(); for (int i = 0; i < clauses.Length; ++i) { int s = solver.zero(); foreach (int lit in clauses[i]) { s = solver.Or(s, lit); } sig = solver.And(sig, s); } return(sig); }
public static void log_execution(string [] args) { SATSolver solver = new SATSolver(); if (args.Length != 1) { WriteLine("Simple SAT Solver using the C# interface"); WriteLine("Usage: sharpSat CNF_file "); return; } FileInfo file = new FileInfo(args[0]); StreamReader input = file.OpenText(); string line; IntVector nodes = new IntVector(4); while (true) { line = input.ReadLine(); if (line == null) { break; } string [] tokens = line.Split(new char[] { ' ', '\t' }); int index = 0; string token = getToken(tokens, ref index); int k = int.Parse(token); token = getToken(tokens, ref index); sharp_assert(token == "="); token = getToken(tokens, ref index); if (token == "INIT_VARS") { solver.SetNumVariables(k); solver.ConvertVarsToPI(); nodes.resize(k + k + 2); for (int i = 0; i < k + k + 2; ++i) { nodes[i] = i; } } else if (token == "CONSTRAINT") { solver.Constraint(nodes[k]); SATStatus status = solver.Solve(); if (status == SATStatus.UNSATISFIABLE) { WriteLine("UNSAT"); } else { WriteLine("SAT"); } } else if (token == "PI") { continue; } else if (token == "CL") { IntVector lits = new IntVector(4); token = getToken(tokens, ref index); while (token != null) { lits.push_back(int.Parse(token)); token = getToken(tokens, ref index); } solver.AddClause(lits.ToArray()); } else if (token == "AND") { token = getToken(tokens, ref index); int i1 = int.Parse(token); token = getToken(tokens, ref index); int i2 = int.Parse(token); int r = solver.And(nodes[i1], nodes[i2]); if (nodes.size() < k + 2) { nodes.resize(k + 2); } nodes[k] = r; nodes [k ^ 1] = (r ^ 1); } else if (token == "XOR") { token = getToken(tokens, ref index); int i1 = int.Parse(token); token = getToken(tokens, ref index); int i2 = int.Parse(token); int r = solver.Xor(nodes[i1], nodes[i2]); if (nodes.size() < k + 1) { nodes.resize(k + 1); } nodes[k] = r; nodes [k ^ 1] = (r ^ 1); } else { fatal("Unrecognized Symbol"); } } }