Home / Expert Answers / Computer Science / please-in-python-3-and-with-comment-thank-you-very-much-the-road-network-a-road-network-can-be-mode-pa122

(Solved): please in python 3 and with comment thank you very much The road network A road network can be mode ...



please in python 3 and with comment thank you very muchThe road network
A road network can be modelled as a graph. Nodes
in the graph represent intersections and locations.
Each noNote also that not every route through an intersection can be traversed simultaneously. For example, intersection 0 in the diQuestion 2
A path through the road network can be described by listing each node the vehicle will travel through. For exampleThe vehicles journey will be as follows:
• Timestep 0: Depart node 1 heading towards intersection 0
• Timestep 1: Arrive at 1
2 (4,1); (1,4)
3 (2,3); (3,2)
4
5
6
7
8
9
10
#Intersection:0
(4,2); (2,4); (1,3); (3,1)
(1,2); (2,1); (4,3); (3,4)
#Interse

The road network A road network can be modelled as a graph. Nodes in the graph represent intersections and locations. Each node is assigned an ID number for ease of identification. Edges between nodes represent roads. The graph is directed, as it is possible for roads to allow travel in one direction but not the other (e.g. one-way streets). One sample road network is shown below: 5 4 6 2 0 3 1 Note in this sample network the yellow squares represent intersections while the blue circles represent locations. A vehicle will leave one location node, potentially travel through a number of intersections, then arrive at another location node. Note also that not every route through an intersection can be traversed simultaneously. For example, intersection 0 in the diagram above would not likely allow a car to travel from 2 to 3 at the same time as another car is travelling from 1 to 4 as this would result in a collision. To describe this behavior, we can consider a particular traffic signal which identifies which paths through the intersection can be traversed at any point in time. This can be modelled as a list of (source, destination) pairs. For example, [(5,6), (6,5)] is a traffic signal for intersection 4 that indicates that cars can travel from node 5 to node 6 or from node 6 to node 5. We can then describe the intersection as a list of traffic signals, each of which is active for one timestep and cycle back to the first traffic signal after the final signal has completed. For example, [[(5,6), (6,5)], [(0, 6), (6, 0), (5, 0), (0, 5)]] indicates: • At timestep 0 cars can travel from 5 to 6 or from 6 to 5 • At timestep 1 cars can travel from 0 to 6, 6 to 0, 5 to 0 or 0 to 5 • At timestep 2 cars can travel from 5 to 6 or from 6 to 5 etc... To simplify our model, we will assume that an intersection allows at most one vehicle through in each permitted direction at each timestep. For example, at timestep 0 one vehicle may travel from 5 to 6 and one vehicle may travel from 6 to 5. All other vehicles must wait at the intersection. Once a vehicle has exited the intersection it will be placed on the road taking it towards the next intersection or location node. For simplicity, we assume it will take a fixed number of timesteps to traverse the road. Each car will begin its journey at one of the location nodes and potentially travel through one or more intersections until it reaches its final destination node. Question 2 A path through the road network can be described by listing each node the vehicle will travel through. For example, the path [2,0,4,6] would indicate that the vehicle starts at node 2, travels through nodes 0 and 4 then arrives at node 6. Recall that the first and last nodes must be location nodes, while all other nodes must be intersections. In this task we wish to understand: • Whether a vehicle starting its journey at timestep 0 is able to traverse the path without being stopped at any intersection, assuming there are no other vehicles on the road network • How long that journey would take Write a function path_cost(path, intersections, road_times). If it is possible to traverse the path starting at timestep 0 without being stopped at any intersections then the function should return the total number of timesteps taken. If not, it should return None. You may assume that the path is a valid, traversable path. Your function should take the following arguments: • path is a list representing the nodes through which the vehicle will traverse • intersections and road_times are the dictionaries that represent the road network, as described in Question 1. A working implementation of the load_road_network function has been made available, you may make calls to this function if you wish. You may assume that a vehicle moves in the timestep during which it enters the road network. For example, if a vehicle enters the network at node 1 at timestep 0 travelling towards node 2 and the distance between nodes 1 and 2 is 1 timestep, then the vehicle would arrive at node 2 at timestep 1. You may further assume that it takes O time to cross an intersection with a favourable traffic signal. A vehicle may cross an intersection and drive along a road during the same timestep only if the vehicle crosses the intersection before driving along the road. For example, consider the following function call: >> simple intersections = {0: [[],[(1,2), (2,1)]]} >> simple_roads = {(0,1):1, (1,0):1, (0,2):1, (2,0):1} >> path_cost([1,0,2], simple intersections, simple_roads) The vehicle's journey will be as follows: • Timestep 0: Depart node 1 heading towards intersection 0 • Timestep 1: Arrive at node 0 and pass through the intersection unimpeded • Timestep 2: Arrive at final location node 2 path_cost([1,0,2], simple_intersections, simple_roads) should therefore return a value of 2. Below are some sample function calls: >> simple intersections = {0: [[],[(1,2), (2,1)]]} >> simple_roads = {(0,1):1, (1,0):1, (0,2):1, (2,0):1} >> print (path_cost([1,0,2], simple intersections, simple_roads)) 2 >> simple intersections = {0: [[(1,2), (2,1)], []]} >> simple_roads = {(0,1):1, (1,0):1, (0,2):1, (2,0):1} >> print (path_cost([1,0,2], simple intersections, simple_roads)) None >> intersections, road_times = load_road_network( 'road_sample.txt') >> print (path_cost([2,0,4,6], intersections, road_times)) None land and natunakelland compla 1 2 (4,1); (1,4) 3 (2,3); (3,2) 4 5 6 7 8 9 10 #Intersection:0 (4,2); (2,4); (1,3); (3,1) (1,2); (2,1); (4,3); (3,4) #Intersection: 4 (5,6); (6,5) (0,6); (6,0); (5,0); (0,5) 11 #Roads 12 (0,1):1 13 (1,0):1 14 (0,2):1 15 (2,0):1 16 (0,3):1 17 (3,0):1 18 (0,4):3 19 (4,0):3 20 (4,5):2 21 (5,4):2 22 (4,6):2 23 (6,4):2


We have an Answer from Expert

View Expert Answer

Expert Answer


My answer Below is the code to copy: #CODE STARTS HERE---------------- from urllib.parse import urlparse def get_suffix(num): #gets suffix for numbers. Ex: 0th, 2nd, 3rd, if num==1: return "1st" elif num==2: return "2nd" elif num==3: return "3rd" els
We have an Answer from Expert

Buy This Answer $5

Place Order

We Provide Services Across The Globe