Simulation has proven useful in studying all sorts of complicated situations. In this project you will use simulation to understand a model describing how vehicle traffic behaves.

Sometimes it can seem like traffic has no cause. A jam spontaneously appears in traffic, moves slowly backwards against the traffic, lasts a long time, and then simply disappears.

For what follows all distances will be measured in feet, all time intervals will be measured in seconds, and all speeds will be measured in feet per second. (Note: For reference 60 miles per hour is 88 feet per second.)

3 cars are on a one-lane infinite road where the speed limit is 88 feet per second. (Imagine the x-axis)

Set up: For each of 3 cars you need to keep track of 2 random variables: their placement on the road, and their speed.

Begin with car A at xA = 200, car B at xB = 100, and car C at xC = 0. At the start all of the cars are moving 80 feet per second.

Imagine each second the drivers do the following:

–If they are not at the maximum speed, they increase their speed by one f=s with probability pi. To simulate different driving styles, each driver will have a diferent value for this probability.

–Then, once all the drivers have updated their speeds, if the distance between them and the car in front of them is smaller than their current velocity, the velocity is reduced to that number to avoid a collision. For example, if the velocity of car B is now 50 feet per second, but there are only 45 feet in front of it before car A, then car B’s velocity is reduced to 45 feet per second.

–To add some randomness to how account for how people actually drive, as long as you are moving (your speed is at least 1f=s), you decrease your speed by 1f=s with probability qi.

–Finally, to make all of these simulations easier, imagine that the cars move in discrete distances at the end of each second. The cars are moved forward the number of feet equal to their velocity. For example, if the velocity is 20 f=s, the car is moved forward 20 feet at the end of that second.

Write an R script that will capture the speeds and distances of the cars for each of the 6000 seconds (100 minutes) based on the parameters:

–xA = 200, xB = 100, xC = 0

–vA = 80, vB = 80, vC = 80

–pA = :8, pB = :5, pC = :5

–qA = :5, qB = :5, qC = :5

–vMax = 88

speedA=c()
speedC=c()

for(k in 1:1)
{
vA=80
vB=80
vC=80
xA=200
xB=100
xC=0
dB=xA-xB
dC=xB-xC

AllxA=c()
AllxB=c()
AllxC=c()
AllvA=c()
AllvB=c()
AllvC=c()
AlldB=c()
AlldC=c()

for(i in 1:6000)
{
if(vA<88)(vA=vA+rbinom(1,1,.8))
if(vB<88)(vB=vB+rbinom(1,1,.5))
if(vC<88)(vC=vC+rbinom(1,1,.5))
if(dB<vB)(vB=dB)
if(dC<vC)(vC=dC)
if(vA>0)(vA=vA-rbinom(1,1,.5))
if(vB>0)(vB=vB-rbinom(1,1,.5))
if(vC>0)(vC=vC-rbinom(1,1,.5))
xA=xA+vA
xB=xB+vB
xC=xC+vC
dB=xA-xB
dC=xB-xC

AllxA=c(AllxA,xA)
AllxB=c(AllxB,xB)
AllxC=c(AllxC,xC)
AllvA=c(AllvA,vA)
AllvB=c(AllvB,vB)
AllvC=c(AllvC,vC)
AlldB=c(AlldB,dB)
AlldC=c(AlldC,dC)

}
speedA=c(speedA,AllvA)
speedC=c(speedC,AllvC)
}

1. Make a scatter plot that shows Car A’s speed as time changes.

2. Make a scatter plot that shows Car C’s speed as time changes.

3. We would like to have a guess of the average speed of the cars. To do this, run your code 100 times, and nd the total distance that each car travels in 6000 seconds. NOTE: This took around 2 minutes of computing time on my computer.

speedA=c()
speedB=c()
speedC=c()

