Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
First half of PyCX 1.0 sample codes
hsayama authored Dec 18, 2019

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent a5a8db7 commit c4f3dc9
Showing 61 changed files with 2,561 additions and 0 deletions.
69 changes: 69 additions & 0 deletions abm-DLA.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import pycxsimulator
from pylab import *

width = 100
height = 100
populationSize = 1000

noiseLevel = 1
collisionDistance = 2
CDsquared = collisionDistance ** 2

toBeRemoved = -1

def initialize():
global time, free, fixed

time = 0

free = []
for i in range(populationSize - 1):
free.append([uniform(0, width), uniform(0, height)])

fixed = []
fixed.append([width / 2, height / 2])

def observe():
cla()
if free != []:
x = [ag[0] for ag in free]
y = [ag[1] for ag in free]
scatter(x, y, color = 'cyan')
if fixed != []:
x = [ag[0] for ag in fixed]
y = [ag[1] for ag in fixed]
scatter(x, y, color = 'blue')
axis('scaled')
axis([0, width, 0, height])
title('t = ' + str(time))

def clip(a, amin, amax):
if a < amin: return amin
elif a > amax: return amax
else: return a

def update():
global time, free, fixed

time += 1

# simulate random motion
for ag in free:
ag[0] += normal(0, noiseLevel)
ag[1] += normal(0, noiseLevel)
ag[0] = clip(ag[0], 0, width)
ag[1] = clip(ag[1], 0, height)

# detect collision and change state
for i in range(len(free)):
for j in range(len(fixed)):
if (free[i][0]-fixed[j][0])**2 + (free[i][1]-fixed[j][1])**2 < CDsquared:
fixed.append(free[i])
free[i] = toBeRemoved
break

# remove "toBeRemoved" free particles
while toBeRemoved in free:
free.remove(toBeRemoved)

pycxsimulator.GUI().start(func=[initialize, observe, update])
89 changes: 89 additions & 0 deletions abm-ants-pheromone.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import pycxsimulator
from pylab import *

width = 50
height = 50
populationSize = 3000

evaporationRate = 0.02
diffusionCoefficient = 0.8
hillClimbingProb = 0.95

def initialize():
global time, agents, envir, nextenvir

time = 0

agents = []
for i in range(populationSize):
newAgent = [randint(width), randint(height)]
agents.append(newAgent)

envir = zeros([height, width])
for y in range(height):
for x in range(width):
envir[y, x] = random()

nextenvir = zeros([height, width])

def observe():
cla()
imshow(envir, cmap = cm.YlOrRd, vmin = 0, vmax = 3)
axis('image')
x = [ag[0] for ag in agents]
y = [ag[1] for ag in agents]
scatter(x, y, cmap = cm.bone, alpha = 0.2)
title('t = ' + str(time))

def clip(a, amin, amax):
if a < amin: return amin
elif a > amax: return amax
else: return a

def update():
global time, agents, envir, nextenvir

time += 1

# diffusion and evaporation of phenomones
for x in range(width):
for y in range(height):
localAv = 0
for dx in range(-1, 2):
for dy in range(-1, 2):
localAv += envir[(y+dy) % height, (x+dx) % width]
localAv /= 9.0
nextenvir[y, x] = envir[y, x] + (localAv - envir[y, x]) * diffusionCoefficient
nextenvir[y, x] *= (1.0 - evaporationRate)

envir, nextenvir = nextenvir, envir

for ag in agents:

if random() < hillClimbingProb:
# simulate hill-climbing motion
maxph = 0
maxdx = 0
maxdy = 0
for dx in range(-1, 2):
for dy in range(-1, 2):
tempx = (ag[0]+dx) % width
tempy = (ag[1]+dy) % height
if maxph < envir[tempy, tempx]:
maxph = envir[tempy, tempx]
maxdx = dx
maxdy = dy
ag[0] += maxdx
ag[1] += maxdy
else:
ag[0] += randint(-1, 2)
ag[1] += randint(-1, 2)

ag[0] = clip(ag[0], 0, width - 1)
ag[1] = clip(ag[1], 0, height - 1)

# production of pheromones
envir[ag[1], ag[0]] += 0.01

pycxsimulator.GUI().start(func=[initialize, observe, update])

69 changes: 69 additions & 0 deletions abm-ants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import pycxsimulator
from pylab import *

width = 50
height = 50
populationSize = 50

free = 0
carrying = 1

garbageProb = 0.8

def initialize():
global time, agents, envir

time = 0

agents = []
for i in range(populationSize):
newAgent = [randint(width), randint(height), free]
agents.append(newAgent)

envir = zeros([height, width])
for y in range(height):
for x in range(width):
if random() < garbageProb:
state = 1
else:
state = 0
envir[y, x] = state

def observe():
cla()
imshow(envir, cmap = cm.YlOrRd, vmin = 0, vmax = 5)
axis('image')
x = [ag[0] for ag in agents]
y = [ag[1] for ag in agents]
s = [ag[2] for ag in agents]
scatter(x, y, c = s, cmap = cm.bwr)
title('t = ' + str(time))

def clip(a, amin, amax):
if a < amin: return amin
elif a > amax: return amax
else: return a

def update():
global time, agents, envir

time += 1

for ag in agents:

# simulate random motion
ag[0] += randint(-1, 2)
ag[1] += randint(-1, 2)
ag[0] = clip(ag[0], 0, width - 1)
ag[1] = clip(ag[1], 0, height - 1)

