-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
192 lines (151 loc) · 2.96 KB
/
model.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
package bohm
import (
"image"
"math"
"math/rand"
)
type Point struct {
X, Y int
}
type Model struct {
wave [][][]bool
changes [][]bool
stationary []float64
distribution []float64
random *rand.Rand
logProb []float64
ModelDep
}
func NewModel(dep ModelDep) Model {
return Model{
ModelDep: dep,
}
}
func (m *Model) Run(seed int64, limit int) bool {
T := len(m.stationary)
if cap(m.logProb) == 0 {
m.logProb = make([]float64, 0, T)
} else {
m.logProb = m.logProb[0:0]
}
for _, s := range m.stationary {
m.logProb = append(m.logProb, math.Log(s))
}
logT := math.Log(float64(T))
m.ModelDep.Clear()
m.random = rand.New(rand.NewSource(seed))
for l := 0; l < limit || limit == 0; l++ {
if result := m.Observe(logT, m.logProb); result != observeNil {
return result == observeTrue
}
for m.ModelDep.Propagate() {
}
}
return true
}
func (m *Model) Clear() {
for x, col := range m.wave {
for y, row := range col {
for t := range row {
row[t] = true
}
m.changes[x][y] = false
}
}
}
type observeState int
const (
observeNil observeState = iota
observeTrue
observeFalse
)
func (m *Model) Observe(logT float64, logProb []float64) observeState {
var min float64 = 1E+3
var sum, mainSum, logSum, noise, entropy float64
argminx, argminy := -1, -1
var amount, T int
for x := range m.wave {
for y := range m.wave[x] {
T = len(m.wave[x])
if m.ModelDep.OnBoundary(x, y) {
continue
}
amount = 0
sum = 0
for t, on := range m.wave[x][y] {
if on {
amount++
sum += m.stationary[t]
}
}
if sum == 0 {
return observeFalse
}
noise = 1E-6 * m.random.Float64()
if amount == 1 {
entropy = 0
} else if amount == T {
entropy = logT
} else {
mainSum = 0
logSum = math.Log(sum)
for t, on := range m.wave[x][y] {
if on {
mainSum += m.stationary[t] * logProb[t]
}
}
entropy = logSum - mainSum/sum
}
if entropy > 0 && entropy+noise < min {
min = entropy + noise
argminx = x
argminy = y
}
}
}
if argminx == -1 && argminy == -1 {
return observeTrue
}
if cap(m.logProb) == 0 {
m.distribution = make([]float64, 0, T)
} else {
m.distribution = m.distribution[0:0]
}
for t, on := range m.wave[argminx][argminy] {
if on {
m.distribution = append(m.distribution, m.stationary[t])
} else {
m.distribution = append(m.distribution, 0)
}
}
r := randIndex(m.distribution, m.random.Float64())
for t := range m.wave[argminx][argminy] {
m.wave[argminx][argminy][t] = t == r
}
m.changes[argminx][argminy] = true
return observeNil
}
func randIndex(a []float64, r float64) int {
var sum float64
for _, n := range a {
sum += n
}
if sum == 0 {
return int(r * float64(len(a)))
}
r *= sum
var x float64
for i, n := range a {
x += n
if r <= x {
return i
}
}
return 0
}
type ModelDep interface {
Clear()
Graphics() (image.Image, error)
OnBoundary(x, y int) bool
Propagate() bool
}