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"
if weather == "with rain":
v_max= 0.8 * v_max_clear
else:
v_max= v_max_clear
# Calculate speed using the Greenshields' model
speed = v_max * (1 - (traffic_density / jam_density))
# Calculate travel time (min)
travel_time = (distance_to_work / speed)*60
#print your result
print(f"Travel {weather} : {travel_time:.2f} minutes")
Travel without rain : 25.00 minutes
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
if israining:
v_max= v_max_clear * 0.8
weather ="with raining"
else:
v_max= v_max_clear
weather ="without raining"
# Calculate speed using the Greenshields' model
speed = v_max_clear * (1 - (traffic_density / jam_density))
# Calculate travel time (min)
travel_time = (distance_to_work / speed)*60
#Print your results
print(f"Travel {weather} : {travel_time:.2f} minutes")
Travel without raining : 25.00 minutes
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)
rainfall_intensities = [*range(0,25,5)]
# For loop to calculate speed reduction factor given different rainfall intensities
speed_reduction_factors = []
for rainfall_intensity in rainfall_intensities:
speed_reduction_factor = (rainfall_intensity / max_rainfall_intensity) * max_speed_reduction
speed_reduction_factors.append(speed_reduction_factor)
print(f"For rainfall of {rainfall_intensity:.1f} mm/hour \
the maximum speed is reduced by {speed_reduction_factor:.2%}")
For rainfall of 0.0 mm/hour the maximum speed is reduced by 0.00%
For rainfall of 5.0 mm/hour the maximum speed is reduced by 7.50%
For rainfall of 10.0 mm/hour the maximum speed is reduced by 15.00%
For rainfall of 15.0 mm/hour the maximum speed is reduced by 22.50%
For rainfall of 20.0 mm/hour the maximum speed is reduced by 30.00%
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
for speed_reduction_factor, rainfall_intensity in zip(speed_reduction_factors,rainfall_intensities):
#New maximum velocity
v_max= v_max_clear * (1-speed_reduction_factor)
# Calculate speed using the Greenshields' model
speed = v_max * (1 - (traffic_density / jam_density))
# Calculate travel time (min)
travel_time = (distance_to_work / speed)*60
#Print output
print(f"For rainfall of {rainfall_intensity:.1f} mm/hour \
the travel time is {travel_time:.0f} minutes")
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
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.
def speed_reduction(rainfall_intensity, max_rainfall_intensity, max_speed_reduction):
"""
Calculate speed reduction factor given rainfall intensity.
Args:
rainfall_intensity (float): The current rainfall intensity.
max_rainfall_intensity (float): The maximum rainfall intensity.
max_speed_reduction (float): The maximum speed reduction factor.
Returns:
float: The speed reduction factor calculated based on the given rainfall intensity.
"""
#Speed reduction factor given rainfall intensity
speed_reduction_factor= (rainfall_intensity / max_rainfall_intensity) * max_speed_reduction
return speed_reduction_factor
def time_reduction(speed_reduction_factor,rainfall_intensity,v_max_clear,
traffic_density, jam_density, distance_to_work):
"""
Calculate travel time based on a speed reduction factor.
Args:
speed_reduction_factor (float): The speed reduction factor.
rainfall_intensity (float): The current rainfall intensity.
v_max_clear (float): The maximum clear velocity.
traffic_density (float): The traffic density.
jam_density (float): The jam density.
distance_to_work (float): The distance to travel to work.
Returns:
float: The travel time calculated based on the given parameters.
"""
#New maximum velocity
v_max= v_max_clear * (1-speed_reduction_factor)
# Calculate speed using the Greenshields' model
speed = v_max * (1 - (traffic_density / jam_density))
# Calculate travel time (min)
travel_time = (distance_to_work / speed)*60
return travel_time
# 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)
for rainfall_intensity in range(0,25,5):
# Calculate speed reduction factor given rainfall intensity
speed_reduction_factor = speed_reduction(rainfall_intensity,
max_rainfall_intensity, max_speed_reduction)
# Calculate travel time based on a speed reduction factor
travel_time = time_reduction(speed_reduction_factor,rainfall_intensity,
v_max_clear, traffic_density, jam_density, distance_to_work)
# Print output
print(f"For rainfall of {rainfall_intensity:.1f} mm/hour \
the maximum speed is reduced by {speed_reduction_factor:.2%}")
print(f"For rainfall of {rainfall_intensity:.1f} mm/hour \
the travel time is {travel_time:.0f} minutes")
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
For rainfall of 10.0 mm/hour the maximum speed is reduced by 15.00%
For rainfall of 10.0 mm/hour the travel time is 29 minutes
For rainfall of 15.0 mm/hour the maximum speed is reduced by 22.50%
For rainfall of 15.0 mm/hour the travel time is 32 minutes
For rainfall of 20.0 mm/hour the maximum speed is reduced by 30.00%
For rainfall of 20.0 mm/hour the travel time is 36 minutes
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)
Solution 1#
#Import your functions here
from Ex3 import speed_reduction, time_reduction
# Loop for different rainfall intensities (mm/hour)
for rainfall_intensity in range(0,25,5):
# Calculate speed reduction factor given rainfall intensity
speed_reduction_factor = speed_reduction(rainfall_intensity,
max_rainfall_intensity, max_speed_reduction)
# Calculate travel time based on a speed reduction factor
travel_time = time_reduction(speed_reduction_factor,rainfall_intensity,
v_max_clear, traffic_density, jam_density, distance_to_work)
# Print output
print(f"For rainfall of {rainfall_intensity:.1f} mm/hour \
the maximum speed is reduced by {speed_reduction_factor:.2%}")
print(f"For rainfall of {rainfall_intensity:.1f} mm/hour \
the travel time is {travel_time:.0f} minutes")
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
For rainfall of 10.0 mm/hour the maximum speed is reduced by 15.00%
For rainfall of 10.0 mm/hour the travel time is 29 minutes
For rainfall of 15.0 mm/hour the maximum speed is reduced by 22.50%
For rainfall of 15.0 mm/hour the travel time is 32 minutes
For rainfall of 20.0 mm/hour the maximum speed is reduced by 30.00%
For rainfall of 20.0 mm/hour the travel time is 36 minutes
Solution 2#
#Import your module here
import Ex3
# Loop for different rainfall intensities (mm/hour)
for rainfall_intensity in range(0,25,5):
# Calculate speed reduction factor given rainfall intensity
speed_reduction_factor = Ex3.speed_reduction(rainfall_intensity,
max_rainfall_intensity, max_speed_reduction)
# Calculate travel time based on a speed reduction factor
travel_time = Ex3.time_reduction(speed_reduction_factor,rainfall_intensity,
v_max_clear, traffic_density, jam_density, distance_to_work)
# Print output
print(f"For rainfall of {rainfall_intensity:.1f} mm/hour \
the maximum speed is reduced by {speed_reduction_factor:.2%}")
print(f"For rainfall of {rainfall_intensity:.1f} mm/hour \
the travel time is {travel_time:.0f} minutes")
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
For rainfall of 10.0 mm/hour the maximum speed is reduced by 15.00%
For rainfall of 10.0 mm/hour the travel time is 29 minutes
For rainfall of 15.0 mm/hour the maximum speed is reduced by 22.50%
For rainfall of 15.0 mm/hour the travel time is 32 minutes
For rainfall of 20.0 mm/hour the maximum speed is reduced by 30.00%
For rainfall of 20.0 mm/hour the travel time is 36 minutes