views:

216

answers:

4

I am working on a offline C# application that can find bus routes. I can extract the timetable/bus/route data. I am searching for the most simple solution that will work with basic data.

What algorithm can be used to find a route from bus stop "A" to bus stop "B"? Is there a open-source solution ready for C#/Java? Is the google GTFS format for database good for a simple solution? http://code.google.com/transit/spec/transit_feed_specification.html

Thanks for any help. I am stuck with this. I don't know where to start - how to store the data and how to find routes. I know about Dijkstra/A* but I have used them only on graphs that were not time dependent...

A: 

Conceptually, you take the same basic algorithm for evaluating distance between A and B, but instead of distance, you should be evaluating time. Dijkstra can do both, if you give it the proper inputs.

You're used to seeing a map as a measure of distance. However, the same map can be a measure of time as well; all you need is to add data about average speed, and the time it takes to cover a particular distance of a particular road will shake itself out. You can even visualize the map in terms of time; routes that take longer will be longer. Dijkstra doesn't care which it's evaluating, really; it just cares about finding the continuous route with the lowest number, and whether that number represents length or time is immaterial.

To incorporate speed, naive algorithms simply use the daytime speed limit and assume you never have to stop while going from A to B; more advanced algorithms can incorporate information about time of day and traffic patterns (which will impact the average speed you travel on that road at that time), and whether a road is a freeway or surface street (and thus make educated guesses about time spent stopped at an intersection). What you use depends on what you have available, but a basic 4- or 5-layer time of day dimension should be adequate for all but the absolute most time-critical applications. For each direction of each road in your map, you need the average speed during morning rush, daytime, evening rush and night, possibly with lunchtime numbers as well. Once you have that, it's a relatively basic change to a Dijkstra algorithm to pass in a time of day and have it evaluate routes based on time.

KeithS
The problem with Dijkstra's algo for this application is that the routes times are variable in the following way: if you have a route from A to B to C, you have to wait at B for your transfer. That waiting time will depend on the rest of the schedule. Then, the route from B to C will in turn depend on which transfer you take, because not all transfers will go directly from B to C.
Pete
That's basically the problem I am facing, the path cost (transport time in my case) changes with time. You can take a path from A to B and it takes 10 minutes. Now from B to C, the paths will depend on the current-time + travel-time. At this point, I am only trying to plan the programming in forward, but it seems too complicated. I tried to google everything, but I didn't find an algorithm that would work with path costs that change according to a timetable. Thank you for your help.
Daniel_sk
Edit: I found something maybe valuable about Dijsktra + timetable here:http://blog.eldslott.org/tag/dijkstra/
Daniel_sk
@Daniel_sk will ur program (1) prepare a plan for me that I can print out and take with me. In this case, I want robustness to failure. (Even if bus 1 is 3 minutes late to B, I still want to follow the original plan.) Or will ur program (2) be an interactive guide (e.g. on my mobile), telling me in real-time which bus to get on, which stop to take, etc - where the plan is updated based on current conditions and robustness is less necessary.
emory
I will try to make it work on .NET CF framework, so it's going to be an offline mobile application (maybe porting to Java/Android later out of fun). Robustness is less needed, it just needs to work and display the results in a reasonable time.
Daniel_sk
@Daniel_sk If BUS101 has time table A 8:30, B 8:59, C 9:30; and BUS102 has time table D 8:30, B 9:00, and C 9:29, then the optimal route from A to C would be take 101 from A-B, and 102 from B-C. But if 101 is late or 102 is early, then I am screwed. I would rather just take 101 from A-C and not worry about the xfer.
emory
A: 

If you are interested in time information, then why not label the distance values on the graph edges using the time information instead of their physical distance from each other. That way you will search for the quickest route, instead of the shortest route. You can then use Dijkstra/A* in order to compute the your result.

I'm a little unclear on what you mean by time dependent. If you mean that that you need to answer queries of the form 'goes from x to y before 10am' then you can calculate what bus routes arrive before 10am, seems like a simple filter on the data. Then apply Dijkstra/A* to the data.

Richard Warburton
+4  A: 

The problem you are working on is not a trivial task. So much so, that is has a name: the mixed integer nonlinear programming problem (MINLP). In the words of one author (Deb 1998):

"When formulated mathematically, the time scheduling problem becomes a mixed integer nonlinear programming problem (MINLP) having a large number of resource- and service-related constraints. Although attempts have been made in the past to find an optimal schedule of a simplified model using classical optimization techniques (Bookbinder & DCsilets, 1992; Kikuchi & Parameswaran, 1993), it is observed that this is an extremely difficult task even for a small transit network. The difficulty arises mainly because of the large number of variables and constraints, discrete nature of variables, and nonlinearities involved in the objective function and the constraints."

In Deb's paper he proposes a genetic algorithm.

Your other option would be to use simulation. Just to throw something out there you can try right away-- choose thousands of random routes that start at your origin, and fish out the ones that work reasonably well at getting to the destination.

Picture the algorithm like this: You are trying to find the quickest route from stop A to stop B, starting at a certain time. You hire 1,000 people and arm them with a quarter to flip. You tell them to flip the coin every time they have a chance to get on or off a bus. Heads, get off (or get on, if already off). Tails, stay on (or keep waiting, if off). They each have an index card to write down the choices they make as they go. You go to point B and wait for the first guy to show up and take his card.

Pete
This a the very popular "vehicle routing problem", which is NP-Complete. Finding the optimal solution is probable, although unlikely. A <insert computational intelligence algorithm> could work, with varying levels of success, the only factor is "how correct" the solution should be.
gpampara
A: 

Try this as your data model.

Bus 1 goes to stops A, B and C. Bus 2 goes to stops B, D and E.

I would store a unique node based on both bus and stop, with distances to nodes being based on time. We would have node A1, B1, C1, B2, D2 and E2. In the special case of transfers apply the wait for the next bus as the distance between nodes. For example, if Bus 1 arrives at stop B 30 minutes before Bus 2, travel time from B1 to B2 is 30 minutes.

You should than be able to apply Dijkstra's Algorithm.

Philip T.