





'''simple docstring'''

from scipy.stats import spearmanr

import datasets


__A	: Union[str, Any]              =       """
The Spearman rank-order correlation coefficient is a measure of the
relationship between two datasets. Like other correlation coefficients,
this one varies between -1 and +1 with 0 implying no correlation.
Positive correlations imply that as data in dataset x increases, so
does data in dataset y. Negative correlations imply that as x increases,
y decreases. Correlations of -1 or +1 imply an exact monotonic relationship.

Unlike the Pearson correlation, the Spearman correlation does not
assume that both datasets are normally distributed.

The p-value roughly indicates the probability of an uncorrelated system
producing datasets that have a Spearman correlation at least as extreme
as the one computed from these datasets. The p-values are not entirely
reliable but are probably reasonable for datasets larger than 500 or so.
"""

__A	: List[str]              =       """
Args:
    predictions (`List[float]`): Predicted labels, as returned by a model.
    references (`List[float]`): Ground truth labels.
    return_pvalue (`bool`): If `True`, returns the p-value. If `False`, returns
            only the spearmanr score. Defaults to `False`.
Returns:
    spearmanr (`float`): Spearman correlation coefficient.
    p-value (`float`): p-value. **Note**: is only returned if `return_pvalue=True` is input.
Examples:
    Example 1:
        >>> spearmanr_metric = datasets.load_metric(\"spearmanr\")
        >>> results = spearmanr_metric.compute(references=[1, 2, 3, 4, 5], predictions=[10, 9, 2.5, 6, 4])
        >>> print(results)
        {'spearmanr': -0.7}

    Example 2:
        >>> spearmanr_metric = datasets.load_metric(\"spearmanr\")
        >>> results = spearmanr_metric.compute(references=[1, 2, 3, 4, 5],
        ...                                     predictions=[10, 9, 2.5, 6, 4],
        ...                                     return_pvalue=True)
        >>> print(results['spearmanr'])
        -0.7
        >>> print(round(results['spearmanr_pvalue'], 2))
        0.19
"""

__A	: Dict              =       R"""\
@book{kokoska2000crc,
  title={CRC standard probability and statistics tables and formulae},
  author={Kokoska, Stephen and Zwillinger, Daniel},
  year={2000},
  publisher={Crc Press}
}
@article{2020SciPy-NMeth,
  author  = {Virtanen, Pauli and Gommers, Ralf and Oliphant, Travis E. and
            Haberland, Matt and Reddy, Tyler and Cournapeau, David and
            Burovski, Evgeni and Peterson, Pearu and Weckesser, Warren and
            Bright, Jonathan and {van der Walt}, St{\'e}fan J. and
            Brett, Matthew and Wilson, Joshua and Millman, K. Jarrod and
            Mayorov, Nikolay and Nelson, Andrew R. J. and Jones, Eric and
            Kern, Robert and Larson, Eric and Carey, C J and
            Polat, {\.I}lhan and Feng, Yu and Moore, Eric W. and
            {VanderPlas}, Jake and Laxalde, Denis and Perktold, Josef and
            Cimrman, Robert and Henriksen, Ian and Quintero, E. A. and
            Harris, Charles R. and Archibald, Anne M. and
            Ribeiro, Ant{\^o}nio H. and Pedregosa, Fabian and
            {van Mulbregt}, Paul and {SciPy 1.0 Contributors}},
  title   = {{{SciPy} 1.0: Fundamental Algorithms for Scientific
            Computing in Python}},
  journal = {Nature Methods},
  year    = {2020},
  volume  = {17},
  pages   = {261--272},
  adsurl  = {https://rdcu.be/b08Wh},
  doi     = {10.1038/s41592-019-0686-2},
}
"""






@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION		,    _KWARGS_DESCRIPTION			)
class       _UpperCamelCase      (			datasets.Metric			):






				'''simple docstring'''





				def        lowercase__   (				self    ):
											"""simple docstring"""
											return datasets.MetricInfo(
											    description=_DESCRIPTION       ,					citation=_CITATION       ,					inputs_description=_KWARGS_DESCRIPTION       ,					features=datasets.Features(
											        {
											            'predictions': datasets.Value('float'    ),
											            'references': datasets.Value('float'    ),
											        }    )       ,					reference_urls=['https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.spearmanr.html']       ,					)






				def        lowercase__   (				self       ,					_a       ,					_a       ,					_a=False    ):
											"""simple docstring"""
											a__            =							spearmanr(_a       ,					_a    )
											if return_pvalue:
																		return {"spearmanr": results[0], "spearmanr_pvalue": results[1]}
											else:
																		return {"spearmanr": results[0]}










from __future__ import annotations

import time
from math import sqrt

# 1 for manhattan, 0 for euclidean
_lowerCamelCase    :		List[str]		      =    0

