00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "cnf.h"
00014
00015
00016 using namespace std;
00017 using namespace CVCL;
00018 using namespace SAT;
00019
00020
00021 unsigned Clause::getMaxVar() const
00022 {
00023 unsigned max = 0;
00024 const_iterator i, iend;
00025 for (i = begin(), iend = end(); i != iend; ++i) {
00026 DebugAssert(!(*i).isNull(), "Null literal found in clause");
00027 if (unsigned((*i).getVar()) > max) max = unsigned((*i).getVar());
00028 }
00029 return max;
00030 }
00031
00032
00033 void Clause::print() const
00034 {
00035 if (isSatisfied()) cout << "*";
00036 const_iterator i, iend;
00037 for (i = begin(), iend = end(); i != iend; ++i) {
00038 if ((*i).isNull()) cout << "NULL";
00039 else if ((*i).isFalse()) cout << "F";
00040 else if ((*i).isTrue()) cout << "T";
00041 else {
00042 if (!(*i).isPositive()) cout << "-";
00043 cout << (*i).getVar();
00044 }
00045 cout << " ";
00046 }
00047 cout << endl;
00048 }
00049
00050
00051 void CNF_Formula::copy(const CNF_Formula& cnf)
00052 {
00053 setNumVars(0);
00054 Clause* c = d_current;
00055
00056 unsigned i, iend;
00057 Clause::const_iterator j, jend;
00058 for (i = 0, iend = cnf.numClauses(); i != iend; ++i) {
00059 newClause();
00060 for (j = cnf[i].begin(), jend = cnf[i].end(); j != jend; ++j) {
00061 addLiteral(*j);
00062 }
00063 if (cnf[i].isUnit()) registerUnit();
00064 if (&(cnf[i]) == cnf.d_current) c = d_current;
00065 }
00066 d_current = c;
00067 }
00068
00069
00070 void CNF_Formula::print() const
00071 {
00072 const_iterator i, iend;
00073 for (i = begin(), iend = end(); i != iend; ++i) {
00074 (*i).print();
00075 }
00076 }
00077
00078
00079 const CNF_Formula& CNF_Formula::operator+=(const CNF_Formula& cnf)
00080 {
00081 Clause* c = d_current;
00082
00083 unsigned i, iend;
00084 Clause::const_iterator j, jend;
00085 for (i = 0, iend = cnf.numClauses(); i != iend; ++i) {
00086 newClause();
00087 for (j = cnf[i].begin(), jend = cnf[i].end(); j != jend; ++j) {
00088 addLiteral(*j);
00089 }
00090 if (cnf[i].isUnit()) registerUnit();
00091 }
00092 d_current = c;
00093 return *this;
00094 }
00095
00096
00097 const CNF_Formula& CNF_Formula::operator+=(const Clause& c)
00098 {
00099 Clause* cur = d_current;
00100 newClause();
00101 Clause::const_iterator j, jend;
00102 for (j=c.begin(), jend = c.end(); j != jend; ++j) {
00103 addLiteral(*j);
00104 }
00105 if (c.isUnit()) registerUnit();
00106 d_current = cur;
00107 return *this;
00108 }
00109
00110
00111 void CNF_Formula_Impl::newClause()
00112 {
00113 d_formula.resize(d_formula.size()+1);
00114 d_current = &(d_formula.back());
00115 }
00116
00117
00118 void CNF_Formula_Impl::registerUnit()
00119 {
00120 DebugAssert(d_current->size()==1,"Expected unit clause");
00121 d_current->setUnit();
00122 Lit l = *(d_current->begin());
00123 d_lits[l.getID()] = true;
00124 }
00125
00126
00127 void CNF_Formula_Impl::simplify()
00128 {
00129 deque<Clause>::iterator i, iend;
00130 Clause::const_iterator j, jend;
00131 for (i = d_formula.begin(), iend = d_formula.end(); i != iend; ++i) {
00132 if ((*i).isUnit()) continue;
00133 for (j=(*i).begin(), jend = (*i).end(); j != jend; ++j) {
00134 if ((*j).isTrue()) {
00135 (*i).setSatisfied();
00136 break;
00137 }
00138 hash_map<int, bool>::iterator it = d_lits.find((*j).getID());
00139 if (it != d_lits.end()) {
00140 (*i).setSatisfied();
00141 break;
00142 }
00143 }
00144 }
00145 }
00146
00147
00148 void CNF_Formula_Impl::reset()
00149 {
00150 d_formula.clear();
00151 d_lits.clear();
00152 d_current = NULL;
00153 d_numVars = 0;
00154 }
00155
00156
00157 void CD_CNF_Formula::newClause()
00158 {
00159
00160 d_current = &(d_formula.push_back(Clause()));
00161 }
00162
00163
00164 void CD_CNF_Formula::registerUnit()
00165 {
00166 DebugAssert(d_current->size()==1,"Expected unit clause");
00167 d_current->setUnit();
00168 }
00169
00170