I am trying to use python to simulate the trajectories of pedestrians walking in an area.
But the searched articles all use the random walk method, which is not quite like the actual pedestrian trajectory. Is there a better way to simulate pedestrian trajectories?
>Solution :
There are several model such as
Social Force Model, Cellular Automata, Agent-Based Model and Optimal Steps Models
Here’s an example of how to simulate pedestrian trajectories using the Social Force Model
import numpy as np
# Define simulation parameters
num_pedestrians = 50
simulation_time = 100 # in seconds
time_step = 0.1 # in seconds
# Define physical constants
kappa = 1.2 * 10**5
tau = 0.5
mass = 80 # in kg
v_0 = 1.3 # in m/s
sigma = 0.3 # in m
# Define simulation area
length = 20 # in m
width = 20 # in m
obstacle_position = np.array([[7, 7], [8, 7], [9, 7], [10, 7], [11, 7], [12, 7], [13, 7], [14, 7]])
# Define initial positions and velocities of pedestrians
pedestrian_positions = np.random.rand(num_pedestrians, 2) * np.array([length, width])
pedestrian_velocities = np.zeros((num_pedestrians, 2))
# Define function for computing social forces
def compute_social_forces(position, velocity):
# Compute attractive force towards destination
destination = np.array([length/2, width/2]) # middle of simulation area
attractive_force = (destination - position) / np.linalg.norm(destination - position)
# Compute repulsive forces from other pedestrians
repulsive_forces = np.zeros_like(velocity)
for i in range(num_pedestrians):
if i == j:
continue
r_ij = position[i] - position[j]
d_ij = np.linalg.norm(r_ij)
if d_ij < 2 * sigma:
repulsive_force = kappa * (2 * sigma - d_ij) * (r_ij / d_ij)
repulsive_forces += repulsive_force
# Compute repulsive forces from obstacles
for obstacle_pos in obstacle_position:
r_ij = position - obstacle_pos
d_ij = np.linalg.norm(r_ij)
if d_ij < 2 * sigma:
repulsive_force = kappa * (2 * sigma - d_ij) * (r_ij / d_ij)
repulsive_forces += repulsive_force
# Compute friction force
friction_force = -mass * tau * velocity
# Compute total force
total_force = attractive_force + repulsive_forces + friction_force
return total_force
# Simulate pedestrian trajectories
for t in np.arange(0, simulation_time, time_step):
for i in range(num_pedestrians):
position = pedestrian_positions[i]
velocity = pedestrian_velocities[i]
# Compute social forces
social_forces = compute_social_forces(position, velocity)
# Compute acceleration
acceleration = social_forces / mass
# Update velocity and position
pedestrian_velocities[i] += acceleration * time_step
pedestrian_positions[i] += pedestrian_velocities[i] * time_step