for(k in 1:100)
{
vA=80
vB=80
vC=80
xA=200
xB=100
xC=0
dB=xA-xB
dC=xB-xC


AllxA=c()
AllxB=c()
AllxC=c()
AllvA=c()
AllvB=c()
AllvC=c()
AlldB=c()
AlldC=c()
for(i in 1:6000)
{
if(vA<88)(vA=vA+rbinom(1,1,.8))
if(vB<88)(vB=vB+rbinom(1,1,.5))
if(vC<88)(vC=vC+rbinom(1,1,.5))
if(dB<vB)(vB=dB)
if(dC<vC)(vC=dC)
if(vA>0)(vA=vA-rbinom(1,1,.5))
if(vB>0)(vB=vB-rbinom(1,1,.5))
if(vC>0)(vC=vC-rbinom(1,1,.5))
xA=xA+vA
xB=xB+vB
xC=xC+vC
dB=xA-xB
dC=xB-xC

AllxA=c(AllxA,xA)
AllxB=c(AllxB,xB)
AllxC=c(AllxC,xC)
AllvA=c(AllvA,vA)
AllvB=c(AllvB,vB)
AllvC=c(AllvC,vC)
AlldB=c(AlldB,dB)
AlldC=c(AlldC,dC)

}
speedA=c(speedA,mean(AllvA))
speedB=c(speedB,mean(AllvB))
speedC=c(speedC,mean(AllvC))
}
mean(speedA)
## [1] 87.15474
mean(speedB)
## [1] 60.60247
mean(speedC)
## [1] 44.74008
(a) What was the smallest average speed for car B in your 100 simulations?
min(speedB)
## [1] 22.811
(b) What was the largest average speed for car B in your 100 simulations?
max(speedB)
## [1] 79.25367
(c) Include a scatter plot of the 100 average speeds for car B.
plot(speedB)

4. What do you think will happen if you change pA to…

(a) .6?

As the probability of A decreases, the chance that Car A increases in speed decreases. This means the average speed that Car A is traveling will decrease, resulting in the the average speed of all the cars decreasing.

(b) .4?

As the probability of A decreases, the chance that Car A increases in speed decreases. This means the average speed that Car A is traveling will decrease, resulting in the the average speed of all the cars decreasing.

5. Run 100 simulations again with pA = :6. What are the average speeds of the cars over these 100 simulations?

speedA=c()
speedB=c()
speedC=c()

for(k in 1:100)
{
vA=80
vB=80
vC=80
xA=200
xB=100
xC=0
dB=xA-xB
dC=xB-xC


AllxA=c()
AllxB=c()
AllxC=c()
AllvA=c()
AllvB=c()
AllvC=c()
AlldB=c()
AlldC=c()
for(i in 1:6000)
{
if(vA<88)(vA=vA+rbinom(1,1,.6))
if(vB<88)(vB=vB+rbinom(1,1,.5))
if(vC<88)(vC=vC+rbinom(1,1,.5))
if(dB<vB)(vB=dB)
if(dC<vC)(vC=dC)
if(vA>0)(vA=vA-rbinom(1,1,.5))
if(vB>0)(vB=vB-rbinom(1,1,.5))
if(vC>0)(vC=vC-rbinom(1,1,.5))
xA=xA+vA
xB=xB+vB
xC=xC+vC
dB=xA-xB
dC=xB-xC

AllxA=c(AllxA,xA)
AllxB=c(AllxB,xB)
AllxC=c(AllxC,xC)
AllvA=c(AllvA,vA)
AllvB=c(AllvB,vB)
AllvC=c(AllvC,vC)
AlldB=c(AlldB,dB)
AlldC=c(AlldC,dC)

}
speedA=c(speedA,mean(AllvA))
speedB=c(speedB,mean(AllvB))
speedC=c(speedC,mean(AllvC))
}
mean(speedA)
## [1] 85.54734
mean(speedB)
## [1] 58.91461
mean(speedC)
## [1] 43.00032

6. Repeat a simulation one time where something has been changed from the previous setup.

(a)
speedA=c()
speedB=c()
speedC=c()

