Skip to content
Snippets Groups Projects
Commit 9e13a5b3 authored by u214892's avatar u214892
Browse files

#169 minor cleanup

parent 5ce5d7e4
No related branches found
No related tags found
No related merge requests found
......@@ -9,52 +9,52 @@ from flatland.envs.rail_generators_city_generator import city_generator
from flatland.envs.schedule_generators import city_schedule_generator
from flatland.utils.rendertools import RenderTool, AgentRenderVariant
if os.path.exists("./../render_output/"):
for itrials in np.arange(1, 1000, 1):
print(itrials, "generate new city")
# init seed
np.random.seed(itrials)
# select distance function used in a-star path finding
dist_fun = Vec2d.get_manhattan_distance
dfsel = (itrials - 1) % 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),
height=40 + np.random.choice(100),
rail_generator=city_generator(num_cities=5 + np.random.choice(10),
city_size=10 + np.random.choice(5),
allowed_rotation_angles=np.arange(0, 360, 6),
max_number_of_station_tracks=4 + np.random.choice(4),
nbr_of_switches_per_station_track=2 + np.random.choice(2),
connect_max_nbr_of_shortes_city=2 + np.random.choice(4),
do_random_connect_stations=itrials % 2 == 0,
a_star_distance_function=dist_fun,
seed=itrials,
print_out_info=False
),
schedule_generator=city_schedule_generator(),
number_of_agents=10000,
obs_builder_object=GlobalObsForRailEnv())
# reset to initialize agents_static
env_renderer = RenderTool(env, gl="PILSVG", screen_width=1400, screen_height=1000,
agent_render_variant=AgentRenderVariant.AGENT_SHOWS_OPTIONS_AND_BOX)
env_renderer.render_env(show=True, show_observations=False, show_predictions=False)
# store rendered file into render_output if the path exists
if os.path.exists("./../render_output/"):
env_renderer.gl.save_image(
os.path.join(
"./../render_output/",
"flatland_frame_{:04d}.png".format(itrials)
))
# close the renderer / window
env_renderer.close_window()
os.mkdir("./../render_output/")
for itrials in np.arange(1, 1000, 1):
print(itrials, "generate new city")
# init seed
np.random.seed(itrials)
# select distance function used in a-star path finding
dist_fun = Vec2d.get_manhattan_distance
dfsel = (itrials - 1) % 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),
height=40 + np.random.choice(100),
rail_generator=city_generator(num_cities=5 + np.random.choice(10),
city_size=10 + np.random.choice(5),
allowed_rotation_angles=np.arange(0, 360, 6),
max_number_of_station_tracks=4 + np.random.choice(4),
nbr_of_switches_per_station_track=2 + np.random.choice(2),
connect_max_nbr_of_shortes_city=2 + np.random.choice(4),
do_random_connect_stations=itrials % 2 == 0,
a_star_distance_function=dist_fun,
seed=itrials,
print_out_info=False
),
schedule_generator=city_schedule_generator(),
number_of_agents=10000,
obs_builder_object=GlobalObsForRailEnv())
# reset to initialize agents_static
env_renderer = RenderTool(env, gl="PILSVG", screen_width=1400, screen_height=1000,
agent_render_variant=AgentRenderVariant.AGENT_SHOWS_OPTIONS_AND_BOX)
env_renderer.render_env(show=True, show_observations=False, show_predictions=False)
# store rendered file into render_output if the path exists
env_renderer.gl.save_image(
os.path.join(
"./../render_output/",
"flatland_frame_{:04d}.png".format(itrials)
))
# close the renderer / window
env_renderer.close_window()
from typing import Tuple, Callable, List
from typing import Tuple, Callable, List, Type
import numpy as np
Vector2D = Tuple[float, float]
IntVector2D = Tuple[int, int]
Vector2D: Type = Tuple[float, float]
IntVector2D: Type = Tuple[int, int]
IntVector2DArray = List[IntVector2D]
IntVector2DArrayArray = List[List[IntVector2D]]
IntVector2DArray: Type = List[IntVector2D]
IntVector2DArrayArray: Type = List[List[IntVector2D]]
Vector2DArray = List[Vector2D]
Vector2DArrayArray = List[List[Vector2D]]
Vector2DArray: Type = List[Vector2D]
Vector2DArrayArray: Type = List[List[Vector2D]]
IntVector2DDistance = Callable[[IntVector2D, IntVector2D], float]
IntVector2DDistance: Type = Callable[[IntVector2D, IntVector2D], float]
class Vec2dOperations:
......@@ -85,10 +85,17 @@ class Vec2dOperations:
"""
calculates the euclidean norm of the 2d vector
:param node: tuple with coordinate (x,y) or 2d vector
:return:
-------
returns the euclidean distance
Parameters
----------
node_a
tuple with coordinate (x,y) or 2d vector
node_b
tuple with coordinate (x,y) or 2d vector
Returns
-------
float
Euclidean distance
"""
return Vec2dOperations.get_norm(Vec2dOperations.subtract(node_b, node_a))
......@@ -98,10 +105,17 @@ class Vec2dOperations:
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:
-------
returns the manhattan distance
Parameters
----------
node_a
tuple with coordinate (x,y) or 2d vector
node_b
tuple with coordinate (x,y) or 2d vector
Returns
-------
float
Mahnhattan distance
"""
delta = (Vec2dOperations.subtract(node_b, node_a))
return np.abs(delta[0]) + np.abs(delta[1])
......@@ -112,10 +126,17 @@ class Vec2dOperations:
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
:Parameters
----------
node_a
tuple with coordinate (x,y) or 2d vector
node_b
tuple with coordinate (x,y) or 2d vector
Returns
-------
float
the chebyshev distance
"""
delta = (Vec2dOperations.subtract(node_b, node_a))
return max(np.abs(delta[0]), np.abs(delta[1]))
......@@ -217,7 +238,7 @@ class Vec2dOperations:
return x1, y1
def position_to_coordinate(depth: int, positions):
def position_to_coordinate(depth: int, positions: List[int]):
"""Converts coordinates to positions::
[ (0,0) (0,1) .. (0,w-1)
......
......@@ -92,8 +92,8 @@ def city_generator(num_cities: int = 5,
IntVector2DArray):
nodes_added = []
start_nodes_added = [[] for _ in range(len(generate_city_locations))]
end_nodes_added = [[] for _ in range(len(generate_city_locations))]
start_nodes_added: IntVector2DArrayArray = [[] for _ in range(len(generate_city_locations))]
end_nodes_added: IntVector2DArrayArray = [[] for _ in range(len(generate_city_locations))]
station_slots = [[] for _ in range(len(generate_city_locations))]
station_tracks = [[[] for _ in range(intern_max_number_of_station_tracks)] for _ in range(len(
generate_city_locations))]
......@@ -230,8 +230,8 @@ def city_generator(num_cities: int = 5,
return graph, np.unique(graph_ids).astype(int)
def connect_sub_graphs(rail_trans: RailEnvTransitions, grid_map: GridTransitionMap,
org_s_nodes: IntVector2DArray,
org_e_nodes: IntVector2DArray,
org_s_nodes: IntVector2DArrayArray,
org_e_nodes: IntVector2DArrayArray,
city_edges: IntVector2DArray,
nodes_added: IntVector2DArray):
_, graphids = calc_nbr_of_graphs(city_edges)
......@@ -261,17 +261,16 @@ def city_generator(num_cities: int = 5,
iteration_counter += 1
# noinspection PyTypeChecker
def connect_stations(rail_trans: RailEnvTransitions,
grid_map: GridTransitionMap,
org_s_nodes: IntVector2DArray,
org_e_nodes: IntVector2DArray,
org_s_nodes: IntVector2DArrayArray,
org_e_nodes: IntVector2DArrayArray,
nodes_added: IntVector2DArray,
intern_connect_max_nbr_of_shortes_city: int):
city_edges = []
s_nodes = copy.deepcopy(org_s_nodes)
e_nodes = copy.deepcopy(org_e_nodes)
s_nodes:IntVector2DArrayArray = copy.deepcopy(org_s_nodes)
e_nodes:IntVector2DArrayArray = copy.deepcopy(org_e_nodes)
for nbr_connected in range(intern_connect_max_nbr_of_shortes_city):
for city_loop in range(len(s_nodes)):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment