Exemple #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);
        }
Exemple #2
0
        // 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();
        }
Exemple #3
0
        //two examples of how to use the interface

        // Test if logic formula represented by s is satisfiable (i.e. exist a
        // primary inputs assignment such that s evaluate to true.
        //

        public SATStatus TestSAT(int s)
        {
            if (s == GetZero())
            {
                return(SATStatus.UNSATISFIABLE);
            }
            else if (s == GetOne())
            {
                return(SATStatus.SATISFIABLE);
            }

            int       c      = Constraint(s);
            SATStatus status = Solve();

            ReleaseConstraint(c);
            return(status);
        }
Exemple #4
0
        public static string StatusToString(SATStatus status)
        {
            switch (status)
            {
            case SATStatus.UNDETERMINED: return("UNDETERMINED");

            case SATStatus.UNSATISFIABLE: return("UNSATISFIABLE");

            case SATStatus.SATISFIABLE: return("SATISFIABLE");

            case SATStatus.TIME_OUT: return("TIME");

            case SATStatus.MEM_OUT: return("MEM");

            case SATStatus.ABORTED: return("ABORTED");
            }

            return("UNKNOWN_RESULT");
        }
Exemple #5
0
        // Test if logic formula represented by s is satisfiable. If it is,
        // return the satisfiable primary input assignments, otherwise return
        // null. Notice this function has about the same complexity as
        // TestSAT(), so you should avoid calling testSAT first and then based
        // on the result call FindSATAssignment. If you don't know whether s is
        // satisfiable, call FindSatAssignment directly, and based on the return
        // value you should know if s is satisfiable or not.
        //
        public int[] FindSatAssignment(int s)
        {
            int[] pi_values = null;

            if (s == GetZero())
            {
                return(null);
            }
            else if (s == GetOne())
            {
                pi_values = new int[NumPI()];
                for (int i = 0; i < pi_values.Length; ++i)
                {
                    pi_values[i] = 2;
                }
                return(pi_values);
            }
            else
            {
                int       c      = Constraint(s);
                SATStatus status = Solve();
                if (status == SATStatus.SATISFIABLE)
                {
                    pi_values = new int[NumPI()];
                    for (int i = 0; i < NumPI(); ++i)
                    {
                        int l = LiteralValue(PrimaryInput(i));
                        if (l != 0 && l != 1)
                        {
                            pi_values[i] = 2;
                        }
                        else
                        {
                            pi_values[i] = l;
                        }
                    }
                }
                ReleaseConstraint(c);
                return(pi_values);
            }
        }
Exemple #6
0
        public SolverStats(bool do_init)
        {
            //regardless of the input, we have to init
            been_reset          = true;
            active_area_changed = true;
            constraint_zero     = 0;
            is_solving          = false;
            outcome             = SATStatus.UNDETERMINED;
            is_mem_out          = false;
            start_cpu_time      = 0;
            finish_cpu_time     = 0;

            total_cpu_time       = 0;
            num_rounds           = 0;
            num_free_variables   = 0;
            num_free_branch_vars = 0;
            num_decisions        = 0;
            num_conflicts        = 0;
            num_backtracks       = 0;
            max_dlevel           = 0;
            num_implications     = 0;

            num_cls_vars         = 0;
            num_orig_clauses     = 0;
            num_orig_literals    = 0;
            num_learned_clauses  = 0;
            num_learned_literals = 0;
            num_deleted_clauses  = 0;
            num_deleted_literals = 0;

            next_restart = 0;
            next_gc      = 0;
            next_decay   = 0;

            num_restarts            = 0;
            num_garbage_collections = 0;

            marked_current_dl = 0;
        }
