Example #1
0
        static bool test_existential_quantification(int n_cls, int n_vars, int n_qvar, int rseed)
        {
            rand = new Random(rseed);
            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();
            int s  = build_irregular_struct(solver, clauses, 1);
            int q1 = solver.expand_exist_quantify(s, qvars);
            //int q2 = solver.enum_exist_quantify(s, qvars);
            int q2 = solver.enum_exist_quantify_smart(s, qvars);

            SATStatus status = solver.solve();

            sharp_assert(status == SATStatus.SATISFIABLE);
            int notequ = solver.Xor(q1, q2);

            solver.Constraint(notequ);
            status = solver.Solve();
            sharp_assert(status == SATStatus.UNSATISFIABLE);
            return(true);
        }
Example #2
0
        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());
        }
Example #3
0
        static bool test_interpolant_structure(int n_vars, int n_cls, int span, int randseed)
        {
            rand = new Random(randseed);
            int [] [] a_clauses;
            int [] [] b_clauses;
            a_clauses = new int [n_cls][];
            b_clauses = new int [n_cls][];
            for (int i = 0; i < n_cls; ++i)
            {
                a_clauses[i] = randclause(1, n_vars / 2 + span);
            }
            for (int i = 0; i < n_cls; ++i)
            {
                b_clauses[i] = randclause(n_vars / 2 - span, n_vars + 1);
            }

            SATSolver solver = new SATSolver();

            solver.SetNumVariables(n_vars);
            solver.ConvertVarsToPI();
            int s_a = build_struct(solver, a_clauses);
            int s_b = build_struct(solver, b_clauses);

            solver.Reference(s_a);
            solver.Reference(s_b);
            //1. Test if A is satisfiable
            int c_a = solver.Constraint(s_a);

            if (solver.Solve() != SATStatus.SATISFIABLE)
            {
                Write("A SAT");
                return(false);
            }
            solver.ReleaseConstraint(c_a);
            //2. Test if B is satisfiable
            int c_b = solver.Constraint(s_b);

            if (solver.Solve() != SATStatus.SATISFIABLE)
            {
                Write("B SAT");
                return(false);
            }
            solver.ReleaseConstraint(c_b);
            //now get interpolant I
            Write("Interpolant ");
            int interpolant = solver.GenInterpolantFromSignals(s_a, s_b);

            if (interpolant == -1)
            {
                Write("AB SAT");
                return(false);
            }
            solver.Reference(interpolant);
            //3. test IB Unsatisfiable
            Write("IB ");
            c_b = solver.Constraint(s_b);
            int       c_i    = solver.Constraint(interpolant);
            SATStatus status = solver.Solve();

            if (status != SATStatus.UNSATISFIABLE)
            {
                sharp_assert(false);
            }
            solver.ReleaseConstraint(c_b);
            //4. test AI Satisfiable
            Write("AI ");
            c_a    = solver.Constraint(s_a);
            status = solver.Solve();
            if (status != SATStatus.SATISFIABLE)
            {
                assert(false);
            }
            //5. test AI' Unsat (i.e. A=>I)
            Write("AI' ");
            solver.ReleaseConstraint(c_i);
            solver.Constraint(solver.Not(interpolant));
            status = solver.Solve();
            if (status != SATStatus.UNSATISFIABLE)
            {
                sharp_assert(false);
            }
            return(true);
        }
Example #4
0
        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");
                }
            }
        }