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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

436 CHAPTER 19. AGENT-BASED MODELStion, which was easy in CA <strong>and</strong> networks because <strong>the</strong> neighborhood relationships wereexplicitly modeled in those modeling frameworks. But in ABM, neighborhood relationshipsmay be implicit, which is <strong>the</strong> case for our model. Therefore we need <strong>to</strong> implement a codethat allows each agent <strong>to</strong> find who is nearby.There are several computationally efficient algorithms available for neighbor detection,but here we use <strong>the</strong> simplest possible method: Exhaustive search. You literally check all<strong>the</strong> agents, one by one, <strong>to</strong> see if <strong>the</strong>y are close enough <strong>to</strong> <strong>the</strong> focal agent. This is notcomputationally efficient (its computational amount increases quadratically as <strong>the</strong> number<strong>of</strong> agents increases), but is very straightforward <strong>and</strong> extremely easy <strong>to</strong> implement. Youcan write such exhaustive neighbor detection using Python’s list comprehension, e.g.:Code 19.7:neighbors = [nb for nb in agentsif (ag.x - nb.x)**2 + (ag.y - nb.y)**2 < r**2 <strong>and</strong> nb != ag]Here ag is <strong>the</strong> focal agent whose neighbors are searched for. The if part in <strong>the</strong> listcomprehension measures <strong>the</strong> distance squared between ag <strong>and</strong> nb, <strong>and</strong> if it is less thanr squared (r is <strong>the</strong> neighborhood radius, which must be defined earlier in <strong>the</strong> code) nb isincluded in <strong>the</strong> result. Also note that an additional condition nb != ag is given in <strong>the</strong> ifpart. This is because if nb == ag, <strong>the</strong> distance is always 0 so ag itself would be mistakenlyincluded as a neighbor <strong>of</strong> ag.Once we obtain neighbors for ag, we can calculate <strong>the</strong> fraction <strong>of</strong> <strong>the</strong> o<strong>the</strong>r agentswhose type is <strong>the</strong> same as ag’s, <strong>and</strong> if it is less than a given threshold, ag’s position isr<strong>and</strong>omly reset. Below is <strong>the</strong> completed simula<strong>to</strong>r code, with <strong>the</strong> visualization functionalso implemented using <strong>the</strong> simple plot function:Code 19.8: segregation.pyimport matplotlibmatplotlib.use(’TkAgg’)from pylab import *n = 1000 # number <strong>of</strong> agentsr = 0.1 # neighborhood radiusth = 0.5 # threshold for movingclass agent:pass

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

Saved successfully!

Ooh no, something went wrong!