-
Notifications
You must be signed in to change notification settings - Fork 19
/
sat.js
145 lines (124 loc) · 3.32 KB
/
sat.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* global Module */
const solveString = Module.cwrap('solve_string', 'string', ['string', 'int']);
class Sat {
constructor(numVars) {
this.numVars = numVars;
this.clauses = [];
}
assert(vars) {
this.clauses.push(vars);
}
assertAtLeast(vars, k) {
const size = vars.length - k + 1;
for (const comb of combinations(vars, size)) {
this.clauses.push(comb);
}
}
assertAtMost(vars, k) {
const size = k + 1;
for (const comb of combinations(vars, size)) {
this.clauses.push(comb.map(n => -n));
}
}
addCounter(vars) {
this.counter = this._addCounter(vars);
}
assertCounterAtLeast(k) {
for (let i = 0; i < k && i < this.counter.length; i++) {
this.assert([this.counter[i]]);
}
}
assertCounterAtMost(k) {
for (let i = k; i < this.counter.length; i++) {
this.assert([-this.counter[i]]);
}
}
/*
See:
https://cs.stackexchange.com/questions/6521/reduce-the-following-problem-to-sat/6522#6522
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.458.7676&rep=rep1&type=pdf
Efficient CNF encoding of Boolean cardinality constraints - Olivier Bailleux and Yacine Boufkhad
*/
_addCounter(vars) {
if (vars.length <= 1) {
return vars;
}
const mid = Math.floor(vars.length/2);
const left = this._addCounter(vars.slice(0, mid));
const right = this._addCounter(vars.slice(mid));
const counter = [];
for (let i = 0; i < vars.length; i++) {
counter.push(++this.numVars);
}
for (let a = 0; a <= left.length; a++) {
for (let b = 0; b <= right.length; b++) {
if (a > 0 && b > 0) {
this.assert([-left[a-1], -right[b-1], counter[a+b-1]]);
} else if (a > 0) {
this.assert([-left[a-1], counter[a-1]]);
} else if (b > 0) {
this.assert([-right[b-1], counter[b-1]]);
}
if (a < left.length && b < right.length) {
this.assert([left[a], right[b], -counter[a+b]]);
} else if (a < left.length) {
this.assert([left[a], -counter[a+b]]);
} else if (b < right.length) {
this.assert([right[b], -counter[a+b]]);
}
}
}
return counter;
}
solveWith(func) {
const saved = this.clauses;
try {
this.clauses = this.clauses.slice();
func();
return this.solve();
} finally {
this.clauses = saved;
}
}
solve() {
const lines = [`p cnf ${this.numVars} ${this.clauses.length}`];
for (const clause of this.clauses) {
lines.push(clause.join(' ') + ' 0');
}
const input = lines.join('\n');
const output = solveString(input, input.length);
if (output.slice(0, 3) !== 'SAT') {
return null;
}
const result = new Array(this.numVars+1).fill(null);
for (const s of output.slice(4).split(' ')) {
const n = parseInt(s, 10);
if (n > 0) {
result[n] = true;
} else {
result[-n] = false;
}
}
return result;
}
}
function combinations(list, n) {
const result = [];
const current = [];
function go(i, n) {
if (i === list.length) {
if (n === 0) {
result.push(current.slice());
}
} else {
if (n > 0) {
current.push(list[i]);
go(i+1, n-1);
current.pop();
}
go(i+1, n);
}
}
go(0, n);
return result;
}