55 Reference: https://www.davidsilver.uk/wp-content/uploads/2020/03/coop-path-AIWisdom.pdf
66"""
77
8- from __future__ import annotations # For typehints of a class within itself
98import numpy as np
109import matplotlib .pyplot as plt
1110from PathPlanning .TimeBasedPathPlanning .GridWithDynamicObstacles import (
1716from collections .abc import Generator
1817import random
1918from functools import total_ordering
19+ from typing import Optional
2020
2121# Seed randomness for reproducibility
2222RANDOM_SEED = 50
@@ -42,10 +42,14 @@ def __init__(
4242 This is what is used to drive node expansion. The node with the lowest value is expanded next.
4343 This comparison prioritizes the node with the lowest cost-to-come (self.time) + cost-to-go (self.heuristic)
4444 """
45- def __lt__ (self , other : Node ):
45+ def __lt__ (self , other : object ):
46+ if not isinstance (other , Node ):
47+ return NotImplementedError (f"Cannot compare Node with object of type: { type (other )} " )
4648 return (self .time + self .heuristic ) < (other .time + other .heuristic )
4749
48- def __eq__ (self , other : Node ):
50+ def __eq__ (self , other : object ):
51+ if not isinstance (other , Node ):
52+ return NotImplementedError (f"Cannot compare Node with object of type: { type (other )} " )
4953 return self .position == other .position and self .time == other .time
5054
5155 def __repr__ (self ):
@@ -64,7 +68,7 @@ def __init__(self, path: list[Node]):
6468 """
6569 Get the position of the path at a given time
6670 """
67- def get_position (self , time : int ) -> Position :
71+ def get_position (self , time : int ) -> Optional [ Position ] :
6872 return self .positions_at_time .get (time )
6973
7074 """
@@ -91,12 +95,12 @@ def __init__(self, grid: Grid, start: Position, goal: Position):
9195 self .goal = goal
9296
9397 def plan (self , verbose : bool = False ) -> NodePath :
94- open_set = []
98+ open_set : list [ Node ] = []
9599 heapq .heappush (
96100 open_set , Node (self .start , 0 , self .calculate_heuristic (self .start ), - 1 )
97101 )
98102
99- expanded_set = []
103+ expanded_set : list [ Node ] = []
100104 while open_set :
101105 expanded_node : Node = heapq .heappop (open_set )
102106 if verbose :
0 commit comments