# simulate interaction between ants and environment
if envir[ag[1], ag[0]] > 0:
if ag[2] == free:
envir[ag[1], ag[0]] -= 1
ag[2] = carrying
else:
envir[ag[1], ag[0]] += 1
ag[2] = free

pycxsimulator.GUI().start(func=[initialize, observe, update])
62 changes: 62 additions & 0 deletions abm-keller-segel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import pycxsimulator
from pylab import *

n = 1000 # number of agents
w = 100 # number of rows/columns in spatial array

k = 1 # rate of cAMP decay
Dc = 0.001 # diffusion constant of cAMP
Dh = 0.01 # spatial resolution for cAMP simulation
Dt = 0.01 # time resolution for cAMP simulation

f = 1 # rate of cAMP secretion by an agent

class agent:
pass

def initialize():
global agents, env, nextenv

agents = []
for i in range(n):
ag = agent()
ag.x = randint(w)
ag.y = randint(w)
agents.append(ag)

env = zeros([w, w])
nextenv = zeros([w, w])

def observe():
global agents, env, nextenv
cla()
imshow(env, cmap = cm.binary, vmin = 0, vmax = 1)
axis('image')
x = [ag.x for ag in agents]
y = [ag.y for ag in agents]
plot(y, x, 'b.') # x and y are swapped to match the orientation of env

def update():
global agents, env, nextenv

# simulating diffusion and evaporation of cAMP
for x in range(w):
for y in range(w):
C, R, L, U, D = env[x,y], env[(x+1)%w,y], env[(x-1)%w,y], \
env[x,(y+1)%w], env[x,(y-1)%w]
lap = (R + L + U + D - 4 * C) / (Dh**2)
nextenv[x,y] = env[x,y] + (- k * C + Dc * lap) * Dt
env, nextenv = nextenv, env

# simulating secretion of cAMP by agents
for ag in agents:
env[ag.x, ag.y] += f * Dt

# simulating chemotaxis of agents
for ag in agents:
newx, newy = (ag.x + randint(-1, 2)) % w, (ag.y + randint(-1, 2)) % w
diff = (env[newx, newy] - env[ag.x, ag.y]) / 0.1
if random() < exp(diff) / (1 + exp(diff)):
ag.x, ag.y = newx, newy

pycxsimulator.GUI().start(func=[initialize, observe, update])
96 changes: 96 additions & 0 deletions abm-predator-prey-evolvable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import pycxsimulator
from pylab import *

import copy as cp

nr = 500. # carrying capacity of rabbits

r_init = 100 # initial rabbit population
mr = 0.03 # magnitude of movement of rabbits
dr = 1.0 # death rate of rabbits when it faces foxes
rr = 0.1 # reproduction rate of rabbits

f_init = 30 # initial fox population
mf = 0.05 # magnitude of movement of foxes
df = 0.1 # death rate of foxes when there is no food
rf = 0.5 # reproduction rate of foxes

cd = 0.02 # radius for collision detection
cdsq = cd ** 2

class agent:
pass

def initialize():
global agents
agents = []
for i in range(r_init + f_init):
ag = agent()
ag.type = 'r' if i < r_init else 'f'
ag.m = mr if i < r_init else mf ###
ag.x = random()
ag.y = random()
agents.append(ag)

def observe():
global agents
cla()
rabbits = [ag for ag in agents if ag.type == 'r']
if len(rabbits) > 0:
x = [ag.x for ag in rabbits]
y = [ag.y for ag in rabbits]
plot(x, y, 'b.')
foxes = [ag for ag in agents if ag.type == 'f']
if len(foxes) > 0:
x = [ag.x for ag in foxes]
y = [ag.y for ag in foxes]
plot(x, y, 'ro')
axis('image')
axis([0, 1, 0, 1])

def update_one_agent():
global agents
if agents == []:
return

ag = choice(agents)

# simulating random movement
m = ag.m ###
ag.x += uniform(-m, m)
ag.y += uniform(-m, m)
ag.x = 1 if ag.x > 1 else 0 if ag.x < 0 else ag.x
ag.y = 1 if ag.y > 1 else 0 if ag.y < 0 else ag.y

# detecting collision and simulating death or birth
neighbors = [nb for nb in agents if nb.type != ag.type
and (ag.x - nb.x)**2 + (ag.y - nb.y)**2 < cdsq]

if ag.type == 'r':
if len(neighbors) > 0: # if there are foxes nearby
if random() < dr:
agents.remove(ag)
return
if random() < rr*(1-sum([1 for x in agents if x.type == 'r'])/nr):
newborn = cp.copy(ag) ###
newborn.m += uniform(-0.01, 0.01) ###
agents.append(newborn) ###
else:
if len(neighbors) == 0: # if there are no rabbits nearby
if random() < df:
agents.remove(ag)
return
else: # if there are rabbits nearby
if random() < rf:
newborn = cp.copy(ag) ###
newborn.m += uniform(-0.01, 0.01) ###
agents.append(newborn) ###

def update():
global agents
t = 0.
while t < 1. and len(agents) > 0:
t += 1. / len(agents)
update_one_agent()

pycxsimulator.GUI().start(func=[initialize, observe, update])
Loading

0 comments on commit c4f3dc9

Please sign in to comment.