-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
332 lines (294 loc) · 6.15 KB
/
main.go
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package main
import (
"bufio"
"errors"
"fmt"
"log"
"math"
"os"
"strconv"
"strings"
)
type cnf struct {
head *clause
tail *clause
}
type clause struct {
literals []int
next *clause
prev *clause
}
type purity struct {
negative uint
positive uint
}
const (
breakPoint = '%'
clauseEND = '0'
comment = 'c'
preamble = 'p'
)
type satSolver interface {
isSatisfied() bool
}
var _ satSolver = (*cnf)(nil)
func newCNF() *cnf {
return &cnf{}
}
func (c *cnf) push(clause *clause) {
if c.head == nil && c.tail == nil {
c.head = clause
} else {
clause.prev = c.tail
clause.prev.next = clause
}
c.tail = clause
}
func (c *cnf) deleteClause(clause *clause) {
if clause != c.head && clause != c.tail {
clause.prev.next = clause.next
clause.next.prev = clause.prev
} else {
if clause == c.head {
c.head = clause.next
if c.head != nil {
c.head.prev = nil
}
}
if clause == c.tail {
c.tail = clause.prev
if c.tail != nil {
c.tail.next = nil
}
}
}
}
func (c *clause) findIndex(literal int) (int, bool) {
for i, l := range c.literals {
if l == literal {
return i, true
}
}
return 0, false
}
func (c *clause) removeLiteral(index int) {
c.literals = append(c.literals[:index], c.literals[index+1:]...)
}
func isSkipped(s string) bool {
return len(s) == 0 || s[0] == clauseEND || s[0] == comment || s[0] == preamble
}
func isBreakPoint(s string) bool {
return s[0] == breakPoint
}
func newClause(literals []int) *clause {
return &clause{literals: append([]int{}, literals...)}
}
func parseLiterals(s string) ([]int, error) {
raw := strings.Fields(s)
literals := make([]int, 0, len(raw)-1)
for _, v := range raw {
if v == string(clauseEND) {
break
}
l, err := strconv.Atoi(v)
if err != nil {
return nil, errors.New("wrong dimacs formats")
}
literals = append(literals, l)
}
return literals, nil
}
func (c *cnf) parseDIMACS(f *os.File) error {
scanner := bufio.NewScanner(f)
for scanner.Scan() {
t := scanner.Text()
if isSkipped(t) {
continue
}
if isBreakPoint(t) {
break
}
literals, err := parseLiterals(t)
if err != nil {
return err
}
c.push(newClause(literals))
}
if c.head == nil {
return errors.New("empty file")
}
return nil
}
func (c *cnf) deleteAllClausesByLiteral(literal int) {
for p := c.head; p != nil; p = p.next {
if _, found := p.findIndex(literal); found {
c.deleteClause(p)
}
}
}
func (c *cnf) deleteLiteralFromAllClauses(literal int) {
for p := c.head; p != nil; p = p.next {
if i, found := p.findIndex(literal); found {
p.removeLiteral(i)
}
}
}
// リテラルLが1つだけの節があれば、Lを含む節を除去し、他の節の否定リテラル¬Lを消去する。
func (c *cnf) simplifyByOneLiteralRule() {
for p := c.head; p != nil; p = p.next {
if len(p.literals) == 1 {
c.deleteAllClausesByLiteral(p.literals[0])
c.deleteLiteralFromAllClauses(-p.literals[0])
p.next = c.head
}
}
}
func (c *cnf) makeLiteralsMap() map[int]*purity {
m := make(map[int]*purity)
for p := c.head; p != nil; p = p.next {
for _, l := range p.literals {
k := absInt(l)
if _, ok := m[k]; !ok {
m[k] = &purity{}
}
if l > 0 {
m[k].positive++
} else {
m[k].negative++
}
}
}
return m
}
func (c *cnf) getPureLiterals(m map[int]*purity) []int {
pureLiterals := make([]int, 0, len(m))
for k, v := range m {
if v.positive > 0 && v.negative == 0 {
pureLiterals = append(pureLiterals, k)
} else if v.positive == 0 && v.negative > 0 {
pureLiterals = append(pureLiterals, -k) // Key of purity map has literals' absolute value.
}
}
return pureLiterals
}
// 節集合の中に肯定と否定の両方が現れないリテラル(純リテラル)Lがあれば、Lを含む節を除去する。
func (c *cnf) simplifyByPureLiteralRule() {
literalsMap := c.makeLiteralsMap()
pureLiterals := c.getPureLiterals(literalsMap)
for _, l := range pureLiterals {
c.deleteAllClausesByLiteral(l)
}
}
func absInt(n int) int {
if n > 0 {
return n
} else {
return -n
}
}
func findCountMaxLiteral(m map[int]*purity) int {
var maxCount uint
literal, _ := strconv.Atoi(string(clauseEND))
for k, v := range m {
if v.positive > maxCount {
maxCount = v.positive
literal = k
}
if v.negative > maxCount {
maxCount = v.negative
literal = -k // Key of purity map has literals' absolute value.
}
}
return literal
}
func (c *cnf) getClausesMinLen() int {
minLen := math.MaxInt
for p := c.head; p != nil; p = p.next {
len := len(p.literals)
if len < minLen {
minLen = len
}
}
return minLen
}
func (c *cnf) getMinLenClauses(minLen int) *cnf {
cnf := newCNF()
for p := c.head; p != nil; p = p.next {
if len(p.literals) == minLen {
cnf.push(newClause(p.literals))
}
}
return cnf
}
func (c *cnf) getAtomicFormula() int {
minLen := c.getClausesMinLen()
if minLen == 0 {
log.Fatal("Illegal Length")
}
cnf := c.getMinLenClauses(minLen)
return findCountMaxLiteral(cnf.makeLiteralsMap())
}
func (c *cnf) deepCopy() *cnf {
cnf := newCNF()
for p := c.head; p != nil; p = p.next {
cnf.push(newClause(p.literals))
}
return cnf
}
func (c *cnf) hasEmptyClause() bool {
for p := c.head; p != nil; p = p.next {
if len(p.literals) == 0 {
return true
}
}
return false
}
func (c *cnf) isSatisfied() bool {
c.simplifyByOneLiteralRule()
c.simplifyByPureLiteralRule()
if c.head == nil {
return true
}
if c.hasEmptyClause() {
return false
}
v := c.getAtomicFormula()
c2 := c.deepCopy()
c2.push(newClause([]int{v}))
if c2.isSatisfied() {
return true
}
c3 := c.deepCopy()
c3.push(newClause([]int{-v}))
return c3.isSatisfied()
}
func main() {
if len(os.Args) == 1 {
cnf := newCNF()
if err := cnf.parseDIMACS(os.Stdin); err != nil {
log.Fatal(err)
}
if cnf.isSatisfied() {
fmt.Println("sat")
} else {
fmt.Println("unsat")
}
} else {
for i := 1; i < len(os.Args); i++ {
f, err := os.Open(os.Args[i])
if err != nil {
log.Fatal("Parse Multiple File Error")
}
defer f.Close()
cnf := newCNF()
if err := cnf.parseDIMACS(f); err != nil {
log.Fatal(err)
}
if cnf.isSatisfied() {
fmt.Println("sat")
} else {
fmt.Println("unsat")
}
}
}
}