Skip to content

Commit

Permalink
Stages as network (#1189)
Browse files Browse the repository at this point in the history
Introduces stages as network. For each stage one can specify how it is connected to the next stage(s), either by a fixed or round robin transition.

Co-authored-by: Ozaq <[email protected]>
  • Loading branch information
schroedtert and Ozaq authored Sep 14, 2023
1 parent 3be130b commit 98e1759
Show file tree
Hide file tree
Showing 28 changed files with 767 additions and 219 deletions.
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ list(APPEND COMMON_COMPILE_OPTIONS
################################################################################
set(FMT_INSTALL OFF)
add_subdirectory(third-party/fmt-10.0.0)
set_property(TARGET fmt PROPERTY POSITION_INDEPENDENT_CODE ON)
set_target_properties(fmt PROPERTIES
POSITION_INDEPENDENT_CODE ON
SYSTEM ON
)
find_package(Boost 1.82
REQUIRED
CONFIG
Expand Down
12 changes: 6 additions & 6 deletions examples/example1.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,16 @@ def main():
exit_id = simulation.add_exit_stage([(18, 4), (20, 4), (20, 6), (18, 6)])

journey = jps.JourneyDescription()
journey.append(stage_id)
journey.append(exit_id)

journey.add(stage_id)
journey.add(exit_id)
journey.set_transition_for_stage(
stage_id, jps.Transition.create_fixed_transition(exit_id)
)
journey_id = simulation.add_journey(journey)

agent_parameters = jps.VelocityModelAgentParameters()
agent_parameters.journey_id = journey_id
agent_parameters.stage_id = stage_id
agent_parameters.orientation = (1.0, 0.0)
agent_parameters.position = (0.0, 0.0)
agent_parameters.profile_id = profile_id
Expand All @@ -84,9 +87,6 @@ def main():
simulation.iterate()
if simulation.iteration_count() % 4 == 0:
writer.write_iteration_state(simulation)
for a in simulation.agents():
print(f"{a.model.e0}")
break
if simulation.iteration_count() == 1300:
if waiting_stage.state == jps.WaitingSetState.ACTIVE:
waiting_stage.state = jps.WaitingSetState.INACTIVE
Expand Down
75 changes: 49 additions & 26 deletions examples/example2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
import logging
import pathlib
import sys

from shapely import GeometryCollection, Polygon, to_wkt

Expand Down Expand Up @@ -71,17 +72,34 @@ def main():
simulation.add_waypoint_stage((60, 60), 1),
]

short_journey = jps.JourneyDescription([waypoints[0], stage_id, exits[0]])
long_journey = jps.JourneyDescription([*waypoints[1:], exits[1]])

short_journey.set_transition_for_stage(
waypoints[0], jps.Transition.create_fixed_transition(stage_id)
)
short_journey.set_transition_for_stage(
stage_id, jps.Transition.create_fixed_transition(exits[0])
)

for waypoint_start, waypoint_end in zip(waypoints[1:-1], waypoints[2:]):
long_journey.set_transition_for_stage(
waypoint_start,
jps.Transition.create_fixed_transition(waypoint_end),
)
long_journey.set_transition_for_stage(
waypoints[-1],
jps.Transition.create_fixed_transition(exits[1]),
)

journeys = [
simulation.add_journey(
jps.JourneyDescription([waypoints[0], stage_id, exits[0]])
),
simulation.add_journey(
jps.JourneyDescription([*waypoints[1:], exits[1]])
),
simulation.add_journey(short_journey),
simulation.add_journey(long_journey),
]

agent_parameters = jps.VelocityModelAgentParameters()
agent_parameters.journey_id = journeys[0]
agent_parameters.stage_id = waypoints[0]
agent_parameters.orientation = (1.0, 0.0)
agent_parameters.position = (0.0, 0.0)
agent_parameters.profile_id = profile_id
Expand All @@ -103,27 +121,32 @@ def main():
redirect_once = True
signal_once = True
while simulation.agent_count() > 0:
agents_at_head_of_waiting = list(
simulation.agents_in_range((60, 50), 1)
)
if redirect_once and len(agents_at_head_of_waiting) == 1:
simulation.switch_agent_journey(
agent_id=agents_at_head_of_waiting[0],
journey_id=journeys[1],
stage_index=0,
try:
agents_at_head_of_waiting = list(
simulation.agents_in_range((60, 50), 1)
)
redirect_once = False
print(
f"Switched journey for agent {agents_at_head_of_waiting[0]} @{simulation.iteration_count()}"
)

if signal_once and any(simulation.agents_in_range((60, 60), 1)):
stage.state = jps.WaitingSetState.INACTIVE
print(f"Stop Waiting @{simulation.iteration_count()}")
signal_once = False
if simulation.iteration_count() % 4 == 0:
writer.write_iteration_state(simulation)
simulation.iterate()
if redirect_once and len(agents_at_head_of_waiting) == 1:
simulation.switch_agent_journey(
agent_id=agents_at_head_of_waiting[0],
journey_id=journeys[1],
stage_id=waypoints[1],
)
redirect_once = False
print(
f"Switched journey for agent {agents_at_head_of_waiting[0]} @{simulation.iteration_count()}"
)

if signal_once and any(simulation.agents_in_range((60, 60), 1)):
stage.state = jps.WaitingSetState.INACTIVE
print(f"Stop Waiting @{simulation.iteration_count()}")
signal_once = False
if simulation.iteration_count() % 4 == 0:
writer.write_iteration_state(simulation)
simulation.iterate()
except KeyboardInterrupt:
writer.end_writing()
print("CTRL-C Recieved! Shuting down")
sys.exit(1)

writer.end_writing()

Expand Down
32 changes: 22 additions & 10 deletions examples/example3.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
import logging
import pathlib
import sys

from shapely import GeometryCollection, Polygon, to_wkt

Expand Down Expand Up @@ -68,10 +69,16 @@ def main():
)

journey = jps.JourneyDescription([stage_id, exit])
journey.set_transition_for_stage(
stage_id, jps.Transition.create_fixed_transition(exit)
)

journey_id = simulation.add_journey(journey)

agent_parameters = jps.VelocityModelAgentParameters()
agent_parameters.journey_id = journey_id
agent_parameters.stage_id = stage_id

agent_parameters.orientation = (1.0, 0.0)
agent_parameters.position = (0.0, 0.0)
agent_parameters.profile_id = profile_id
Expand All @@ -85,16 +92,21 @@ def main():
while (
simulation.agent_count() > 0 and simulation.iteration_count() < 20_000
):
if (
simulation.iteration_count() > 100 * 52
and simulation.iteration_count() % 400 == 0
):
queue.pop(1)
print("Next!")

if simulation.iteration_count() % 4 == 0:
writer.write_iteration_state(simulation)
simulation.iterate()
try:
if (
simulation.iteration_count() > 100 * 52
and simulation.iteration_count() % 400 == 0
):
queue.pop(1)
print("Next!")

if simulation.iteration_count() % 4 == 0:
writer.write_iteration_state(simulation)
simulation.iterate()
except KeyboardInterrupt:
writer.end_writing()
print("CTRL-C Recieved! Shuting down")
sys.exit(1)

writer.end_writing()

Expand Down
75 changes: 60 additions & 15 deletions examples/example4.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import logging
import pathlib
import sys
import time

from shapely import GeometryCollection, Polygon, to_wkt

Expand Down Expand Up @@ -38,7 +37,22 @@ def main():
jps.set_error_callback(log_error)

area = GeometryCollection(
Polygon(shell=[(0, 0), (1000, 0), (1000, 5000), (0, 5000), (0, 0)])
Polygon(
shell=[
(-8, -8),
(-24, -8),
(-24, 8),
(-8, 8),
(-8, 24),
(8, 24),
(8, 8),
(24, 8),
(24, -8),
(8, -8),
(8, -24),
(-8, -24),
]
)
)
geometry = jps.geometry_from_shapely(area)

Expand All @@ -53,37 +67,68 @@ def main():
model = model_builder.build()

simulation = jps.Simulation(model=model, geometry=geometry, dt=0.01)
exit = simulation.add_exit_stage(
[(999, 2000), (999, 3000), (1000, 3000), (1000, 2000)]
exit_left = simulation.add_exit_stage(
[(-24, -8), (-24, 8), (-23, 8), (-23, -8)]
)
exit_top = simulation.add_exit_stage(
[(-8, 24), (8, 24), (8, 23), (-8, 23)]
)
exit_right = simulation.add_exit_stage(
[(24, -8), (24, 8), (23, 8), (23, -8)]
)

queue_id = simulation.add_queue_stage(
[
(0, -4),
(0, -8),
]
)
queue = simulation.get_stage_proxy(queue_id)

waypoint_middle = simulation.add_waypoint_stage((0, 0), 1)

journey = jps.JourneyDescription([exit])
journey = jps.JourneyDescription(
[queue_id, waypoint_middle, exit_left, exit_top, exit_right]
)

journey.set_transition_for_stage(
queue_id,
jps.Transition.create_fixed_transition(waypoint_middle),
)
journey.set_transition_for_stage(
waypoint_middle,
jps.Transition.create_round_robin_transition(
[(exit_left, 5), (exit_top, 1), (exit_right, 3)]
),
)
journey_id = simulation.add_journey(journey)

agent_parameters = jps.VelocityModelAgentParameters()
agent_parameters.journey_id = journey_id
agent_parameters.stage_id = queue_id
agent_parameters.orientation = (1.0, 0.0)
agent_parameters.position = (0.0, 0.0)
agent_parameters.profile_id = profile_id

for y in range(0, 5000):
for x in range(0, 12):
agent_parameters.position = (0.5 + x, y + 0.5)
for y in range(-23, -12, 2):
for x in range(-7, 8, 2):
agent_parameters.position = (x, y)
simulation.add_agent(agent_parameters)

writer = jps.SqliteTrajectoryWriter(pathlib.Path("example4_out.sqlite"))
writer.begin_writing(5, to_wkt(area, rounding_precision=-1))
while simulation.agent_count() > 0:
try:
if simulation.iteration_count() % 20 == 0:
if (
simulation.iteration_count() > 100 * 5
and simulation.iteration_count() % 100 == 0
):
queue.pop(1)
print("Next!")

if simulation.iteration_count() % 4 == 0:
writer.write_iteration_state(simulation)
before = time.perf_counter_ns()
simulation.iterate()
duration = time.perf_counter_ns() - before
print(
f"Iteration: {simulation.iteration_count():3.0f} / Time taken: {duration / 1000000}ms",
end="\r",
)
except KeyboardInterrupt:
writer.end_writing()
print("CTRL-C Recieved! Shuting down")
Expand Down
Loading

0 comments on commit 98e1759

Please sign in to comment.