Preface
This course provides an introduction to agent-based modeling and simulation. A core goal is to allow the reader to quickly get up and running: experimenting with, modifying, and creating simulation models. The models in this course have a social-scientific emphasis, but the tools are generalizable to many other fields.
Manipulation and and modification of existing models is the best way to become famililar with the current scope of agent-based social science. This requires a few basic programming skills, which this course introduces gently and systematically. No prior programming experience is needed.
Agent-based research requires careful analysis of simulation results, This course focuses on illustrative analyses of simple pedagogical models. Investigation of these models draws on a small number of data-science concepts and tools. No prior statistics background is assumed; some basic statistical tools are introduced as needed. This provides readers with the background needed to create and analyze new simulations in support of original research.
get the reader “up and running”
provide illustrative applications that demonstrate the scope of social science simulation
introduce tools to support social science computing
provide adequate programming background to allow original research
introduce the analysis of simulation results
explore the real-world relevance of simulation results
Overview
Approach
The approach of this course is impatient, incomplete, incremental, and pragmatic. The structure assumes the reader is eager to explore social-science simulations, and it introduces useful and illustrative programming projects as soon as possible. A quickstart approach requires a simple and approachable programming language. Code examples in this course therefore use the NetLogo programming language, which prides itself on being easy-to-learn. Additionally, NetLogo is powerful and flexible, with many academic and business applications.
While this course introduces many core features of NetLogo, the treatment of NetLogo intentionally remains incomplete. We do not attempt to document the NetLogo programming language. Excellent documentation exists online, and this course freely references this documentation. There are also many excellent books that focus on advanced details of the NetLogo language. ([Railsback.Grimm-2019-PrincetonUP] is an excellent example.) This course is intentionally incomplete in another way as well. The scope of agent-based modeling is enormous, including physical sciences, biological sciences, operations research, and computer science. This course emphasizes topics that are likely to be useful to social-science projects.
Model development in this course is incremental. Early chapters introduce concepts without treating them in exhaustive detail, and later chapters return to concepts and develop them more fully. Each lecture introduces details and advanced features on an as needed basis. This is core to a pragmatic and project-oriented approach to simulation modeling. Most discussions of computer-science niceties remain in appendices or refer to external resources.
Target Audience
This impatient, incomplete, incremental, and pragmatic approach reflects this course’s target audience: students, teachers, and researchers in the social sciences who seek an introduction to agent-based modeling and simulation (ABMS). This course largely ignores the multi-agent modeling in other domains—including the physical sciences, biological sciences, medicine, and business. There is a strong social-science emphasis, with particular weight on economics and sociology.
This course is suitable for use as a primary textbook in undergraduate courses or as a supplementary textbook in graduate courses. It intentionally limits the prerequisite knowledge, so motivated individuals will find the course useful even without a supporting course. The only mathematics needed is basic algebra. Some lectures make limited use of basic statistics, but the relevant statistical concepts will be introduced as needed. More advanced material appears on occasion, but it is always isolated from the core topic developments.
This course loosely considers a model to be “agent-based” to the extent that it explicitly characterizes the attributes and behavior of individual actors (the agents). (A refined defintion is developed within.) Agent-based modeling thereby contrasts with aggregative models, which characterize only the average behavior of large groups of individuals. Modern agent-based modeling requires the use of computers, since keeping track of many individuals by hand quickly becomes tiresome and error prone. Correspondingly, some programming is required.
However, this course assumes no prior knowledge of programming. Of course, reading this course will require reading computer code, and project completion requires the reader to write some code as well. An unprepared reader who peers ahead at the later chapters may find that they appear somewhat intimidating. However, early chapters teach the requisite programming skills. The material in this course has been used successfully in the classroom with students who have had no prior programming experience.
Discrete-Time Models
This course emphasizes discrete-time dynamical systems. Time progresses in discrete increments, called the timestep, period, or tick. By using a small time step, we can make time as close to continuous as needed. In computational social science, and especially in agent-based models, it is unusual to use a time-step of less than a day. The discussion of securities trading in the lecture on zero-intelligence traders is an exception to this rule.
The simplest models in computational social science, including many of the models in this course, often leave the time scale rather vaguely defined. However, time still evolves in discrete steps. This course does not cover continuous time modeling, nor does it pay more than cursory attention to discrete-event simulation.
Programming Language
There is no simple rule when choosing a programming language for agent-based simulation. Practitioners take many different approaches. Some rely on general purpose programming languages, such as Python or Java. Others rely on languages specially designed to facilitate agent-based simulation, known as domain-specific languages. Domain-specific languages for ABMS address many of the common needs of agent-based simulation. This class of languages includes NetLogo. In contrast with most treatments of agent-based modeling, this course strives to provide language-agnostic descriptions of agent-based models. However, illustrative code is always provided in NetLogo.
Among the domain specific languages, NetLogo offers a unique combination of popularity, ease of use, and power. Ease of use is particularly important for new modelers, and reflects NetLogo’s long history as a teaching language. In particular, the NetLogo language emphasizes readability and intuitive syntax. ([Wilensky.Rand-2015-MIT] suggest that NetLogo is as readable as psuedocode.) For more background, see the Introduction to NetLogo supplement.
Power becomes more important as a modeler advances and places new demands on the language. Despite its ease of use, NetLogo powerful enough to support serious research projects. (And should its limits ever become important in a particular project, NetLogo is also readily extensible.)
NetLogo's popularity derives in large part from its combination of ease of use and power. But the popularity of the language adds other advantages: there many existing models, and there is an active and responsive community of NetLogo modelers. (For a sense of this, visit http://stackoverflow.com/questions/tagged/netlogo.) The latter advantage serves as an example of a network effect, which is a key social-science concept. The existing NetLogo community increases the value of NetLogo for community members and for new users.
Among the domain-specific languages for multi-agent simulation, NetLogo yet another advantage. The NetLogo tookit ships with curated code examples and pedagogical models. The NetLogo Models Library is an enormously important feature for the use of NetLogo in the classroom. Some of the models simply illustrate NetLogo coding strategies. Many offer simple examples of models with real-word importance, including applications in mathematics, the physical sciences, and the social sciences. This course will draw on the social-sciences models in the NetLogo Models Library, sometimes directly and often in exercises. The value of these resources for students cannot be overstated.
Features
This course is designed for “hands on” learning. It is best read near a computer, where one can try the code examples and explore the models.
Our primary model selection criteria were simplicity and their pedagogical utility. Some models primarily illustrate computational tools useful for social-science. Other models introduce no new tools but foster useful insight into the nature of agent-based social-science.
All models include code examples in the NetLogo programming language. Additionally, the code for the models presented in this course is available online, at the associated website. Nevertheless, a key feature of the course is a reliance on relatively language-agnositc model descriptions. This means that the course is also useful for readers who wish to use a different language for the exercises.
In support of these language-agnositc descriptions,
variable names follow somewhat different conventions than is typical in NetLogo models.
For example,
although NetLogo is case insensitive,
we use capitalization to enhance readability.
We also avoid punctuation in variable names.
It most languages, in contrast to NetLogo,
question marks and hyphens are not valid characters in variable identifiers.
Where a NetLogo programmer might use names like n-fish
and n-casts
,
we instead use nFish
and nCasts
.
Similary, we do not include a question mark when naming a boolean attribute.
NetLogo programmers are more likely to name a variable hungry?
than isHungry
,
but we choose the latter convention.
We adopt these conventions so that our variable names can be reused in many other languages.
From the perspective of the course, the use of NetLogo carries with in one primary disadvantage. NetLogo encourages a conflation between model-focused considerations and graphical user interface (GUI) considerations. The NetLogo designers, and many users, consider this an intentional design feature. In fact, many of the models in the NetLogo Models Library deliberately use (even at the level of code) colors to represent important agent attributes in the conceptual model. This course shies away from that practice. In contrast to many NetLogo-focused discussion of agent-based modeling, this course attempts to keep model-based considerations distinct from GUI considerations. This distinction is not forced in the very small number of places where NetLogo renders it a bit awkward.
Who Uses Agent-based Modeling?
ABMS has found wide application in natural science, social science, public policy, and education.
Natural systems (science, engineering etc.)
Many existing simulation techniques, question is purpose, strengths, weaknesses
Social systems (humanities, social science, business/marketing etc.)
Often the best or only laboratory available
Applications
Real-world simulation, optimization
Research
Classroom
Agent-Based Modeling and Simulation (ABMS)
In Economics, ABMS is sometimes known as agent-based computational economics (ACE).
Autonomous agents represent economic actors who make their own decisions (perhaps constrained).
ABMS is suitable for interactions which are bottom- up, not top-down. Interactions are decentralised.
E.g., social and market interactions, rather than engineering or internal organizational interactions.
Choice of Modeling Language
Substantial research in agent-based modeling and simulation (ABMS) has been conducted in a variety of general purpose programming languages, including procedural languages such as Fortran, C, or Pascal; object oriented languages such as Objective-C, Smalltalk, C++, Python, or Java; very-high level matrix languages such as MATLAB or GAUSS; and even symbolic algebra languages such as Maple or Mathematica. There are also a variety of special purpose toolkits for ABMS, including SWARM and MAML (built in Objective-C); Ascape, NetLogo, and Repast (built in Java or Scala); and many others [gilbert.bankes-2002-pnas]. [1] In addition, there are a few efforts at general environments in which a variety of ABMS experiments can be run. (Bremer’s GLOBUS model, Hughes’s IFs model, and Tesfatsion’s Trade Network Game model are salient examples.) Researchers occasionally express the hope that one of these might emerge as a lingua franca for ABMS research [luna.stefansson-2000-swarm]. Working against this, as others note, are the interfaces of capable toolkits and environments, which are often as difficult to master as a full-blown programming language [gilbert.bankes-2002-pnas]. A variety of languages and toolkits remain in use, and this seems likely to persist, along with efforts to assess their relative utility [railsback.lytinen.jackson-2006-simulation].
Although this course focuses on implementing models in NetLogo, it also provides relatively language-agnostic model descriptions. These will be useful for readers who wish to attempt an implementation in another language. Some of the slighly idiosyncratic vocabulary in this course is meant to support language agnosticism. For examples, agents in this course have attributes and behaviors. The term behavior translates to procedure (for implementations in procedural languages such as NetLogo) or to procedure method (for implementations in object-oriented languages). This translation is left to the reader.
This course uses the term function to refer to any subroutine that explicitly returns a value.
Functions may be associated with agents,
especially when agent state is an implicit argument of the function.
The agent is considered to be the execution context (or just context)
of such a function.
To clarify this when discussing the function,
the agent type becomes a modifier.
For example, a Voter
function is a function that runs in Voter
context.
In an object-oriented language, this would be a function method of the Voter
type.
In a functional language, this would be a function that takes a Voter
type
as its first argument.
Again, this translation is left to the reader.
At times, language agnostic descriptions become awkward, so efforts in this direction are circumscribed. In particular, although this course presents some functional-programming concepts and techniques, it makes no serious attempt to accommodate readers who choose to model in a functional programming language. For example, the typical purpose of a procedure is to mutate values, and algorithm presentations generally assume the language in use provides definite and indefinite procedural loops. That said, readers who have chosen a functional programming language will often be sophisticated enough to work around the limited accommodation of their programming paradigm in this course.
Conformance to NetLogo Idioms
This course encourages separating display concerns from modeling concerns.
For better or worse,
many NetLogo programmers prefer to mix these together.
For example,
it is common to use the pcolor
attribute of patches
to encode patch state (not just its GUI representation).
Patch colors display in the model View provided by NetLogo’s GUI,
and this offers a natural representation of agent state.
Other aspects of the NetLogo language encourage this
mixing of display concerns with modeling concerns.
This is particularly true of NetLogo’s handling of its iteration counter,
ticks
.
This counter must be initialized with the reset-ticks command,
but this command also runs any setup code and update code contained in plot widgets.
(Additionally, buttons may be disabled until ticks
is initialized,
but no other conditional disabling is allowed.)
Similarly, the tick
command does not just increment ticks
,
it also runs any update code contained in plot widgets.
Furthermore, the tick
command updates the View in the Interface
tab
whenver the model is set to use tick-based updates.
(This is the only alternative to continuous updates,
which by default happen \(30\) times per second.)
In order to better separate display concerns from modeling concerns,
this course therefore often deviates from a small number common NetLogo idioms.
For example, GUI coloration and agent state are usually separated.
As another example, a separate steps
counter is often
introduced as a global variable in models,
rather than relying on ticks
.
Editing a model to better match the idioms in NetLogo’s Models Library
generally requires small changes in no more than three or four lines of code.
In one additional deviation from a common NetLogo idiom,
this course often includes plotting code in the NetLogo Code
tab
rather than within the plot widget.
The reason for this is purely pedagogical:
it is often easier to examine the code for multiple plots
if it is all grouped together rather than hidden in separate plot widgets.
This advantage comes at a fairly small cost:
plot widgets must be specified with the set-current-plot
command.
One payoff to this course’s attempts to better separate modeling concerns from presentation concerns is a more language-agnostic presentation. Most model descriptions in this course should be readily implemented in a variety of programming languages, without requiring use of a GUI toolkit.
Typographic Conventions
This book uses the following typographical conventions.
- new terms
Potentially unfamiliar terms may receive emphasis, especially near an initial definition.
code
Code snippets are monospaced.
- <substitutions>
Angle brackets indicate a needed substition by the reader (e.g., of a local file path). The substitution replaces the brackets as well as the enclosed term.