_lowerCamelCase    :		Tuple		      =    [
    [0, 0, 0, 0, 0, 0, 0],
    [0, 1, 0, 0, 0, 0, 0],  # 0 are free path whereas 1's are obstacles
    [0, 0, 0, 0, 0, 0, 0],
    [0, 0, 1, 0, 0, 0, 0],
    [1, 0, 1, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 1, 0, 0],
]

_lowerCamelCase    :		Tuple		      =    [[-1, 0], [0, -1], [1, 0], [0, 1]]  # up, left, down, right

_lowerCamelCase    :		Union[str, Any]		      =    tuple[int, int]


class 						lowercase    :
						'''simple docstring'''


						def __init__(  self    :				Optional[Any]       ,	snake_case    :				int       ,	snake_case    :				int       ,	snake_case    :				int       ,	snake_case    :				int       ,	snake_case    :				int       ,	snake_case    :				Node | None       ,	):



											'''simple docstring'''


											SCREAMING_SNAKE_CASE							:     Tuple						=				pos_x
											SCREAMING_SNAKE_CASE							:     str						=				pos_y
											SCREAMING_SNAKE_CASE							:     Any						=				(pos_y, pos_x)
											SCREAMING_SNAKE_CASE							:     List[str]						=				goal_x
											SCREAMING_SNAKE_CASE							:     Optional[Any]						=				goal_y
											SCREAMING_SNAKE_CASE							:     int						=				g_cost
											SCREAMING_SNAKE_CASE							:     Any						=				parent
											SCREAMING_SNAKE_CASE							:     List[Any]						=				self.calculate_heuristic()
											SCREAMING_SNAKE_CASE							:     str						=				self.g_cost + self.h_cost







						def       lowerCamelCase_				(  self    :				Optional[int]	):



											'''simple docstring'''


											SCREAMING_SNAKE_CASE							:     int						=				self.pos_x - self.goal_x
											SCREAMING_SNAKE_CASE							:     str						=				self.pos_y - self.goal_y
											if HEURISTIC == 1:
																return abs(snake_case	) + abs(snake_case	)
											else:
																return sqrt(dy**2 + dx**2	)







						def __lt__(  self    :				Any       ,	snake_case    :				Node	):



											'''simple docstring'''


											return self.f_cost < other.f_cost


class 						lowercase    :
						'''simple docstring'''


						def __init__(  self    :				Optional[Any]       ,	snake_case    :				TPosition       ,	snake_case    :				TPosition	):



											'''simple docstring'''


											SCREAMING_SNAKE_CASE							:     Union[str, Any]						=				Node(start[1]       ,	start[0]       ,	goal[1]       ,	goal[0]       ,	0       ,	snake_case	)
											SCREAMING_SNAKE_CASE							:     int						=				Node(goal[1]       ,	goal[0]       ,	goal[1]       ,	goal[0]       ,	99999       ,	snake_case	)

											SCREAMING_SNAKE_CASE							:     Any						=				[self.start]
											SCREAMING_SNAKE_CASE							:     list[Node]						=				[]

											SCREAMING_SNAKE_CASE							:     int						=				False







						def       lowerCamelCase_				(  self    :				str	):



											'''simple docstring'''


											while self.open_nodes:
																# Open Nodes are sorted using __lt__
																self.open_nodes.sort()
																SCREAMING_SNAKE_CASE							:     Dict						=				self.open_nodes.pop(0	)

																if current_node.pos == self.target.pos:
																					return self.retrace_path(snake_case	)

																self.closed_nodes.append(snake_case	)
																SCREAMING_SNAKE_CASE							:     int						=				self.get_successors(snake_case	)

																for child_node in successors:
																					if child_node in self.closed_nodes:
																										continue

																					if child_node not in self.open_nodes:
																										self.open_nodes.append(snake_case	)
																					else:
																										# retrieve the best current path
																										SCREAMING_SNAKE_CASE							:     Union[str, Any]						=				self.open_nodes.pop(self.open_nodes.index(snake_case	)	)

																										if child_node.g_cost < better_node.g_cost:
																															self.open_nodes.append(snake_case	)
																										else:
																															self.open_nodes.append(snake_case	)

											return [self.start.pos]







						def       lowerCamelCase_				(  self    :				str       ,	snake_case    :				Node	):



											'''simple docstring'''


											SCREAMING_SNAKE_CASE							:     Tuple						=				[]
											for action in delta:
																SCREAMING_SNAKE_CASE							:     Optional[Any]						=				parent.pos_x + action[1]
																SCREAMING_SNAKE_CASE							:     Union[str, Any]						=				parent.pos_y + action[0]
																if not (0 <= pos_x <= len(grid[0]	) - 1 and 0 <= pos_y <= len(snake_case	) - 1):
																					continue

																if grid[pos_y][pos_x] != 0:
																					continue

																successors.append(
																    Node(
																        snake_case       ,	snake_case       ,	self.target.pos_y       ,	self.target.pos_x       ,	parent.g_cost + 1       ,	snake_case       ,	)	)
											return successors







						def       lowerCamelCase_				(  self    :				Optional[int]       ,	snake_case    :				Node | None	):



											'''simple docstring'''


											SCREAMING_SNAKE_CASE							:     Optional[int]						=				node
											SCREAMING_SNAKE_CASE							:     List[Any]						=				[]
											while current_node is not None:
																path.append((current_node.pos_y, current_node.pos_x)	)
																SCREAMING_SNAKE_CASE							:     Dict						=				current_node.parent
											path.reverse()
											return path


