solver/DPLLSolver.h
00001 /*
00002  * DPLLSolver.h
00003  *
00004  *  Created on: Jul 30, 2009
00005  *      Author: tdillig
00006  */
00007 
00008 #ifndef DPLLSOLVER_H_
00009 #define DPLLSOLVER_H_
00010 
00011 #include <set>
00012 #include <string>
00013 #include <vector>
00014 #include <map>
00015 using namespace std;
00016 
00017 #include "bignum.h"
00018 #include "ClauseSolve.h"
00019 #include "SkeletonSolver.h"
00020 #include "True.h"
00021 
00022 class CNode;
00023 class Term;
00024 
00025 
00026 
00027 class DPLLSolver {
00028 
00029 
00030 private:
00031 
00032         enum elem_status
00033         {
00034                 SAT,
00035                 UNSAT,
00036                 NOT_QUERIED
00037         };
00038 
00039         struct stack_elem
00040         {
00041                 CNode* constraint;
00042                 CNode* cumulative_constraint;
00043                 set<CNode*> leaves;
00044                 elem_status status;
00045                 map<Term*, SatValue> assignment;
00046 
00047                 stack_elem(CNode* node)
00048                 {
00049                         constraint = node;
00050                         cumulative_constraint = node;
00051                         constraint->get_all_leaves(leaves);
00052                         status = NOT_QUERIED;
00053                 }
00054 
00055         };
00056 
00057 
00058 
00059         CNode* assumptions;
00060         bool sat;
00061 
00062         int total_ticks;
00063         int check_prev_ticks;
00064         int num_sat_calls;
00065         int num_delta;
00066         int ticks_delta;
00067         int clause_ticks;
00068         int sat_clause_ticks;
00069         int num_clause;
00070         int num_sat_clauses;
00071         int num_sat_by_prev_assignment;
00072         int num_unknown_by_prev_assignment;
00073         map<set<CNode*>, int> sat_clauses_with_same_leaves;
00074         map<set<CNode*>, vector<map<CNode*, bool> > >  leaves_to_assignments;
00075 
00076         int num_consecutive_boolean_complete;
00077 
00078         vector<stack_elem> stack;
00079 
00080         CNode* current_assignment;
00081 
00082         SkeletonSolver s;
00083 
00084         map<Term*, SatValue>* assignments;
00085 
00086         set<Term*>& ilp_terms;
00087 
00088 
00089 public:
00090         DPLLSolver(set<Term*>& ilp_terms, map<Term*, SatValue>* assignments = NULL);
00091 
00092         /*
00093          * Constructor to be used if a set of theory-specific axioms are known.
00094          */
00095         DPLLSolver(CNode* theory_tautologies, set<Term*>& ilp_terms,
00096                         map<Term*, SatValue>* assignments = NULL);
00097 
00098         void push(CNode* c);
00099         void pop();
00100         CNode* get_stack();
00101 
00102         /*
00103          * Sat assignment should respect the specified background.
00104          * This function can only be called before additional constraints are
00105          * pushed on the stack and it cannot be popped.
00106          */
00107         void restrict(CNode* background);
00108 
00109         bool is_sat();
00110         CNode* get_background_assumptions();
00111         ~DPLLSolver();
00112         string get_stats();
00113         CNode* get_current_assignment();
00114 
00115         inline int get_num_consecutive_boolean_complete() const
00116         {
00117                 return num_consecutive_boolean_complete;
00118         }
00119         inline double boolean_complete_ratio() const
00120         {
00121                 return ((double)num_sat_clauses)/((double)(num_clause));
00122         }
00123         inline double time_verifying_boolean_assignment() const
00124         {
00125                 return ((double)clause_ticks)/((double)(CLOCKS_PER_SEC));
00126         }
00127 
00128         inline int num_clauses_checked_in_theory() const
00129         {
00130                 return this->num_clause;
00131         }
00132 
00133         /*
00134          * Substitutes the current assignment of the stack into the given constraint.
00135          * This may be either a full or partial assignment for the constraint, or
00136          * it may not satisfy the constraint.
00137          */
00138         CNode* substitute_partial_assignment(CNode* constraint);
00139 
00140 
00141 private:
00142         /*
00143          * Initializes various ivars.
00144          */
00145         void init();
00146 
00147         /*
00148          * Checks if we encountered a constraint with same leaves and whether any
00149          * of the previous assignments satisfies this formula
00150          */
00151         inline bool satisfied_by_prev_assignment(CNode* formula);
00152         inline bool satisfied_by_prev_assignment_internal(CNode* formula);
00153 
00154         string stack_to_string();
00155 };
00156 
00157 #endif /* DPLLSOLVER_H_ */