### Added the possibility to block routes

parent 6e1e8974
 ... ... @@ -2,7 +2,22 @@ from math import inf from data_validation import generate_matrix def mans_cels(city_1, city_2, matrix, cities): def mans_cels(city_1, city_2, matrix, cities, block_tuple_list=None): print(f"Finding the route between {city_1} and {city_2}") if block_tuple_list is not None: for blockage in block_tuple_list: block_1 = blockage # City to block from block_2 = blockage # City to block to distance = blockage # Distance to use for "blocking" print( f"Changing the distance from {block_1} to {block_2} with ", f"distance {distance}" ) block_1_idx = cities.index(block_1) block_2_idx = cities.index(block_2) matrix[block_1_idx][block_2_idx] = distance start_node = cities.index(city_1) end_node = cities.index(city_2) sum_dist = [inf] * len(cities) ... ... @@ -14,8 +29,8 @@ def mans_cels(city_1, city_2, matrix, cities): for node_to_visit in range(0, len(sum_dist)): length = matrix[curr_node][node_to_visit] node_visited = visited[node_to_visit] visiting_curr = node_to_visit == curr_node or length >= 100 cannot_reach = length == inf visiting_curr = node_to_visit == curr_node cannot_reach = length == inf or length > 100 # Determine wether we're trying to access an invalid city if visiting_curr or cannot_reach or node_visited: ... ... @@ -38,29 +53,42 @@ def mans_cels(city_1, city_2, matrix, cities): # If we're at the end node, trace back the graph if visited[end_node]: route = [cities[end_node]] pos = end_node pos = end_node # The node we're on. while (pos != start_node): for i_node in range(0, len(sum_dist)): if i_node != pos and matrix[i_node][pos] != inf and sum_dist[pos] - \ matrix[i_node][pos] == sum_dist[i_node]: # Check if the the distance from the node we're looking for # and the node we're on is reachable length = matrix[i_node][pos] cannot_reach = length != inf or length > 100 length_looking_for = sum_dist[i_node] length_we_have = sum_dist[pos] - length if i_node != pos and cannot_reach and length_we_have == length_looking_for: route.append(cities[i_node]) pos = i_node break route.reverse() print(route) break city_to_visit = cities[curr_smallest_id] print(f"Next city to visit {city_to_visit}") return route curr_node = curr_smallest_id if __name__ == "__main__": matrix_data = generate_matrix() cities_string = ", ".join(matrix_data['cities']) print(f"Availaible cities {cities_string}") mans_cels( # print(f"Availaible cities {cities_string}") route = mans_cels( "Ventspils", "Riga", matrix_data["matrix"], matrix_data["cities"] ) print(route, '\n') block_tuple_list = [ ("Ventspils", "Talsi", inf), ("Talsi", "Ventspils", inf)] route = mans_cels( "Ventspils", "Riga", matrix_data["matrix"], matrix_data["cities"], block_tuple_list=block_tuple_list ) print(route)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!