diff --git a/examples/simple_example_city_railway_generator.py b/examples/simple_example_city_railway_generator.py index 14de854a9e169d2461c9d9e0f9d97c8565f6236f..98156c6aa2edfbbf8c0c87c10d7a903cf233628e 100644 --- a/examples/simple_example_city_railway_generator.py +++ b/examples/simple_example_city_railway_generator.py @@ -17,9 +17,12 @@ if os.path.exists("./../render_output/"): np.random.seed(itrials) # select distance function used in a-star path finding - dist_fun = Vec2d.get_euclidean_distance - if np.random.choice(1) == 0: - dist_fun = Vec2d.get_manhattan_distance + dist_fun = Vec2d.get_manhattan_distance + dfsel = itrials % 3 + if dfsel == 1: + dist_fun = Vec2d.get_euclidean_distance + elif dfsel == 2: + dist_fun = Vec2d.get_chebyshev_distance # create RailEnv and use the city_generator to create a map env = RailEnv(width=40 + np.random.choice(100), diff --git a/flatland/core/grid/grid_utils.py b/flatland/core/grid/grid_utils.py index 3fda39b1d0e027c281c7b2b41545632ef0fcf3f9..47cbb944e02044fb704e8efd8116a39b2d30df85 100644 --- a/flatland/core/grid/grid_utils.py +++ b/flatland/core/grid/grid_utils.py @@ -71,6 +71,7 @@ class Vec2dOperations: def get_norm(node: Vector2D) -> float: """ calculates the euclidean norm of the 2d vector + [see: https://lyfat.wordpress.com/2012/05/22/euclidean-vs-chebyshev-vs-manhattan-distance/] :param node: tuple with coordinate (x,y) or 2d vector :return: @@ -95,6 +96,7 @@ class Vec2dOperations: def get_manhattan_distance(node_a: Vector2D, node_b: Vector2D) -> float: """ calculates the manhattan distance of the 2d vector + [see: https://lyfat.wordpress.com/2012/05/22/euclidean-vs-chebyshev-vs-manhattan-distance/] :param node: tuple with coordinate (x,y) or 2d vector :return: @@ -104,6 +106,20 @@ class Vec2dOperations: delta = (Vec2dOperations.subtract(node_b, node_a)) return np.abs(delta[0]) + np.abs(delta[1]) + @staticmethod + def get_chebyshev_distance(node_a: Vector2D, node_b: Vector2D) -> float: + """ + calculates the chebyshev norm of the 2d vector + [see: https://lyfat.wordpress.com/2012/05/22/euclidean-vs-chebyshev-vs-manhattan-distance/] + + :param node: tuple with coordinate (x,y) or 2d vector + :return: + ------- + returns the chebyshev distance + """ + delta = (Vec2dOperations.subtract(node_b, node_a)) + return max(np.abs(delta[0]), np.abs(delta[1])) + @staticmethod def normalize(node: Vector2D) -> Tuple[float, float]: """