forked from fogleman/physarum
/
model.go
206 lines (178 loc) · 4.4 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package physarum
import (
"math"
"math/rand"
"runtime"
"sync"
)
type Model struct {
W int
H int
BlurRadius int
BlurPasses int
ZoomFactor float32
Configs []Config
AttractionTable [][]float32
Grids []*Grid
Particles []Particle
Iteration int
}
func NewModel(
w, h, numParticles, blurRadius, blurPasses int, zoomFactor float32,
configs []Config, attractionTable [][]float32) *Model {
grids := make([]*Grid, len(configs))
numParticlesPerConfig := int(math.Ceil(
float64(numParticles) / float64(len(configs))))
actualNumParticles := numParticlesPerConfig * len(configs)
particles := make([]Particle, actualNumParticles)
m := &Model{
w, h, blurRadius, blurPasses, zoomFactor,
configs, attractionTable, grids, particles, 0}
m.StartOver()
return m
}
func (m *Model) StartOver() {
numParticlesPerConfig := len(m.Particles) / len(m.Configs)
m.Particles = m.Particles[:0]
m.Iteration = 0
for c := range m.Configs {
m.Grids[c] = NewGrid(m.W, m.H)
for i := 0; i < numParticlesPerConfig; i++ {
x := rand.Float32() * float32(m.W)
y := rand.Float32() * float32(m.H)
a := rand.Float32() * 2 * math.Pi
p := Particle{x, y, a, uint32(c)}
m.Particles = append(m.Particles, p)
}
}
}
func (m *Model) Step() {
updateParticle := func(rnd *rand.Rand, i int) {
p := m.Particles[i]
config := m.Configs[p.C]
grid := m.Grids[p.C]
// u := p.X / float32(m.W)
// v := p.Y / float32(m.H)
sensorDistance := config.SensorDistance * m.ZoomFactor
sensorAngle := config.SensorAngle
rotationAngle := config.RotationAngle
stepDistance := config.StepDistance * m.ZoomFactor
xc := p.X + cos(p.A)*sensorDistance
yc := p.Y + sin(p.A)*sensorDistance
xl := p.X + cos(p.A-sensorAngle)*sensorDistance
yl := p.Y + sin(p.A-sensorAngle)*sensorDistance
xr := p.X + cos(p.A+sensorAngle)*sensorDistance
yr := p.Y + sin(p.A+sensorAngle)*sensorDistance
C := grid.GetTemp(xc, yc)
L := grid.GetTemp(xl, yl)
R := grid.GetTemp(xr, yr)
da := rotationAngle * direction(rnd, C, L, R)
// da := rotationAngle * weightedDirection(rnd, C, L, R)
p.A = Shift(p.A+da, 2*math.Pi)
p.X = Shift(p.X+cos(p.A)*stepDistance, float32(m.W))
p.Y = Shift(p.Y+sin(p.A)*stepDistance, float32(m.H))
m.Particles[i] = p
}
updateParticles := func(wi, wn int, wg *sync.WaitGroup) {
seed := int64(m.Iteration)<<8 | int64(wi)
rnd := rand.New(rand.NewSource(seed))
n := len(m.Particles)
batch := int(math.Ceil(float64(n) / float64(wn)))
i0 := wi * batch
i1 := i0 + batch
if wi == wn-1 {
i1 = n
}
for i := i0; i < i1; i++ {
updateParticle(rnd, i)
}
wg.Done()
}
updateGrids := func(c int, wg *sync.WaitGroup) {
config := m.Configs[c]
grid := m.Grids[c]
for _, p := range m.Particles {
if uint32(c) == p.C {
grid.Add(p.X, p.Y, config.DepositionAmount)
}
}
grid.BoxBlur(m.BlurRadius, m.BlurPasses, config.DecayFactor)
wg.Done()
}
combineGrids := func(c int, wg *sync.WaitGroup) {
grid := m.Grids[c]
for i := range grid.Temp {
grid.Temp[i] = 0
}
for i, other := range m.Grids {
factor := m.AttractionTable[c][i]
for j, value := range other.Data {
grid.Temp[j] += value * factor
}
}
wg.Done()
}
var wg sync.WaitGroup
// step 1: combine grids
for i := range m.Configs {
wg.Add(1)
go combineGrids(i, &wg)
}
wg.Wait()
// step 2: move particles
wn := runtime.NumCPU()
for wi := 0; wi < wn; wi++ {
wg.Add(1)
go updateParticles(wi, wn, &wg)
}
wg.Wait()
// step 3: deposit, blur, and decay
for i := range m.Configs {
wg.Add(1)
go updateGrids(i, &wg)
}
wg.Wait()
m.Iteration++
}
func (m *Model) Data() [][]float32 {
result := make([][]float32, len(m.Grids))
for i, grid := range m.Grids {
result[i] = make([]float32, len(grid.Data))
copy(result[i], grid.Data)
}
return result
}
func direction(rnd *rand.Rand, C, L, R float32) float32 {
if C > L && C > R {
return 0
} else if C < L && C < R {
return float32((rnd.Int63()&1)<<1 - 1)
} else if L < R {
return 1
} else if R < L {
return -1
}
return 0
}
func weightedDirection(rnd *rand.Rand, C, L, R float32) float32 {
W := [3]float32{C, L, R}
D := [3]float32{0, -1, 1}
if W[0] > W[1] {
W[0], W[1] = W[1], W[0]
D[0], D[1] = D[1], D[0]
}
if W[0] > W[2] {
W[0], W[2] = W[2], W[0]
D[0], D[2] = D[2], D[0]
}
if W[1] > W[2] {
W[1], W[2] = W[2], W[1]
D[1], D[2] = D[2], D[1]
}
a := W[1] - W[0]
b := W[2] - W[1]
if rnd.Float32()*(a+b) < a {
return D[1]
}
return D[2]
}