forked from nhpackard/MICREAgents
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimulator.py
127 lines (101 loc) · 3.35 KB
/
simulator.py
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
"""Simulator of a Chemical Swarm System.
Run the simulator with
$ python simulator.py PROGRAM
where PROGRAM is a python module (without the traling .py) that can be
imported from the current directory. PROGRAM must provide exactly one
instance of a subclass of api.Space. The simulator must be stopped by
a KILL signal.
The simulator displays the time evolution of the system specified in
the program.
"""
import matplotlib
matplotlib.use('TkAgg')
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import Normalize, LinearSegmentedColormap
from scipy.sparse import spdiags,linalg,eye
import api
cmap = LinearSegmentedColormap('divergent', {
'red': [(0., 0., 0.),
(0.5, 1., 1.),
(1., 1., 1.)],
'green': [(0., 0., 0.),
(0.5, 1., 1.),
(1., 0., 0.)],
'blue': [(0., 1., 1.),
(0.5, 1., 1.),
(1., 0., 0.)],
})
norm = Normalize(vmin=0., vmax=1., clip=True)
def integrate(space, dt) :
"""Advance system time by dt"""
# diffusion and decay
def diffuse(u,it=iter(space.chemicals)) :
c = it.next()
return linalg.spsolve(space._II - dt*c.diffusion*space._A,u) - dt*c.decay*u
space.fields = np.apply_along_axis(diffuse, -1, space.fields)
# agent movement
for agent in space.agents :
dis = agent.displacement * dt**-0.5 * np.random.uniform(-1,1,2)
for chem,speed in agent.ascent.items() :
field = space.fields[space.chemicals.index(chem)]
grad = space._gradient(field, agent.pos)
if grad.any() :
if isinstance(speed, property) :
speed = speed.fget(agent)
dis += dt * speed * grad/np.linalg.norm(grad)
agent.pos += space._clip(agent.pos, dis)
# reaction
for agent in space.agents :
i = space._pos_to_index(*agent.pos)
dc = agent.reaction(space.fields[:,i])
if type(dc) is not np.array : dc = np.array(dc)
space.fields[:,i] += dt/space.resolution**2 * dc
dc = agent.exchange(space.fields[:,i])
if type(dc) is not np.array : dc = np.array(dc)
space.fields[:,i] -= dt/space.resolution**2 * dc
agent.reservoir += dt/space.resolution**2 * dc
for agent in space.agents :
# adhere to clock cycle
if space.t % agent.clock > dt : continue
# set agent sensors
for sensor in agent.sensors.values() :
i = space.chemicals.index(sensor.chemical)
if isinstance(sensor, api.ReservoirSensor) :
u = agent.reservoir[i]
elif isinstance(sensor, api.Sensor) :
u = space.fields[i,space._pos_to_index(*agent.pos)]
sensor.value = u >= sensor.threshold
# agent transitions
if agent.transitions :
inp = agent.state, tuple(s.value for s in agent.sensors.values())
agent.state = agent.transitions[inp]
space.t += dt
return dt
if __name__ == '__main__' :
import sys
sys.path.append('.')
model = __import__(sys.argv[1])
#import example as model
space = api.Space.__subclasses__()[0]()
space.init()
x = np.linspace(space.size_x[0], space.size_x[1], space._mx)
y = np.linspace(space.size_y[0], space.size_y[1], space._my)
plt.ion()
while True :
integrate(space, dt=space.resolution/2)
if True :
plt.clf()
# chemical field
U=space.fields[-1].reshape((space._my,space._mx))
plt.pcolormesh(x,y,U, norm=norm, cmap=cmap)
plt.colorbar
plt.axis('image')
plt.title(str(space.t))
# agents
i = [a.pos[0] for a in space.agents]
j = [a.pos[1] for a in space.agents]
plt.scatter(i,j)
plt.draw()
#print [a.reservoir[-1] for a in space.agents]
plt.ioff()