Skip to content

Commit

Permalink
Enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
chraibi committed Apr 30, 2024
1 parent 61a7902 commit ab1023a
Showing 1 changed file with 54 additions and 28 deletions.
82 changes: 54 additions & 28 deletions notebooks/direct_steering.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"\n",
"This notebook can be directly downloaded {download}`here <./direct_steering.ipynb>` to run it locally.\n",
"\n",
"It demonstrates the use of [direct steering](https://www.jupedsim.org/stable/concepts/routing.html#direct-steering). of agents.\n",
"It demonstrates the use of [direct steering](https://www.jupedsim.org/stable/concepts/routing.html#direct-steering) of agents.\n",
"\n",
"Am agent (leader) embarks on a journey defined by specific waypoints and a final destination. Meanwhile, the remaining agents trail behind, constantly adjusting their course to align with the leader's current position."
"An agent (leader) embarks on a journey defined by specific waypoints and a final destination. Meanwhile, the remaining agents trail behind, constantly adjusting their course to align with the leader's current position."
]
},
{
Expand Down Expand Up @@ -249,10 +249,20 @@
"direct_steering_journey_id = simulation.add_journey(direct_steering_journey)"
]
},
{
"cell_type": "markdown",
"id": "0d643d8f",
"metadata": {},
"source": [
"## Add agents\n",
"\n",
"First, add leader, then its followers."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "335348ab",
"id": "86acd974",
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -287,37 +297,53 @@
" )\n",
" for pos in pos_in_spawning_area[1:]\n",
" ]\n",
")\n",
")"
]
},
{
"cell_type": "markdown",
"id": "18018147",
"metadata": {},
"source": [
"## Simulation loop"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "335348ab",
"metadata": {},
"outputs": [],
"source": [
"while simulation.agent_count() > 0:\n",
" # Find leader's position\n",
" for agent in simulation.agents():\n",
" if agent.id == leader_id:\n",
" position_leader = agent.position\n",
" if leader_id in simulation.removed_agents():\n",
" leader_id = None\n",
" if leader_id:\n",
" position_leader = simulation.agent(leader_id).position\n",
"\n",
" # Move followers towards leader\n",
" for follower_id in followers_ids:\n",
" for agent in simulation.agents():\n",
" if agent.id == follower_id:\n",
" # Define a target position near the leader with some randomness\n",
" near_leader = (\n",
" position_leader[0] + random.normalvariate(1, 0.1),\n",
" position_leader[1] + random.normalvariate(1, 0.1),\n",
" )\n",
" near_leader_point = Point(near_leader[0], near_leader[1])\n",
" for agent in simulation.agents():\n",
" if agent.id == leader_id:\n",
" continue\n",
" # Define a target position near the leader with some randomness\n",
" near_leader = (\n",
" position_leader[0] + random.normalvariate(1, 0.1),\n",
" position_leader[1] + random.normalvariate(1, 0.1),\n",
" )\n",
" near_leader_point = Point(near_leader[0], near_leader[1])\n",
"\n",
" # If the target position is inside the walkable area, set it as the agent's target\n",
" target = (\n",
" near_leader\n",
" if any(\n",
" geom.contains(near_leader_point) for geom in area.geoms\n",
" )\n",
" else position_leader\n",
" )\n",
" agent.target = target\n",
" # If the target position is inside the walkable area, set it as the agent's target\n",
" target = (\n",
" near_leader\n",
" if any(geom.contains(near_leader_point) for geom in area.geoms)\n",
" else position_leader\n",
" )\n",
" agent.target = target\n",
"\n",
" # Check if the agent reached the exit and mark it for removal if so\n",
" if Point(agent.position).distance(exit_area.centroid) < 1:\n",
" simulation.mark_agent_for_removal(follower_id)\n",
" # Check if the agent reached the exit and mark it for removal if so\n",
" if Point(agent.position).distance(exit_area.centroid) < 1:\n",
" simulation.mark_agent_for_removal(agent.id)\n",
"\n",
" simulation.iterate()"
]
Expand Down

0 comments on commit ab1023a

Please sign in to comment.