-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathslursat.cc
67 lines (55 loc) · 1.62 KB
/
slursat.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <cassert>
#include <random>
#include "util/CNFFormula.h"
#include "trail.h"
std::vector<Lit> get_permutation(size_t nVars, size_t seed) {
std::vector<Lit> perm(nVars);
for (int i = 0; i < nVars; i++) {
perm[i] = Lit(i+1, false);
}
std::mt19937 g(seed);
std::shuffle(perm.begin(), perm.end(), g);
return perm;
}
// SLUR algorithm
bool slursat(std::vector<Lit> lits, Trail& trail) {
for (Lit lit : lits) {
if (trail.value(lit.var()) == trail.UNASSIGNED) {
trail.newLevel();
trail.push(lit);
if (!trail.propagate()) {
trail.backtrack();
trail.newLevel();
trail.push(~lit);
if (!trail.propagate()) {
return false;
}
}
}
}
return true;
}
int main(int argc, char **argv) {
if (argc < 2) {
std::cout << "USAGE: ./slur <path/to/dimacs/cnf/formula> [seed]" << std::endl;
return 0;
}
CNFFormula formula(argv[1]);
int seed = argc > 2 ? std::stoi(argv[2]) : 0;
std::cout << "c Testing if formula " << argv[1] << " can be solved by SLUR algorithm." << std::endl;
Trail trail {};
if (!trail.init(formula)) {
std::cout << "s UNSATISFIABLE" << std::endl;
return 0;
}
std::vector<Lit> perm = get_permutation(formula.nVars(), seed);
if (slursat(perm, trail)) {
std::cout << "s SATISFIABLE" << std::endl;
trail.print();
assert(trail.satisfies(formula));
} else {
std::cout << "s UNKNOWN" << std::endl;
std::cout << "c GAVE UP" << std::endl;
}
return 0;
}