Exemple #7
0
        // this demonstrate the clause interface, it reads in a CNF file and
        // Solve it, just as a regular SAT solver should do
        static void test_clause_solver(string[] args)
        {
            SATSolver solver = new SATSolver();

            // NB: on Singularity, args[0] is the exe image name
            if (args.Length != 2)
            {
                WriteLine("Simple SAT Solver using the C# interface");
                WriteLine("Usage: sharpSat CNF_file ");
                return;
            }
            StreamReader input = null;

            try {
                input = new StreamReader(args[1]);
            }
            catch (Exception e) {
                WriteLine("Error opening file '" + args[1] + "' :" + e.ToString());
                return;
            }

            string line;

            WriteLine(String.Format("Solving {0}", args[1]));
            while (true)
            {
                try{
                    line = input.ReadLine();
                }
                catch (Exception e) {
                    WriteLine("Error reading file '" + args[1] + "' :" + e.ToString());
                    line = null;
                }
                if (line == null)
                {
                    break;
                }
                string [] tokens = line.Split(new char[] { ' ', '\t' });
                int       index  = 0;
                string    token  = getToken(tokens, ref index);
                if (token == "c")
                {
                    continue;
                }
                if (token == "p")
                {
                    token = getToken(tokens, ref index);
                    if (token != "cnf")
                    {
                        WriteLine("Unrecognized Header");
                        return;
                    }
                    else
                    {
                        token = getToken(tokens, ref index);
                        solver.SetNumVariables(int.Parse(token));
                        continue;
                    }
                }
                else
                {
                    ArrayList lits = new ArrayList();
                    while (token != null && int.Parse(token) != 0)
                    {
                        lits.Add(int.Parse(token));
                        token = getToken(tokens, ref index);
                    }


                    if (lits.Count > 0)
                    {
                        int [] clause = new int [lits.Count];
                        for (int k = 0; k < lits.Count; ++k)
                        {
                            clause[k] = (int)lits[k];
                            if (clause[k] > 0)
                            {
                                clause[k] += clause[k];
                            }
                            else
                            {
                                clause[k] = 1 - clause[k] - clause[k];
                            }
                        }
                        solver.AddClause(clause);
                    }
                }
            }
            input.Close();
            SATStatus     result = solver.Solve();
            StringBuilder sb     = new StringBuilder();

            if (result == SATStatus.SATISFIABLE)
            {
                WriteLine("SAT");
                for (int i = 1; i <= solver.GetNumVariables(); ++i)
                {
                    if (solver.VariableValue(i) == 1)
                    {
                        sb.Append(String.Format("{0} ", i));
                    }
                    else if (solver.VariableValue(i) == 0)
                    {
                        sb.Append(String.Format("-{0} ", i));
                    }
                    else
                    {
                        sb.Append(String.Format("({0}) ", i));
                    }
                    if (i % 10 == 0)
                    {
                        WriteLine(sb.ToString());
                        sb.Length = 0;
                    }
                }
                WriteLine(sb.ToString());
            }
            else if (result == SATStatus.UNSATISFIABLE)
            {
                WriteLine("UNSAT");
            }

            WriteLine(String.Format("Num Variables            {0}", solver.num_variables()));
            WriteLine(String.Format("Num Orig. Clauses        {0}", solver.stats.num_orig_clauses));
            WriteLine(String.Format("Num Learned Clauses      {0}", solver.stats.num_learned_clauses));
            WriteLine(String.Format("Num Learned Literals     {0}", solver.stats.num_learned_literals));
            WriteLine(String.Format("Num Garbage Collection   {0}", solver.stats.num_garbage_collections));
            WriteLine(String.Format("Num Deleted Clauses      {0}", solver.stats.num_deleted_clauses));
            WriteLine(String.Format("Num Deleted Literals     {0}", solver.stats.num_deleted_literals));
            WriteLine(String.Format("Num Decisions            {0}", solver.stats.num_decisions));
            WriteLine(String.Format("Num Backtracks           {0}", solver.stats.num_backtracks));
            WriteLine(String.Format("Num Implications         {0}", solver.stats.num_implications));
            WriteLine(String.Format("Total Runtime            {0}", solver.stats.total_cpu_time));
            WriteLine(String.Format("Instance is              {0}", SATCommon.StatusToString(result)));
        }
Exemple #8
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);
        }
Exemple #9
0
        static bool test_interpolant_clauses(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);
            int a_gid = solver.AllocGID();
            int b_gid = solver.AllocGID();

            //1. Test if A is satisfiable
            for (int i = 0; i < n_cls; ++i)
            {
                solver.AddClause(a_clauses[i], a_gid);
            }
            if (solver.Solve() != SATStatus.SATISFIABLE)
            {
                Write("A SAT");
                return(false);
            }
            solver.DeleteGroup(a_gid);
            //1. Test if B is satisfiable
            for (int i = 0; i < n_cls; ++i)
            {
                solver.AddClause(b_clauses[i], b_gid);
            }
            if (solver.Solve() != SATStatus.SATISFIABLE)
            {
                Write("B SAT");
                return(false);
            }
            //3. Generate Interpolant
            a_gid = solver.AllocGID();
            for (int i = 0; i < n_cls; ++i)
            {
                solver.AddClause(a_clauses[i], a_gid);
            }
            Write("Interpolant ");
            int interpolant = solver.GenInterpolantFromClauseGroups(a_gid, b_gid);

            if (interpolant == -1)
            {
                Write("AB SAT");
                return(false);
            }
            solver.Reference(interpolant);
            //now test IB Unsatisfiable
            Write("IB ");
            solver.DeleteGroup(a_gid);
            sharp_assert(solver.stats.num_orig_clauses == n_cls);
            int       c      = solver.Constraint(interpolant);
            SATStatus status = solver.Solve();

            if (status != SATStatus.UNSATISFIABLE)
            {
                sharp_assert(false);
            }

            //4. test AI Satisfiable
            Write("AI ");
            solver.DeleteGroup(b_gid);
            a_gid = solver.AllocGID();
            for (int i = 0; i < n_cls; ++i)
            {
                solver.AddClause(a_clauses[i], a_gid);
            }
            status = solver.Solve();
            if (status != SATStatus.SATISFIABLE)
            {
                assert(false);
            }
            //5. test AI' Unsat (i.e. A=>I)
            Write("AI' ");
            solver.ReleaseConstraint(c);
            c      = solver.Constraint(solver.Not(interpolant));
            status = solver.Solve();
            if (status != SATStatus.UNSATISFIABLE)
            {
                sharp_assert(false);
            }
            solver.ReleaseConstraint(c);
            return(true);
        }
Exemple #10
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");
                }
            }
        }