Skip to content

Commit

Permalink
Allow to select path generation mode in load and random
Browse files Browse the repository at this point in the history
  • Loading branch information
albireox committed Oct 19, 2022
1 parent f285654 commit 8e48b6b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
25 changes: 23 additions & 2 deletions python/jaeger/actor/commands/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ async def _load_design(
epoch: float | None = None,
epoch_delay: float = 0.0,
get_paths: bool = True,
path_generation_mode: str | None = None,
):
"""Helper to load or preload a design."""

Expand Down Expand Up @@ -205,7 +206,10 @@ async def _load_design(
if get_paths and configuration.is_cloned is False:
command.info("Calculating trajectories.")
configuration.set_command(command)
await configuration.get_paths(decollide=True)
await configuration.get_paths(
decollide=True,
path_generation_mode=path_generation_mode,
)

return configuration

Expand Down Expand Up @@ -291,6 +295,11 @@ def configuration():
help="If the new design has the same target set as the currently loaded one, "
"does not clone the configuration and instead loads the new design.",
)
@click.option(
"--path-generation-mode",
type=click.Choice(["greedy", "mdp"], case_sensitive=False),
help="The path generation algorithm to use.",
)
@click.argument("DESIGNID", type=int, required=False)
async def load(
command: Command[JaegerActor],
Expand All @@ -310,6 +319,7 @@ async def load(
scale: float | None = None,
fudge_factor: float | None = None,
no_clone: bool = False,
path_generation_mode: str | None = None,
):
"""Creates and ingests a configuration from a design in the database."""

Expand Down Expand Up @@ -352,6 +362,7 @@ async def load(
epoch=epoch,
epoch_delay=epoch_delay,
get_paths=False,
path_generation_mode=path_generation_mode,
)

if configuration is False:
Expand All @@ -378,7 +389,10 @@ async def load(
):
try:
command.info("Calculating trajectories.")
await fps.configuration.get_paths(decollide=not from_positions)
await fps.configuration.get_paths(
decollide=not from_positions,
path_generation_mode=path_generation_mode,
)
except Exception as err:
return command.fail(error=f"Failed generating paths: {err}")

Expand Down Expand Up @@ -862,6 +876,11 @@ async def slew(
default=10,
help="Home many retries to allow due to deadlocks.",
)
@click.option(
"--path-generation-mode",
type=click.Choice(["greedy", "mdp"], case_sensitive=False),
help="The path generation algorithm to use.",
)
async def random(
command: Command[JaegerActor],
fps: FPS,
Expand All @@ -871,6 +890,7 @@ async def random(
collision_buffer: float | None = None,
send_trajectory: bool = True,
max_retries: int = 10,
path_generation_mode: str | None = None,
):
"""Executes a random, valid configuration."""

Expand Down Expand Up @@ -918,6 +938,7 @@ async def random(
trajectory = await configuration.get_paths(
decollide=False,
collision_buffer=collision_buffer,
path_generation_mode=path_generation_mode,
)
except JaegerError as err:
return command.fail(error=f"jaeger random failed: {err}")
Expand Down
18 changes: 16 additions & 2 deletions python/jaeger/target/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ async def get_paths(
simple_decollision: bool = False,
resolve_deadlocks: bool = True,
n_deadlock_retries: int = 5,
path_generation_mode: str | None = None,
force: bool = False,
) -> dict:
"""Returns a trajectory dictionary from the folded position.
Expand All @@ -338,6 +339,11 @@ async def get_paths(
minimise what robots move.
resolve_deadlocks
Whether to solve for deadlocks after decollision.
n_deadlock_retries
How many times to try solving deadlocks.
path_generation_mode
The path generation mode, either ``'greedy'`` or ``'mdp'``. If not
defined, uses the default path generation mode.
force
If `False`, fails if the robot grid is deadlocked. Always fails if there
are collisions.
Expand Down Expand Up @@ -413,7 +419,11 @@ async def get_paths(
# Fix deadlocks (this sets the trajectories in the instance).
self.log("Generating path pair.")
n_retries = n_deadlock_retries if resolve_deadlocks else -1
unlocked = await self._resolve_deadlocks(n_retries=n_retries, force=force)
unlocked = await self._resolve_deadlocks(
n_retries=n_retries,
path_generation_mode=path_generation_mode,
force=force,
)

self._update_coordinates(unlocked)

Expand All @@ -428,6 +438,7 @@ async def get_paths(
async def _resolve_deadlocks(
self,
n_retries: int = 5,
path_generation_mode: str | None = None,
force: bool = False,
) -> list[int]:
"""Iteratively fix deadlocks."""
Expand All @@ -439,7 +450,10 @@ async def _resolve_deadlocks(
decollided: list[int] = []

while True:
result = await get_path_pair_in_executor(self.robot_grid)
result = await get_path_pair_in_executor(
self.robot_grid,
path_generation_mode=path_generation_mode,
)
self.to_destination, self.from_destination, did_fail, deadlocks = result

n_deadlocks = len(deadlocks)
Expand Down
13 changes: 11 additions & 2 deletions python/jaeger/target/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ async def create_random_configuration(
deadlock_retries: int = 5,
n_failed: int = 0,
max_retries: int = 5,
path_generation_mode: str | None = None,
**kwargs,
):
"""Creates a random configuration using Kaiju."""
Expand Down Expand Up @@ -229,7 +230,10 @@ async def create_random_configuration(
except JaegerError:
raise JaegerError("Decollision failed. Cannot create random configuration.")

_, _, did_fail, deadlocks = await get_path_pair_in_executor(robot_grid)
_, _, did_fail, deadlocks = await get_path_pair_in_executor(
robot_grid,
path_generation_mode=path_generation_mode,
)

# If too many deadlocks, just try a new seed.
n_deadlock = len(deadlocks)
Expand All @@ -245,6 +249,7 @@ async def create_random_configuration(
collision_buffer=collision_buffer,
deadlock_retries=deadlock_retries,
n_failed=n_failed + 1,
path_generation_mode=path_generation_mode,
)

if did_fail and n_deadlock > 0:
Expand Down Expand Up @@ -283,7 +288,10 @@ async def create_random_configuration(
"Failed creating random configuration: cannot remove deadlocks."
)

_, _, did_fail, deadlocks = await get_path_pair_in_executor(robot_grid)
_, _, did_fail, deadlocks = await get_path_pair_in_executor(
robot_grid,
path_generation_mode=path_generation_mode,
)
if did_fail is False:
log.info("Random configuration has been unlocked.")
break
Expand All @@ -299,6 +307,7 @@ async def create_random_configuration(
uniform=uniform,
collision_buffer=collision_buffer,
deadlock_retries=deadlock_retries,
path_generation_mode=path_generation_mode,
)

pT = calibration.positionerTable.copy().reset_index()
Expand Down

0 comments on commit 8e48b6b

Please sign in to comment.