How to Implement Continuous-Time Multi-Agent Crowd Simulation

Table of Contents
Introduction
In this article, we discuss the mathematics on how to implement a continuous-time multi-agent crowd simulation. The article covers two agent models, circular, unorientable model, and three-circle, orientable model. We include extensive discussion about simulation objects, simulation mechanics, and implementation details and conclude by addressing the shortcoming and challenges of this type of simulation. We intended this article as a reference implementation for crowd simulation. We have experimented with the implementation in the crowddynamics library.
We define crowd dynamics as the movement of crowds of people in respect of time. We implemented the simulation model using a continuous-time, Newtonian mechanics with an agent-based model, where each agent is a rigid body, in a two-dimensional lattice. In the model, the agents are intelligent self-propelled particles, whose movement is produced by non-physical and physical forces. The non-physical forces are referred to as social forces, for example, the desire to move toward a goal or collision avoidance. Hence, we refer to the model as the social force model, a term originating from 1.
There also exist other types of models, such as cellular automata which models agents as an occupied or unoccupied cells in a discrete lattice, that is, a grid, and continuum-based models which model the crowd as a continuous mass. The agent-based model has the advantage of being the most realistic model, but it is also computationally the most challenging model.
There are real-life applications for research on how crowds of people move, for example:
- Improving the safety of buildings, mass events, and crowded places.
- Estimating evacuation times and improving the evacuation process.
- Venue design and improving the flow rate of the crowd in crowded places, such as subways.
- Producing realistic movement in games, animations, and virtual reality.
The authors of the book 2 give a comprehensive look at the subject of crowd dynamics.
In the following sections, we are going to explain in-depth the mathematics behind the agent-based model and how to implement the simulation.
Geometric Primitives
We define two-dimensional geometric primitives as follows:
- Point is denoted as
- Circle is defined as a pair
where is the center, and is the radius. - Line segment is defined as a pair of points
. - Polygonal chain is a sequence of points
where each pair for is a line segment. - Closed polygonal chain is a polygonal chain where the endpoints
form a line segment. - Polygon is a filled shape whose exterior is a closed polygonal chain.
We use these geometric primitives for implementing some of the objects in the simulation.
Objects
We will refer to the collection of objects that exists in the simulation as the field. The field consists of the following objects:
The domain
, a subset of the lattice which contains all the other objects.The set of agents
for where are the indices. An agent is an impassable, dynamic object. We will denote two distinct agents using the indices as where .The set of obstacles
. An obstacle is an impassable, static object. We denote obstacles using the subscript , that is, .The set of targets
. A target is a passable, static object.The set of sources
. A source is a passable, static object. Initially, we place the agents inside a source without overlapping each other. For example, we can sample agent positions inside the source from a uniform distribution and whether the agent overlaps with other agents. If not, place the agent; otherwise, we will resample.
We implement the obstacles using polygonal chains and the domain, targets, and sources using polygons. We describe the implementation of the agent’s shape in the Agents section.
Agents
In the agent-based model, each agent is a discrete object, modeled as a rigid body. We use two models for modeling the shape of the agent; circular and three-circle, models. In addition to shape, agents have other attributes such as mass and desired velocity.
Circular Model
In the circular model, we model the agent’s shape using a circle. It is the simplest model and rotationally symmetrical. It is easiest to implement and compute and work well for sparse and medium-density crowds. However, it is not a very realistic model of the cross-section of the human torso. In reality, the human body is narrower and can fit through smaller spaces. In dense crowds, the circle model can result in artifacts and unrealistic density measures.
Each circular agent
center of mass velocity force desired direction desired velocity radius mass
Three-circle Model
The three-circle model consists of three circles representing the shoulders and midsection. The three-circle model is a more realistic representation of the human cross-section. However, since it is not rotationally symmetrical, simulation mechanics become more complex. For example, the model must include rotational motion and more complicated collision detection. In dense crowds, agents can move sideways, which requires solving desired body rotation. 3 4 5 6
Each orientable agent has following additional attributes:
orientation angular velocity torque desired orientation desired angular velocity rotational moment
We constrained the values of the orientations
In addition to the attributes of the circular and rotatable agent, each three-circle agent has attributes as follows.
torso radius shoulder radius torso-to-shoulder radius
Identity
The following sections will discuss the simulation mechanics, including forces and torques. We cover the mechanics for the circular model in detail. The forces for the circular model can be generalized for the three-circle agent model by deriving the variables, social force, and contact force between three-circle agents and three-circle agent and line obstacle. However, for simplicity reasons, we have left the generalization out from this article for now.
Variables
We define the relative position
Between Two Distinct Circular Agents
Between two distinct circular agents
Between Circular Agent and Line-obstacle
Between a circular agent
Now, we determine the relative position of the center of mass compared to the line segment. We have three distinct cases:
: Center of mass is on the left of the line perpendicular to intersecting . : Center of mass is on the right of the line perpendicular to intersecting .- Otherwise: The center of mass
is between the perpendicular lines.
We define the variables as follows: Skin-to-skin distance is
Normal vector is
Tangent vector
Total Force
The total force on an agent
Adjusting Force
The adjusting force accounts for the agent’s desire to reach its destination. It works by adjusting the total force to align with the desired direction
Contact Force
The contact force account for the physical contact with agents and other objects. We define the contact force as the sum of tangential friction, counter compressive, and damping forces
It is important to note that the friction model models the whole friction between two objects. Therefore, if an agent touches multiple line obstacles, we must compute the force as a weighted average of the contact forces between all of the line-obstacles. Otherwise, the model would give inconsistent results for friction between similar objects. For example, if we compute we naively computed the friction between the agent and line obstacle, and then split the line obstacle into two line-obstacles in the point of contact, the model would give twice as high friction between the split obstacle.
Fluctuation Force
Fluctuation force is a stochastic force analogous to heat in particle systems. It accounts for the lateral movement of the agents and breaks symmetries in the simulation.
We draw the magnitude of the fluctuation force from a truncated normal distribution
Social Force
The social force between agents accounts for collision avoidance. The original social force model used social force only dependent on the distance between agents. However, 7 8 introduced a more realistic, statistical mechanics-based approach for modeling this interaction. The interaction depends on the linearly estimated future positions on the agents, which takes into account the positions and velocities of the agents. The authors derived it from a pair-wise interaction potential that follows the power law, and it is backed up by experimental data. The potential is defined
For two circular agents with radius
Total Torque
The total torque on agent
Adjusting Torque
The adjusting torque accounts for the agent’s desire to rotate its body to aligh with the desired orientation
The function
We can set the desired orientation
Contact Torque
The contact torque account for the torque caused by the contact force
The cross product between two
Fluctuation Torque
The fluctuation torque accounts for the stochastic fluctuation in the agent’s orientation. We define it as
Pathfinding and Obstacle Avoidance
](/post/how-to-implement-continuous-time-multi-agent-crowd-simulation/figures/AvoidObstacle_hu8c29c7041332a87ebc553910a86f55f4_517591_adbd93aac9f13199a11ab3b5dc4db006.webp)
In this simulation, we handle the psychological avoidance of obstacles by altering the agent’s desired direction away from obstacles if they get too close. We handle the pathfinding by assuming that agents follow the shortest path to their targets, taking into account the finite size of the agents. The approach is computationally efficient since we have to compute the shortest path only once. 9 10
Continuous Shortest Path
The solution to eikonal equation gives us the minimal amount of time required to travel from
We will also define a unit-vector field, referred as a direction map, which points towards the boundary with the lowest exit-time penalty, as the normalized gradient
We can solve the Eikonal equation using existing algorithms, such as Fast Marching Method.
Desired Direction
We define a avoidance radius as
Let
We will set the avoidance radius
Exponential λ-function
For example, we can use the exponential function, defined as
gives us therefore implies- Lastly, we verify that the function is decreasing, that is,
Note that because
The resulting function
Piecewise Linear λ-function
We can also use a piecewise linear function such that
Computing Interactions
In the simulation, we have two types of interactions, agent-agent and agent-obstacle interactions. Interactions are computationally the most challenging part of the simulation, and therefore it is worth-while to optimize how we compute them.
The equations
Contact interactions only take place if the objects collide with each other.
Social force approaches zero at exponential speed as time-to-collision increases, that is, as the distance between agents increases. Therefore, we can define a cutoff distance
such that when , the social force is close enough to zero that we can approximate it as zero.
We will refer to these interactions that have a finite range as local interactions. For local interactions, computing the total interaction potential requires summing the interaction potentials with the pairs of objects within the interaction radius
Agent-Agent Interactions
To find neighboring agent pairs within distance
- Agent-agent interactions are local.
- Agents have a finite size. Therefore, only a finite amount of agents can fit inside a finite area.
In practice, Cell Lists is better once the number of agent
Agent-Obstacle Interactions
Implementing efficient collision detection between agents and obstacles is not covered in this article. We recommend looking at spatial hashing techniques or how other physics engines implement collision detection.
Updating the System
In order to update the system, we need to solve the second-order differential equations numerically. The translational motion is defined by the equation
The rotational motion is defined by the equation
Because computing the total force on the agents is computationally expensive, we will use the first-order integrator. We chose to use Verlet integration with adaptive timestep
Adaptive Timestep
Adaptive timestep is selected from interval
The adaptive timestep helps to avoid numerical errors in the simulation.
Verlet Integration
Verlet integration algorithm updates the new velocity and position at iteration
The main difference to Euler integration is that Verlet integration updates the velocity using the mean of current and previous accelerations. Verlet integration for the rotational motion work similarly.
Also, we wrap the new orientation to pi
Parameter and Attribute Values
adult | male | female | child | eldery | |
---|---|---|---|---|---|
torso ratio | 0.5882 | 0.5926 | 0.5833 | 0.5714 | 0.6 |
shoulder ratio | 0.3725 | 0.3704 | 0.375 | 0.3333 | 0.36 |
torso-to-shoulder ratio | 0.6275 | 0.6296 | 0.625 | 0.6667 | 0.64 |
average radius | 0.255±0.035 | 0.27±0.02 | 0.24±0.02 | 0.21±0.015 | 0.25±0.02 |
average velocity | 1.25±0.3 | 1.35±0.2 | 1.15±0.2 | 0.9±0.3 | 0.8±0.3 |
average mass | 73.5 | 80 | 67 | 57 | 70 |
Table: Anthropological data of human dimensions. 11
Agent
: The center of mass is drawn from random uniform distribution inside a designated source without overlapping with other agents or obstacles. : The velocity is set zero vector or to an instance dependent value. : The force is set to zero vector. : The desired direction is set to zero vector or to an instance dependent value. : The desired velocity is set according to the anthropological data, for example, as the average walking speed of a human. : The radius is set according to the anthropological data. : The mass is set according to the anthropological data.
Orientable agent model initial values and attributes:
: The orientation is set to same angle an initial direction. : The angular velocity is set to zero. : The torque is set to zero. : The desired orientation set to same angle an initial direction. : The desired angular velocity is set to : The rotational moment is set to value where and , which means that agent with mass and radius has moment and other values are scaled according to the equation for moment of inertia.
Three-circle agent model attributes:
: Torso radius is set according to the anthropological data. : Shoulder radius is set according to the anthropological data. : Torso-to-shoulder radius is set according to the anthropological data.
Force parameters:
Torque parameters:
Integrator parameters:
Implementation
We have experimented with the algorithms in the crowddynamics library and verified that they operate correctly. However, based on the experiences of implementing the library, we learned that creating large, complex simulations is implausible without a graphical user interface (GUI) and extendable program architecture. For example, if we wish to create and edit the field, logic, and set initial parameter and attribute values graphically, we require these features. For this reason, we advise building the simulation on top of existing software, such as a physics engine or game engine. We have the following suggestion for the software features required of this software.
Optional GUI. Ability to simulate with or without the graphical user interface. For example, we may wish to develop the simulation on our local machine with graphics and then run multiple scenarios in parallel in the cloud without the graphics only to obtain numerical results.
IO. Support for saving and loading simulation data and configurations from and to file. Essential for sharing and reproducing simulations.
Interactive graphics. Display the simulation progress in real-time. Buttons for starting, stopping, and resuming the simulation. Menu options for choosing the simulation data and configurations files.
Field, parameter, and attribute editor. Ability to manipulate simulation fields graphically. Create, edit, and remove obstacles, agents, sources, and targets. Set attribute values for agents and select their source positions and targets.
Logic editor. Ability to create individual simulation logic blocks (e.g., forces, torque, integrator) and then connect them into the full simulation logic graphically. Allows changing individual parts of simulation logic for developing and testing new algorithms without having to break simulations that depend on different logic configurations.
The programming language choice depends on the chosen engine. Engines typically use C++. Also, installation and getting started should be made as easy as possible to attract users outside the research community.
Conclusion
In reality, human movement can be very complicated. For example, the motivation to move to a specific direction can be affected by many factors, such as sensory inputs and internal motivations. It would be difficult and even undesirable to attempt to model all the aspects of human movement. Therefore, the models we have presented in this article focus on capturing only the essential aspects of the human movement. It requires making simplifying assumptions in the models.
Newtonian model. Numerical simulation of Newtonian mechanics can create artifacts and non-realistic behavior, which we should account for in the implementation. For these reasons, we use adaptive timestep and Verlet integration.
Planar motion. In reality, human movement is 3-dimensional, but creating 3-dimensional simulation is significantly more challenging.
Agent models. The circular agent model is sufficient for modeling low-density crowds, but in high-density crowds, it is unrealistic. The orientable, three-circle model improves this shortcoming by more accurately representing the human torso and allowing body rotation. However, it requires algorithms for rotational motion and improved collision detection. Also, desired body rotation is not a completely solved problem.
The results from simulations are stochastic. Therefore, we need to run multiple simulations with different initial configurations and analyze the resulting data using statistical methods to obtain meaningful information.
Contribute
If you enjoyed or found benefit from this article, it would help me share it with other people who might be interested. If you have feedback, questions, or ideas related to the article, you can contact me via email. For more content, you can follow me on YouTube or join my newsletter. Creating content takes time and effort, so consider supporting me with a one-time donation.
References
Helbing, D., Farkas, I., & Vicsek, T. (2000). Simulating dynamical features of escape panic. Nature, 407(6803), 487–490. https://doi.org/10.1038/35035023 ↩︎ ↩︎
Gibelli, L., & Bellomo, N. (2019). Crowd Dynamics, Volume 1: Theory, Models, and Safety Problems. Springer International Publishing. Retrieved from https://books.google.com.br/books?id=Pd2EDwAAQBAJ ↩︎
Langston, P. A., Masling, R., & Asmar, B. N. (2006). Crowd dynamics discrete element multi-circle model. Safety Science. https://doi.org/10.1016/j.ssci.2005.11.007 ↩︎ ↩︎
Korhonen, T., Hostikka, S., Heliövaara, S., & Ehtamo, H. (2008). FDS+ Evac: An Agent Based Fire Evacuation Model. Pedestrian and Evacuation Dynamics 2008, 109–120. https://doi.org/10.1007/978-3-642-04504-2 ↩︎
Stüvel, S. A. (2016). Dense Crowds of Virtual Humans. ↩︎
Hidalgo, R. C., Parisi, D. R., & Zuriguel, I. (2017). Simulating competitive egress of noncircular pedestrians. PHYSICAL REVIEW E, 95. https://doi.org/10.1103/PhysRevE.95.042319 ↩︎
Karamouzas, I., Skinner, B., & Guy, S. J. (2014). Universal power law governing pedestrian interactions. Physical Review Letters. https://doi.org/10.1103/PhysRevLett.113.238701 ↩︎
Karamouzas, I., Skinner, B., & Guy, S. J. (2014). Supplemental material for: Universal Power Law Governing Pedestrian Interactions Ioannis. Physical Review Letters. https://doi.org/10.1103/PhysRevLett.113.238701 ↩︎
Kretz, T., Große, A., Hengst, S., Kautzsch, L., Pohlmann, A., & Vortisch, P. (2011). Quickest Paths in Simulations of Pedestrians. Advances in Complex Systems, 14(5), 733–759. https://doi.org/10.1142/S0219525911003281 ↩︎
Cristiani, E., & Peri, D. (2015). Handling obstacles in pedestrian simulations: Models and optimization. Retrieved from http://arxiv.org/abs/1512.08528 ↩︎
Korhonen, T., Hostikka, S., Technical, V. T. T., Ehtamo, H., Analysis, S., Box, T. P. O., … The, I. (2008). FDS+Evac: Modelling Social Interactions in Fire Evacuation. https://doi.org/10.1007/978-3-642-04504-2_8 ↩︎