for(k in 1:1)
{
vA=80
vB=80
vC=80
xA=200
xB=100
xC=0
dB=xA-xB
dC=xB-xC


AllxA=c()
AllxB=c()
AllxC=c()
AllvA=c()
AllvB=c()
AllvC=c()
AlldB=c()
AlldC=c()
for(i in 1:6000)
{
if(vA<44)(vA=vA+rbinom(1,1,.8))
if(vB<44)(vB=vB+rbinom(1,1,.5))
if(vC<44)(vC=vC+rbinom(1,1,.5))
if(dB<vB)(vB=dB)
if(dC<vC)(vC=dC)
if(vA>0)(vA=vA-rbinom(1,1,.5))
if(vB>0)(vB=vB-rbinom(1,1,.5))
if(vC>0)(vC=vC-rbinom(1,1,.5))
xA=xA+vA
xB=xB+vB
xC=xC+vC
dB=xA-xB
dC=xB-xC

AllxA=c(AllxA,xA)
AllxB=c(AllxB,xB)
AllxC=c(AllxC,xC)
AllvA=c(AllvA,vA)
AllvB=c(AllvB,vB)
AllvC=c(AllvC,vC)
AlldB=c(AlldB,dB)
AlldC=c(AlldC,dC)

}
speedA=c(speedA,AllvA)
speedB=c(speedB,AllvB)
speedC=c(speedC,AllvC)
}
mean(speedA)
## [1] 43.38783
mean(speedB)
## [1] 22.02667
mean(speedC)
## [1] 15.61633

Since all the vehicles are initially traveling at a speed greater than the maximum allowed, the only option they will have is to slow down at a pace equal to 1 ft/s with probability qA=qB=qC=.5

(b)
speedA=c()
speedB=c()
speedC=c()

for(k in 1:1)
{
vA=80
vB=80
vC=80
xA=10
xB=5
xC=0
dB=xA-xB
dC=xB-xC


AllxA=c()
AllxB=c()
AllxC=c()
AllvA=c()
AllvB=c()
AllvC=c()
AlldB=c()
AlldC=c()
for(i in 1:6000)
{
if(vA<88)(vA=vA+rbinom(1,1,.8))
if(vB<88)(vB=vB+rbinom(1,1,.5))
if(vC<88)(vC=vC+rbinom(1,1,.5))
if(dB<vB)(vB=dB)
if(dC<vC)(vC=dC)
if(vA>0)(vA=vA-rbinom(1,1,.5))
if(vB>0)(vB=vB-rbinom(1,1,.5))
if(vC>0)(vC=vC-rbinom(1,1,.5))
xA=xA+vA
xB=xB+vB
xC=xC+vC
dB=xA-xB
dC=xB-xC

AllxA=c(AllxA,xA)
AllxB=c(AllxB,xB)
AllxC=c(AllxC,xC)
AllvA=c(AllvA,vA)
AllvB=c(AllvB,vB)
AllvC=c(AllvC,vC)
AlldB=c(AlldB,dB)
AlldC=c(AlldC,dC)

}
speedA=c(speedA,AllvA)
speedB=c(speedB,AllvB)
speedC=c(speedC,AllvC)
}
mean(speedA)
## [1] 87.14983
mean(speedB)
## [1] 18.54567
mean(speedC)
## [1] 14.22733

When all the cars are initially traveling much closer to each other, car A will be unaffected because its speed is independent of the position of the other vehicles. Car B and C, however, will start at 80 ft/s but instantly be decelerated to a speed equal to the distance between them. Car B and C will both be travelling at a speed of 5 ft/s, resulting in car A getting extremely far ahead. The average speed of the B and C will be considerably lower than the average speed of car A since they are travelling much slower and have an equal chance of slowing down as they do speeding up.

(c)
speedA=c()
speedB=c()
speedC=c()

for(k in 1:1)
{
vA=0
vB=0
vC=0
xA=200
xB=100
xC=0
dB=xA-xB
dC=xB-xC


AllxA=c()
AllxB=c()
AllxC=c()
AllvA=c()
AllvB=c()
AllvC=c()
AlldB=c()
AlldC=c()
for(i in 1:6000)
{
if(vA<88)(vA=vA+rbinom(1,1,.8))
if(vB<88)(vB=vB+rbinom(1,1,.5))
if(vC<88)(vC=vC+rbinom(1,1,.5))
if(dB<vB)(vB=dB)
if(dC<vC)(vC=dC)
if(vA>0)(vA=vA-rbinom(1,1,.5))
if(vB>0)(vB=vB-rbinom(1,1,.5))
if(vC>0)(vC=vC-rbinom(1,1,.5))
xA=xA+vA
xB=xB+vB
xC=xC+vC
dB=xA-xB
dC=xB-xC

AllxA=c(AllxA,xA)
AllxB=c(AllxB,xB)
AllxC=c(AllxC,xC)
AllvA=c(AllvA,vA)
AllvB=c(AllvB,vB)
AllvC=c(AllvC,vC)
AlldB=c(AlldB,dB)
AlldC=c(AlldC,dC)

}
speedA=c(speedA,AllvA)
speedB=c(speedB,AllvB)
speedC=c(speedC,AllvC)
}
mean(speedA)
## [1] 84.96433
mean(speedB)
## [1] 12.08617
mean(speedC)
## [1] 11.87483

