Steady-States of the One-Lane Roundabout

Embed Size (px)

Citation preview

  • 8/11/2019 Steady-States of the One-Lane Roundabout

    1/29

    Steady-States of the One-Lane Roundabout

    Introduction to Computer Simulations

    Abhi Agarwal & Philip Ottesen

    1 Introduction

    In this simulation, we examine the behaviour of stable and unstable pointsin the one-lane roundabout model. We solve a differential equation in theaverage distance to the car ahead, and present a graphical argument forstable vs. unstable points in the model. The final state of the program isshown for cases where we have multiple steady-states. An analytical solu-tion to the tangential case with one steady-state is given. Furthermore, weevaluate approximations such as V(d) v, where d and v are cumulative

    means of distance to the car ahead and velocity of a car given that distance,computed and updated at each time-step. We show that the error increasesfor increasing simulated time, but remains reasonable in the range employedwhen investigating the relationship between stable and unstable points tothe behaviour of the system. For two steady-states, we show a bias towardsthe stable point and present a correction to this bias by changing initial con-ditions of the program. By using the bisection method, we find a sufficientaverage distance to the car ahead such that, for multiple runs of the simula-tion, the steady-state behaviour of the model fluctuates between congealedand uncongealed states.

    2 Theory

    Since the velocity of each car depends upon the velocity of the car ahead, wedefine the velocity function as follows.

    1

  • 8/11/2019 Steady-States of the One-Lane Roundabout

    2/29

    V(d) =vmaxlog( d

    dmin)

    log( dmaxdmin

    )

    LetVdenote the velocity of a car given some distanced to the car ahead.To find the point of tangency , we set the time-derivative of the velocityfunction equal to the slope of a straight line, V

    d. Then,

    dVdd

    = vmaxlog( dmax

    dmin)

    dmind

    1dmin

    = Vd

    Solving forV, we find that dcancels, yielding

    V = vmax

    log(

    dmax

    dmin )

    However, recall that this solution for V must equal V(d). Hence,

    vmax

    log( dmaxdmin

    )=vmax

    log( ddmin

    )

    log( dmaxdmin

    )

    This implies that log( ddmin

    ) = 1 or d = e dmin. Hence, the slope of

    our straight line must be V(edmin)edmin

    , and = (e dmin, V(edmin)). Since

    the slope of the line is Rp

    d, we have that Rp

    = V(edmin)edmin

    . We use this analyti-cal solution forto demonstrate the case where there is only one stable point.

    To observe the tendency of the system towards the theoretical stablepoints on the plot of R

    pd and V(d), we plot the point (d, V(d)) on the plot.

    This allows us to show that the stable points are approximate, and the sys-tem may tend to move near them but will not be exactly equal. We also plotthe average distance to the car ahead and the average velocity. A stable statewill then be shown as a somewhat constant rate of change ofd and v. Notethat v is computed and updated for each time-step, whereas V(d) simplytakes the average distance and returns the corresponding velocity. We willcompute the percentage difference in each trial ofV(d) and vto estimate thevalidity of this approximation. For cases where the system reaches a stable-

    state, we will also show whether the final dis close to thed component of thecorresponding intersection point. For an uncongealed steady-state, we wouldexpect dto be approximately equal to the right-most point of intersection.

    The steady-state behaviour of the system, if no stochasticity were intro-

    2

  • 8/11/2019 Steady-States of the One-Lane Roundabout

    3/29

  • 8/11/2019 Steady-States of the One-Lane Roundabout

    4/29

    3.1.1 k= 2

    (a) Shows the circular traffic with

    R= 0.5, p= 0.34

    (b) Blue - average distance to the car

    ahead, Green - average velocity

    Figure 1

    (a) Average number of cars (b) Shows that it is a two-intersection

    case

    Figure 2

    Figure 1a indicates that the stable-state observed at the end of the simula-tion reflects uncongealed traffic, with blocks of varying traffic density, butrelatively high davg as compared to the next case of less free-flowing traffic.

    Red circles indicate an entry-point into the system, where a car may enteror exit. Since entry points are discretized in this manner, congealed traffictends to become more congealed since it takes longer for cars to approach anentry point, but new cars may spawn at any time-step independent of carson the roundabout. Therefore, we say that once a state of congealed traffic

    4

  • 8/11/2019 Steady-States of the One-Lane Roundabout

    5/29

    is reached, the time required for an epoch of negligible entries compared to

    exits is larger than the time considered in these simulations, since the proba-bility for this to happen k times is pk, giving a very small probability. Aftert = 150 minutes, we have a total of 79 cars on the roundabout. Figure 1bshows that the period between approx. 60 to 80 minutes has a minimum rateof change of N. The system oscillates between periods of high dN

    dt, as seen

    in the period 45 to 55 minutes and 95 to 105 minutes. However, the dura-tion of these periods seem to decrease over time for the uncongealed case.Indeed, despite the fluctuations in N, figure 2b shows that after approx. 45minutes, average distance between cars settles down. d oscillates between[0.1136, 0.1961], with an average across the range ofdavg = 0.1482.

    In figure 2a, we have two points of intersection. The first,P1= (0.0110, 0.0162),and the second at P2 = (0.1610, 0.2367). Note that the point denotedby the diamond is the point R = (d, v), where d and v are cumulativemeans computed and updated for each time-step. From figure 4, R =(0.12658, 0.20177), a distance 0.04538 away from P2. We find that davg lies

    within |0.1482P2,x

    P2,x= 0.01280.1610 = 0.0795 or a difference of 7.95% from the average

    distance between cars for the uncongealed state. As stated in Theory, weexpect v v(d). For this trial, we found that v = 0.2018 miles per hour,and v(d) =v(0.2163) = 0.2163 miles per hour, giving a percent difference of7.19%. It can be shown that with a smaller t, the percentage differences are

    smaller. We use t = 50 minfor the next two cases, but t = 150 min was usedhere to emphasize that the number of cars over time was indeed stable.

    5

  • 8/11/2019 Steady-States of the One-Lane Roundabout

    6/29

    3.1.2 k= 1

    (a) Shows the circular traffic with

    R= 1.06468, p= 0.34

    (b) Average number of cars

    Figure 3

    (a) Shows that it is a tangent case (b) Blue - average distance to the car

    ahead, Green - average velocity

    Figure 4

    Figure 3a shows the final state of the system after t= 50 minutes of simu-lated time, with 196 cars on the road. Recall that the initial number of carsN0 = 0 here. The number of cars on the road for the same time t as the pre-

    vious case is larger fork

    = 1 thank

    = 2. This relates to the physical meaningof the slope Rp

    . For Rdt >> p, we have more cars entering the system thanexiting at any given time-step. Hence, we expect more cars to appear onthe road compared to the previous case for an equal time-interval. Figure3b indicates that the rate of change of the number of cars on the road falls

    6

  • 8/11/2019 Steady-States of the One-Lane Roundabout

    7/29

    off with time, anticipating a steady-state with respect to the number of cars

    entering and exiting the roundabout at each time-step. However, althoughthe apparent trend is a negative second derivative, this need not be the caseast grows. Later, we study a case where steady-states will oscillate, althoughwe focus on demonstrating that such an oscillation will occur at least once.From figure 4b, the average distance between cars remains relatively stablein the range [5, 50] minutes. Recall that the green line indicates the averagevelocity of cars. Using the approach outlined in Theory, we are able to gen-erate the tangent case, where P = (edmin, v(edmin)). As expected, thepoint lies near P.

    vafter the final time-step was found to be 0.1343 miles per hour, with d=

    0.0510. As shown in previous trials for varyingk,vavg agrees well withv(d) =0.1389 miles per hour, with a percentage difference of 3.39%. Interestingly,for a higher R

    p, the percentage difference between the two quantities is less

    than or comparable to the percentage difference for a lower Rp

    , as seen in theprevious case.

    3.1.3 k= 0

    (a) Shows the circular traffic withR= 5.0, p= 0.34

    (b) Shows that it is a zero-intersectioncase

    Figure 5

    7

  • 8/11/2019 Steady-States of the One-Lane Roundabout

    8/29

    (a) Average number of cars (b) Blue - average distance to the car

    ahead, Green - average velocity

    Figure 6

    With no stable-points for this plot, we expect to see congealed traffic. Fromfigure 5a, we find that with R = and p =, traffic along the roundaboutcomes to a full-stop. Figure 6b indicates that the average distance betweencars falls to 0 approx. 10 minutes into the trial. Figure 6a identifies theconsequences of congealed traffic - as opposed to earlier cases, where wesee epochs of varying dN

    dt , the number of cars grows approx. linearly with

    time. Lastly, figure 5b places the current state of the system identified byR= (0.0039746, 1.0452E 14) very close to the origin (0, 0), but with an x-position less thandmin = 0.01 miles. SinceRdt >> p, the expected number of

    cars exiting the system per time-step is negligible compared to the expectednumber of cars entering the system. As before, we compare v and v(d). Asexpected, v = 0 and v(d) = v(0.004) = 0 miles per hour, since d < dminforces a cars to remain at rest.

    3.2 N0 >0

    In this case were exporing when we have an N0 number of cars placed onthe roundabout before the traffic has started. We are beginning with N carsequidistant from each other placed on the road. We first need to look athow we figure out the N0, which is the initial number of cars placed on the

    roundabout. Using a Matlab Library, Intersection, we were able to figure outthe x-coordinate of the first intersection, and let us call this m. We use thefloor function on the total length of the roundabout divided by m, whichis NbL

    m . The explaination of this is we can say m is the unstable point, and

    8

  • 8/11/2019 Steady-States of the One-Lane Roundabout

    9/29

    length of roundaboutm

    would give us a value for the equadistant length between

    each car.For the first study we looked to find cases where we fix the ratio of Rand p, and the ratio has two points of intersections. We started with a casewhere N0 was zero. We ran that same case multiple times and we expectedto find that there in some of the runs it should lead to congealed traffic, andin the other it should lead to uncongealed traffic.

    However, we ran into a difficulty. There is a bias that is introduced whenN0 is zero as the diamond point is initially on the right as there is a highvelocity and distance between each car, because of that fact that there isonly one car. Therefore, we needed a way to control the diamond point, andplace it on the curve by varying Rand p. We introduced N0 number of cars

    on the road in the next few runs of the simulation. Below you can see a casegenerated when we have a N0 number of cars on the road.

    (a) Blue - average distance to the carahead, Green - average velocity

    (b) Shows that it is a two-intersectioncase

    Figure 7

    In this particular case we set Rto 0.5, and pto 0.34. However, we foundno luck as we ran this particular case multiple times. The diamond pointwould always tend towards the stable point. Now we tried to experimentby altering the values of N0, and reached a conclusion that there was aparticular distance between cars that would lead to a reduction in the bias,

    and therefore allowing the simulation to go either towards a congealed stateor towards a uncongealed state. We used the principles of binary search totry and find bounds on the values so we are able to use an analytical solutionrather than a brute-force method.

    9

  • 8/11/2019 Steady-States of the One-Lane Roundabout

    10/29

    Our methodology was to search between the values of an upper bound

    and a lower bound. The lower bound was the case where they intersected justright of the tangent case, and the upper bound was the intersection that wasat the right-most hand on the graph. We recoreded these values and usedthe binary search principle to find the value. We were eventually able to findthe value where on multiple-runs the outcome would be different states fora particular value of R and p. This particular value wasR = 0.78234, andp= 0.34. I have included the graphs below:

    (a) Blue - average distance to the carahead, Green - average velocity

    (b) Shows that it is a two-intersectioncase

    Figure 8

    (a) Blue - average distance to the carahead, Green - average velocity

    (b) Shows that it is a two-intersectioncase

    Figure 9

    The graphs above show the case where it leads to a congealed state, anda uncongealed state. They were repeated on the same values ofRand p.

    10

  • 8/11/2019 Steady-States of the One-Lane Roundabout

    11/29

    4 Conclusion and Future Work

    In conclusion, we have shown that the approximations of the model holdwhile asymptotic stability with noise allows for the possibility of fluctuatingstates between runs. For k = 0, we saw congealed traffic as expected, with alinearly increasing rate of change of the number of cars on the road. With in-creasingk , this behaviour tended towards congealed traffic states, and a biasin the steady-state behaviour of the model was pointed out. WhenN0 = 0,we showed that the current state of the system always approached the sec-ond intersection. For k = 2, this restricts the order of states reached by thesystem. Recall that the current state was represented by a point P = (d, v),where dand vare means ofd and v for all cars on the road, updated at each

    time-step. With N0 > 0, we were able to place this point at the unstablepoint using the bisection method. Thus, the bias towards the uncongealedstable point was removed to some extent, where the equidistant spacing be-tween cars was fine-tuned to a fixed Rand p.

    In the future, we would like to improve and build upon the model we haveestablished in this project.

    Firstly, one thing we were not able to study in depth was to find a partic-ular ratio ofR and p that would show state-switching explicitly. The stateswitch we modelled was measuring the different states in different runs, butthere can also exist a ratio ofR and p such that the states can switch duringa single run of the program.

    In addition, we also wanted to study the environmental factors that af-fected traffic. Circular highways are an interesting case in environmentalfactors such as rain. Because there is traffic coming in from multiple sidesthey can create massive traffic jams if not handled properly. For cases suchas these we are also able to introduce variable entry/exit rates so were ableto control traffic jams in streets coming in and leaving the circular highway.

    Secondly, it might be useful to run a Monte Carlo simulation on theN0 > 0 case for values of equidistant spacing that allow for the variablebehaviour in steady states between runs. Using such a method, one can

    determine which state the model tends towards for a given set of parameters,including R and p, when N0 > 0, and perform some statistical analysis onthe bias observed empirically.

    Lastly, we are also about to connect the circular highway to junctions inthe city, and study cases where circular highways lead to traffic jams, and

    11

  • 8/11/2019 Steady-States of the One-Lane Roundabout

    12/29

    cases where we see uncongealed traffic. We can use the grid based traffic

    system, and see the changes that occur when there is a circular highway, andwhen there is not.

    12

  • 8/11/2019 Steady-States of the One-Lane Roundabout

    13/29

    5 References

    The Matlab file intersections.mwas imported from the URL below.

    http://www.mathworks.com/matlabcentral/fileexchange/11837-fast-and-robust-curve-intersections/content/intersections.m

    vcar.mand circle.mwere adapted from Chengcheng Huangs recitation noteson the One-Lane roundabout.

    6 Appendix A: circle.m

    1

    2 % c i r c u l a r b lo ck s , c a rs not i n o rd er3

    4 c l e a r a l l5 c l o s e a l l6

    7 g l o b a l dmin dmax vmax ;8

    9 % i n i t i a l i z a t i o n10 n b l o c k s = 1 0 ;11 L = 1 ; % m i l e % L>dmax12

    13 % R = 0 . 5 , p = 0 . 5 > 2 SS14 % R = 0 . 5 6 , p = 0 . 5 > 2 SS15 % R = 1 . 3 , p = 0 . 5 > 2 v ery c l o s e SS16 % R = 1 . 25 ( x = 0 . 0 15 , y = 0 . 0 37 5 ) , 1 ( x =0 .0 6 , y =0 .1 5)17 % R = 1 . 2 , p = 0 . 5 > 2 maybe c l o s e ( x = 0. 02 25 , y

    = 0. 05 4) 1 ( x = 0. 06 75 , y = 0. 16 2) 218 % R = 0 . 9 25 , p = 0 . 35 ( x = 0 . 02 2 5 , y = 0 . 05 9 46 ) 1 , ( x =

    0 . 05 2 5 , y = 0 . 1 38 8 ) 2

    19

    20 % mi l e /mi n21 d m i n = 0 . 0 1 ; % m i l e22 d m a x = 0 . 5 ; % m i l e % dmax

  • 8/11/2019 Steady-States of the One-Lane Roundabout

    14/29

  • 8/11/2019 Steady-States of the One-Lane Roundabout

    15/29

    60 % We u s e t h e61

    % f l o o r f u nc t io n on t he t o t a l l en gt h o f th e roundaboutd i v i d e d by m, w hi ch62 % i s Number o f b l o c k s L /xc oo rd in at e o f f i r s t

    i n t e r s e c t i o n .63 % The e x pl a in a ti o n o f t h i s i s we c an say m i s t he

    u n s t a b l e p o in t , and64 % l e ng t h o f r ou nd a bl e65 % m would g i ve us a v al ue f o r t he e q ua d is t an t l e ng t h

    be tw ee n e ac h66 % c a r .67

    68

    69 % 2 . 8 0 0 2 . 8 2 570 % 2 . 8 1 2 5 went up71 % 2. 815 w ent down72

    73 % narrow i t f u r t h e r up74 % i n c r e a s e i t l i t t l e more up75

    76 % g = ( ( m ( 3 ) 2 . 8 1 3 7 5 m( 2 ) ) / 2 ) ;77 %78

    % % N c a r s a t b eg in ni n g wit h an e q u i d i s ta n t s p ac in g o f g . Uncomment l i n e s 386 405 f o r N 0 > 0 .79 % N = f l o o r ( ( n b l o c ks L) /( g ) ) ;80 % c o u n t = N ;81 % x = z e r o s ( 1 , N) n b l o c k s L ;82 %83 % x = l i n s p a c e ( 0 , n b l oc k s Lm(2 ) , N) ;84 %85 % x = s o r t ( x , d e sc en d ) ;86 % f o r b = 1 : n bl oc ks87 % i nd = f i n d ( x < b L&x >= ( b1) L) ;88 % i f isempty ( i nd )89 % f i r s t c a r ( b ) = 0 ; l a s t c a r ( b ) = 0 ;90 % e l s e91 % f i r s t c a r ( b ) = ind ( 1 ) ; l a s t c ar ( b ) = i nd ( end ) ;92 % end

    15

  • 8/11/2019 Steady-States of the One-Lane Roundabout

    16/29

    93 % end94

    % n e xt c ar = 2 :N ; n e xt c ar ( l a s t c a r ) = 0 ;95 % t e n t e r = z e r o s ( 1 ,N) ; t e x i t = [ ] ;96

    97 f i g u r e( 1) ;98 s e t(g cf , doubl e , on )99 r a d i u s = n b l o c k s L / (2 p i) ;100 t he ta = x / r a d iu s ;101 h1 = s c a t t e r ( r a d i u s c o s( t h e t a ) , r a d i u s s i n( th et a ) ,

    50 o n e s (s i z e( x) ) , f i l l ) ;102 a x i s ([ r a d i u s 1 .5 r a di u s 1 . 5 r a d i u s 1 .5 r a di u s

    1 . 5 ] )103 a x i s e q u a l104 a x i s manual105 t = 0;106 h2= t i t l e( s p r i n t f ( t i me =%0.2 f min , t o t a l # of c ar s=%d ,

    t , N) ) ;107 hol d on108 % v i s c i r c l e s ( [ 0 0 ] , r a d i u s , E dg eC ol or , k ) ;109 t h e t a = l i n s p a c e( 0 , 2 pi, n b lo c ks + 1 ) ;110 s c a t t e r ( r a d i u s c o s( t h e t a ) , r a d i u s s i n( the ta ) , 100

    o n e s ( 1 , n b l o c k s + 1 ) , r ) ; % e x i t s111

    112 f o r c l o c k = 1: c lockmax113 t = c l o c k dt ;114 t sav e (c l o c k) = t ;115 % e nt ry o f c a rs116 f o r b = 1 : n b l o c ks117 i f rand < R dt118 c ou nt = c ou nt + 1 ; % c ar i nd ex119 i f ( l a s t c a r ( b ) == 0 ) % i f t he b l o c k i s

    empty120 f i r s t c a r ( b ) = c ou nt ;

    121 l a s t c a r ( b ) = c ou nt ;122 n e x t ca r ( c ou nt ) = 0 ;123 e l s e124 ne x t c ar ( l a st c a r ( b) ) = c ount ;125 n e x t ca r ( c ou nt ) = 0 ;

    16

  • 8/11/2019 Steady-States of the One-Lane Roundabout

    17/29

    126 l a s t c a r ( b ) = c ou nt ;127

    en d128 x ( c o un t ) = ( b1) L ;129 t e n t e r ( c ou nt ) = t ;130 en d131 en d132 % motion and e x i t o f c a r s133 f o r b = 1 : n b l o c ks134 b ne xt = b + 1 n b l o c k s (b == nbl ock s ) ; %

    c i r c u l a r b lo ck s135 i f f i r s t c a r ( b ) = 0 % i f b lo ck i s not empty136 c = f i r s t c a r ( b) ;137 i f l a s t c a r ( b n ex t ) == 0138 d = dmax ;139 e l s e140 d = x ( l a s t c a r ( b n ex t ) ) x ( c ) + n b l o c k s

    L ( bnext == 1) ; % when near x=0141 en d142 x ( c ) = x ( c ) + dt vca r (d ) ;143 i f x ( c ) > b L % i f c ar p as se d end o f

    b l o c k b144 i f ( f i r s t c a r ( b ) == l a s t c a r ( b ) )145

    f i r s t c a r ( b) = 0 ;146 l a s t c a r ( b ) = 0 ;147 e l s e148 f i r s t c a r ( b ) = n e xt c ar ( c ) ;149 en d150 n e xt c ar ( c ) = 0 ;151 i f rand < p % di d th e c ar e x i t ?152 t e x i t ( c ) = t ;153 x ( c ) = 1;154 e l s e155 i f l a s t c a r ( b ne xt ) == 0

    156 f i r s t c a r ( b next ) = c ;157 l a s t c a r ( b n ex t ) = c ;158 e l s e159 n e x t c a r ( l a s t c a r ( b n ex t ) ) = c ;160 l a s t c a r ( b n ex t ) = c ;

    17

  • 8/11/2019 Steady-States of the One-Lane Roundabout

    18/29

    161 en d162

    x ( c ) = x ( c ) ( bne xt == 1) n b l o c k s L ;163 en d164 en d165 c p = c ; % t r a i l i n g p oi nt er166 c = n e x t ca r ( c ) ;167 w h i l e c = 0168 x ( c ) = x ( c ) + dt v c ar ( x ( c p ) x ( c ) ) ;169 c p = c ;170 c = n e x t ca r ( c ) ;171 en d172 en d173 en d174 % a v er a ge d and v175 Nsave(c l o c k) = l e n g t h( t e nt e r ) nn z ( t e x i t ) ;176 i n d o n r o ad = s e t d i f f ( 1 : c ou nt , f i n d( te xi t ) ) ; % i n d e x

    o f c a rs t ha t a re on road177 x s o r t = s o r t(x ( ind onro ad ) ) ;178 i f i se mp ty ( x so r t ) == 0179 d = [ d i f f ( x s o r t ) , x s o r t ( 1 ) + n b l oc k s L

    x s o r t ( en d ) ] ;180

    d a v e r a g e (c l o c k) = mean (d ) ;181 v a v e r a g e (c l o c k) = mean ( vcar (d ) ) ;182 en d183

    184 t h et a = ( x ( x>0) / r a d i u s ) ;185

    186 s e t( h1 , x data , r ad i us c o s( thet a ) , ydata ,r a d i u s s i n( th et a ) , s i z e d a t a , 50 o n e s (s i z e(th et a ) ) )

    187 s e t( h2 , s t r i n g , s p r i n t f ( t i me =%0.2 f min , t o t a l #of ca rs=%d , t , Ns ave (c l o c k) ) )

    188 drawnow189

    190 f i g u r e( 4 ) %c ou ld be more e f f i c i e n t191 d p l o t = l i n s p a c e(0 , dmax 1 . 5 , 1 0 1) ;192 p l o t( d p l o t , v ca r ( d p l o t ) , d p l o t , R / p d p l o t )

    18

  • 8/11/2019 Steady-States of the One-Lane Roundabout

    19/29

    193 t e x t( d av e r ag e (c l o c k) , v a v e r a g e (c l o c k) , s t r c a t ( \

    d i am o nd s ui t , ( , num2str ( d av e r ag e (c l o c k) ) ,, , num2str ( v av e r a ge (c l o c k) ) , ) ) )194 a x i s( [ 0 dmax 1 . 5 0 vmax 1 . 5 ] )195

    196 en d197

    198 f i g u r e( 2 )199 p l o t( t sav e , N save )200

    201 f i g u r e( 3 )202 p l o t( t s a ve , d a v er a g e , t s av e , v a v e r a g e )203

    204 % p e r ce n t ag e e r r o r205 100 ( abs ( v av e r ag e ( c l oc k max ) vcar ( d aver age (

    clockmax ) ) ) /( v ave rag e ( clockmax ) ) )

    7 Appendix B: vcar.m

    1 f u n c t i o n v = v c a r ( d )2 g l o b a l dmin dmax vmax ;3 v = z e r o s(s i z e( d) ) ;4

    f o r k = 1 :l e n g t h( d )5 i f ( d ( k ) dmin ) . ( d ( k ) < dmax) + vmax ( d ( k ) >= dmax) ;

    9 en d10 en d11 en d

    8 Appendix B: intersections.m

    1 f u n c t i o n [ x0 , y0 , i o u t , j o u t ] = i n t e r s e c t i o n s ( x 1 , y1 , x2 , y2 ,rob ust )

    2 %INTERSECTIONS I n t e r s e c t i o n s of cu rv es .

    19

  • 8/11/2019 Steady-States of the One-Lane Roundabout

    20/29

    3 % Computes t h e ( x , y ) l o c a t i o n s where two c ur ve s

    i n t e r s e c t . The c u r ve s4 % can be b ro ke n w i th NaNs o r h ave v e r t i c a l s eg me nt s .5 %6 % Example:7 % [ X0, Y0] = i nt e r s e c t i o ns ( X1, Y1, X2, Y2, ROBUST) ;8 %9 % w he re X1 and Y1 a r e e q ua ll en gt h v ec to rs o f at l e a s t

    two p o i n t s and10 % re p r es e n t c ur ve 1 . S i m il a r ly , X2 and Y2 r e p r es e n t

    c ur ve 2 .11 % X0 a nd Y0 a r e column v e c t o r s c o n t a i n in g t he p o i n t s a t

    w hi ch t h e two12 % c u rv es i n t e r s e c t .13 %14 % ROBUST ( o p t i o n a l ) s e t t o 1 o r t r u e means t o u s e a

    s l i g h t v a ri a ti o n o f t he15 % al g o ri t hm t h at mig ht r e t ur n d u p l i c a t e s o f some

    i n t e r s e c t i o n p oi nt s , and16 % th en remove t ho s e d u p l i c a t e s . The d e f a u l t i s t ru e ,

    but s i n c e t he17 % a lg or it hm i s s l i g h t l y s lo we r you can s e t i t t o f a l s e

    i f you know t h a t18 % y ou r c u rv e s don t i n t e r s e c t a t any s eg me nt b o un d ar i es

    . Also , th e r o bu st19 % v e rs i o n p r op e rl y h an dl es p a r a l l e l and o v er l ap p in g

    segmen ts .20 %21 % The a l g or i th m can r e t ur n two a d d i t i o n a l v e c t o r s t h at

    i n d i c a t e wh ich22 % segment p a i r s c o nt a in i n t e r s e c t i o n s and where t he y

    are :23 %

    24 % [ X0, Y0, I , J ] = i n t e r s e c t i on s ( X1, Y1, X2, Y2, ROBUST) ;25 %26 % For e ac h e le me nt o f t he v e c t or I , I ( k ) = ( s eg men t

    number of (X1, Y1) ) +27 % (how f a r a lo ng t h i s segment t he i n t e r s e c t i o n i s ) .

    20

  • 8/11/2019 Steady-States of the One-Lane Roundabout

    21/29

    For ex am ple , i f I ( k ) =28

    % 4 5. 25 then t he i n t e r s e c t i o n l i e s a q ua rt er o f th e wayb et ween t he l i n e29 % se gme nt c onn e c t i ng ( X1( 45) , Y1( 45 ) ) and ( X1( 46 ) , Y1( 46 )

    ) . S i m il ar ly f or30 % th e v e c t o r J and t h e s eg me nt s i n ( X2 , Y2 ) .31 %32 % You can a l s o g et i n t e r s e c t i o n s o f a c ur ve wi th i t s e l f

    . Simply pa ss i n33 % o n l y o ne c u rv e , i . e . ,34 %35 % [ X0, Y0] = i nt e r s e c t i o ns ( X1, Y1, ROBUST) ;36 %37 % w her e , as be f or e , ROBUST i s op t i o na l .38

    39 % V e rs i o n : 1 . 1 2 , 2 7 J an ua ry 2 01 040 % A ut ho r : D o ug l as M. S ch wa rz41 % E ma il : dm sch wa rz= i e e e org , dmschwarz=urgra d

    r o c h e s t e r ed u42 % R e a l e m a i l = r e g e x p r e p ( E ma il , { = , }, {@ , . } )43

    44

    45

    % T heo ry o f o p e r a t i o n :46 %47 % Gi ven two l i n e s eg me nt s , L1 and L2 ,48 %49 % L1 e nd po i nt s : ( x1 ( 1 ) , y1 ( 1 ) ) and ( x1 ( 2 ) , y1 ( 2 ) )50 % L2 e nd po i nt s : ( x2 ( 1 ) , y2 ( 1 ) ) and ( x2 ( 2 ) , y2 ( 2 ) )51 %52 % we ca n w r i t e f o u r e q u a t i o n s w it h f o u r unknowns and

    t he n s o l v e them . The53 % f o u r unknowns a r e t 1 , t 2 , x0 and y0 , w he re ( x0 , y0 ) i s

    the i n t e r se c t i on o f

    54 % L1 and L2 , t 1 i s th e d i st a nc e from t he s t a r t i n g p oi nto f L1 to th e

    55 % i n t e r s e c ti o n r e l a t i v e to the l en gt h o f L1 and t2 i st he d i s t a n c e fro m t h e

    56 % s ta r t in g p oi nt o f L2 t o the i n t e r se c t i on r e l a t i v e to

    21

  • 8/11/2019 Steady-States of the One-Lane Roundabout

    22/29

    t he l en gt h o f L2 .57

    %58 % So , t he f o ur e qu a ti o ns a re59 %60 % ( x1 ( 2 ) x 1( 1 ) ) t 1 = x 0 x 1 ( 1 )61 % ( x2 ( 2 ) x 2( 1 ) ) t 2 = x 0 x 2 ( 1 )62 % ( y1 ( 2 ) y 1( 1 ) ) t 1 = y 0 y 1 ( 1 )63 % ( y2 ( 2 ) y 2( 1 ) ) t 2 = y 0 y 2 ( 1 )64 %65 % R ea r ra n gi n g and w r i t i n g i n m a tr ix form ,66 %67 % [ x1 ( 2)x1 ( 1 ) 0 1 0 ; [ t1 ; [

    x 1( 1 ) ;68 % 0 x2 ( 2 )x 2 ( 1 ) 1 0 ; t2 ; =

    x 2( 1 ) ;69 % y1 ( 2 )y1 ( 1 ) 0 0 1; x0 ;

    y 1( 1 ) ;70 % 0 y2 ( 2 )y2 ( 1 ) 0 1] y0 ]

    y 2( 1 ) ]71 %72 % L et s c a l l t ha t AT = B . We can s o l ve f o r T with T =

    A\B.73

    %74 % Once we h ave o ur s o l u t i o n we j u s t have t o l o ok a t t 1

    and t 2 t o d e te r mi n e75 % whether L1 a nd L2 i n t e r s e c t . I f 0

  • 8/11/2019 Steady-States of the One-Lane Roundabout

    23/29

    82 % sm al l es t e n cl o si n g r e ct a ng l e s ( w ith s i d es p a r a l l e l to

    t he a xe s ) f o r e ac h83 % l i n e segment p a ir and s ee i f they o ve rl ap . I f they

    do t he n w e h av e t o84 % compute t 1 and t 2 ( v i a t h e A\B co mpu ta ti on ) t o s e e i f

    t he l i n e s eg me nt s85 % c r os s , bu t i f t he y don t t he n t he l i n e s eg me nt s

    cannot c r o s s . In a86 % t y p i ca l a p pl i ca t io n , t h i s t ec hn iq ue w i l l e l im i na t e

    most o f t he p o t e n t i a l87 % l i n e s eg men t p a i r s .88

    89

    90 % I n p ut c h e c k s .91 e r r o r( nargchk(2 ,5 ,n a r g i n) )92

    93 % Ad j us tm en t s w hen f e w e r t ha n f i v e a rg u me nt s a r es u p p l i e d .

    94 s w i t c h n a r g i n95 c as e 296 r o b u st = t r u e ;97 x2 = x1 ;98

    y2 = y1 ;99 s e l f i n t e r s e c t = t ru e ;100 c as e 3101 r o b u s t = x 2 ;102 x2 = x1 ;103 y2 = y1 ;104 s e l f i n t e r s e c t = t ru e ;105 c as e 4106 r o b u st = t r u e ;107 s e l f i n t e r s e c t = f a l s e ;108 c as e 5

    109 s e l f i n t e r s e c t = f a l s e ;110 en d111

    112 % x1 and y1 must b e v e c t o r s w it h same number o f p o i n t s( a t l e a s t 2) .

    23

  • 8/11/2019 Steady-States of the One-Lane Roundabout

    24/29

    113 i f sum(s i z e( x 1 ) > 1 ) = 1 | | sum (s i z e( y 1 ) > 1 ) = 1 | |

    . . .114 l e n g t h( x 1 ) = l e n g t h( y 1 )115 e r r o r( X1 and Y1 must be e q uall e ng t h v e c to r s

    o f at l e a s t 2 p oi nt s . )116 en d117 % x2 and y2 must b e v e c t o r s w it h same number o f p o i n t s

    ( a t l e a s t 2) .118 i f sum(s i z e( x 2 ) > 1 ) = 1 | | sum (s i z e( y 2 ) > 1 ) = 1 | |

    . . .119 l e n g t h( x 2 ) = l e n g t h( y 2 )120 e r r o r( X2 and Y2 must be e q uall e ng t h v e c to r s

    o f at l e a s t 2 p oi nt s . )121 en d122

    123

    124 % Fo rce a l l i n pu t s t o be column v e c to r s .125 x 1 = x 1 ( : ) ;126 y 1 = y 1 ( : ) ;127 x 2 = x 2 ( : ) ;128 y 2 = y 2 ( : ) ;129

    130

    % Compute n umber o f l i n e s e gm e nt s i n e a ch c u r v e andsome d i f f e r e n c e s we l l131 % n eed l a t e r .132 n1 = l e n g t h( x 1 ) 1 ;133 n2 = l e n g t h( x 2 ) 1 ;134 xy1 = [ x1 y1 ] ;135 xy2 = [ x2 y2 ] ;136 dxy1 = d i f f ( xy1 ) ;137 dxy2 = d i f f ( xy2 ) ;138

    139 % D et er mi ne t he c om b in a ti o ns o f i and j where t he

    r e ct a n gl e e n cl o s i ng t he140 % i th l i n e segment o f c ur ve 1 o v er l a ps wi th t he

    r e ct a n gl e e n cl o s i ng t he141 % j th l i n e segment o f c ur ve 2 .142 [ i , j ] = f i n d(repmat ( min ( x 1 ( 1 : end 1) , x1 ( 2 :en d ) ) ,1 , n2)

  • 8/11/2019 Steady-States of the One-Lane Roundabout

    25/29

    . . .143

    repmat(max( x 2 ( 1 : end 1) , x2 ( 2 :en d ) ) . , n1 , 1) & . . .144 repmat(max( x 1 ( 1 : end 1) , x1 ( 2 :en d ) ) , 1 , n2) >= . . .145 repmat( min ( x 2 ( 1 : end 1) , x2 ( 2 :en d ) ) . , n1 , 1) & . . .146 repmat( min ( y 1 ( 1 : end 1) , y1 ( 2 :en d ) ) , 1 , n2) = . . .149 repmat( min ( y 2 ( 1 : end 1) , y2 ( 2 :en d ) ) . , n1 , 1 ) ) ;150

    151 % F or ce i and j t o be column v e ct o r s , ev en when t h e i rl e n g th i s z er o , i . e . ,

    152 % we want them t o b e 0by1 i n st ea d o f 0by 0.153 i = r e s h a p e( i , [ ] , 1 ) ;154 j = r e s h a p e( j , [ ] , 1 ) ;155

    156 % Find s eg me nt s p a i r s wh ich ha ve a t l e a s t on e v e r te x =NaN and remove them .

    157 % This l i n e i s a f a s t way o f f i n di n g such segment p a i rs. We t ak e

    158 % ad va nt ag e o f t h e f a c t t h a t NaNs p r o pa g a te t hr ou ghc a l cu l a t io n s , i n

    159 % p a r t i c ul a r s u bt ra c ti o n ( i n t he c a l c u l a ti o n o f dxy1

    and dxy2 , which we160 % n ee d anyway ) and a d d i t i o n .161 % At t h e same t i me we c an r em ov e r e du n da n t c o m b i n a t io n s

    o f i and j i n t he162 % c as e o f f i n di n g i n t e r s e c t i o n s o f a l i n e with i t s e l f .163 i f s e l f i n t e r s e c t164 remove = i s n a n( sum ( dxy1( i , : ) + dxy2( j , : ) ,2 ) ) |

    j

  • 8/11/2019 Steady-States of the One-Lane Roundabout

    26/29

    172 % one column a t a ti me . AA i s a 3D e x te ns i on o f A

    w he re we l l u s e o ne173 % p l a ne a t a t im e .174 n = l e n g t h( i ) ;175 T = z e r o s(4 ,n ) ;176 AA = z e r o s(4 ,4 ,n ) ;177 AA( [ 1 2 ] , 3 , : ) = 1;178 AA( [ 3 4 ] , 4 , : ) = 1;179 AA ( [ 1 3 ] , 1 , : ) = dx y1 ( i , : ) . ;180 AA ( [ 2 4 ] , 2 , : ) = dx y2 ( j , : ) . ;181 B = [x1( i ) x2( j ) y1( i ) y2( j ) ] . ;182

    183 % Loop thro ugh p o s s i b i l i t i e s . Trap s i n g u l a r i t y warningand t he n u s e

    184 % l as tw ar n to s ee i f t ha t p la ne o f AA i s n ea r s i n g u la r .P r o c es s any s uc h

    185 % segment p a i rs to d ete rm in e i f they a re c o l i n e a r (o v e r l a p ) o r m er el y

    186 % p a r a l l e l . That t e s t c o ns i s t s o f c h e c k i n g to s ee i f on e o f t he e n dp o i nt s

    187 % o f t he c ur ve 2 segment l i e s on t he c ur ve 1 segment .T hi s i s done b y

    188

    % c he c ki n g t he c r o s s p ro du ct189 %190 % ( x1 ( 2) , y1 ( 2) ) ( x 1 ( 1 ) , y 1 ( 1 ) ) x ( x 2 ( 2 ) , y 2 ( 2 ) ) ( x1

    ( 1) , y 1( 1 ) ) .191 %192 % I f t h i s i s c l o s e to z er o then th e seg ments o ve rl ap .193

    194 % I f t he r ob us t o pt io n i s f a l s e th en we a ssume n o twos eg men t p a i r s a r e

    195 % p a r a l l e l and j u s t go a head and do t he c om pu ta ti on .I f A i s e v e r s i n g u l a r

    196 % a warning w i l l a ppear . This i s f a s t e r and o bv io us lyyou s ho ul d u se i t

    197 % o n ly when y ou k now y ou w i l l n e ve r h av e o v e r l a p p i n g o rp a r a l l e l s eg men t

    198 % p a i r s .

    26

  • 8/11/2019 Steady-States of the One-Lane Roundabout

    27/29

    199

    200

    i f r o b u s t201 o v er l ap = f a l s e ( n , 1 ) ;202 w a r n i n g s t a t e = w ar ni ng ( o f f , MATLAB:

    s i n g u l a r M a t r i x ) ;203 % Use t r yc at ch t o g ua ra nt ee o r i g i n a l war nin g

    s t a te i s r e st o re d .204 t r y205 l ast w a r n ( )206 f o r k = 1 : n207 T ( : , k ) = AA ( : , : , k ) \B ( : , k ) ;208 [ unuse d , l as t w ar n ] = l ast w a r n ;209 l ast w a r n ( )210 i f s tr cm p ( l ast w ar n , MATLAB:

    s i n g u l a r M a t r i x )211 % F or ce i n r a n g e ( k ) t o

    be f a l s e .212 T ( 1 , k ) = NaN ;213 % D et er mi ne i f t h e s e

    s eg me nt s o v e r l a p o ra re j u st p a r a l l e l .

    214 o v e r l a p ( k ) = rcond( [

    dxy1 ( i (k ) , : ) ; xy2( j (k) , : ) x y1( i ( k ) , : ) ] )< eps;

    215 en d216 en d217 w a rn i ng ( w a r n i n g s t a t e )218 c at ch e r r219 w a rn i ng ( w a r n i n g s t a t e )220 r e t hr ow ( e r r )221 en d222 % F in d w he re t 1 and t2 a r e b et we en 0 and 1 and

    r e t ur n t he c o r re s p on d i n g223 % x0 and y0 v a l u e s .224 i n r a n g e = (T ( 1 , : ) >= 0 & T( 2 , : ) >= 0 & T( 1 , : )

  • 8/11/2019 Steady-States of the One-Lane Roundabout

    28/29

    w i l l r et ur n a n226

    % i n t e r se c t i on p oi nt t ha t i s a t the c en te r o f t he o v e rl a p p i ng r e g i o n .227 i f any ( ove rla p )228 i a = i ( ov e r l ap ) ;229 j a = j ( ov e r l ap ) ;230 % s e t x0 and y0 t o m id dl e o f

    o v e rl a p p i ng r e g i o n .231 T ( 3 , ov e r l ap ) = (max( min ( x1( ia ) ,x1 ( ia +1)

    ) , min ( x2( ja ) ,x2( ja +1)) ) + . . .232 min (max( x1( ia ) ,x1 ( ia +1)) , max( x2

    ( ja ) ,x2 ( ja +1)) ) ) . / 2 ;233 T ( 4 , ov e r l ap ) = (max( min ( y1( ia ) ,y1 ( ia +1)

    ) , min ( y2( ja ) ,y2( ja +1)) ) + . . .234 min (max( y1( ia ) ,y1 ( ia +1)) , max( y2

    ( ja ) ,y2 ( ja +1)) ) ) . / 2 ;235 s e l e c te d = i n r a ng e | o v e r l a p ;236 e l s e237 s e l e c t e d = i n r a n ge ;238 en d239 x y0 = T ( 3 : 4 , s e l e c t e d ) . ;240

    241

    % Remove d u p l i c a t e i n t e r s e c t i o n p oi n t s .242 [ xy0 , index ] = unique (xy0 , rows ) ;243 x 0 = x y0 ( : , 1 ) ;244 y 0 = x y0 ( : , 2 ) ;245

    246 % Compute how f a r a l o n g ea ch l i n e s eg me nt t h ei n t e r s e c t i o n s a re .

    247 i f n ar go ut > 2248 s e l i n d ex = f i n d( s e l e c t e d ) ;249 s e l = s e l i n d e x ( i n de x ) ;250 i o u t = i ( s e l ) + T ( 1 , s e l ) . ;

    251 j o u t = j ( s e l ) + T ( 2 , s e l ) . ;252 en d253 e l s e % nonr o bu s t o p ti o n254 f o r k = 1 : n255 [ L , U] = l u(AA( : , : , k ) ) ;

    28

  • 8/11/2019 Steady-States of the One-Lane Roundabout

    29/29

    256 T ( : , k ) = U\(L\B( : , k ) ) ;257

    en d258

    259 % F in d w he re t 1 and t2 a r e b et we en 0 and 1 andr e t ur n t he c o r re s p on d i n g

    260 % x0 and y0 v a l u e s .261 i n r a n g e = (T ( 1 , : ) >= 0 & T( 2 , : ) >= 0 & T( 1 , : )

    < 1 & T( 2 , : ) < 1) . ;262 x 0 = T ( 3 , i n r ang e ) . ;263 y 0 = T ( 4 , i n r ang e ) . ;264

    265 % Compute how f a r a l o n g ea ch l i n e s eg me nt t h e

    i n t e r s e c t i o n s a re .266 i f n ar go ut > 2267 i o u t = i ( i n r a n g e ) + T ( 1 , i n r a n g e ) . ;268 j o u t = j ( i n r a n g e ) + T ( 2 , i n r a n g e ) . ;269 en d270 en d271

    272 % Pl ot t he r e s u l t s ( u s e f u l f o r d eb ug gi ng ) .273 % pl ot (x1 , y1 , x2 , y2 , x0 , y0 , ok ) ;

    29