-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.go
555 lines (512 loc) · 11 KB
/
utils.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
package spn
import (
"fmt"
"math"
"github.com/RenatoGeh/gospn/common"
"github.com/RenatoGeh/gospn/sys"
"github.com/RenatoGeh/gospn/utils"
)
// Some of the following functions are non-recursive versions of equivalent spn.SPN methods. They
// are done using a Queue or Stack to perform the graph search instead of the recursion call stack.
// When the SPN is dense, running the recursive versions can take exponential time (as we do not
// account for already visited vertices). In these static function versions, all searches are done
// in time linear to the graphs. For this reason, unless the SPN is a tree (or the graph sparse
// enough), the preferred method is using the static function version. When the SPN is a tree, the
// best method is the recursive version, as it takes less memory and same time usage in average
// when compared to the static versions.
// InferenceY returns the value of S(I, Y=y). This convenience function allows for fast computation
// of soft inference values without having to create another VarSet for each valuation of Y.
func InferenceY(S SPN, I VarSet, Y, y int) float64 {
if len(S.Ch()) == 0 {
return 0
}
J := map[int]int{Y: y}
O := common.Queue{}
TopSortTarjan(S, &O)
V := make(map[SPN]float64)
for !O.Empty() {
s := O.Dequeue().(SPN)
switch t := s.Type(); t {
case "leaf":
if varid := s.Sc()[0]; varid == Y {
V[s] = s.Value(J)
} else {
V[s] = s.Value(I)
}
case "sum":
sum := s.(*Sum)
ch := sum.Ch()
W := sum.Weights()
n := len(ch)
vals := make([]float64, n)
for i, cs := range ch {
v := V[cs]
vals[i] = v + math.Log(W[i])
}
V[s] = sum.Compute(vals)
case "product":
prod := s.(*Product)
ch := prod.Ch()
n := len(ch)
vals := make([]float64, n)
for i, ch := range ch {
vals[i] = V[ch]
}
V[s] = prod.Compute(vals)
}
}
return V[S]
}
// Inference simply returns the value of S(I), without storing values for later use.
func Inference(S SPN, I VarSet) float64 {
if len(S.Ch()) == 0 {
return 0
}
O := common.Queue{}
TopSortTarjan(S, &O)
V := make(map[SPN]float64)
for !O.Empty() {
s := O.Dequeue().(SPN)
switch t := s.Type(); t {
case "leaf":
V[s] = s.Value(I)
case "sum":
sum := s.(*Sum)
ch := sum.Ch()
W := sum.Weights()
n := len(ch)
vals := make([]float64, n)
for i, cs := range ch {
v := V[cs]
vals[i] = v + math.Log(W[i])
}
V[s] = sum.Compute(vals)
case "product":
prod := s.(*Product)
ch := prod.Ch()
n := len(ch)
vals := make([]float64, n)
for i, ch := range ch {
vals[i] = V[ch]
}
V[s] = prod.Compute(vals)
}
}
return V[S]
}
// StoreInference takes an SPN S and stores the values for an instance I on a DP table storage
// at the position designated by the ticket tk. Returns S and the ticket used (if tk < 0,
// StoreInference creates a new ticket).
func StoreInference(S SPN, I VarSet, tk int, storage *Storer) (SPN, int) {
if len(S.Ch()) == 0 {
return nil, -1
}
if tk < 0 {
tk = storage.NewTicket()
}
// Get topological order.
O := common.Queue{}
TopSortTarjan(S, &O)
sys.Free()
table, _ := storage.Table(tk)
for !O.Empty() {
s := O.Dequeue().(SPN)
switch t := s.Type(); t {
case "leaf":
table.StoreSingle(s, s.Value(I))
case "sum":
sum := s.(*Sum)
ch := sum.Ch()
W := sum.Weights()
n := len(ch)
vals := make([]float64, n)
for i, cs := range ch {
v, e := table.Single(cs)
if !e {
fmt.Println("Error: The SPN graph has sums or products as leaves.")
sys.Free()
return nil, -1
}
vals[i] = v + math.Log(W[i])
}
if n == 0 {
table.StoreSingle(s, utils.LogZero)
} else {
table.StoreSingle(s, sum.Compute(vals))
}
case "product":
prod := s.(*Product)
ch := prod.Ch()
n := len(ch)
vals := make([]float64, n)
for i, cs := range ch {
vals[i], _ = table.Single(cs)
}
table.StoreSingle(s, prod.Compute(vals))
}
}
sys.Free()
return S, tk
}
// StoreMAP takes an SPN S and stores the MAP values for an instance I on a DP table storage
// at the position designated by the ticket tk. Returns S and the ticket used (if tk < 0,
// StoreMAP creates a new ticket).
func StoreMAP(S SPN, I VarSet, tk int, storage *Storer) (SPN, int, VarSet) {
if len(S.Ch()) == 0 {
return nil, -1, nil
}
if tk < 0 {
tk = storage.NewTicket()
}
// Get topological order.
T := common.Queue{}
TopSortTarjan(S, &T)
tab, _ := storage.Table(tk)
// Find max values.
for !T.Empty() {
s := T.Dequeue().(SPN)
switch t := s.Type(); t {
case "leaf":
m := s.Max(I)
tab.StoreSingle(s, m)
case "sum":
sum := s.(*Sum)
W := sum.Weights()
ch := s.Ch()
mv := math.Inf(-1)
for i, c := range ch {
v, _ := tab.Single(c)
u := math.Log(W[i]) + v
if u > mv {
mv = u
}
}
tab.StoreSingle(s, mv)
case "product":
ch := s.Ch()
var v float64
for _, c := range ch {
cv, _ := tab.Single(c)
v += cv
}
tab.StoreSingle(s, v)
}
}
Q := common.Queue{}
V := make(map[SPN]bool)
Q.Enqueue(S)
V[S] = true
M := make(VarSet)
// Find MAP states.
for !Q.Empty() {
s := Q.Dequeue().(SPN)
switch t := s.Type(); t {
case "leaf":
N, _ := s.ArgMax(I)
for k, v := range N {
M[k] = v
}
case "sum":
sum := s.(*Sum)
W := sum.Weights()
ch := s.Ch()
m := math.Inf(-1)
var mv []SPN
for i, c := range ch {
v, _ := tab.Single(c)
u := math.Log(W[i]) + v
if u > m {
mv, m = []SPN{c}, u
} else if u == m {
mv = append(mv, c)
}
}
// Randomly break ties.
mvc := mv[sys.RandIntn(len(mv))]
if mvc != nil && !V[mvc] {
Q.Enqueue(mvc)
V[mvc] = true
}
case "product":
ch := s.Ch()
for _, c := range ch {
if !V[c] {
Q.Enqueue(c)
V[c] = true
}
}
}
}
return S, tk, M
}
func norm(v []float64) {
var min float64
for _, u := range v {
if u < min {
min = u
}
}
if min < 0 {
for i := range v {
v[i] += min
}
}
var norm float64
for i := range v {
norm += v[i]
}
for i := range v {
v[i] /= norm
}
}
// NormalizeSPN recursively normalizes the SPN S.
func NormalizeSPN(S SPN) SPN {
Q := common.Queue{}
V := make(map[SPN]bool)
Q.Enqueue(S)
V[S] = true
for !Q.Empty() {
s := Q.Dequeue().(SPN)
if s.Type() == "sum" {
z := s.(*Sum)
W := z.Weights()
norm(W)
}
ch := s.Ch()
for _, c := range ch {
if !V[c] {
Q.Enqueue(c)
V[c] = true
}
}
}
return S
}
// ComputeHeight computes the height of a certain SPN S.
func ComputeHeight(S SPN) int {
T := common.Stack{}
V := make(map[SPN]int)
T.Push(S)
V[S] = 0
var h int
for !T.Empty() {
s := T.Pop().(SPN)
if s.Type() == "leaf" && V[s] > h {
h = V[s]
}
ch := s.Ch()
for _, c := range ch {
if _, e := V[c]; !e {
T.Push(c)
V[c] = V[s] + 1
}
}
}
return h
}
// ComputeScope computes the scope of a certain SPN S.
func ComputeScope(S SPN) []int {
T := common.Queue{}
TopSortTarjan(S, &T)
for !T.Empty() {
s := T.Dequeue().(SPN)
ch := s.Ch()
if len(ch) > 0 {
msc := make(map[int]bool)
for _, c := range ch {
csc := c.rawSc()
for _, v := range csc {
msc[v] = true
}
}
var sc []int
for k, _ := range msc {
sc = append(sc, k)
}
s.setRawSc(sc)
}
}
return S.rawSc()
}
// Complete returns whether the SPN is complete.
func Complete(S SPN) bool {
ComputeScope(S)
Q := common.Queue{}
V := make(map[SPN]bool)
Q.Enqueue(S)
V[S] = true
for !Q.Empty() {
s := Q.Dequeue().(SPN)
ch := s.Ch()
if s.Type() == "sum" {
sc := s.rawSc()
v := make(map[int]int)
for _, u := range sc {
v[u]++
}
for _, c := range ch {
csc := c.rawSc()
// Invariant: ComputeScope guarantees that there will be no duplicates.
if len(csc) != len(sc) {
sys.Printf("len(csc)=%d != len(sc)=%d\n", len(csc), len(sc))
sys.Printf("%v\n%v\n", csc, sc)
return false
}
for _, u := range csc {
_, e := v[u]
if !e {
sys.Printf("v[%d] does not exist\n", u)
return false
}
v[u]++
}
}
k := len(ch) + 1
for _, u := range v {
if u != k {
sys.Printf("u=%d != k=%d\n", u, k)
return false
}
}
}
for _, c := range ch {
if !V[S] && c.Type() != "leaf" {
Q.Enqueue(c)
V[c] = true
}
}
}
return true
}
// Decomposable returns whether the SPN is decomposable.
func Decomposable(S SPN) bool {
ComputeScope(S)
Q := common.Queue{}
V := make(map[SPN]bool)
Q.Enqueue(S)
V[S] = true
for !Q.Empty() {
s := Q.Dequeue().(SPN)
ch := s.Ch()
if s.Type() == "product" {
sc := s.rawSc()
v := make(map[int]int)
for _, u := range sc {
v[u]++
}
n := len(sc)
for _, c := range ch {
csc := c.rawSc()
// Invariant: ComputeScope guarantees that there will be no duplicates.
for _, u := range csc {
_, e := v[u]
if !e {
return false
}
v[u]++
}
n -= len(csc)
}
for _, u := range v {
if u != 2 {
return false
}
}
if n != 0 {
return false
}
}
for _, c := range ch {
if !V[S] && c.Type() != "leaf" {
Q.Enqueue(c)
V[c] = true
}
}
}
return true
}
// TraceMAP returns the max child index of each sum node in a map. We assume decomposability and
// completeness. When this condition is not met, one arbitrary MAP state is chosen. When the SPN is
// both decomposable and complete, it is easy to see that the induced MAP trace of the SPN's graph
// is a tree, and thus no two paths from the root to a leaf intersect. For the negative case, there
// can be two paths that do intersect, and thus we could have randomly chosen different max
// children in case of ties. In this case, TraceMAP chooses the first child it finds to meet the
// criteria.
func TraceMAP(S SPN, I VarSet) map[SPN]int {
tab := make(map[SPN]float64)
P := make(map[SPN]int)
// Get topological order.
T := common.Queue{}
TopSortTarjan(S, &T)
// Find max values.
for !T.Empty() {
s := T.Dequeue().(SPN)
switch t := s.Type(); t {
case "leaf":
m := s.Max(I)
tab[s] = m
case "sum":
sum := s.(*Sum)
W := sum.Weights()
ch := s.Ch()
mv := math.Inf(-1)
for i, c := range ch {
v := tab[c]
u := math.Log(W[i]) + v
if u > mv {
mv = u
}
}
tab[s] = mv
case "product":
ch := s.Ch()
var v float64
for _, c := range ch {
cv := tab[c]
v += cv
}
tab[s] = v
}
}
Q := common.Queue{}
V := make(map[SPN]bool)
Q.Enqueue(S)
V[S] = true
for !Q.Empty() {
s := Q.Dequeue().(SPN)
switch t := s.Type(); t {
case "sum":
sum := s.(*Sum)
W := sum.Weights()
ch := s.Ch()
m := math.Inf(-1)
var mi []int
for i, c := range ch {
v := tab[c]
u := math.Log(W[i]) + v
if u > m {
mi, m = []int{i}, u
} else if u == m {
mi = append(mi, i)
}
}
// Randomly break ties.
i := sys.RandIntn(len(mi))
mvc := ch[mi[i]]
if mvc != nil && !V[mvc] {
Q.Enqueue(mvc)
V[mvc] = true
P[s] = mi[i]
}
case "product":
ch := s.Ch()
for _, c := range ch {
if !V[c] {
Q.Enqueue(c)
V[c] = true
}
}
}
}
return P
}