Simultech 2021 4 CR
Simultech 2021 4 CR
Abstract: In the world of Agent-Based Modeling (ABM), NetLogo reigns as the most widely used platform. The NetL-
ogo world of agents interacting in a two-dimensional space seems to provide just the right level of simplicity
and abstraction for a wide range of models. Regrettably, the NetLogo language makes model development
more painful than necessary.
This combination—widespread popularity accompanied by unnecessary coding pain—motivated the develop-
ment of PyLogo, a NetLogo-like modeling and simulation environment in which developers write their models
in Python. Although other NetLogo-like systems exist, as far as we know PyLogo is the only NetLogo-like
system in Python at this level of completeness. This paper examines a number of issues with the NetLogo
language and offers a simple, illustrative PyLogo example model.
PyLogo is open source and is available at this GitHub repository. We welcome collaborators.
• To document some of NetLogo’s awkward fea- NetLogo (Tisue and Wilensky, 2004a; Tisue and
tures. (Section 3). Wilensky, 2004b) teased the possibility of writing
models in pseudo-English.
• To discuss a small PyLogo model. (Section 4).
It stands apart as the best platform for both begin-
ners and serious scientific modelers. Many if not
most public scientific ABMs are implemented in
NetLogo, which has become a standard platform Tisue and Wilensky continue:
for agent-based simulation (Railsback et al., 2017).
Logo is best known for its “turtle graphics,” in
NetLogo’s popularity triggered work in multiple which a virtual being or “turtle” moves around the
directions. For example, mechanisms exist for inter- screen drawing figures by leaving a trail behind it-
acting with NetLogo from other languages. self. NetLogo generalizes Logo’s single turtle to
support hundreds or thousands of turtles all mov-
• NetLogo Mathematica allows Mathematica to ing around and interacting.
control NetLogo. (Bakshy and Wilensky, 2007). Different “breeds” of turtle may be defined, and
• NL4Py (Gunaratne and Garibay, 2018) and Pynet- different variables and behaviors can be associated
logo (Jaxa-Rozen and Kwakkel, 2018) offer ac- with each breed. In effect, NetLogo adds a primi-
cess to and control over NetLogo from Python. A tive object-oriented overlay to Logo.
NetLogo extension allows calls to Python. Turtles inhabit a grid of programmable “patches.”
Collectively, the turtles and patches are called
• RNetLogo (Thiele, 2014) enables one to write R “agents.” Agents can interact with each other.
code that calls NetLogo. A NetLogo extension A collection of agents—e.g., the set of all turtles or
allows one to call R from NetLogo. the set of all patches—is known as an agentset. One
Finally, a number of NetLogo-like systems allow can make custom agentsets on the fly: for example
model developers to write in a language other than the the set of all red turtles or the set of patches with
a given X coordinate. One can ask all agents in an
standard NetLogo script. agentset, to perform a series of operations.
• ReLogo (Ozik et al., 2013), an offspring of Repast,
allows users to write models in Groovy, a script- NetLogo is specified in a clearly-written user
like version of Java. manual (Wilensky, 2019).
The remainder of this section discusses concerns
• Mesa (Masad and Kazil, 2015) comes closest to about the language.
PyLogo. Although Mesa less complete (no links),
Mesa models are written in Python. Mesa consists
of a “headless” modeling component along with a
3.2 Keywords
JavaScript visualization component.
NetLogo is somewhat ambiguous about keywords.
NetLogo Critiques. We found no earlier work The Programming Guide says,
critiquing NetLogo as a language. The only keywords are globals, breed, turtles-
own, patches-own, to, to-report, and end, plus
extensions and the experimental includes.
3 THE NetLogo LANGUAGE But tacking -own onto a breed name makes the
combination a keyword: if cats is a breed, cats-own
This section discusses the NetLogo language and functions like turtles-own.
some of the issues that motivated PyLogo. NetLogo has a number of such “keyword-
We wish to acknowledge (again) NetLogo’s ex- constructors.” For example, breeds of links are de-
traordinary success. Wilensky (Wilensky, 2020) re- clared using the “keywords” undirected-link-breed
ports that since January 2019 there have been at least and directed-link-breed.
700k unique visitors to the NetLogo website, 130k Furthermore,
NetLogo downloads, 7000 papers that use NetLogo, Built-in primitive names may not be shadowed or
and 600 courses that use NetLogo. Wilensky believes redefined, so they are effectively a kind of keyword.
these are significant under-estimates.
Although the NetLogo dictionary lists roughly
500 built-in names, this is less of a problem than one
3.1 A Brief History of NetLogo might imagine. Most built-in names are either con-
stants (like true) or function names (like sin).
Tisue and Wilensky (Tisue and Wilensky, 2004a),
More confusingly, control constructs such as if,
(Tisue and Wilensky, 2004b) discuss NetLogo.
ifelse, ifelse-value, and while appear as if they were
Logo, as originally developed by Seymour Papert keywords. According to the Programming Guide,
and Wally Feurzeig (Feurzeig and Papert, 1967), is
derived from Lisp but has a syntax that seems to Control structures such as if and while are special
non-programmers to be similar to English. forms. You can’t define your own special forms, so
you can’t define your own control structures.
Although Logo’s debt to Lisp is clear, see (Harvey,
1982), its commitment to being “English-like” led to Infix operators such as of and with, and identifiers
an imperative-style. such as self and myself also function like keywords.
It would appear that and, or, set, let, filter, map. 3.4.1 ask and foreach
and other functions are also defined via special forms.
It adds confusion to deny keyword status to identifiers Consider a world of five turtles and two lists, both or-
defined via special forms. dered by who-number: a list of their heading degrees,
Finally, breed is both a built-in own variable, and a list of distances to move. We want our turtles
which records—and can be set to change—an entity’s to turn by the indicated number of degrees and then
breed, and a keyword used to declare breeds. to move forward the indicated distance. This seems
tailor-made for foreach.
3.3 A Primitive Object-oriented But turtles is an agentset and foreach requires
Capability lists—and the problem requires that they be ordered.
Our solution will be to use sort-on [who] turtles
NetLogo offers a primitive form of object-oriented to convert the turtles agentset into an ordered list of
programming through its breed mechanism. If one turtles. Then, since we are requiring the turtles to per-
thinks of NetLogo’s turtle as similar to Python’s ob- form turtle methods, we will embed an ask in the fore-
ject, a breed is something like a subclass of turtle. ach anonymous procedure. Not very pretty code.
Both turtles and breeds may have the equivalent of ( f o r e a c h s o r t −on [ who ] t u r t l e s
instance variables—declared through the -own key- [ 30 40 120 50 270 ]
word constructor. [ 40 20 50 10 30 ]
But breeds differ in important ways from classes [ [ t deg d i s t ] −>
in most object-oriented languages. a s k t [ r t deg f d d i s t ] ] )
n e x t − t u r t l e some− t u r t l e
parses and correctly produces nothing. Why require
users to remember which form is required?
produces the correct answer. But attempting
[ n e x t − t u r t l e ] o f some− t u r t l e 3.7 Non-standard Terminology
produces the error message: NEXT-TURTLE expected NetLogo uses the terms reporter and report for con-
1 input. (See Section 3.8 for why.) Furthermore, one cepts for which virtually all other languages use func-
may not wrap next-turtle in an anonymous reporter. tion and return.
Agent primitives such as of and with don’t accept The functions word and sentence are also non-
anonymous procedures. standard and confusing.
The following are all equivalent. • The function word converts its argument(s) to
0 . n e x t − t u r t l e some− t u r t l e ; ; as above strings, which it concatenates.
1 . [ n e x t − t u r t l e s e l f ] o f some− t u r t l e • The function sentence combines the functionality
2 . f i r s t map n e x t − t u r t l e
of list and what might traditionally be called flat-
( l i s t some− t u r t l e )
3. ( runresult
ten. Confusingly, sentence has no specific con-
[ t −> n e x t − t u r t l e t ] some− t u r t l e ) nection to words or strings.
3.8 Higher-order Functions For a list containing those function, write
Since almost the very beginning, NetLogo has in- list [ n −> d o u b l e n ] [ n −> s q u a r e n ]
cluded the standard trio of higher-order functions: fil-
ter, map, and reduce. However, it appears to be quite rather than
awkward for model developers to create and use their
own higher order functions. list double square
Consider an attempted definition of the trivial
function application-of that expects a reporter proce-
dure as its first argument and an argument for that re- We could test this as follows.
porter procedure as its second. application-of applies
t o t e s t −a p p l i c a t i o n −o f [ k ]
its first argument to its second and returns the result. l e t l s t l i s t [ n −> d o u b l e n ]
(One might want this if one wants to select a function [ n −> s q u a r e n ]
from a list and apply it to some element.) f o r e a c h l s t [ f n −>
show a p p l i c a t i o n −o f f n k ]
to−r e p o r t a p p l i c a t i o n −o f [ f n a r g ] end
report fn arg
end t e s t −a p p l i c a t i o n −o f 6 ; ; => 12 36
That’s too much of a burden for comfort. 3.13 Dot Notation for Accessing
Parentheses are required around any construct Instance Variables and Methods
that includes a function (such as foreach, ifelse, map,
list, and others) that (a) may take a variable number Most object-oriented languages use dot-notation for
of arguments and (b) is given two or more. instance variables and methods. NetLogo fore-
Parentheses are not required if only one argument goes dot notation: [who] of turtle-x rather than
appears—except for list which requires parentheses turtle-x.who. Dot notation is simpler and cleaner.
for one argument but not for two.
penultimate line includes setup, go once, and go but- • step() executes at each tick. If the number of
tons, similar to NetLogo models. The go button, ticks has exceeded the user-settable “burst tick,”
initially green, turns red and is renamed stop when (not displayed), the static method Agent.update -
clicked. The exit button terminates the model. The agent velocities() is called. That, in turn, calls up-
top five lines allow the user to set model parameters. date velocity() for each Starburst Agent.
Starburst consists of a Starburst Agent subclass of update velocity() updates an agent’s velocity as
the PyLogo Agent class and a Starburst World sub- a function of its distances from its neighbors.
class of the PyLogo World class. (See Listing 1.) The influence radius slider determines how close
• setup() consults the gui to determine the num- agents must be to effect each other.
ber of agents. It creates those agents and sets Agent.forces cache stores the inter-agent forces so
their velocities as described earlier. PyLogo pro- that they are computed only once each tick.
vides substantial agent-motion support, including Agent.update agent positions() updates agent po-
velocity as an agent attribute. sitions based on their positions and velocities.
5 CONCLUSION Masad, D. and Kazil, J. (2015). Mesa: an agent-based mod-
eling framework. In 14th PYTHON in Science Confer-
PyLogo has two major weaknesses. ence, pages 53–60.
Nagpal, A. and Gabrani, G. (2019). Python for data ana-
• PyLogo is not optimized. It runs more slowly lytics, scientific and technical applications. In 2019
than NetLogo, and it can gracefully handle only Amity International Conference on Artificial Intelli-
a smaller number of agents. Even so, it handles gence (AICAI), pages 140–145. IEEE.
the standard NetLogo examples quite well. North, M. J., Collier, N. T., Ozik, J., Tatara, E. R., Macal,
C. M., Bragen, M., and Sydelko, P. (2013). Com-
• PyLogo doesn’t offer graphing. plex adaptive systems modeling with repast simphony.
Notwithstanding these limitations, PyLogo was Complex adaptive systems modeling, 1(1):3.
more than adequate for teaching an ABM course. Ozik, J., Collier, N. T., Murphy, J. T., and North, M. J.
PyLogo’s primary advantage is that model devel- (2013). The relogo agent-based modeling language.
In 2013 Winter Simulations Conference (WSC), pages
opers write in Python. For people accustomed to writ- 1560–1568. IEEE.
ing software, writing in Python is much less frustrat- Railsback, S., Ayllón, D., Berger, U., Grimm, V., Lytinen,
ing than writing in NetLogo. Experienced program- S., Sheppard, C., and Thiele, J. (2017). Improving
mers often feel that they are fighting the language execution speed of models implemented in netlogo.
when writing in NetLogo. The opposite is generally Journal of Artificial Societies and Social Simulation,
true when writing in Python. 20(1).
PyLogo confirms that something as simple and Taghawi-Nejad, D., Tanin, R. H., Chanona, R. M. D. R.,
intuitive as agents interacting on a grid (using tick- Carro, A., Farmer, J. D., Heinrich, T., Sabuco, J., and
based scheduling) accommodates a very wide range Straka, M. J. (2017). Abce: A python library for eco-
nomic agent-based modeling. In International Con-
of models. ference on Social Informatics, pages 17–30. Springer.
The development of PyLogo—a fully operational
Taillandier, P., Gaudou, B., Grignard, A., Huynh, Q.-N.,
core completed in a month, with additional features Marilleau, N., Caillou, P., Philippon, D., and Dro-
plus a range of models added while using the system goul, A. (2019). Building, composing and experi-
for a class—demonstrates that Python enables rapid menting complex spatial models with the gama plat-
development of a fairly sophisticated system. form. GeoInformatica, 23(2):299–322.
Thiele, J. C. (2014). R marries netlogo: introduction to
PyLogo is comparatively small: 10 core files and the rnetlogo package. Journal of Statistical Software,
15 models of 2,000 SLOC and 3,000 SLOC respec- 58(2):1–41.
tively. It is open-source and available for download. Tisue, S. and Wilensky, U. (2004a). Netlogo: A simple en-
vironment for modeling complexity. In International
conference on complex systems, volume 21, pages 16–
21. Boston, MA.
REFERENCES Tisue, S. and Wilensky, U. (2004b). Netlogo: Design and
implementation of a multi-agent modeling environ-
Badham, J. (2015). Review of: Wilensky, An Introduction ment. In Proceedings of agent, volume 2004, pages
to Agent-Based Modeling. Journal of Artificial Soci- 7–9.
eties and Social Simulation, 18(4).
Vahdati, A. R. (2019). Agents.jl: agent-based modeling
Bakshy, E. and Wilensky, U. (2007). Netlogo-mathematica framework in julia. Journal of Open Source Software,
link. Center for Connected Learning and Computer- 4(42):1611.
Based Modeling, Northwestern University, Evanston,
IL. Wilensky, U. (2019). Netlogo user manual.
Feurzeig, W. and Papert, S. (1967). The logo programming Wilensky, U. (2020). Private communication.
language. ODP-Open Directory Project.
Grigoryev, I. (2015). Anylogic 7 in three days. A quick
course in simulation modeling, 2.
Gunaratne, C. and Garibay, I. (2018). Nl4py: Agent-
based modeling in python with parallelizable netlogo
workspaces. arXiv preprint arXiv:1808.03292.
Harvey, B. (1982). Why logo. Byte, 7(8):163–193.
Jaxa-Rozen, M. and Kwakkel, J. H. (2018). Pynetlogo:
Linking netlogo with python. Journal of Artificial So-
cieties and Social Simulation, 21(2).
Luke, S., Cioffi-Revilla, C., Panait, L., Sullivan, K., and
Balan, G. (2005). Mason: A multiagent simulation
environment. Simulation, 81(7):517–527.