-
Notifications
You must be signed in to change notification settings - Fork 3
/
cardinality.go
236 lines (183 loc) · 5.45 KB
/
cardinality.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
package constraints
import (
// "fmt"
"github.com/vale1410/bule/sat"
)
type OneTranslationType int
const (
Naive OneTranslationType = iota
Sort
Split
Count
Heule
Log
Pigeon
)
type CardTranslation struct {
PB *Threshold
Typ OneTranslationType
Aux []sat.Literal
Clauses sat.ClauseSet
}
//// all weights are 1
//// threshold is K
//func TranslateCardinality() (pb *Threshold) {
//
// for i, e := range pb.Entries {
//
// }
//
//}
func TranslateAtMostOne(typ OneTranslationType, tag string, lits []sat.Literal) (trans CardTranslation) {
var clauses sat.ClauseSet
switch typ {
case Naive:
for i, l := range lits {
for j := i + 1; j < len(lits); j++ {
clauses.AddTaggedClause(tag, sat.Neg(l), sat.Neg(lits[j]))
}
}
case Split:
// a constant that should be exposed,
// its the cuttoff for the split method of atMostOne
cutoff := 5
if len(lits) <= cutoff {
return TranslateAtMostOne(Naive, tag, lits)
} else {
aux := sat.NewAtomP1(sat.Pred("split"), newId())
trans.Aux = append(trans.Aux, sat.Literal{true, aux})
for _, l := range lits[:len(lits)/2] {
clauses.AddTaggedClause(tag, sat.Literal{true, aux}, sat.Neg(l))
}
for _, l := range lits[len(lits)/2:] {
clauses.AddTaggedClause(tag, sat.Literal{false, aux}, sat.Neg(l))
}
clauses.AddClauseSet(TranslateAtMostOne(typ, tag, lits[:len(lits)/2]).Clauses)
clauses.AddClauseSet(TranslateAtMostOne(typ, tag, lits[len(lits)/2:]).Clauses)
}
case Count:
pred := sat.Pred("c")
counterId := newId()
auxs := make([]sat.Literal, len(lits))
for i := range auxs {
auxs[i] = sat.Literal{true, sat.NewAtomP2(pred, counterId, i)}
}
trans.Aux = auxs
// S_i -> S_{i-1}
for i := 1; i < len(lits); i++ {
clauses.AddTaggedClause(tag, auxs[i-1], sat.Neg(auxs[i]))
}
// X_i -> S_i
for i := 0; i < len(lits); i++ {
clauses.AddTaggedClause(tag, auxs[i], sat.Neg(lits[i]))
}
// X_i-1 -> -S_i
for i := 1; i < len(lits); i++ {
clauses.AddTaggedClause(tag, sat.Neg(auxs[i]), sat.Neg(lits[i-1]))
}
// (S_i-1 /\ -S_i) -> X_i-1
for i := 1; i <= len(lits); i++ {
if i != len(lits) {
clauses.AddTaggedClause(tag, sat.Neg(auxs[i-1]), auxs[i], lits[i-1])
} else {
clauses.AddTaggedClause(tag, sat.Neg(auxs[i-1]), lits[i-1])
}
}
case Heule:
k := 4 // fixed size for the heule encoding
if len(lits) > k+1 {
aux := sat.NewAtomP1(sat.Pred("heule"), newId())
trans.Aux = append(trans.Aux, sat.Literal{true, aux})
front := make([]sat.Literal, k+1)
copy(front, lits[:k])
front[k] = sat.Literal{true, aux}
trans2 := TranslateAtMostOne(Naive, tag, front)
clauses.AddClauseSet(trans2.Clauses)
back := make([]sat.Literal, len(lits)-k+1)
copy(back, lits[k:])
back[len(lits)-k] = sat.Literal{false, aux}
trans2 = TranslateAtMostOne(typ, tag, back)
trans.Aux = append(trans.Aux, trans2.Aux...)
clauses.AddClauseSet(trans2.Clauses)
} else {
trans2 := TranslateAtMostOne(Naive, tag, lits)
clauses.AddClauseSet(trans2.Clauses)
}
case Log:
cutoff := 5 //will be a parameter of this encoding
clauses = buildLogEncoding(sat.Pred("logE"), newId(), cutoff, 0, tag, lits)
case Sort:
panic("CNF translation for this type not implemented yet")
default:
panic("CNF translation for this type not implemented yet")
}
trans.Typ = typ
trans.Clauses = clauses
return
}
func TranslateExactlyOne(typ OneTranslationType, tag string, lits []sat.Literal) (trans CardTranslation) {
var clauses sat.ClauseSet
switch typ {
case Heule, Log, Naive, Split:
trans2 := TranslateAtMostOne(typ, tag, lits)
trans.Aux = append(trans.Aux, trans2.Aux...)
clauses.AddClauseSet(trans2.Clauses)
clauses.AddTaggedClause(tag, lits...)
case Count:
pred := sat.Pred("cx")
counterId := newId()
auxs := make([]sat.Literal, len(lits))
for i := range auxs {
auxs[i] = sat.Literal{true, sat.NewAtomP2(pred, counterId, i)}
}
trans.Aux = auxs
// S_i -> S_{i-1}
for i := 1; i < len(lits); i++ {
clauses.AddTaggedClause(tag, auxs[i-1], sat.Neg(auxs[i]))
}
// X_i -> S_i
for i := 0; i < len(lits); i++ {
clauses.AddTaggedClause(tag, auxs[i], sat.Neg(lits[i]))
}
// X_i-1 -> -S_i
for i := 1; i < len(lits); i++ {
clauses.AddTaggedClause(tag, sat.Neg(auxs[i]), sat.Neg(lits[i-1]))
}
// (S_i-1 /\ -S_i) -> X_i-1
for i := 1; i <= len(lits); i++ {
if i != len(lits) {
clauses.AddTaggedClause(tag, sat.Neg(auxs[i-1]), auxs[i], lits[i-1])
} else {
clauses.AddTaggedClause(tag, sat.Neg(auxs[i-1]), lits[i-1])
}
}
clauses.AddTaggedClause(tag, auxs[0])
case Sort:
panic("CNF translation for this type not implemented yet")
default:
panic("CNF translation for this type not implemented yet")
}
trans.Typ = typ
trans.Clauses = clauses
return
}
func buildLogEncoding(pred sat.Pred, uId int, cutoff int, depth int, tag string, lits []sat.Literal) (clauses sat.ClauseSet) {
if len(lits) <= cutoff {
trans2 := TranslateAtMostOne(Naive, tag, lits)
clauses.AddClauseSet(trans2.Clauses)
} else {
atom := sat.NewAtomP2(pred, uId, depth)
first := lits[:len(lits)/2]
for _, l := range first {
clauses.AddTaggedClause(tag, sat.Literal{true, atom}, sat.Neg(l))
}
second := lits[len(lits)/2:]
for _, l := range second {
clauses.AddTaggedClause(tag, sat.Literal{false, atom}, sat.Neg(l))
}
depth++
clauses.AddClauseSet(buildLogEncoding(pred, uId, cutoff, depth, tag, first))
clauses.AddClauseSet(buildLogEncoding(pred, uId, cutoff, depth, tag, second))
}
return
}