From 6f9df1918d8c5040039687a6713041ecb4f33999 Mon Sep 17 00:00:00 2001 From: MLErik <baerenjesus@gmail.com> Date: Wed, 25 Sep 2019 18:05:16 -0400 Subject: [PATCH] update the the way we fix cells in the map. Now we first fix empty cells with incoming connections then we fix cells with rails and wrong transition maps. --- examples/flatland_2_0_example.py | 2 +- flatland/core/transition_map.py | 22 ++++++++++++++++++++++ flatland/envs/rail_env.py | 1 - flatland/envs/rail_generators.py | 25 +++++++++++++++++++------ 4 files changed, 42 insertions(+), 8 deletions(-) diff --git a/examples/flatland_2_0_example.py b/examples/flatland_2_0_example.py index 40c1e860..1178f59b 100644 --- a/examples/flatland_2_0_example.py +++ b/examples/flatland_2_0_example.py @@ -41,7 +41,7 @@ env = RailEnv(width=50, seed=15, # Random seed grid_mode=True, nr_inter_connections=1, - max_nr_connection_points=8 + max_nr_connection_points=12 ), schedule_generator=sparse_schedule_generator(), number_of_agents=50, diff --git a/flatland/core/transition_map.py b/flatland/core/transition_map.py index 07678add..f594fae2 100644 --- a/flatland/core/transition_map.py +++ b/flatland/core/transition_map.py @@ -422,6 +422,28 @@ class GridTransitionMap(TransitionMap): continue else: return False + # If the cell is empty but has incoming connections we return false + if binTrans < 1: + connected = 0 + + for iDirOut in np.arange(4): + gdRC = gDir2dRC[iDirOut] # row,col increment + gPos2 = grcPos + gdRC # next cell in that direction + + # Check the adjacent cell is within bounds + # if not, then ignore it for the count of incoming connections + if np.any(gPos2 < 0): + continue + if np.any(gPos2 >= grcMax): + continue + + # Get the transitions out of gPos2, using iDirOut as the inbound direction + # if there are no available transitions, ie (0,0,0,0), then rcPos is invalid + + for orientation in range(4): + connected += self.get_transition((gPos2[0], gPos2[1], orientation), mirror(iDirOut)) + if connected > 0: + return False return True diff --git a/flatland/envs/rail_env.py b/flatland/envs/rail_env.py index 5472f0c5..86277431 100644 --- a/flatland/envs/rail_env.py +++ b/flatland/envs/rail_env.py @@ -252,7 +252,6 @@ class RailEnv(Environment): rc_pos = (r, c) check = self.rail.cell_neighbours_valid(rc_pos, True) if not check: - self.rail.fix_transitions(rc_pos) warnings.warn("Invalid grid at {} -> {}".format(rc_pos, check)) if replace_agents: diff --git a/flatland/envs/rail_generators.py b/flatland/envs/rail_generators.py index bb746a87..266d2bec 100644 --- a/flatland/envs/rail_generators.py +++ b/flatland/envs/rail_generators.py @@ -707,6 +707,7 @@ def sparse_rail_generator(num_cities=5, num_intersections=4, num_trainstations=2 connection_points[trainstation_node][corner_node_idx], (station_x, station_y)) if len(connection) != 0: + if (connection_points[trainstation_node][corner_node_idx], trainstation_node) in boarder_connections: boarder_connections.remove( @@ -717,6 +718,7 @@ def sparse_rail_generator(num_cities=5, num_intersections=4, num_trainstations=2 if len(train_stations[trainstation_node]) > 0: train_stations[trainstation_node].pop(-1) else: + built_num_trainstation += 1 # Adjust the number of agents if you could not build enough trainstations if num_agents > built_num_trainstation: @@ -749,14 +751,25 @@ def sparse_rail_generator(num_cities=5, num_intersections=4, num_trainstations=2 boarder_connections.remove(tbd) print(boarder_connections) # Fix all nodes with illegal transition maps - flat_trainstation_list = [item for sublist in train_stations for item in sublist] - for cell_to_fix in flat_trainstation_list: - grid_map.fix_transitions(cell_to_fix) + empty_to_fix = [] + rails_to_fix = [] + for r in range(height): + for c in range(width): + rc_pos = (r, c) + check = grid_map.cell_neighbours_valid(rc_pos, True) + if not check: + if grid_map.grid[rc_pos] == 0: + empty_to_fix.append(rc_pos) + else: + rails_to_fix.append(rc_pos) - flat_list = [item for sublist in connection_points for item in sublist] + # Fix empty cells first to avoid cutting the network + for cell in empty_to_fix: + grid_map.fix_transitions(cell) - for cell_to_fix in flat_list: - grid_map.fix_transitions(cell_to_fix) + # Fix all other cells + for cell in rails_to_fix: + grid_map.fix_transitions(cell) # Generate start and target node directory for all agents. # Assure that start and target are not in the same node -- GitLab