15.08.2015 Views

Introduction to the Modeling and Analysis of Complex Systems

introduction-to-the-modeling-and-analysis-of-complex-systems-sayama-pdf

introduction-to-the-modeling-and-analysis-of-complex-systems-sayama-pdf

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

19.2. BUILDING AN AGENT-BASED MODEL 437def initialize():global agentsagents = []for i in xrange(n):ag = agent()ag.type = r<strong>and</strong>int(2)ag.x = r<strong>and</strong>om()ag.y = r<strong>and</strong>om()agents.append(ag)def observe():global agentscla()white = [ag for ag in agents if ag.type == 0]black = [ag for ag in agents if ag.type == 1]plot([ag.x for ag in white], [ag.y for ag in white], ’wo’)plot([ag.x for ag in black], [ag.y for ag in black], ’ko’)axis(’image’)axis([0, 1, 0, 1])def update():global agentsag = agents[r<strong>and</strong>int(n)]neighbors = [nb for nb in agentsif (ag.x - nb.x)**2 + (ag.y - nb.y)**2 < r**2 <strong>and</strong> nb != ag]if len(neighbors) > 0:q = len([nb for nb in neighbors if nb.type == ag.type]) \/ float(len(neighbors))if q < th:ag.x, ag.y = r<strong>and</strong>om(), r<strong>and</strong>om()import pycxsimula<strong>to</strong>rpycxsimula<strong>to</strong>r.GUI().start(func=[initialize, observe, update])When you run this code, you should set <strong>the</strong> step size <strong>to</strong> 50 under <strong>the</strong> “Settings” tab <strong>to</strong>speed up <strong>the</strong> simulations.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!