When the initial speed of all vehicles are 0, car A will again end up pulling ahead because it has a greater chance of speeding up and is unaffected by the cars behind it. The average speed of car B and C will be much lower than A because once B and C reach a speed above 0 ft/s there is an equal chance the car speeds up or slows down.

(d)
speedA=c()
speedB=c()
speedC=c()
speedD=c()
speedE=c()
speedF=c()

for(k in 1:1)
{
vA=80
vB=80
vC=80
vD=80
vE=80
vF=80
xA=200
xB=100
xC=0
xD=-100
xE=-200
xF=-300
dB=xA-xB
dC=xB-xC
dD=xC-xD
dE=xD-xE
dF=xE-xF


AllxA=c()
AllxB=c()
AllxC=c()
AllxD=c()
AllxE=c()
AllxF=c()
AllvA=c()
AllvB=c()
AllvC=c()
AllvD=c()
AllvE=c()
AllvF=c()
AlldB=c()
AlldC=c()
AlldD=c()
AlldE=c()
AlldF=c()
for(i in 1:6000)
{
if(vA<88)(vA=vA+rbinom(1,1,.8))
if(vB<88)(vB=vB+rbinom(1,1,.5))
if(vC<88)(vC=vC+rbinom(1,1,.5))
if(vD<88)(vD=vD+rbinom(1,1,.5))
if(vE<88)(vE=vE+rbinom(1,1,.5))
if(vF<88)(vF=vF+rbinom(1,1,.5))
if(dB<vB)(vB=dB)
if(dC<vC)(vC=dC)
if(dD<vD)(vD=dD)
if(dE<vE)(vE=dE)
if(dF<vF)(vF=dF)
if(vA>0)(vA=vA-rbinom(1,1,.5))
if(vB>0)(vB=vB-rbinom(1,1,.5))
if(vC>0)(vC=vC-rbinom(1,1,.5))
if(vD>0)(vD=vD-rbinom(1,1,.5))
if(vE>0)(vE=vE-rbinom(1,1,.5))
if(vF>0)(vF=vF-rbinom(1,1,.5))
xA=xA+vA
xB=xB+vB
xC=xC+vC
xD=xD+vD
xE=xE+vE
xF=xF+vF
dB=xA-xB
dC=xB-xC
dD=xC-xD
dE=xD-xE
dF=xE-xF

AllxA=c(AllxA,xA)
AllxB=c(AllxB,xB)
AllxC=c(AllxC,xC)
AllxD=c(AllxD,xD)
AllxE=c(AllxE,xE)
AllxF=c(AllxF,xF)
AllvA=c(AllvA,vA)
AllvB=c(AllvB,vB)
AllvC=c(AllvC,vC)
AllvD=c(AllvD,vD)
AllvE=c(AllvE,vE)
AllvF=c(AllvF,vF)
AlldB=c(AlldB,dB)
AlldC=c(AlldC,dC)
AlldD=c(AlldD,dD)
AlldE=c(AlldE,dE)
AlldF=c(AlldF,dF)

}
speedA=c(speedA,AllvA)
speedB=c(speedB,AllvB)
speedC=c(speedC,AllvC)
speedD=c(speedD,AllvD)
speedE=c(speedE,AllvE)
speedF=c(speedF,AllvF)
}
mean(speedA)
## [1] 87.1585
mean(speedB)
## [1] 49.74283
mean(speedC)
## [1] 34.41567
mean(speedD)
## [1] 34.42333
mean(speedE)
## [1] 32.152
mean(speedF)
## [1] 20.89817

When the amount of cars were increased from 3 to 6, the cars were still spaced 100 ft from each other and none of the other parameters such as speed or the probabilities were changed. The only exception was the probability of car A increasing was higher than the rest at 0.8 as compared to 0.5. This resulted in car A being on average the fastest and the rest of the cars behind A having slower average velocities in the order in which they started. This is a trend which could be predicted knowing the probabilities of all the cars behind car A, are increasing and decreasing at the same rate.