class 						lowercase    :
						'''simple docstring'''


						def __init__(  self    :				str       ,	snake_case    :				TPosition       ,	snake_case    :				TPosition	):



											'''simple docstring'''


											SCREAMING_SNAKE_CASE							:     Any						=				AStar(snake_case       ,	snake_case	)
											SCREAMING_SNAKE_CASE							:     Optional[int]						=				AStar(snake_case       ,	snake_case	)
											SCREAMING_SNAKE_CASE							:     str						=				False







						def       lowerCamelCase_				(  self    :				Optional[int]	):



											'''simple docstring'''


											while self.fwd_astar.open_nodes or self.bwd_astar.open_nodes:
																self.fwd_astar.open_nodes.sort()
																self.bwd_astar.open_nodes.sort()
																SCREAMING_SNAKE_CASE							:     Dict						=				self.fwd_astar.open_nodes.pop(0	)
																SCREAMING_SNAKE_CASE							:     Any						=				self.bwd_astar.open_nodes.pop(0	)

																if current_bwd_node.pos == current_fwd_node.pos:
																					return self.retrace_bidirectional_path(
																					    snake_case       ,	snake_case	)

																self.fwd_astar.closed_nodes.append(snake_case	)
																self.bwd_astar.closed_nodes.append(snake_case	)

																SCREAMING_SNAKE_CASE							:     str						=				current_bwd_node
																SCREAMING_SNAKE_CASE							:     int						=				current_fwd_node

																SCREAMING_SNAKE_CASE							:     str						=				{
																    self.fwd_astar: self.fwd_astar.get_successors(snake_case	),
																    self.bwd_astar: self.bwd_astar.get_successors(snake_case	),
																}

																for astar in [self.fwd_astar, self.bwd_astar]:
																					for child_node in successors[astar]:
																										if child_node in astar.closed_nodes:
																															continue

																										if child_node not in astar.open_nodes:
																															astar.open_nodes.append(snake_case	)
																										else:
																															# retrieve the best current path
																															SCREAMING_SNAKE_CASE							:     List[Any]						=				astar.open_nodes.pop(
																															    astar.open_nodes.index(snake_case	)	)

																															if child_node.g_cost < better_node.g_cost:
																																				astar.open_nodes.append(snake_case	)
																															else:
																																				astar.open_nodes.append(snake_case	)

											return [self.fwd_astar.start.pos]







						def       lowerCamelCase_				(  self    :				str       ,	snake_case    :				Node       ,	snake_case    :				Node	):



											'''simple docstring'''


											SCREAMING_SNAKE_CASE							:     Union[str, Any]						=				self.fwd_astar.retrace_path(snake_case	)
											SCREAMING_SNAKE_CASE							:     Optional[Any]						=				self.bwd_astar.retrace_path(snake_case	)
											bwd_path.pop()
											bwd_path.reverse()
											SCREAMING_SNAKE_CASE							:     Union[str, Any]						=				fwd_path + bwd_path
											return path


if __name__ == "__main__":
			# all coordinates are given in format [y,x]
			_lowerCamelCase    :		Tuple		      =    (0, 0)
			_lowerCamelCase    :		Dict		      =    (len(grid) - 1, len(grid[0]) - 1)
			for elem in grid:
						print(elem)

			_lowerCamelCase    :		Union[str, Any]		      =    time.time()
			_lowerCamelCase    :		List[str]		      =    AStar(init, goal)
			_lowerCamelCase    :		Optional[int]		      =    a_star.search()
			_lowerCamelCase    :		str		      =    time.time() - start_time
			print(f"""AStar execution time = {end_time:f} seconds""")

			_lowerCamelCase    :		Optional[Any]		      =    time.time()
			_lowerCamelCase    :		Union[str, Any]		      =    BidirectionalAStar(init, goal)
			_lowerCamelCase    :		List[Any]		      =    time.time() - bd_start_time
			print(f"""BidirectionalAStar execution time = {bd_end_time:f} seconds""")