Skip to content

Commit

Permalink
feat: add cost now accepts initial_default_weights argument ( known…
Browse files Browse the repository at this point in the history
… use case is for air_vs_ground grid )

Closes #81
  • Loading branch information
eladyaniv01 committed Sep 17, 2020
1 parent c1a6b69 commit 7fe542f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
5 changes: 3 additions & 2 deletions MapAnalyzer/MapData.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ def pathfind(self, start: Union[Tuple[int, int], Point2], goal: Union[Tuple[int,
sensitivity=sensitivity)

def add_cost(self, position: Tuple[int, int], radius: int, grid: ndarray, weight: int = 100, safe: bool = True,
) -> ndarray:
initial_default_weights: int = 0) -> ndarray:
"""
:rtype: numpy.ndarray
Expand All @@ -304,7 +304,8 @@ def add_cost(self, position: Tuple[int, int], radius: int, grid: ndarray, weight
When ``safe=False`` the Pather will not adjust illegal values below 1 which could result in a crash`
"""
return self.pather.add_cost(position=position, radius=radius, arr=grid, weight=weight, safe=safe)
return self.pather.add_cost(position=position, radius=radius, arr=grid, weight=weight, safe=safe,
initial_default_weights=initial_default_weights)

"""Utility methods"""

Expand Down
5 changes: 4 additions & 1 deletion MapAnalyzer/Pather.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def pathfind(self, start: Tuple[int, int], goal: Tuple[int, int], grid: Optional
return None

def add_cost(self, position: Tuple[int, int], radius: int, arr: ndarray, weight: int = 100,
safe: bool = True) -> ndarray:
safe: bool = True, initial_default_weights: int = 0) -> ndarray:
ri, ci = skdraw.disk(center=(int(position[0]), int(position[1])), radius=radius, shape=arr.shape)
if len(ri) == 0 or len(ci) == 0:
# this happens when the center point is near map edge, and the radius added goes beyond the edge
Expand All @@ -196,6 +196,9 @@ def in_bounds_ri(y):
ri_vec = np.vectorize(in_bounds_ri)
ci = ci_vec(ci)
ri = ri_vec(ri)
if initial_default_weights > 0:
arr[ri, ci] = np.where(arr[ri, ci] == 1, initial_default_weights, arr[ri, ci])

arr[ri, ci] += weight
if np.any(arr < 1) and safe:
self.map_data.logger.warning(
Expand Down
4 changes: 3 additions & 1 deletion run.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ def get_map_file_list() -> List[str]:
reg_end = map_data.where_all(map_data.bot.enemy_start_locations[0].position)[0]
p0 = Point2(reg_start.center)
p1 = Point2(reg_end.center)
influence_grid = map_data.get_air_vs_ground_grid()
influence_grid = map_data.get_air_vs_ground_grid(default_weight=50)
p = (50, 130)
influence_grid = map_data.add_cost(grid=influence_grid, position=p, radius=10, initial_default_weights=50)
map_data.plot_influenced_path(start=p0, goal=p1, weight_array=influence_grid, allow_diagonal=True)
map_data.show()
break
Expand Down

0 comments on commit 7fe542f

Please sign in to comment.