Question: this was the code from program 2. I need this to be written in python. import random from math import sqrt d = { 'A':[('B',0.1,5),('C',0.9,6)],

 this was the code from program 2. I need this to

be written in python. import random from math import sqrt d =

this was the code from program 2. I need this to be written in python.

import random from math import sqrt

d = { 'A':[('B',0.1,5),('C',0.9,6)], 'B':[('D',0.2,4),('E',0.8,7)], 'C':[('E',0.3,4),('F',0.7,6)], 'D':[('G',0.1,4),('H',0.9,6)], 'E':[('H',0.4,6),('I',0.6,4)], 'F':[('I',0.2,3),('J',0.8,7)], 'G':[('K',1.0,4)], 'H':[('K',0.3,4),('L',0.7,8)], 'I':[('L',0.5,6),('M',0.5,4)], 'J':[('M',1.0,5)], 'K':[('N',1.0,4)], 'L':[('N',0.4,5),('O',0.6,6)], 'M':[('O',1.0,5)], 'N':[('P',1.0,5)], 'O':[('P',1.0,5)], }

def simulateNextTown(c): r = random.random() if r

def simulateOneCar(): curr, end = 'A', 'P' timetaken = 0 while curr!=end: nxt,cost = simulateNextTown(curr) curr = nxt timetaken += cost return timetaken

def avg(arr): s = 0.0 s = sum(arr)*1.0 return s/len(arr)

def stdDev(arr): mu=avg(arr) s = 0.0 for x in arr: s += (x-mu)**2 return sqrt(s/len(arr))

# simulate 10K cars arr = [] for _ in range(10000): arr.append(simulateOneCar()) print("Simulate traffic flow with 10,000 cars") print("Average transit time: %.3f"%(avg(arr))) print("Standard deviation: %.3f "%(stdDev(arr)))

arr = [] for _ in range(10): arr2 = [] for _ in range(10000): arr2.append(simulateOneCar()) arr.append(avg(arr2)) print("Simulate 10 runs of 10,000 cars") print("Average transit time: %.3f"%(avg(arr))) print("Standard deviation: %.3f "%(stdDev(arr)))

arr = [] for _ in range(100000): arr.append(simulateOneCar()) print("Simulate traffic flow with 100,000 cars") print("Average transit time: %.3f"%(avg(arr))) print("Standard deviation: %.3f "%(stdDev(arr)))

CS 355/555, Spring 2019, Program III The region in program II has made changes to the road system described there. In each town is a traffic light and traffic going through a town takes one of two or three routes out of the town, depending on the traffic light. The lights have been retimed so that one route out of town does not dominate. In addition, they have discovered that a car does not travel from one town to the next in exactly the same amount of time each time, but the time can vary uniformly randomly one minute greater than or less than the amount specified below. Also, they have discovered that each day about 1000 cars travel from A to P, so they want to find averages and standard deviations based on 1000 cars a day. If a car enters town A, it moves to B or C, then to D, E, or F, and proceeds through the network reaching P. The car leaves one town going to any of the destination towns according to probabilities in the following table: 4.6 5.5 4 .6 6.4 6 .4 6.4 4 .6 1.0 1.0 1.0 CS 355/555, Spring 2019, Program III The t ime to drive the roads between to wns in minutes is given in the table below: 4 6 4 7 Write a program to simulate traffic flow with 1000 cars. Calculate the average Run this program 10 times and compute the average transit time for 00,000 cars Compare the 10 runs of 1000 with the one run of 10,000 transit time from A to P and calculate the standard deviation. and the standard deviation CS 355/555, Spring 2019, Program III The region in program II has made changes to the road system described there. In each town is a traffic light and traffic going through a town takes one of two or three routes out of the town, depending on the traffic light. The lights have been retimed so that one route out of town does not dominate. In addition, they have discovered that a car does not travel from one town to the next in exactly the same amount of time each time, but the time can vary uniformly randomly one minute greater than or less than the amount specified below. Also, they have discovered that each day about 1000 cars travel from A to P, so they want to find averages and standard deviations based on 1000 cars a day. If a car enters town A, it moves to B or C, then to D, E, or F, and proceeds through the network reaching P. The car leaves one town going to any of the destination towns according to probabilities in the following table: 4.6 5.5 4 .6 6.4 6 .4 6.4 4 .6 1.0 1.0 1.0 CS 355/555, Spring 2019, Program III The t ime to drive the roads between to wns in minutes is given in the table below: 4 6 4 7 Write a program to simulate traffic flow with 1000 cars. Calculate the average Run this program 10 times and compute the average transit time for 00,000 cars Compare the 10 runs of 1000 with the one run of 10,000 transit time from A to P and calculate the standard deviation. and the standard deviation

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!