## On Optimizing the Traffic Systems of the World Using Markov Chains (How to Predict Traffic Jams)

So it has been a recent dream of mine to optimize the traffic system of my hometown, and of Mexico in general, and even more generally of the world. Â I seldom drive, people seem always to be on the cellphone (which, by law apparently, they shouldn't use at all when driving), crossing red lights, and so on. Â More tragically in Guadalajara, the civil engineering of bridges and crossways intended to better circulation flow are often done seemingly with little thought (a bridge intended for double circulation is now for single circulation, and to do so they closed the entryway coming this way and modified it to go that way, so that a column splits the throughway in wedge-form and it's infinitely more prone to accidents than otherwise... another bridge is "closed" on full-of-traffic Sundays for bicycles... and another bridge-pass literally zigzags for entry to one major artery of the city at Los Cubos again increasing accidents in the zigzag portion). Â There may be people that benefit, don't get me wrong: perhaps those that get the concession to build the bridge in the first place (often the highschooler cousin of the governor or mayor).

So as I walk to work, a little office but two blocks from my house, purposely selected to minimize my having to use a car or transport to get there, I have to traverse a street which has been fitted with a light. Â It gets chaotic during a time of the day, and jams because of kindergarten schools, some 4 or 5 (which shouldn't be there in a residentially-zoned area). Â A cycle of the four installed semaphores, from what I gather, lasts around 2 minutes, and it is apparently optimized so that the heaviest-traffic lane light lasts a little longer than the others. Â I imagine this is the case for any crossroad lights, but it is working optimally only at heaviest traffic (and assuming that the heaviest traffic lane at high traffic times is always heavy in traffic); Â when there is little traffic, cars still have to wait a better part of the 2 minute cycle for their turn. Â In other words, the way the traffic lights are optimized works for a specific time of day and assuming heaviest traffic in a particular direction, which means it's suboptimal in all other cases.

So for the better part of a year now, maybe around 8 months, I have been thinking that lights should be adaptive, and of course I realize this is easier said than done. Â In fact I have gone to friends at the local government to try to find ways to put my ideas into action, but to no satisfying results (it was actually dismissed outright). Â I also tried entering a contest with this traffic idea (and a couple unrelated others). Â The contest was called "Iniciativa Mexico," but it turned out to be a silly PR ploy that left many of us contestants further saddened and disillusioned at the state of our country (and questioning the lasting impact of the projects that did win).

Since it is my belief that this project could benefit all countries around the world, I'm making it available to the public through my weblog, on a couple conditions:

1. Â That the use of these ideas and the use of ideas derived from these ideas be free, in that they generate no fee, because they are of benefit to the public and our world (in that these ideas intend to reduce CO2 and smog levels, for example), even if this means installation of special equipment on traffic lights and cars in general (such costs should be absorbed by the government without raising taxes for the purpose, by optimization of traffic budgets already in place).

2. Â It is the understanding of private companies interested in implementing this system in cities and towns or traffic systems that they will obtain zero profit or will be at a loss in the production of traffic equipment that use these ideas or ideas derived from these. Â Likewise, consulting services that use these ideas or ideas derived from these will be at zero profit for the private company, including analyses of traffic that use the following ideas or ideas derived from them.

3. Â Effectively, these ideas and ideas derived from these should generate no monetary profit for anyone who wishes to use them, implement them, or otherwise benefit from their functioning or infrastructure anymore than the optimization of the traffic flow and CO2 level and smog level reduction, EXCEPT when optimized-with-these-ideas-or-derived-ideas traffic system is further proven, mathematically and through simulations and to the satisfaction of a majority of a committee of 7 Markov-trained and renown statisticians (who ask no money for this purpose), impossible, and implementation of a fee-system, for example in high-density areas, is a *last-resort* to regulate heavy traffic.

4. If any company or country or government, local or otherwise, obtains profit from these ideas or derived ideas, other companies or countries or governments should exert pressure so that the program remain free and of benefit to humanity. Â The spirit of these clauses is that these ideas and derived ideas benefit the people of a country as a whole without incurring in extra cost to the people. Â Private companies give infrastructure and consulting services because they benefit, and likewise for governments: there is no advantage to be taken except that which is beneficial for everyone who uses a car or other transport, in the form of better traffic flow, avoidance of traffic jams, and quicker or optimized movement to their destination.

5. Â That being said, I reserve the right to modify the clauses as I see fit, in keeping with my original vision and a spirit of good-naturedness toward humanity and this world.

Our Earth needs a little breathing room, and this is my contribution to reducing the carbon dioxide signature, as best I know how currently.

We must think of a traffic network of a city as a *system. * Each car enters a "state" when it enters a new, directed-sense BLOCK (not street: a sense street is a state, a countersense same-street is another, within a block). Â We can number the blocks of a city in any way we like, but it's best that we do so in a consistent manner: for example, suppose we are at a crossroads; there are 8 states if all streets are bidirectional, and so, going, say, clockwise, each state receives numbers 1 through 8.

If the optimal light cycle length is about two minutes (I surmise it so, I'm not sure it is; but the optimality should derive from, for example, psychological considerations on the amount of waiting time that reduces road-rage), let us declare this time as "universal time," in the sense that every two minutes we will monitor the current state of the system by sampling or calculating exactly how many cars are in which state (this will require cars to be fitted with, for example, RFID tags and scanners at the entrance of a street and block; not all cars necessarily but a substantial sample perhaps). Â We seek to adjust, at every universal time-step, at an intersection, the proportion of time a light in a particular direction should stay *on* based on *need*. Â This need could be something like this:

Where J is the possibility of a jam in the current (directed) state, R is the city-set preference (North-South streets may have preference, or toward-downtown streets may have the right-of way, e.g.), A is the average waiting time in a state is differing from the speed limit times length of block, corrected for outliers (one could use several techniques to eliminate or weigh-by-less parked vehicles that would contribute significantly to the waiting time average otherwise: as by a filter). Â Other terms could be added but these seem to me the most significant.

The first term necessitates that we learn how to predict traffic jams. Â But note that we know how many cars are in what state every 2 minutes or so. Â The only thing we have to define is when a traffic jam occurs: perhaps if 80% of the (directed) street block capacity is exceeded. Â So we need to know what the "working capacity" of such is: it's the length of the street divided by the average length of a car (buses and trucks could be excluded and then count as two cars), times the lanes that actually serve to move traffic: in most instances, two (the third lane may be reserved for parking; even a single parked car in the lane may disrupt flow enough to consider it not useful to move traffic).

So let's therefore define a jam if . Â We can also now define the "total working capacity of the system" as by , which is independent of time because it's defined on (directed) street block static properties. Â Each state now has a working capacity, so create the vector M with all capacity entries ordered by state, and let "m" be the normalized vector: divide all entries by the total working capacity of the system. Â This vector gives us a threshold of the proportion of cars that each state can afford! Â If we exceed it the system breaks because the total capacity of each street is exceeded.

Now on to the prediction bit. Â Since we are sampling the state of the system every two minutes, we may have a history where we can count how many cars are at each time-step. Â Let's suppose that we can count exactly where all cars are for now: we may have information on how many cars are in states 1-8 at . Â We may not have knowledge of because that is information of the *current* ongoing cycle. Â At any rate, we can form vectors that describe the quantity of cars in each state. Â In other words, we have the "total occupancy" of the system at any point in the past. Â Let's take a look at the trajectory of a single car, C1. Â Such a car may have been at in state 3 and at in state 1. Â In other words, we can create the historical progress of C1 by tracing, at each 2 minute interval, its state:

C1 historical progress: .

In reality, we don't need the whole historical progress, we just need the and state, because what we seek to count to produce a Markov chain matrix is how many cars jumped from what state to what state during the latest cycles. Â For example, say we have several cars and three states:

C1: {1,2}

C2: {2,1}

C3: {1,1}

C4: {3,1}

C5: {1,2}

If we are to order by entries, we can quickly see how cars in state 1, say, have transitioned (moved) in traffic: 1/3 stayed in the same spot (awaiting the next cycle idly or parked; but let's say that there are no parked cars at the moment, for the sake of simplicity), 2/3 jumped to state 2. Â We can do the same for the other states and obtain the following Markov matrix: the rows are the states, ordered in the standard way, and the columns are the states the cars transitioned *to*, also ordered in the standard way.

So if cars move in the same proportion as they did in the previous couple light cycles, the powers of the matrix represent the proportions of cars at a particular state in the next time-step(s): Â The second power of the matrix is the current light cycle system-wide state (the proportion of cars we can expect to see in each street-block state), the 3rd power is the next, the 4th power is the next next, and so on. Â Let's say we can calculate up to the 11th power, or 10 steps-ahead. Â This is a prediction of the state of the system 20 minutes forward in time! (Recall that a Markov matrix is really a convenient way to describe non-coupled linear differential equations). We can do it for more steps but now I want to explain what I meant by the J variable in the "need" calculation of the lights.

We said that at time we can count the number of cars to obtain the vector V(-1), which tells us how many cars are in each state at that point in time, right? Â If we normalize this vector by summing all entries, then dividing each entry by the total, we obtain the initial state vector v(-1). Â So now

gives the current proportion across states. Â Similarly, and so on. Â If at any entry of the vectors , we have a jam because that's how we defined a jam (exceeding 80% of the working capacity, the threshold). Â Indeed the jam is at state and at time .

Since we can now see a jam at any state 20 (say) minutes in advance (on a per cycle of the semaphore lights), we can now qualify the need of traffic light with respect to potential traffic jams. Â Say, if the traffic jam is expected in the current about-to-begin time-cycle, give it a grade of 10. Â If it's in the next-next, give it a 9, and so, the 9th time-cycle (20 minutes from now) can be given a grade of 1. If no jam is expected in 9+1 cycles, give it a grade of 0. Since we are grading on a scale of 10, let the variable J be the proportion of the grade over the maximal score. Â Thus J is higher if a jam is imminent, and lower if it's predicted within the next 20 minutes. Â Each light at an intersection can be therefore given a "jam need" score, which is to say when will the state they control exceedÂ 80 percent the working capacity, i.e., the threshold value.

The "preference" or right-of-way score can be simply 1 or 0 depending.

How the average differs from the maximal traversal time, the variable I gleefully assigned the letter A, may be a bit controversial, in the sense that, if you notice, it's not necessarily orthogonal to the jam variable J: the jam variable J measures predictively how a state or street-block is going to jam in a long-while; that is, it will measure the accumulation of the proportion of cars on that state after a time period, so it indirectly measures the waiting time at that state. Â But explicitly calculating the deviation from the "city-proposed time" it should take to traverse a block-street, this "proposed time" being the length of the street times the reciprocal of the speed limit, may give additional information (than redundant) as to the immediate waiting times (within a couple traffic light cycles, than 10). Â The stronger the deviation, the higher the need of the traffic light to be green longer. J also doesn't really take into account "empty" streets that may suddenly be filled in a cycle, which A might just catch. Â So then let A be the percent deviation from the "proposed time."

How can we calculate the deviation from the "city-proposed-time"? Â If each car history contains the additional information of when the car transitioned to a given state, (recall each position contains the state and the order describes which 2-minute universal timeÂ interval it belongs to, up to the 1-current), then we can enrich our knowledge, as by (assume universal time is every two minutes from the o'clock):

Car 1: {... 1 at 12:01:00 (uni 12:00:00 - 12:01:59), 2 at 12:02:05 (uni 12:02:00 - 12:03:59)}

Car 2: {... 1 at 12:05:40 (uni 12:04:00 - 12:05:59), Â 1 still (uni 12:06:00 - 12:07:59), 1 still (uni 12:08:00 - 12:09:59), 5 at 12:10:00 (uni 12:10:00 -12:11:59)}, etc.,

In this case when the current time measurement needs to be done we look at a 1-step lag transition time, so for Car 1 it would be: 1 minute 5 seconds stayed in state 1 (for uni step 12:04:00), and for Car 2 we would measure for uni step 12:12:00, 4 min 30 seconds in state 1. Â We must look at when an actual transition has occurred for this, as we are looking for the time it takes to "jump" to another state. Â There is the issue of getting rid of outlier influence, as by parked cars, but I suppose there are statistical methods to do so: using robust statistics, like the median time, or other Winsorized or trimmed estimators.

Once we calculate the need for each traffic light at an intersection (say 4), we need assign only the proportional amount of time each light should be green. Â I assume a circular transition: a green light means go forward, left, right, or U-turn, and no other light can be green at the same time. Â If we weigh each variable J, A, R (jar-jar!) equally, then each light has need:

with can range between zero and one.

The proportion of time of a cycle a particular light should be green is thus: with is the number of traffic lights at an intersection, which is usually 4. Â Higher-need lights will stay green longer (and it's possible that a light be green for longer than 1 cycle). Â This has the happy consequence of maximizing the probability that a car find a traffic light is green in its traffic trajectory if: 1), a jam is prognosticated, for the effects of avoiding it, 2) the car is in a right-of-way street, and/or 3) there's a blockage that trumps the traffic flow that is relatively short-lived.

Having the ability to forecast jams gives us the tremendous ability of being able to alert the driver that such may happen and to convey to him, from BLOCKS AWAY, that he may want to choose alternative routes. Â Personally, I would add informative LEDs under every light that say jam ahead, jam east, jam west. Â Calculation of a Markov matrix is done, of course, at every 2 minute interval, and thus information is updated continually.

This sort of analysis with a Markov transition matrix has several cool consequences, in addition to those stated here. Â Of course there aren't lights at every intersection, but inspection of the system could show systematic jam accumulations at intersections with no lights. Â One could therefore install a traffic light at that intersection, or, better yet, run a simulation as to what would happen if a light were there. Â Perhaps the light is not needed at exactly that intersection, but nearby, thereby liberating jams at other intersections down the street-way. Â In any case, the Markov transition matrix allows us to simulate "what if" scenarios in flexible ways. Â Another example: one has now criteria to evaluate whether speed bumps are necessary in certain streets: the net effect is to slow traffic so that more cars proportionately remain in a particular state or street block. Â One can now understand if putting a speed bump or removing it in a street will have positive or detrimental effects for the system overall. Â It could be more than one is needed! Â In yet another example, it is often claimed that adding a roundabout (traffic circle, rotary) makes traffic more efficient. Â Segmenting the roundabout into states can show this to be true or false within a traffic system. Â It may be true in particular places, and false in others. Â Adding a bridge or traffic level to alleviate traffic problems can now also be simulated with an additional entry on the Markov matrix: will it in effect better the flow, or make it worse? Â This method allows us the systematic analysis that traffic needs in many cities around the world.

The traffic system as a whole can now function adaptively, in the sense that each light competes with others at the intersection for "green time," and this will depend largely on the time of day (more or less cars) and their aggregate movement around the city. Â The adaptivity of the system-wide traffic lights to their need gives them flexibility and the system becomes a moving "organism" that, in adapting to the traffic, will more-efficiently-than-the-current system move cars around.Â Dynamic Programming methods and theorems suggest that, by optimizing individually each intersection and traffic cycle, we arrive at the global optimal flow of traffic system-wide.

If this method of analyzing traffic is reminiscent to you of quantum mechanics, do tell. Â We move proportions around in this method, but they correlate with actual physical objects (cars) in aggregate. Â I think this is very cool and wonder if we cannot apply this in other subjects where flow across a network is important (but I'm not too learned about networks in general :( ).

In a next post, I think I may want to post an Excel sheet with an 8-state and 22-state system based on nearby blocks to my house, to make things a bit clearer.