This repository has been archived by the owner on Dec 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
solver_learn.go
76 lines (66 loc) · 1.5 KB
/
solver_learn.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
package sat
import (
"github.com/mitchellh/go-sat/cnf"
)
// learn performs the clause learning process after a conflict is found.
// This returns the learned clause as well as the level to backjump to.
func (s *Solver) learn(c cnf.Clause) int {
// Determine our learned clause
pathC := 0
s.learned = s.learned[:1]
p := cnf.LitUndef
idx := len(s.trail) - 1
for {
j := 0
if p != cnf.LitUndef {
j = 1
}
for ; j < len(c); j++ {
q := c[j]
qVar := q.Var()
qLevel := s.level(qVar)
if s.seen[qVar] == 0 && qLevel > 0 {
s.seen[qVar] = 1
if qLevel >= s.decisionLevel() {
pathC++
} else {
s.learned = append(s.learned, q)
}
}
}
// Select next clause
for s.seen[s.trail[idx].Var()] == 0 {
idx--
}
idx--
p = s.trail[idx+1]
c = s.varinfo[p.Var()].reason
s.seen[p.Var()] = 0
pathC--
if pathC <= 0 {
break
}
}
s.learned[0] = p.Neg()
// Determine the level to backjump to. This is simply the maximum
// level represented in our learned clause.
backjumpLevel := 0
if len(s.learned) > 1 {
maxI := 1
maxLevel := s.level(s.learned[maxI].Var())
for i := 2; i < len(s.learned); i++ {
if l := s.level(s.learned[i].Var()); l > maxLevel {
maxI = i
maxLevel = l
}
}
s.learned[maxI], s.learned[1] = s.learned[1], s.learned[maxI]
backjumpLevel = maxLevel
}
// Clear seen for learned clause so that learning can visit them
// again on the next go-around.
for _, l := range s.learned {
s.seen[l.Var()] = 0
}
return backjumpLevel
}