-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProbableDirection.go
394 lines (294 loc) · 11 KB
/
ProbableDirection.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
///////////////////////////////////////////////////////////////
//
// Can we maintain particle cluster coherence with a centre of mass
// guided by a psi wave and probabilistic directional affinity?
// This initial condition is crucial in determining direction
// as the directional gradient is x,y symmetrical. Even with heavily
// skewed directions , the mass spreads out along the wave, since it
// travels quite slowly, depending on the relative speed / acceptance threshold.
// In this model, there is no centre of mass cohesive "force", so a body can
// just dissociate freely
//
///////////////////////////////////////////////////////////////
package main
import (
"fmt"
"time"
C "Cellibrium"
)
// **********************************************************
const DoF = 10000
const wrange = 10000
const PERIOD = C.WAVELENGTH * wrange
// ****************************************************************
func main () {
C.MODEL_NAME = "ProbableDirection"
var st [C.Ylim]string
st[0] = "....................................."
st[1] = "....................................|"
st[2] = "....................................|"
st[3] = "....................................|"
st[4] = "....................................|"
st[5] = "....................................|"
st[6] = "....................................|"
st[7] = "....................................|"
st[8] = "....................................|"
st[9] = "....................................|"
st[10] = "....................................|"
st[11] = "....................................|"
st[12] = "....................................|"
st[13] = "....................................|"
st[14] = "....................................|"
st[15] = "....................................|"
st[16] = "....................................|"
st[17] = "....................................|"
st[18] = "....................................|"
st[19] = "....................................|"
st[20] = "....................................|"
st[21] = "....................................|"
st[22] = "....................................|"
st[23] = "....................................|"
st[24] = "....................................|"
st[25] = "....................................|"
st[26] = "....................................|"
st[27] = "....................................|" // X
st[28] = "....................................|" // X
st[29] = "....................................|" // X
st[30] = "....................................|"
st[31] = "....................................|"
st[32] = "....................................|"
st[33] = "....................................|"
st[34] = "....................................|"
st[35] = "....................................|"
st[36] = "....................................|"
st[37] = "....................................|"
st[38] = "....................................|"
st[39] = ">...................................|"
st[40] = ">...................................|"
st[41] = ">...................................|"
st[42] = ">...................................|"
st[43] = ">...m..............................*|"
st[44] = ">..m.m.............................*|"
st[45] = ">.m.m.m............................*|"
st[46] = ">m.m.m.m...........................*|"
st[47] = ">.m.m.m............................*|"
st[48] = ">..m.m.............................*|"
st[49] = ">...m..............................*|"
st[50] = ">...................................|"
st[51] = ">...................................|"
st[52] = ">...................................|"
st[53] = ">...................................|"
st[54] = "....................................|"
st[55] = "....................................|"
st[56] = "....................................|"
st[57] = "....................................|"
st[58] = "....................................|"
st[59] = "....................................|"
st[60] = "....................................|"
st[61] = "....................................|"
st[62] = "....................................|"
st[63] = "....................................|"
st[64] = "....................................|"
st[65] = "....................................|"
st[66] = "....................................|"
st[67] = "....................................|"
st[68] = "....................................|"
st[69] = "....................................|"
st[70] = "....................................|"
st[71] = "....................................|"
st[72] = "....................................|"
st[73] = "....................................|"
st[74] = "....................................|"
st[75] = "...***..............................."
// Keep the data structures for agents global too for convenience
C.Initialize(st,DoF)
C.ShowState(st,1,37,76,"+")
EquilGuideRail()
C.ShowState(st,C.MAXTIME,37,76,"+")
//ShowMomentum(st,C.MAXTIME,37,76,"+")
fmt.Println("")
}
// ****************************************************************
func EquilGuideRail() {
for i := 1; i < C.Adim; i++ {
go UpdateAgent_Flow(i)
}
}
// ****************************************************************
func UpdateAgent_Flow(agent int) {
// Start with an unconditional promise to break the deadlock symmetry
const mass = 1.0
for direction := 0; direction < C.N; direction++ {
neighbour := C.AGENT[agent].Neigh[direction]
C.AGENT[agent].P[direction] = 0
if neighbour != 0 {
var breaker C.Message
breaker.Value = C.AGENT[agent].Psi
breaker.Intent = C.AGENT[agent].Intent
breaker.Phase = C.TICK
C.CHANNEL[agent][neighbour] = breaker
}
}
C.CausalIndependence(true)
const delayed_start = 800
for t := 0; t < C.MAXTIME; t++ {
// Every pair of agents has a private directional channel that's not overwritten by anyone else
// Messages persist until they are read and cannot unseen
for direction := 0; direction < C.N ; direction++ {
var send,recv C.Message
d := direction % C.N
dbar := (direction + C.N/2) % C.N
neighbour := C.AGENT[agent].Neigh[d]
if neighbour == 0 {
continue // wall signal
}
// We need to wait for a positive signal indicating a new transfer to avoid double/empty reading
recv = C.AcceptFromChannel(neighbour,agent)
// ****************** PROCESS *********************
switch recv.Phase {
case C.TICK: // me
C.AGENT[agent].V[d] = recv.Value // the value here is psi
C.AGENT[agent].M[d] = recv.MassID
if C.AGENT[agent].M[d] > 0 {
// only accept momentum from non-empty cells
C.AGENT[agent].Intent = recv.Intent
}
C.AGENT[agent] = EvolveAgents(C.AGENT[agent],d)
// In this phase we can choose to make an offer to accept
// a neighbouring massID, we have to have received an update first
if t > delayed_start && AcceptingMass(C.AGENT[agent],d,dbar) > 0 {
send.Phase = C.TAKE
send.Value = mass
C.ConditionalChannelOffer(agent,neighbour,send)
// if I'm taking one, I shouldn't take another
continue
} else {
send.MassID = C.AGENT[agent].MassID
send.Value = C.AGENT[agent].Psi
send.Intent = C.AGENT[agent].Intent
send.Angle = C.AGENT[agent].Theta
send.Phase = C.TICK
C.ConditionalChannelOffer(agent,neighbour,send)
}
case C.TAKE: // YOU
send.Phase = C.TACK
C.AGENT[agent].MassID = 0 // assume I'll take it
C.ConditionalChannelOffer(agent,neighbour,send)
case C.TACK: // ME
// Once we've accepted the momentum, rotate the direction clock
C.AGENT[agent].MassID = mass
C.AGENT[agent].Intent = C.Rotate(C.AGENT[agent].Intent)
send.Phase = C.TOCK
C.ConditionalChannelOffer(agent,neighbour,send)
case C.TOCK: // YOU - initiate a change / Xfer
C.AGENT[agent].MassID = 0
send.MassID = C.AGENT[agent].MassID
send.Intent = C.AGENT[agent].Intent
send.Value = C.AGENT[agent].Psi
send.Angle = C.AGENT[agent].Theta
send.Phase = C.TICK
C.ConditionalChannelOffer(agent,neighbour,send)
}
}
}
}
// ****************************************************************
func AcceptingMass(agent C.STAgent,d,dbar int) int {
// We look to accept some mass from d if dbar looks empty
if agent.Psi * agent.Psi < 0.1 {
return 0
}
affinity := agent.Psi * agent.Psi // agent.V[d] * agent.V[d] - agent.Psi * agent.Psi
const psi_threshold = 20.0 // keep this high to maintain approx conservation of M
alignment := agent.Intent[0]
if affinity > psi_threshold && alignment == dbar {
if (agent.M[d] > 0) && (agent.MassID == 0) {
return 1
}
}
return 0
}
// ****************************************************************
func EvolveAgents(agent C.STAgent,direction int) C.STAgent { // Laplacian
agent.Theta += dTheta(agent,direction) // float64(int(dTheta(agent)+0.5) % PERIOD)
agent.Psi += dPsi(agent)
return agent
}
// ******************************************************************
func dTheta(agent C.STAgent,di int) float64 { // Laplacian
var d2 float64 = 0
const dt = 0.01
const mass = 2.0
// Velocity = laplaciant gradient
d2 += agent.V[di] - agent.Psi
// This is negative when Psi is higher than neighbours
dtheta := dt * d2 / float64(C.N * mass)
return dtheta
}
// ******************************************************************
func dPsi(agent C.STAgent) float64 { // Laplacian
const dt = 0.01
deltaPsi := agent.Theta * dt
return deltaPsi
}
// ****************************************************************
func ShowMomentum(st_rows [C.Ylim]string,tmax,xlim,ylim int,mode string) {
var fieldwidth,numwidth string
switch mode {
case "+": fieldwidth = fmt.Sprintf("%c%ds",'%',10)
default: fieldwidth = fmt.Sprintf("%c%ds",'%',8)
numwidth = fmt.Sprintf("%c%d.1f",'%',8)
}
for t := 1; t < tmax; t++ {
fmt.Printf("\x1b[2J") // CLS
count := 0.0
mass_count := 0.0
for y := 0; y < ylim; y++ {
for x := 0; x < xlim; x++ {
if !C.Blocked(st_rows,x,y) {
mass := C.AGENT[C.COORDS[x][y]].MassID
if mass > 0 {
fmt.Printf(fieldwidth,Momentum(C.AGENT[C.COORDS[x][y]].Intent))
mass_count += mass
continue
}
observable := C.AGENT[C.COORDS[x][y]].Psi
count += observable
if observable != 0 {
if mode == "+" {
if observable > 1 {
fmt.Printf(fieldwidth,"+")
} else if observable < -1 {
fmt.Printf(fieldwidth,"-")
} else {
fmt.Printf(fieldwidth,".")
}
} else {
if observable*observable > 1 {
fmt.Printf(numwidth,observable)
} else {
fmt.Printf(fieldwidth,".")
}
}
} else {
fmt.Printf(fieldwidth,".")
}
} else {
fmt.Printf(fieldwidth," ")
}
}
fmt.Println("")
}
fmt.Println("TOTAL =",count, "MASS=", mass_count)
const noflicker = 10
time.Sleep(noflicker * time.Duration(50) * time.Millisecond) // random noise
}
}
// ***********************
func Momentum(p [C.MOMENTUMPROCESS]int) string {
var s string
for i := 0; i < C.MOMENTUMPROCESS; i++ {
s += fmt.Sprintf("%d",p[i])
}
return s
}