PyCX - socrateslab/zh GitHub Wiki
The PyCX Project aims to develop an online repository of simple, crude, yet easy-to-understand Python sample codes for dynamic complex systems simulations, including iterative maps, cellular automata, dynamical networks and agent-based models. http://pycx.sourceforge.net/
=Book=
Introduction to the Modeling and Analysis of Complex Systems Introduction to the Modeling and Analysis of Complex Systems http://textbooks.opensuny.org/introduction-to-the-modeling-and-analysis-of-complex-systems/
PyCXBook Code http://bingweb.binghamton.edu/~sayama/textbook/
=GUI= 遗憾的是,PyCX为了展现动态的变化需要使用Python的图像用户界面(GUI),因而不能在jupyter里使用。但可以在spyder当中使用,不过需要注意的是要选择以plain python console来运行,ipython console是不能够使用的。我的测试结果表明虽然可用,但是在打开的时候往往出错。
=例子=
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, rdata, fdata agents = [] rdata = [] fdata = [] for i in xrange(r_init + f_init): ag = agent() ag.type = 'r' if i < r_init else 'f' ag.x = random() ag.y = random() agents.append(ag)
def observe(): global agents, rdata, fdata
subplot(2, 1, 1)
cla()
rabbits = [ag for ag in agents if ag.type == 'r']
rdata.append(len(rabbits))
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']
fdata.append(len(foxes))
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])
subplot(2, 1, 2)
cla()
plot(rdata, label = 'prey')
plot(fdata, label = 'predator')
legend()
def update(): global agents if agents == []: return
ag = agents[randint(len(agents))]
# simulating random movement
m = mr if ag.type == 'r' else mf
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):
agents.append(cp.copy(ag))
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:
agents.append(cp.copy(ag))
def update_one_unit_time(): global agents t = 0. while t < 1.: t += 1. / len(agents) update()
import pycxsimulator pycxsimulator.GUI().start(func=[initialize, observe, update_one_unit_time])
=参考文献=