WR Long MS325 TMA 04Question 3# Part (a)restart:with(plots):with(Statistics):# Functions needed from unitsdrive := (pl,sl,len) -> (pl+sl) mod len:accelerate := proc(sl,vmax)
local n;
[seq(min(sl[n]+1,vmax),n=1..nops(sl))];
end proc:brake := proc(pl,sl,len)
local n;
[seq(min(sl[n],(pl[n+1]-pl[n]-1) mod len),n=1..nops(sl)-1),
min(sl[-1],(pl[1]-pl[-1]-1) mod len)]
end proc:dawdle := proc(sl,rs)
local n;
[seq(max(sl[n]-round(rs[n]),0),n=1..nops(sl))];
end proc:carplot := n -> plot([seq([pos[t,n],t],t=1..nops(pos))],style=point,symbolsize=10):
carline := n -> plot([seq([pos[t,n],t],t=1..nops(pos))],style=line,colour=black,thickness=1):############## Pelican Function ################pelican := proc(pl,sl,len)
# reduces speed of all cars to prevent any car crossing
# the pelican at cell n=0.So simply reduce the speed of
# any car by max of 0 and (position+speed-track length+1)
local n;
[seq(sl[n]-max(pl[n]+sl[n]-len+1,0),n=1..nops(sl))];
end proc:# Test pelican functionpelican([2,5,8],[3,3,3],10);pelican([2,5,8,13,18],[4,4,4,4,4],20);pelican([0,1,2,3,4],[4,4,4,4,4],20);pelican([19,17,11,7,0],[4,4,4,4,4],20);pelican([19,18,16,15,14],[4,4,4,4,4],20);pelican([15,16,17,18,19],[4,4,4,4,4],20);#as expected# Part (b)pelicantraffic := proc(len,cars,vmax,dawprob,nstep)
local pl,sl,i,j,n,rsl,rs;
uses Statistics;
if cars>len then
return("error - too many cars");
end if;
pl := [[seq(trunc(n*len/cars),n=0..cars-1)]];
sl := [0$cars];
rsl := Sample(Bernoulli(dawprob),nstep*cars);
for i to nstep do
rs := [seq(rsl[j],j=(i-1)*cars+1..i*cars)];
sl := accelerate(sl,vmax);
sl := pelican(pl[-1],sl,len);
sl := brake(pl[-1],sl,len);
sl := dawdle(sl,rs);
# The next line can replace the preceeding 4, but
# is not quite so self-documenting.
# sl := dawdle(brake(pl[-1],pelican(pl[-1],accelerate(sl,vmax),len),len),rs);
pl := [op(pl),drive(pl[-1],sl,len)];
end do;
pl;
end proc:pos := pelicantraffic(20,5,5,0.1,10):
display(seq(carplot(n),n=1..5),carline(2));#Each single car drives, accelerates and brakes according to the usual rules but upon reaching position 19 (as per the pelican rule) or there is a stationary car in the adjacent position, proceeds no further. Since no overtaking is possible the first car to reach this position is the 5th one. Once the fourth car reaches position 18, it can proceed no further. Thus a queue builds up. Eventually all the cars are queued up at the pelican, occupying postions, 19, 18, 17, 16 and 15 and none of the cars can preceed further. Occasionally, before reaching position 19 the dawdle probability results in a car slowing down.# Part (c)pelicantraffic1 := proc(len,cars,vmax,dawprob,nstep,pedprob,redtime)
local pl,sl,i,j,n,rsl,rs,redlight,pedlist;
uses Statistics;
if cars>len then
return("error - too many cars");
end if;
pl := [[seq(trunc(n*len/cars),n=0..cars-1)]];
sl := [0$cars];
rsl := Sample(Bernoulli(dawprob),nstep*cars); #rvs for dawdle
pedlist := Sample(Bernoulli(pedprob),nstep); #rvs for arrival of peds
redlight:=0; #traffic light shows green initially.
for i to nstep do
rs := [seq(rsl[j],j=(i-1)*cars+1..i*cars)];
sl :=accelerate(sl,vmax);
if redlight = 0 then
if pedlist[i] = 1 then
redlight := redtime;
print(i);
end if;
end if;
if redlight>0 then
sl :=pelican(pl[-1],sl,len);
redlight := redlight-1;
end if;
sl := brake(pl[-1],sl,len);
sl := dawdle(sl,rs);
pl := [op(pl),drive(pl[-1],sl,len)];
end do;
pl;
end proc:pos := pelicantraffic1(20,5,5,0.1,20,0.2,3):
display(seq(carplot(n),n=1..5),carline(2));#Evidently, no cars cross a red light since at each of the four instances printed, in the following 3 cycles, no car passes across from the far right to the left of the plot, while any car can cross while the red light is not showing.# Part (d)# First with pedprob=0.2pos := pelicantraffic1(500,100,5,0.1,500,0.2,3):
display(seq(carplot(n),n=1..100),carline(1));# Now with pedprob=0.1pos := pelicantraffic1(500,100,5,0.1,500,0.1,3):
display(seq(carplot(n),n=1..100),carline(1));# Now with pedprob=0.01pos := pelicantraffic1(500,100,5,0.1,500,0.01,3):
display(seq(carplot(n),n=1..100),carline(1));# Comparisons and Interpretation: For smaller values of pedprob, the lights changes to red less often, which results in less queuing and so car 1 is able to make more complete circuits of the track as shown by the black carline plot. With pedprob=0.2, only 1 full cycle is made, while at 0.1, 2 full cycles are made, and at 0.01 3 full cycles are made. Moreover, with the larger probabilities, the queuing at the lights is clearly seen in the steeper angle of the carline plot for car 1 on the right side of the plots - indicating slow speeds. With probability 0.01, even though the lights change 6 times (in this particular realisation) the progreession appears largely unaffected by it - indeed the speed of car 1 while approaching the pelican is not curtailed. The unevennes of the carline plot in this case is due to the dawdling probability causing temporary traffic jams/slowdowns.