Clauses Term::getClauses(Assigment a) { Clauses cs; for (unsigned int i = 0; i < clauses.size(); i++) { Clause c; auto addClause = true; for (auto it : clauses[i]) { auto var = abs(it); if (!a.isSet(var)) { c.insert(it); continue; } if (it > 0 && a.isTrue(var) || it < 0 && a.isFalse(var)) { addClause = false; break; } if (it > 0 && a.isFalse(var) || it < 0 && a.isTrue(var)) continue; c.insert(it); } if (addClause) cs.push_back(c); else { c.clear(); c.insert(0); cs.push_back(c); } } return cs; }
Clause Term::getClause(Assigment a, int i) { Clause c; auto addClause = true; for (auto it:clauses[i]) { auto var = abs(it); if (!a.isSet(var)) { c.insert(it); continue; } if (it > 0 && a.isTrue(var) || it < 0 && a.isFalse(var)) { addClause = false; break; } else if (it > 0 && a.isFalse(var) || it < 0 && a.isTrue(var)) continue; else c.insert(it); } if (addClause) return c; c.clear(); c.insert(0); return c; }
Clause CDCL::resolve(Clause c1, Clause c2, Variable v) { Clause c; for (auto it : c1) if (it != v && it != -v) c.insert(it); for (auto it : c2) if (it != v && it != -v) c.insert(it); return c; }
// get clause data int Atomic::getClauses(ombt::Set_List<Clause> &clist, Clause &c, int storeClause, int negated, int concluded) const { Atom atom; switch (type) { case Constant: atom = Atom(value); if (negated) { if (atom == Atom("true")) atom = Atom("false"); else atom = Atom("true"); } break; case Variable: if (negated) atom = ~Atom(name); else atom = Atom(name); break; default: MustBeTrue(0); return(NOTOK); } c.setPartOfConclusion(concluded); c.insert(atom); return(OK); }
// rename variables in clause int Clause::renameVariables() { Clause newcl; Map<String, String> nvs; // rename variables in positive clause BinaryTreeIterator_InOrder<Tuple<Literal, List<Literal> > > pclIter(positiveClause); for ( ; !pclIter.done(); pclIter++) { ListIterator<Literal> dataIter(pclIter().data); for ( ; !dataIter.done(); dataIter++) { Literal literal(dataIter()); if (literal.renameVariables(nvs) != OK) return(NOTOK); if (newcl.insert(literal) != OK) return(NOTOK); } } // rename variables in negative clause BinaryTreeIterator_InOrder<Tuple<Literal, List<Literal> > > nclIter(negativeClause); for ( ; !nclIter.done(); nclIter++) { ListIterator<Literal> dataIter(nclIter().data); for ( ; !dataIter.done(); dataIter++) { Literal literal(dataIter()); if (literal.renameVariables(nvs) != OK) return(NOTOK); if (newcl.insert(literal) != OK) return(NOTOK); } } // overwrite existing clauses *this = newcl; // all done return(OK); }
bool CNFGenerator::OnePersonOneTable() { Clause onePersonAtLeastOneTable; Clause onePersonAtMaxOneTable; for (int i =0;i<people;i++) { onePersonAtLeastOneTable.clear(); onePersonAtMaxOneTable.clear(); //cout <<"atleast 1 ="; for (int j=0;j<tables;j++) { Literal newLiteral(i,j,false); //cout <<newLiteral; onePersonAtLeastOneTable.insert(newLiteral); } //cout<<endl; //sort(onePersonAtLeastOneTable.begin(),onePersonAtLeastOneTable.end()); SetOfClauses.insert(pair<Clause,int> (onePersonAtLeastOneTable,++CNFGenerator::globalClauseCount)); for (int j=0;j<tables;j++) { for (int k=j+1;k<tables;k++) { onePersonAtMaxOneTable.clear(); Literal newLiteral1(i,j,true); Literal newLiteral2(i,k,true); onePersonAtMaxOneTable.insert(newLiteral1); onePersonAtMaxOneTable.insert(newLiteral2); //sort(onePersonAtMaxOneTable.begin(),onePersonAtMaxOneTable.end()); SetOfClauses.insert(pair<Clause,int> (onePersonAtMaxOneTable,++CNFGenerator::globalClauseCount)); } } } return true; }
bool CNFGenerator::EnemiesGenerator() { Clause newEnemyClause; for (vector<pair <short,short> >::iterator it = Enemies.begin();it!=Enemies.end();it++) { for (int i =0; i< tables;i++) { newEnemyClause.clear(); Literal newLiteral1(it->first,i,true); Literal newLiteral2(it->second,i,true); newEnemyClause.insert(newLiteral1); newEnemyClause.insert(newLiteral2); //sort(newEnemyClause.begin(),newEnemyClause.end()); SetOfClauses.insert(pair<Clause,int>(newEnemyClause,++CNFGenerator::globalClauseCount)); } } return true; }
Clause CDCL::solver(int decisionLevel) { while (true) { auto clauses = statement.getClauses(conclusion); satisfiable = true; for (unsigned int i = 0; i < clauses.size(); i++) { if (clauses[i].count(0) == 0) satisfiable = false; if (clauses[i].size() == 0) { // start learning return statement.getClauses()[i]; } } if (satisfiable) { Clause c; c.insert(0); return c; } auto p = unitPropagation(); if (p.first != -1) { auto c = solver(decisionLevel); if (c.size() == 0) { // UNSAT conclusion.unset(p.first); return c; } if (c.count(0) > 0) { // SAT return c; } conclusion.unset(p.first); auto c2 = statement.getClauses()[p.second]; if (c.count(p.first) > 0 || c.count(-p.first) > 0) return resolve(c, c2, p.first); return c; } auto var = 0; unsigned int clause_length = 100000; for (auto c:clauses) { if (c.count(0) > 0) continue; // true if (c.size() > 0 && c.size() < clause_length) { clause_length = c.size(); var = abs(*c.begin()); } } bool value = rand() % 2; conclusion.set(var, value); auto c = solver(decisionLevel + 1); if (c.count(0) != 0) { // SAT return c; } if (c.count(var) == 0 && c.count(-var) == 0) { conclusion.unset(var); return c; } conclusion.unset(var); statement.add(c); } }
// rename variables in clause int Clause::renameVariables() { Clause newcl; Map<String, String> nvs; // rename variables in positive clause BinaryTree_AVL_Iterator_InOrder<Literal> pclIter(positiveClause); for ( ; !pclIter.done(); pclIter++) { Literal literal(pclIter()); if (literal.renameVariables(nvs) != OK) { ERROR("renameVariables failed.", errno); return(NOTOK); } if (newcl.insert(literal) != OK) { ERROR("insert failed.", errno); return(NOTOK); } } // rename variables in negative clause BinaryTree_AVL_Iterator_InOrder<Literal> nclIter(negativeClause); for ( ; !nclIter.done(); nclIter++) { Literal literal(nclIter()); if (literal.renameVariables(nvs) != OK) { ERROR("renameVariables failed.", errno); return(NOTOK); } if (newcl.insert(literal) != OK) { ERROR("insert failed.", errno); return(NOTOK); } } // rename variables in answers clause ListIterator<Literal> ansIter(answers); for ( ; !ansIter.done(); ansIter++) { Literal literal(ansIter()); if (literal.renameVariables(nvs) != OK) { ERROR("renameVariables failed.", errno); return(NOTOK); } if (newcl.insertAnswer(literal) != OK) { ERROR("insert failed.", errno); return(NOTOK); } } #if 0 // update table of renamed variables if (updateVariableNames(nvs) != OK) return(NOTOK); #endif // overwrite existing clauses newcl.setDepth(getDepth()); newcl.setNumber(getNumber()); newcl.setConclusion(getConclusion()); newcl.setQuery(getQuery()); newcl.setSOS(getSOS()); *this = newcl; // all done return(OK); }
ClauseSets::CLS operator()() const { ClauseSets::CLS G; // Non-triviality clause: {Clause C; for (Encoding::clause_index_t i = 0; i < enc.ncs; ++i) C.insert(Lit(enc.csvar(i), Pol::p)); assert(C.size() == enc.ncs); G.push_back(std::move(C)); ++c_cs; litocc += enc.ncs; } // Defining the pass's: {for (auto it = enc.all_solutions.first.begin(); it != enc.all_solutions.first.end(); ++it) { const Encoding::Pass_p phi_p = &*it; const ClauseSets::Pass& phi = *it; const Var tphi = enc.pavar(phi_p); // from left to right, i.e., t(phi) -> and_{v in var(phi)} t(v,phi(v)): {const Lit negtphi = Lit(tphi, Pol::n); for (const auto& pair : phi) { Clause C; C.insert(negtphi); C.insert(Lit(enc.bfvar(pair.first, pair.second), Pol::p)); assert(C.size() == 2); G.push_back(std::move(C)); ++c_palr; litocc += 2; } } // from right to left, i.e., (and_{v in var(phi)} t(v,phi(v))) -> t(phi): {Clause C; C.insert(Lit(tphi,Pol::p)); for (const auto& pair : phi) C.insert(Lit(enc.bfvar(pair.first, pair.second), Pol::n)); assert(C.size() == 1 + phi.size()); G.push_back(std::move(C)); ++c_parl; litocc += 1 + phi.size(); } } } {for (Encoding::clause_index_t i = 0; i < enc.ncs; ++i) { // t(C) -> P(C): const Var tc = enc.csvar(i); {Clause C; C.insert(Lit(tc,Pol::n)); const auto& S = enc.all_solutions.second[i]; for (const Encoding::Pass_p& phi_p : S) C.insert(Lit(enc.pavar(phi_p),Pol::p)); assert(C.size() == 1 + S.size()); G.push_back(std::move(C)); ++c_P; litocc += 1 + S.size(); } // -t(C) -> N(C): for (const Lit x : enc.dclauses[i]->P.second) { const Var v = enc.E_index[var(x)]; assert(v < F.ne); for (Var bfi = enc.bfvar_indices[v]; bfi < enc.bfvar_indices[v+1]; ++bfi) { Clause C; C.insert(Lit(tc,Pol::p)); C.insert(Lit(bfi,Pol::n)); assert(C.size() == 2); G.push_back(std::move(C)); ++c_N; litocc += 2; } } } } // Amo-clauses for bf-variables: {for (Var i = 0; i < F.ne; ++i) { const Var beg = enc.bfvar_indices[i], end = enc.bfvar_indices[i+1]; for (Var v = beg; v < end; ++v) for (Var w = v+1; w < end; ++w) { Clause C; C.insert(Lit(v,Pol::n)), C.insert(Lit(w,Pol::n)); assert(C.size() == 2); G.push_back(std::move(C)); ++c_amo; litocc += 2; } } } return G; }