-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathctw_test.go
197 lines (170 loc) · 4.12 KB
/
ctw_test.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
package ctw
import (
"flag"
"io/ioutil"
"log"
"math"
"os"
"sync"
"testing"
"github.com/fumin/ctw/ac/witten"
)
// TestUpdateSunehag tests the examples in the slides by Peter Sunehag and Marcus Hutter
// http://cs.anu.edu.au/courses/COMP4620/2013/slides-ctw.pdf
func TestSunehag(t *testing.T) {
t.Parallel()
root := &treeNode{}
depth := 3
bits := []int{1, 1, 0}
source := []int{0, 1, 0, 0, 1, 1, 0}
for _, b := range source {
update(root, bits[len(bits)-depth:], b)
bits = append(bits, b)
}
if math.Abs(root.LogProb-math.Log(7.0/2048)) > 1e-8 {
t.Errorf("%f", root.LogProb)
}
b := 0
update(root, bits[len(bits)-depth:], b)
bits = append(bits, b)
if math.Abs(root.LogProb-math.Log(153.0/65536)) > 1e-8 {
log.Printf("%f", root.LogProb)
t.Errorf("%f", root.LogProb)
}
}
// TestUpdateEIDMA tests the examle in the EIDMA report by F.M.J. Willems and Tj. J. Tjalkens.
// Complexity Reduction of the Context-Tree Weighting Algorithm: A Study for KPN Research, Technical University of Eindhoven, EIDMA Report RS.97.01
func TestEIDMA(t *testing.T) {
t.Parallel()
root := &treeNode{}
depth := 3
bits := []int{0, 1, 0}
source := []int{0, 1, 1, 0, 1, 0, 0}
for _, b := range source {
update(root, bits[len(bits)-depth:], b)
bits = append(bits, b)
}
if math.Abs(root.LogProb-math.Log(95.0/32768)) > 1e-8 {
t.Errorf("%f", root.LogProb)
}
}
// TestRevert tests that revert reverts the state of the tree to its original one.
func TestRevert(t *testing.T) {
t.Parallel()
root := &treeNode{}
depth := 3
bits := []int{0, 1, 0}
source := []int{0, 1, 1, 0, 1, 0, 0}
for _, b := range source {
traversal := update(root, bits[len(bits)-depth:], b)
seqP := root.LogProb
revert(traversal)
update(root, bits[len(bits)-depth:], b)
seqPAfter := root.LogProb
if seqP != seqPAfter {
t.Errorf("%f %f", seqP, seqPAfter)
}
bits = append(bits, b)
}
}
func TestCTWReverter(t *testing.T) {
t.Parallel()
model := NewCTW(make([]int, 48))
x := []int{1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0}
for _, xi := range x {
model.Observe(xi)
}
prob0 := model.Prob0()
reverter := NewCTWReverter(model)
y := []int{0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0}
for _, yi := range y {
reverter.Observe(yi)
reverter.Unobserve()
reverter.Observe(yi)
reverter.Observe(yi)
reverter.Unobserve()
}
prob0Updated := model.Prob0()
if prob0Updated == prob0 {
t.Fatalf("%f %f", prob0Updated, prob0)
}
for _, _ = range y {
reverter.Unobserve()
}
prob0Reverted := model.Prob0()
if prob0Reverted != prob0 {
t.Fatalf("%f %f", prob0Reverted, prob0)
}
}
func TestEncode(t *testing.T) {
t.Parallel()
// Prepare data
// x := []int{1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0}
contents, err := ioutil.ReadFile("gettysburg.txt")
if err != nil {
t.Fatalf("%v", err)
}
x := []int{}
for _, bt := range contents {
for i := uint(0); i < 8; i++ {
x = append(x, int(bt)&(1<<i)>>i)
}
}
// Encode
src := make(chan int)
go func() {
for _, b := range x {
src <- b
}
close(src)
}()
encoded := []int{}
dst := make(chan int)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
for b := range dst {
encoded = append(encoded, b)
}
}()
witten.Encode(dst, src, NewCTW(make([]int, 48)))
wg.Wait()
// t.Logf("encoded bits: %d, original bits: %d", len(encoded), len(x))
// Decode
dsrc := make(chan int)
go func() {
for i := range encoded {
dsrc <- encoded[i]
}
close(dsrc)
}()
decoded := []int{}
ddst := make(chan int)
wg = sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
for b := range ddst {
decoded = append(decoded, b)
}
}()
if err := witten.Decode(ddst, dsrc, NewCTW(make([]int, 48)), int64(len(x))); err != nil {
t.Fatalf("%v", err)
}
wg.Wait()
// Check that the decoded result is correct.
if len(x) != len(decoded) {
t.Fatalf("%d != %d", len(x), len(decoded))
}
for i, b := range x {
if decoded[i] != b {
t.Errorf("%d: %d != %d", i, b, decoded[i])
}
}
}
func TestMain(m *testing.M) {
flag.Parse()
log.SetFlags(log.LstdFlags | log.Lmicroseconds | log.Lshortfile)
os.Exit(m.Run())
}