Exercise 3 - Python Programming
#
Objectives#
If you can do this exercise, this means that you are comfortable with
preforming loops and conditional statements
writing and calling a function
writing, importing, and calling functions from a script file
Travel time under different weather conditions#
We will utilizes the Greenshields model for traffic flow to estimate travel time under different weather conditions, specifically when it’s dry versus when it’s raining. The Greenshields model is a fundamental traffic flow model that describes the relationship between the traffic density (k) and the speed of the traffic (v). According to this model, the speed decreases linearly with the increase of traffic density until it reaches a maximum density where the speed is zero.
The model follows the equation: $\(v = v_{max} \left( 1 - \frac{k}{k_{max}} \right)\)$
where:
\(v\) is the speed of the traffic at a particular density,
\(v_{max}\) is the maximum (free flow),
\(k\) is the current traffic density,
\(k_{max}\) is the jam density (where traffic comes to a halt).
Exercise 3.1 - If condition#
Exercise 3.1.1 - if condition with string variable#
In this problem, you will calculate the travel time under two different scenarios: clear weather and rainy weather. We will assume that rainfall reduces the maximum speed under clear condition (\(v_{max}\)) by 20%, due to lower visibility and increased braking distances. Write a code that calculates the time of travel (\(t\)) from home to work when it is raining and when it is not raining. Note \(v=d/t\)
For the values for your variables, you can use a distance (\(d\)) from home to work of 20 kilometers, a max speed (\(v_{max}\)) under clear conditions of 60 km/h, a current traffic density (\(k\)) of 30 vehicles/km, and a jam density (\(k_{max}\)) of 150 vehicles/km.
Your output should look like this
Travel without rain : 25.00 minutes
Note the formatting.
# Variables:
distance_to_work = 20 # Distance to work in kilometers
v_max_clear = 60 # Max speed under clear conditions (km/h)
traffic_density = 30 # Current traffic density (vehicles/km)
jam_density = 150 # Jam density (vehicles/km)
#If-condition to determine the travel time based on the weather ("with rain" or "without rain")
weather="without rain"
# Calculate speed using the Greenshields' model
# Calculate travel time (min)
#print your result
Exercise 3.1.2 - if condition with a boolen variable#
Repeat the above code using a boolen variable. For example, define a boolen variable israining=False
, and use this variable to do your if condition.
# Variables:
distance_to_work = 20 # Distance to work in kilometers
v_max_clear = 60 # Max speed under clear conditions (km/h)
traffic_density = 30 # Current traffic density (vehicles/km)
jam_density = 150 # Jam density (vehicles/km)
# If condition for cases of "with rain" and "without rain" using boolen variable (True / False)
israining=False
# Calculate speed using the Greenshields' model
# Calculate travel time (min)
#Print your results
Exercise 3.2 - For loop#
It is not only raining or not raining, but the rainfall intensity could also have an impact. There is no universal relationship between rainfall intensity and the reduction in maximum travel speed because it depends on various factors such as road conditions, vehicle characteristics, driver behavior, visibility, and local regulations. We can use big data and machine learning to develop a relationship for roads of interest. However, for the purpose of this exercise, we can create a simplified model that assumes a linear relationship between rainfall intensity and a reduction in maximum speed. Let’s say that for every mm/hour increase in rainfall intensity, the maximum speed is reduced by a certain percentage. To keep things simple, we can introduce speed_reduction_factor
to the maximum speed based on the rainfall intensity:
speed_reduction_factor = (rainfall_intensity / max_rainfall_intensity) * max_speed_reduction
where rainfall_intensity
is the current rainfall intensity (mm/hour); max_rainfall_intensity
is the maximum expected rainfall intensity based on a return period (mm/hour); max_speed_reduction
is the max speed reduction at max rainfall intensity.
Exercise 3.2.1 Calculate speed reduction factor#
Calculate the speed reduction based on different rainfall intensities [0,5, ... , 20]
.
Here is a sample of your output
For rainfall of 0.00 mm/hour the maximum speed is reduced by 0.00%
For rainfall of 5.00 mm/hour the maximum speed is reduced by 7.50%
For rainfall of 10.00 mm/hour the maximum speed is reduced by 15.00%
For rainfall of 15.00 mm/hour the maximum speed is reduced by 22.50%
For rainfall of 20.00 mm/hour the maximum speed is reduced by 30.00%
Pay attention to formatting. You can use the following values for your traffic and weather variable.
# Traffic variables:
distance_to_work = 20 # Distance to work in kilometers
v_max_clear = 60 # Max speed under clear conditions (km/h)
traffic_density = 30 # Current traffic density (vehicles/km)
jam_density = 150 # Jam density (vehicles/km)
# Weather variables:
max_speed_reduction = 0.3 # 30% speed reduction at max rainfall intensity
max_rainfall_intensity = 20 # Maximum expected rainfall intensity (mm/hour)
#List of rainfall intensities (mm/hour)
# For loop to calculate speed reduction factor given different rainfall intensities
Exercise 3.2.2 Calcuate reduction in travel time#
Based on different speed reduction factors, calculate different travel times accordingly.
Your output should look like this:
For rainfall of 0.0 mm/hour, the travel time is 25 minutes
For rainfall of 5.0 mm/hour, the travel time is 27 minutes
For rainfall of 10.0 mm/hour, the travel time is 29 minutes
For rainfall of 15.0 mm/hour, the travel time is 32 minutes
For rainfall of 20.0 mm/hour, the travel time is 36 minutes
Pay attention to formatting.
#For loop to calculate travel time based on different speed reduction factors
You might have noticed that solving a simple problem like this with for loop is tedious. Later we will learn NumPy that does array operations similar to MATLAB, which can solve this problem in a couple of lines
Exercise 3.3 - Functions#
Repeat Exercise 3.2 using functions. Write the function speed_reduction()
to calculate speed reduction factor, and the function time_reduction()
to calculate the travel time reduction. Your output should look like this:
For rainfall of 0.0 mm/hour the maximum speed is reduced by 0.00%
For rainfall of 0.0 mm/hour the travel time is 25 minutes
For rainfall of 5.0 mm/hour the maximum speed is reduced by 7.50%
For rainfall of 5.0 mm/hour the travel time is 27 minutes
...
… and so on.
# Add your two functions here
# Traffic variables:
distance_to_work = 20 # Distance to work in kilometers
v_max_clear = 60 # Max speed under clear conditions (km/h)
traffic_density = 30 # Current traffic density (vehicles/km)
jam_density = 150 # Jam density (vehicles/km)
# Weather variables:
max_speed_reduction = 0.3 # 30% speed reduction at max rainfall intensity
max_rainfall_intensity = 20 # Maximum expected rainfall intensity (mm/hour)
# Loop for different rainfall intensities (mm/hour)
Exercise 3.4 - Script file#
Repeat Exercise 3.3 by adding your two functions in a script file ‘Ex3.py’ and importing them to solve the problem.
# Traffic variables:
distance_to_work = 20 # Distance to work in kilometers
v_max_clear = 60 # Max speed under clear conditions (km/h)
traffic_density = 30 # Current traffic density (vehicles/km)
jam_density = 150 # Jam density (vehicles/km)
# Weather variables:
max_speed_reduction = 0.3 # 30% speed reduction at max rainfall intensity
max_rainfall_intensity = 20 # Maximum expected rainfall intensity (mm/hour)
#Import your functions or module here
# Loop for different rainfall intensities (mm/hour)