00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef _XCHAFF_H_
00011 #define _XCHAFF_H_
00012
00013 #include "sat_api.h"
00014 #include "xchaff_solver.h"
00015
00016 using namespace std;
00017
00018 class Xchaff :public SatSolver {
00019 CSolver *_solver;
00020
00021 Lit (*_decision_hook)(void *, bool *);
00022 void (*_assignment_hook)(void *, Var, int);
00023 void *_decision_hook_cookie;
00024 void *_assignment_hook_cookie;
00025
00026 static Var mkVar(int id) { Var v; v.id = id; return v; }
00027 static Lit mkLit(int id) { Lit l; l.id = id; return l; }
00028 static Clause mkClause(int id) { Clause c; c.id = id; return c; }
00029
00030 public:
00031 Xchaff() { _solver = new CSolver; }
00032 ~Xchaff() { delete _solver; }
00033
00034
00035
00036
00037
00038 int NumVariables()
00039 { return _solver->num_variables(); }
00040 Var AddVariables(int nvars)
00041 { return mkVar(_solver->add_variables(nvars)); }
00042 Var GetVar(int varIndex)
00043 { return mkVar(varIndex); }
00044 int GetVarIndex(Var v)
00045 { return v.id; }
00046 Var GetFirstVar()
00047 { Var v; if (_solver->num_variables() != 0) v.id = 1; return v; }
00048 Var GetNextVar(Var var)
00049 { Var v;
00050 if (var.id != _solver->num_variables()) v.id = var.id+1; return v; }
00051 Lit MakeLit(Var var, int phase)
00052 { return mkLit((var.id << 1)+phase); }
00053 Var GetVarFromLit(Lit lit)
00054 { return mkVar(lit.id >> 1); }
00055 int GetPhaseFromLit(Lit lit)
00056 { return lit.id & 1; }
00057 int NumClauses()
00058 { return _solver->num_clauses(); }
00059 Clause AddClause(vector<Lit>& lits)
00060 { return mkClause(_solver->add_clause((vector<int>&)lits)); }
00061 Clause GetClause(int clauseIndex);
00062 Clause GetFirstClause()
00063 { Clause c;
00064 for (unsigned i=0; i< _solver->clauses().size(); ++i)
00065 if ( _solver->clause(i).in_use()) { c.id = i; break; }
00066 return c; }
00067 Clause GetNextClause(Clause clause)
00068 { Clause c;
00069 for (unsigned i= clause.id + 1; i< _solver->clauses().size(); ++i)
00070 if ( _solver->clause(i).in_use()) { c.id = i; break; }
00071 return c; }
00072 void GetClauseLits(Clause clause, vector<Lit>* lits);
00073 SatSolver::SATStatus Satisfiable(bool allowNewClauses);
00074 int GetVarAssignment(Var var)
00075 { return _solver->variable(var.id).value(); }
00076
00077
00078 SatSolver::SATStatus Continue();
00079 void Restart() { assert(0); }
00080 void Reset() { assert(0); }
00081
00082 void RegisterDLevelHook(void (*f)(void *, int), void *cookie)
00083 { _solver->RegisterDLevelHook(f, cookie); }
00084
00085 static int TranslateDecisionHook(void *cookie, bool *done)
00086 {
00087 Xchaff *b = (Xchaff*)cookie;
00088 Lit lit = b->_decision_hook(b->_decision_hook_cookie, done);
00089 return lit.id;
00090 }
00091
00092 void RegisterDecisionHook(Lit (*f)(void *, bool *), void *cookie)
00093 { _decision_hook = f; _decision_hook_cookie = cookie;
00094 _solver->RegisterDecisionHook(TranslateDecisionHook, this); }
00095
00096 static void TranslateAssignmentHook(void *cookie, int var, int value)
00097 {
00098 Xchaff *b = (Xchaff*)cookie;
00099 b->_assignment_hook(b->_assignment_hook_cookie, mkVar(var), value);
00100 }
00101
00102 void RegisterAssignmentHook(void (*f)(void *, Var, int), void *cookie)
00103 { _assignment_hook = f; _assignment_hook_cookie = cookie;
00104 _solver->RegisterAssignmentHook(TranslateAssignmentHook, this); }
00105 void RegisterDeductionHook(void (*f)(void *), void *cookie)
00106 { _solver->RegisterDeductionHook(f, cookie); }
00107 bool SetBudget(int budget)
00108 { _solver->set_time_limit(float(budget)); return true; }
00109 bool SetMemLimit(int mem_limit)
00110 { _solver->set_mem_limit(mem_limit); return true; }
00111 bool SetRandomness(int n)
00112 { _solver->set_randomness(n); return true; }
00113 bool SetRandSeed(int seed)
00114 { _solver->set_random_seed(seed); return true; }
00115 bool EnableClauseDeletion()
00116 { _solver->enable_cls_deletion(true); return true; }
00117 bool DisableClauseDeletion()
00118 { _solver->enable_cls_deletion(false); return true; }
00119 int GetBudgetUsed()
00120 { return int(_solver->total_run_time()); }
00121 int GetMemUsed()
00122 { return _solver->estimate_mem_usage(); }
00123 int GetNumDecisions()
00124 { return _solver->num_decisions(); }
00125 int GetNumConflicts()
00126 { return -1; }
00127 int GetNumExtConflicts()
00128 { return -1; }
00129 float GetTotalTime()
00130 { return _solver->total_run_time(); }
00131 float GetSATTime()
00132 { return -1; }
00133 int GetNumLiterals()
00134 { return _solver->num_literals(); }
00135 int GetNumDeletedClauses()
00136 { return _solver->num_deleted_clauses(); }
00137 int GetNumDeletedLiterals()
00138 { return _solver->num_deleted_literals(); }
00139 int GetNumImplications()
00140 { return _solver->num_implications(); }
00141 int GetMaxDLevel()
00142 { return _solver->max_dlevel(); }
00143 };
00144
00145 #endif
00146
00147
00148
00149
00150
00151
00152