void reportResult(const SatSolver& solver, bool result) { solver.printStats(); cout << (result? "SAT" : "UNSAT") << endl; if (result) { for (size_t i = 0, n = gates.size(); i < n; ++i) cout << solver.getValue(gates[i]->getVar()) << endl; } }
// Solve whether two gates are fuctionally equivalent // If they are equivalent, return true // else return false bool CirMgr::solveGateEqBySat(SatSolver& s, CirGate* g1, CirGate* g2, bool inverse){ Var newV = s.newVar(); vector<Var> vars; vars.push_back(g1->getVar()); vars.push_back(g2->getVar()); s.addXorCNF(newV, vars, inverse); s.assumeRelease(); // Clear assumptions s.assumeProperty(newV, true); return !s.assumpSolve(); }
TEST(AIM_TEST, YES){ namespace fs = boost::filesystem; fs::path dir("./aim_yes"); BOOST_FOREACH(const fs::path& p, make_pair(fs::directory_iterator(dir), fs::directory_iterator())) { if (!fs::is_directory(p)){ SatSolver solver; vector<vector<int> > cs; load_file(p.string(), cs); cerr << p.string() << endl; ASSERT_TRUE(solver.Solve(cs)); } } }
TEST(AIM_TEST, NO){ // https://sites.google.com/site/boostjp/tips/filesystem namespace fs = boost::filesystem; fs::path dir("./aim_no"); BOOST_FOREACH(const fs::path& p, make_pair(fs::directory_iterator(dir), fs::directory_iterator())) { if (!fs::is_directory(p)){ SatSolver solver; vector<vector<int> > cs; load_file(p.string(), cs); cerr << p.string() << endl; ASSERT_FALSE(solver.Solve(cs)); } } }
TEST(SMALL_TEST, TEST1){ vector<int> c0 = {-1, 2, 3}; vector<int> c1 = {1, -2}; vector<int> c2 = {2, 3}; vector<int> c3 = {-3}; vector<int> c4 = {-1, -2}; SatSolver solver; vector<vector<int> > cs; cs.push_back(c0); cs.push_back(c1); cs.push_back(c2); cs.push_back(c3); cs.push_back(c4); ASSERT_FALSE(solver.Solve(cs)); }
TEST(SMALL_TEST, TEST0){ vector<int> c0 = {-1, 2, 3}; vector<int> c1 = {1, -2}; vector<int> c2 = {2, 3}; vector<int> c3 = {-3}; SatSolver solver; vector<vector<int> > cs; cs.push_back(c0); cs.push_back(c1); cs.push_back(c2); cs.push_back(c3); ASSERT_TRUE(solver.Solve(cs)); ASSERT_EQ(solver.model[1], true); ASSERT_EQ(solver.model[2], true); ASSERT_EQ(solver.model[3], false); }
// convert all aigGates in dfs to clauses and put in satSolver void CirMgr::genProofModel(SatSolver& s) { for (unsigned i=0, m=_dfsList.size(); i<m; ++i) { Var v = s.newVar(); _dfsList[i]->setVar(v); } for (unsigned i=0, m=_dfsList.size(); i<m; ++i) { _dfsList[i]->genCNF(s); } }
void CirMgr::doSimBySAT(const SatSolver& s) { unsigned* simValue = new unsigned[_piList.size()]; for (unsigned i = 0, m=_piList.size(); i<m; ++i){ int temp = s.getValue(getGateById(_piList[i])->getVar()); if (temp == -1) temp = 0; simValue[i] = temp + (rand() & ~1u); } checkFec(simValue); delete[] simValue; }
void genProofModel(SatSolver& s) { // Allocate and record variables; No Var ID for POs for (size_t i = 0, n = gates.size(); i < n; ++i) { Var v = s.newVar(); gates[i]->setVar(v); } // Hard code the model construction here... // [2] AIG 4 1 2 ==> [2] = [0] & [1] s.addAigCNF(gates[2]->getVar(), gates[0]->getVar(), false, gates[1]->getVar(), false); // [4] AIG 5 1 3 ==> [4] = [0] & [3] s.addAigCNF(gates[4]->getVar(), gates[0]->getVar(), false, gates[3]->getVar(), false); // [5] AIG 6 !4 !5 ==> [5] = ![2] & ![4] s.addAigCNF(gates[5]->getVar(), gates[2]->getVar(), true, gates[4]->getVar(), true); // [7] AIG 7 !2 !3 ==> [7] = ![1] & ![3] s.addAigCNF(gates[7]->getVar(), gates[1]->getVar(), true, gates[3]->getVar(), true); // [8] AIG 8 !7 1 ==> [8] = ![7] & [0] s.addAigCNF(gates[8]->getVar(), gates[7]->getVar(), true, gates[0]->getVar(), false); }
int main(int argc, char **argv) { str_param[0] = ""; getCommandLine(int_ident, int_param, nia, str_ident, str_param, nsa, &(argv[1]), argc-1); if( argc < 2 || int_param[0] != NOVAL || !strcmp( argv[argc-1], "-h" ) ) { outputHelpMessage(); } else { SatSolver solver; solver.params.verbosity = ( int_param[1] != NOVAL ? int_param[1] : 4 ); solver.params.time_limit = ( (double)(int_param[2] != NOVAL ? int_param[2] : -1 ) ); solver.params.seed = ( int_param[3] != NOVAL ? int_param[3] : 11041979 ); solver.params.randomization = ( int_param[4] != NOVAL ? abs(int_param[4]) : 2 ); if(solver.params.randomization == 0) solver.params.randomization = 1; solver.params.shuffle = ( int_param[4] > 0 ); solver.params.restart_base = ( int_param[5] != NOVAL ? int_param[5] : 200 ); solver.params.restart_limit = ( int_param[5] != NOVAL ? int_param[5] : 200 ); solver.params.restart_factor = ( strcmp(str_param[0],"nil") ? atof(str_param[0]) : 1.05 ); solver.params.decay = ( strcmp(str_param[1],"nil") ? atof(str_param[1]) : .96 ); solver.params.forgetfulness = ( strcmp(str_param[2],"nil") ? atof(str_param[2]) : .75 ); solver.setPolicy ( ( strcmp(str_param[3],"luby") ? GEOMETRIC : LUBY ) ); solver.parseDimacs(argv[1]); solver.printAll(std::cout); solver.solve(); } return 0; }
void CirMgr::cec() { solveBuf(); SatSolver solver; solver.initialize(); genProofModel(solver); // cout << endl; for (int i=0; i<(int)_fecGrps.size(); ++i){ for (unsigned j=1, n=_fecGrps[i]->size(); j<n; ++j){ unsigned g1Num = _fecGrps[i]->at(0); unsigned g2Num = _fecGrps[i]->at(j); CirGate* g1 = getGateById(g1Num/2); CirGate* g2 = getGateById(g2Num/2); assert(g1 != 0 && g2 != 0); // already solve g1 == g2; if (g2->getEqGate() == g1) continue; bool inv = ((g1Num%2) ^ (g2Num%2)); // cursorToPrevLine(); cursorClearAfter(); // cout << _fecGrps.size() << " FEC groups left, " // << "solving " << setw(6) << g1->getId() // << " with " << setw(6) << g2->getId() << " ...... "; if (solveGateEqBySat(solver, g1, g2, inv)){ // cout << "Equivalence!!" << endl; g2->setEqGate(g1); solver.addBufCNF(g1->getVar(), g2->getVar(), inv); } else{ // cout << "UnEquivalence!!" << endl; doSimBySAT(solver); i = -1; break; } } // finish solving one group } }
void SATMgr::indBmc(const V3NetId& monitor, SatProofRes& pRes) { SatSolver* satSolver = pRes.getSatSolver(); bind(satSolver); uint32_t i = 0; V3NetId I = buildInitState(); satSolver->addBoundedVerifyData( I, i ); satSolver->assertProperty(I, false, i ); // Start Bounded Model Checking for (uint32_t j = pRes.getMaxDepth(); i < j; ++i) { // Add time frame expanded circuit to SAT Solver satSolver->addBoundedVerifyData(monitor, i); satSolver->assumeRelease(); satSolver->assumeProperty(monitor, false, i); satSolver->simplify(); // Assumption Solver: If SAT, diproved! if(satSolver->assump_solve()) { pRes.setFired(i); break; } satSolver->assertProperty(monitor, true, i); } }
TEST(SUDOKU_TEST, YES){ vector<string> board = { "--A----C-----O-I", "-J--A-B-P-CGF-H-", "--D--F-I-E----P-", "-G-EL-H----M-J--", "----E----C--G---", "-I--K-GA-B---E-J", "D-GP--J-F----A--", "-E---C-B--DP--O-", "E--F-M--D--L-K-A", "-C--------O-I-L-", "H-P-C--F-A--B---", "---G-OD---J----H", "K---J----H-A-P-L", "--B--P--E--K--A-", "-H--B--K--FI-C--", "--F---C--D--H-N-", }; SatSolver solver; int n = board.size(); vector<vector<int> > cs; for (int r = 0; r < n; r++){ for (int c = 0; c < n; c++){ if (board[r][c] == '-'){ vector<int> v(n); for (int m = 0; m < n; m++) v[m] = (n * n) * m + (r * n + c) + 1; cs.push_back(v); } else { int m = board[r][c] - 'A'; cs.push_back({(n * n) * m + (r * n + c) + 1}); } } } for (int r = 0; r < n; r++){ for (int j = 0; j < n; j++){ for (int k = j + 1; k < n; k++){ for (int m = 0; m < n; m++){ int ac = (n * n) * m + (r * n + j) + 1; int bc = (n * n) * m + (r * n + k) + 1; cs.push_back({-ac, -bc}); } } } } for (int c = 0; c < n; c++){ for (int j = 0; j < n; j++){ for (int k = j + 1; k < n; k++){ for (int m = 0; m < n; m++){ int ac = (n * n) * m + (j * n + c) + 1; int bc = (n * n) * m + (k * n + c) + 1; cs.push_back({-ac, -bc}); } } } } int d = sqrt(n) + 0.5; for (int r1 = 0; r1 < n; r1++){ for (int r2 = 0; r2 < n; r2++){ for (int c1 = 0; c1 < n; c1++){ for (int c2 = 0; c2 < n; c2++){ if (r1 / d != r2 / d || c1 / d != c2 / d) continue; if (r1 == r2 && c1 == c2) continue; for (int m = 0; m < n; m++){ int ac = (n * n) * m + (r1 * n + c1) + 1; int bc = (n * n) * m + (r2 * n + c2) + 1; cs.push_back({-ac, -bc}); } } } } } ASSERT_TRUE(solver.Solve(cs)); for (int r = 0; r < n; r++){ for (int c = 0; c < n; c++){ int cnt = 0; for (int m = 0; m < n; m++){ if (solver.model[(n * n) * m + r * n + c + 1]){ cout << (char)('A' + m); cnt++; } } ASSERT_EQ(cnt, 1); } cout << endl; } cout << endl; }
int main(int argc, char** argv) { Options options; if (!parseCommandLine(argc, argv, options)) { return 1; } std::cout << "-- Reading pattern from file: " << options.pattern << std::endl; Pattern pat; std::ifstream f(options.pattern); if (!f) { std::cout << "-- Error: Cannot open " << options.pattern << std::endl; return 1; } try { pat.load(f); } catch (std::exception& e) { std::cout << "-- Error: " << e.what() << std::endl; return 1; } SatSolver s; std::cout << "-- Building formula for " << options.evolutions << " evolution steps..." << std::endl; std::vector<Field> fields; for (int g = 0; g <= options.evolutions; ++g) { fields.push_back(Field(s, pat.width(), pat.height())); if (g > 0) { transition(s, fields[g-1], fields[g]); } } if (options.backwards) { std::cout << "-- Setting pattern constraint on last generation..." << std::endl; patternConstraint(s, fields.back(), pat); } else { std::cout << "-- Setting pattern constraint on first generation..." << std::endl; patternConstraint(s, fields.front(), pat); } std::cout << "-- Solving formula..." << std::endl; if (!s.solve()) { std::cout << "-- Formula is not solvable. The selected pattern is probably too restrictive!" << std::endl; return 1; } std::cout << std::endl; for (int g = 0; g <= options.evolutions; ++g) { if (options.backwards) { if (g == 0) { std::cout << "-- Initial generation:" << std::endl; } else if (g == options.evolutions) { std::cout << "-- Evolves to final generation (from pattern):" << std::endl; } else { std::cout << "-- Evolves to:" << std::endl; } } else { if (g == 0) { std::cout << "-- Initial generation (from pattern):" << std::endl; } else if (g == options.evolutions) { std::cout << "-- Evolves to final generation:" << std::endl; } else { std::cout << "-- Evolves to:" << std::endl; } } fields[g].print(std::cout, s); std::cout << std::endl; } return 0; }
int main() { initCircuit(); SatSolver solver; solver.initialize(); // genProofModel(solver); bool result; // k = Solve(Gate(5) ^ !Gate(8)) Var newV = solver.newVar(); solver.addXorCNF(newV, gates[5]->getVar(), false, gates[8]->getVar(), true); solver.assumeRelease(); // Clear assumptions solver.assumeProperty(newV, true); // k = 1 result = solver.assumpSolve(); reportResult(solver, result); cout << endl << endl << "======================" << endl; // k = Solve(Gate(3) & !Gate(7)) newV = solver.newVar(); solver.addAigCNF(newV, gates[3]->getVar(), false, gates[7]->getVar(), true); solver.assumeRelease(); // Clear assumptions solver.assumeProperty(newV, true); // k = 1 result = solver.assumpSolve(); reportResult(solver, result); }