-
Notifications
You must be signed in to change notification settings - Fork 6
/
optimizer.go
136 lines (114 loc) · 2.54 KB
/
optimizer.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
package main
import (
"encoding/gob"
"flag"
"fmt"
"math/rand"
"os"
"time"
"github.com/egonelbre/exp/bit"
"github.com/egonelbre/exp/coder/arith"
"github.com/egonelbre/exp/physicscompress/physics"
)
func check(err error) {
if err != nil {
panic(err)
}
}
var (
tries = flag.Int("tries", 1e6, "how many random tries should it make")
)
func RandomModel() (m1, m2 arith.Model, desc string) {
switch rand.Intn(1) {
case 0:
x1 := arith.Shift2{
P0: arith.P(rand.Intn(arith.MaxP / 2)),
I0: byte(rand.Intn(8) + 1),
P1: arith.P(rand.Intn(arith.MaxP / 2)),
I1: byte(rand.Intn(8) + 1),
}
x2 := x1
m1, m2 = &x1, &x2
case 1:
x1 := arith.Shift4{
P: [4]arith.P{
arith.P(rand.Intn(arith.MaxP / 4)),
arith.P(rand.Intn(arith.MaxP / 4)),
arith.P(rand.Intn(arith.MaxP / 4)),
arith.P(rand.Intn(arith.MaxP / 4)),
},
I: [4]byte{
byte(rand.Intn(7) + 1),
byte(rand.Intn(7) + 1),
byte(rand.Intn(7) + 1),
byte(rand.Intn(7) + 1),
},
}
x2 := x1
m1, m2 = &x1, &x2
}
desc = fmt.Sprintf("%#v", m1)
return
}
func RandomTree(nbits uint) (m1 arith.Model, desc string) {
x1 := arith.Shift2{
P0: arith.P(rand.Intn(arith.MaxP / 2)),
I0: byte(rand.Intn(8) + 1),
P1: arith.P(rand.Intn(arith.MaxP / 2)),
I1: byte(rand.Intn(8) + 1),
}
m1 = arith.NewTree(nbits, func() arith.Model {
x := x1
return &x
})
desc = fmt.Sprintf("%#v", x1)
return
}
func main() {
flag.Parse()
rand.Seed(time.Now().UnixNano())
var baseline []physics.Cube
var current []physics.Cube
file, err := os.Open(flag.Arg(0))
check(err)
dec := gob.NewDecoder(file)
dec.Decode(¤t)
dec.Decode(&baseline)
minimal := 1 << 10
items := []int{}
for i := range current {
cube, base := ¤t[i], &baseline[i]
if *cube != *base {
items = append(items, i)
}
}
items6 := physics.Index6(items, len(current))
cur6 := physics.Delta6(baseline, current)
max := uint64(0)
for _, i := range items6 {
ext := uint64(bit.ZEncode(int64(cur6(i))))
if max < ext {
max = ext
}
}
nbits := bit.ScanRight(max) + 1
for i := 0; i < *tries; i += 1 {
menc, desc := RandomTree(nbits)
enc := arith.NewEncoder()
for _, i := range items {
cube := ¤t[i]
menc.Encode(enc, uint(cube.Interacting^1))
}
for _, i := range items {
cube, base := ¤t[i], &baseline[i]
v := uint(cube.Largest ^ base.Largest)
menc.Encode(enc, v&1)
menc.Encode(enc, v>>1)
}
enc.Close()
if len(enc.Bytes()) < minimal {
fmt.Printf("%6d %5d - %s\n", i, len(enc.Bytes()), desc)
minimal = len(enc.Bytes())
}
}
}