-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Collaborative astar #1247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SchwartzCode
wants to merge
22
commits into
AtsushiSakai:master
Choose a base branch
from
SchwartzCode:jbs/collaborative_astar
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+566
−288
Open
Collaborative astar #1247
Changes from 12 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
9fb1b7b
consolidate Node definition
SchwartzCode 1c5be77
add base class for single agent planner
SchwartzCode a481f61
add base class for single agent planner
SchwartzCode 20ccb3e
its working
SchwartzCode e93116c
use single agent plotting util
SchwartzCode 7f4cf58
cleanup, bug fix, add some results to docs
SchwartzCode 43bc927
remove seeding from sta* - it happens in Node
SchwartzCode 777b5ce
remove stale todo
SchwartzCode 7495fe6
rename CA* and speed up plotting
SchwartzCode 63aef91
paper
SchwartzCode d722adc
proper paper (ofc its csail)
SchwartzCode 224dd3b
some cleanup
SchwartzCode ede80e4
update docs
SchwartzCode c89f47f
add unit test
SchwartzCode 6391677
add logic for saving animation as gif
SchwartzCode e49b6cf
address github bot
SchwartzCode 8f63c88
Revert "add logic for saving animation as gif"
SchwartzCode 92689ed
fix tests
SchwartzCode d150de5
docs lint
SchwartzCode 46646f4
add gifs
SchwartzCode 5bd8d42
copilot review
SchwartzCode e97236a
appease mypy
SchwartzCode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from abc import ABC, abstractmethod | ||
from dataclasses import dataclass | ||
import math | ||
from PathPlanning.TimeBasedPathPlanning.GridWithDynamicObstacles import ( | ||
Grid, | ||
Position, | ||
) | ||
from PathPlanning.TimeBasedPathPlanning.Node import NodePath | ||
import random | ||
import numpy.random as numpy_random | ||
|
||
# Seed randomness for reproducibility | ||
RANDOM_SEED = 50 | ||
random.seed(RANDOM_SEED) | ||
numpy_random.seed(RANDOM_SEED) | ||
|
||
class SingleAgentPlanner(ABC): | ||
""" | ||
Base class for single agent planners | ||
""" | ||
|
||
@staticmethod | ||
@abstractmethod | ||
def plan(grid: Grid, start: Position, goal: Position, verbose: bool = False) -> NodePath: | ||
pass | ||
|
||
@dataclass | ||
class StartAndGoal: | ||
# Index of this agent | ||
index: int | ||
# Start position of the robot | ||
start: Position | ||
# Goal position of the robot | ||
goal: Position | ||
|
||
def distance_start_to_goal(self) -> float: | ||
return pow(self.goal.x - self.start.x, 2) + pow(self.goal.y - self.start.y, 2) | ||
|
||
class MultiAgentPlanner(ABC): | ||
""" | ||
Base class for multi-agent planners | ||
""" | ||
|
||
@staticmethod | ||
@abstractmethod | ||
def plan(grid: Grid, start_and_goal_positions: list[StartAndGoal], verbose: bool = False) -> list[NodePath]: | ||
""" | ||
Plan for all agents. Returned paths are in the order of the `StartAndGoal` list this object was instantiated with | ||
""" | ||
pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
from dataclasses import dataclass | ||
from functools import total_ordering | ||
import numpy as np | ||
|
||
@dataclass(order=True) | ||
class Position: | ||
x: int | ||
y: int | ||
|
||
def as_ndarray(self) -> np.ndarray: | ||
return np.array([self.x, self.y]) | ||
|
||
def __add__(self, other): | ||
if isinstance(other, Position): | ||
return Position(self.x + other.x, self.y + other.y) | ||
raise NotImplementedError( | ||
f"Addition not supported for Position and {type(other)}" | ||
) | ||
|
||
def __sub__(self, other): | ||
if isinstance(other, Position): | ||
return Position(self.x - other.x, self.y - other.y) | ||
raise NotImplementedError( | ||
f"Subtraction not supported for Position and {type(other)}" | ||
) | ||
|
||
def __hash__(self): | ||
return hash((self.x, self.y)) | ||
|
||
@dataclass() | ||
# Note: Total_ordering is used instead of adding `order=True` to the @dataclass decorator because | ||
# this class needs to override the __lt__ and __eq__ methods to ignore parent_index. Parent | ||
# index is just used to track the path found by the algorithm, and has no effect on the quality | ||
# of a node. | ||
@total_ordering | ||
class Node: | ||
position: Position | ||
time: int | ||
heuristic: int | ||
parent_index: int | ||
|
||
""" | ||
This is what is used to drive node expansion. The node with the lowest value is expanded next. | ||
This comparison prioritizes the node with the lowest cost-to-come (self.time) + cost-to-go (self.heuristic) | ||
""" | ||
def __lt__(self, other: object): | ||
if not isinstance(other, Node): | ||
return NotImplementedError(f"Cannot compare Node with object of type: {type(other)}") | ||
return (self.time + self.heuristic) < (other.time + other.heuristic) | ||
|
||
""" | ||
Note: cost and heuristic are not included in eq or hash, since they will always be the same | ||
for a given (position, time) pair. Including either cost or heuristic would be redundant. | ||
""" | ||
def __eq__(self, other: object): | ||
if not isinstance(other, Node): | ||
return NotImplementedError(f"Cannot compare Node with object of type: {type(other)}") | ||
return self.position == other.position and self.time == other.time | ||
|
||
def __hash__(self): | ||
return hash((self.position, self.time)) | ||
|
||
class NodePath: | ||
path: list[Node] | ||
positions_at_time: dict[int, Position] | ||
|
||
def __init__(self, path: list[Node]): | ||
self.path = path | ||
self.positions_at_time = {} | ||
for i, node in enumerate(path): | ||
reservation_finish_time = node.time + 1 | ||
if i < len(path) - 1: | ||
reservation_finish_time = path[i + 1].time | ||
|
||
for t in range(node.time, reservation_finish_time): | ||
self.positions_at_time[t] = node.position | ||
|
||
""" | ||
Get the position of the path at a given time | ||
""" | ||
def get_position(self, time: int) -> Position | None: | ||
return self.positions_at_time.get(time) | ||
|
||
""" | ||
Time stamp of the last node in the path | ||
""" | ||
def goal_reached_time(self) -> int: | ||
return self.path[-1].time | ||
|
||
def __repr__(self): | ||
repr_string = "" | ||
for i, node in enumerate(self.path): | ||
repr_string += f"{i}: {node}\n" | ||
return repr_string |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.