From ac00c08ed49670d694ba822092cc3ac59c54961a Mon Sep 17 00:00:00 2001 From: "Egli Adrian (IT-SCI-API-PFI)" <adrian.egli@sbb.ch> Date: Wed, 18 Sep 2019 12:13:51 +0200 Subject: [PATCH] chebyshev distance added --- .../simple_example_city_railway_generator.py | 9 ++++++--- flatland/core/grid/grid_utils.py | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/examples/simple_example_city_railway_generator.py b/examples/simple_example_city_railway_generator.py index 14de854a..98156c6a 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 3fda39b1..47cbb944 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]: """ -- GitLab