solver/MSAFinder.h
00001 /*
00002  * MSAFinder.h
00003  *
00004  *  Created on: Aug 22, 2011
00005  *      Author: isil
00006  */
00007 
00008 #ifndef MSAFINDER_H_
00009 #define MSAFINDER_H_
00010 #include <set>
00011 #include <vector>
00012 #include <map>
00013 #include <string>
00014 #include <deque>
00015 using namespace std;
00016 
00017 class CNode;
00018 class Term;
00019 class VariableTerm;
00020 class FunctionTerm;
00021 class ConstantTerm;
00022 #include "SatValue.h"
00023 
00024 typedef  pair<VariableTerm*, int> var_cost_pair;
00025 
00026 class MSAFinder {
00027 
00028 public:
00029         CNode* node;
00030         map<VariableTerm*, int>& costs;
00031         int min_estimate;
00032         set<VariableTerm*> msa;
00033         map<VariableTerm*, Term*> var_to_fun_map;
00034 
00035         /*
00036          * If <t, {S1, S2, ...}> is in this map, this means that t cannot be
00037          * universally quantified if all variables in Si
00038          * have been universally quantified.
00039          */
00040         map<Term*, set<set<Term*> > > dependencies;
00041 
00042         int mpi_estimate;
00043 
00044 
00045         int fun_counter;
00046         int num_e_elims;
00047         int e_elim_time;
00048         int num_a_elims;
00049         int a_elim_time;
00050         int a_approximate;
00051         int a_approximate_time;
00052         int num_unsat_universal;
00053         int num_unsat_approx_universal;
00054         int dependence_time;
00055         int num_dep_success;
00056         int simplify_time;
00057         int mpi_time;
00058         int prune1_solve;
00059         int prune2_solve;
00060 
00061         int e_only;
00062         int a_only;
00063         int total;
00064         int cost_pruned;
00065         int max_cost;
00066 
00067         set<Term*> vars_in_min_pi; // vars in minimum prime implicant
00068 
00069 
00070 public:
00071         MSAFinder(CNode* node, map<VariableTerm*, int>& costs);
00072         MSAFinder(CNode* c, set<CNode*>& background,
00073                         map<VariableTerm*, int>& costs);
00074 
00075         const set<VariableTerm*>& get_vars_in_msa();
00076         int get_cost();
00077         string get_stats();
00078         string res_to_string();
00079         ~MSAFinder();
00080 
00081 private:
00082 
00083         void find_msa(CNode* c, set<CNode*>& background,
00084                         map<VariableTerm*, int>& costs);
00085 
00086         int compute_msa(CNode* constraint, set<CNode*>& background,
00087                         int cur_cost, set<VariableTerm*>& cur_msa,
00088                         set<Term*>& cur_universal);
00089 
00090 
00091 
00092         int get_cheapest_cost(const vector<VariableTerm*>& vars);
00093 
00094         /*
00095          * Result of universally quantifying v in c and performing
00096          * quantifier elimination
00097          */
00098         CNode* universally_quantify(CNode* c, VariableTerm* v);
00099 
00100         /*
00101          * Test if Av. c is UNSAT by plugging in a few "relevant" values for v
00102          * and seeing if any of them is UNSAT.
00103          */
00104         bool quick_test_universal_unsat(CNode* c, VariableTerm* v);
00105 
00106         /*
00107          * Result of existentially quantifying v in c and performing
00108          * quantifier elimination
00109          */
00110         CNode* existentially_quantify(CNode* c, VariableTerm* v);
00111 
00112         /*
00113          * Sums up costs of all remaining vars
00114          */
00115         int get_max_remaining_cost(deque<VariableTerm*>& unassigned_vars);
00116 
00117         /*
00118          * Initialize var_to_fun_map containing a unique function term for each
00119          * variable
00120          */
00121         void init_var_to_fun_map(set<Term*> vars);
00122 
00123         /*
00124          * Gives the currently unassigned variables in constraint
00125          */
00126         void get_unassigned_vars(CNode* constraint, deque<VariableTerm*>& vars);
00127 
00128 
00129         /*
00130          * Gives the cost of this variable
00131          */
00132         int get_cost(VariableTerm* vt);
00133 
00134 
00135         /*
00136          * Computes a conservative estimate for the cost of the minimum
00137          * satisfying assignment. This is done by computing the cost of the
00138          * minimum sized prime implicant of the formula.
00139          */
00140         void compute_initial_bound(set<CNode*>& background);
00141 
00142 
00143         // ------------------------------
00144 
00145         int compute_msa_naive(CNode* constraint, set<VariableTerm*> & msa);
00146         void compute_subsets(set<Term*> vars,
00147                         set<set<Term*> >& subsets);
00148 
00149         void get_candidate_msa(set<Term*>& vars, const set<Term*>& quantified,
00150                         set<Term*>& candidate_msa);
00151         int get_cost(const set<Term*>& vars);
00152         CNode* universally_quantify(CNode* c, set<Term*> vars);
00153 
00154         void get_constants_to_check(CNode* c, vector<Term*>& constants);
00155 
00156         VariableTerm* pick_next_unassigned_var(CNode* c,
00157                         deque<VariableTerm*>& unassigned_vars);
00158 
00159         void compute_dependencies(CNode* c);
00160         void compute_dependencies_rec(CNode* context, CNode* c);
00161         void print_dependencies();
00162 
00163         bool is_subset_of(const set<Term*>& s1, const set<Term*>& s2);
00164         bool is_subset_of(const set<VariableTerm*>& s1,
00165                         const set<VariableTerm*>& s2);
00166 
00167         CNode* propagate_dependencies(CNode* c, set<VariableTerm*>& cur_msa,
00168                         const set<Term*>& cur_universals, int& cur_cost);
00169 
00170         int compute_msa_using_blocking_clause(CNode* c, set<VariableTerm*>& msa);
00171         void compute_msa_bc(CNode* c, CNode* bc,
00172                         int& best_cost, set<VariableTerm*>& msa);
00173 
00174         bool bc_conjunct_redundant(set<VariableTerm*>& v_bar, VariableTerm* vt);
00175 
00176         void randomize_order(map<Term*, SatValue>& assignment,
00177                         deque<pair<Term*, SatValue> >& new_order);
00178 
00179         void order_assignments(map<Term*, SatValue>& assignment,
00180                                 deque<pair<Term*, SatValue> >& new_order);
00181 
00182 
00183 
00184 
00185 };
00186 
00187 #endif /* MSAFINDER_H_ */