How to add bus routes on map and connect it with OSRM? - algorithm

I am saving all bus routes data in my database. Whenever user requests if there is available routes from point A to point B, server should give the user response containing the available bus routes from point A to point B, and one more condition if there is no straight bus routes from point A to point B, then the response should give the available routes by transfers. So if there are no straight routes, first we need to find intersection point of route 1 and route 2, in which route 1 contains point A and route 2 contains point B. From A to Intersection point, and from Intersection point to point B.
I am saving route data as array of bus stops, and bus stops are consisting of latitude and longitude.

Related

What algorithm and data structure would fit the use case of overlapping traffic on a roadway

I have a problem where I have a road that has multiple entry points and exits. I am trying to model it so that traffic can flow into an entry and go out the exit. The entry points also act as exits. All the entrypoints are labelled 1 to 10 (i.e. we have 10 entry and exits).
A car is allowed to enter and exit at any point however the entry is always lower number than the exit. For example a car enters at 3 and goes to 8, it cannot go from 3 to 3 or from 8 to 3.
After every second the car moves one unit on the road. So from above example the car goes from 3 to 4 after one second. I want to continuously accept cars at different entrypoints and update their positions after each second. However I cannot accept a car at an entry if there is already one present at that location.
All cars are travelling at the same speed of 1 unit per second and all are same size and occupy just the space at the point they are in. Once a car reaches its destination, its removed from the road.
For all new cars that come into the entrypoint and are waiting, we need to assign a waiting time. How would that work? For example it needs to account for when it is able to find a slot where it can be put on the road.
Is there an algorithm that this problem fits into?
What data structure would I model this in - for example for each entrypoints, I was thinking something like a queue or like an ordered map and for the road, maybe a linkedlist?
Outside of a top down master algorithm that decides what each car does and when, there is another approach that uses agents that interact with their environment and amongst themselves, with a limited set of simple rules. This often give rise to complex behaviors: You could maybe code simple rules into car objects, to define these interactions?
Maybe something like this:
emerging behavior algorithm:
a car moves forward if there are no cars just in front of it.
a car merges into a lane if there are no car right on its side (and
maybe behind that slot too)
a car progresses towards its destination, and removes itself when destination is reached.
proposed data structure
The data structure could be an indexed collection of "slots" along which a car moves towards a destination.
Two data structures could intersect at a tuple of index values for each.
Roads with 2 or more lanes could be modeled with coupled data structures...
optimial numbers
Determining the max road use, and min time to destination would require running the simulation several times, with varying parameters of the number of cars, and maybe variations of the rules.
A more elaborate approach would us continuous space on the road, instead of discrete slots.
I can suggest a Directed Acyclic Graph (DAG) which will store each entry point as a node.
The problem of moving from one point to another can be thought of as a graph-flow problem, which has a number of algorithms for determining movement in a graph.

How do we get the shortest distance route from point A to B by default from Google Direction API

How do we get the shortest distance route from point A to B by default from Google Direction API suggested alternative routes? By default it gives us shortest duration routes depending upon the current traffic conditions. I have noticed that google responds with multiple alternative routes if you turn on "provideRouteAlternatives=true", I was wondering if we could send a parameter to Google API so that it will always return shortest distance route by default
As Rameshwor has mentioned, the suggested route returned by Google may be optimised for journey time rather than journey distance. If one or more waypoints have been specified then only one route may be returned anyway, but programatically it's best to assume that more than one route will always be returned.
The following example shows a simple way to find the route with the shortest journey distance using jQuery; please note this code isn't optimised but it should work:
var route_options = [];
for (var i = 0; i < response.routes.length; i++)
{
var route = response.routes[i];
var distance = 0;
// Total the legs to find the overall journey distance for each route option
for (var j = 0; j < route.legs.length; j++)
{
distance += route.legs[j].distance.value; // metres
}
route_options.push({
'route_id': i,
'distance': distance
});
}
/*
route_options = [
{route_id:0, distance:35125},
{route_id:1, distance:22918},
{route_id:2, distance:20561}
];
*/
// Sort the route options; shortest to longest distance in ascending order
route_options.sort(function(a, b) {
return parseInt(a.distance) - parseInt(b.distance);
});
/*
route_options = [
{route_id:2, distance:20561},
{route_id:1, distance:22918},
{route_id:0, distance:35125}
];
*/
var shortest_distance = (route_options[0]['distance'] * 0.001); // convert metres to kilometres
You can then access the "route_id" value to access the correct route in the response object.
By default Google gives us shortest duration routes depending upon the current traffic conditions. I have noticed that google responds with multiple alternative routes if you turn on “provideRouteAlternatives=true”. When you do not ask for alternate routes, you get the most optimized route by default, although this isn’t necessarily optimized for distance.
If you’re trying to get the shortest route, then the way to do this would be to have your application evaluate the total distance of each route in the response, and programmatically select the one with the shortest distance. There isn’t a query parameter that you can pass to Google in the request to say “return only the shortest route”.
Check this demo http://webavenue.com.au/demo/google-shortest-distance/route.html
Using the shortest route rather than the fastest route is generally not a good idea in practice.
When the shortest route is not the fastest, it is likely to be a lower quality route in terms of time, fuel efficiency and sometimes even personal safety. These factors are more important to the majority of drivers on the road.
There are a few workarounds that could potentially yield shorter routes, but they have significant drawbacks, so I'd recommend against them:
Request routes in both directions.
Directions from A to B may not yield a feasible route from B to A due to situations like one-way streets, turn restrictions and different locations of highway exits. Requesting routes in both directions and taking the shortest route may yield a route that is not usable in one direction.
Request alternative routes.
Asking for alternative routes and picking the shortest route can yield a shorter route than that returned by default. However, alternative routes are not generally stable (may change over time as short-term road conditions change) nor guaranteed to include the shortest route. This means that the shortest route may still not be available, and also the shortest route found by this approach may change over time, giving an impression of instability.
Oftentimes I've seen requests for the shortest routes come from use cases where the goal is rather a realistic driving distance at specific times of the day and week, e.g. workers commuting to/from work. In these cases, driving directions can be requested with departure_time set to the relevant time of the day (and day of the week) within a week (in the future, not past) to obtain a route influenced by typical traffic conditions at that time of the day and week.
Note: this is only available if request includes an API key or a Google Maps APIs Premium Plan client ID.

Routing Information Protocol implementation

I am trying to implement a simulator for the Routing Information Protocol. I think I have most of the implementation done but I am not too sure about one part of the algorithm used when a node receives a table from a different node.
One of the if statements is as follows:
if address is known by p1 with a link of p2 then:
if the cost for p2 is not exactly one less than p1's cost:
act as if this address was unknown to p1
Where p1 is receiving a table from p2. Does this mean that if p1 has the address in its table and that the link associated with that address in p1 is a link to p2 then check whether the cost for p2 is not exactly less than p1s cost?
Thanks
Yes, that's what it means.
When p1 gets routing information about some network n from p2, it must decide whether or not to use the new route. Normally, it will use the new route (setting the link to p2) only if it is better than the route it currently knows for n. However, in the case that p2 was the gateway it already had for n -- in other words, it got the information from p2 earlier -- then it accepts the new information, even if the new cost is higher than the old cost.
This allows p2 to inform its neighbours that it has lost connectivity to some other network (by setting its cost to the RIP equivalent of infinity). If the neighbours previously relied on p2 to reach that network, they will now invalidate their routes to that network and await information from some other gateway which does have connectivity. It also allows p2 to inform its neighbours that the cost of reaching n has increased and they should use a cheaper route if they can find one.

Database design for bus reservation

I'm developing a reservation module for buses and I have trouble designing the right database structure for it.
Let's take following case:
Buses go from A to D with stopovers at B and C. A Passenger can reserve ticket for any route, ie. from A to B, C to D, A to D, etc.
So each route can have many "subroutes", and bigger contain smaller ones.
I want to design a table structure for routes and stops in a way that would help easily search for free seats. So if someone reserves seat from A to B, then seats from B to C or D would be still be available.
All ideas would be appreciated.
I'd probably go with a "brute force" structure similar to this basic idea:
(There are many more fields that should exist in the real model. This is only a simplified version containing the bare essentials necessary to establish relationships between tables.)
The ticket "covers" stops through TICKET_STOP table, For example, if a ticket covers 3 stops, then TICKET_STOP will contain 3 rows related to that ticket. If there are 2 other stops not covered by that ticket, then there will be no related rows there, but there is nothing preventing a different ticket from covering these stops.
Liberal usage or natural keys / identifying relationships ensures two tickets cannot cover the same seat/stop combination. Look at how LINE.LINE_ID "migrates" alongside both edges of the diamond-shaped dependency, only to be merged at its bottom, in the TICKET_STOP table.
This model, by itself, won't protect you from anomalies such as a single ticket "skipping" some stops - you'll have to enforce some rules through the application logic. But, it should allow for a fairly simple and fast determination of which seats are free for which parts of the trip, something like this:
SELECT *
FROM
STOP CROSS JOIN SEAT
WHERE
STOP.LINE_ID = :line_id
AND SEAT.BUS_NO = :bus_no
AND NOT EXIST (
SELECT *
FROM TICKET_STOP
WHERE
TICKET_STOP.LINE_ID = :line_id
AND TICKET_STOP.BUS_ID = :bus_no
AND TICKET_STOP.TRIP_NO = :trip_no
AND TICKET_STOP.SEAT_NO = SEAT.SEAT_NO
AND TICKET_STOP.STOP_NO = STOP.STOP_NO
)
(Replace the parameter prefix : with what is appropriate for your DBMS.)
This query essentially generates all combinations of stops and seats for given line and bus, then discards those that are already "covered" by some ticket on the given trip. Those combinations that remain "uncovered" are free for that trip.
You can easily add: STOP.STOP_NO IN ( ... ) or SEAT.SEAT_NO IN ( ... ) to the WHERE clause to restrict the search on specific stops or seats.
From the perspective of bus company:
Usually one route is considered as series of sections, like A to B, B to C, C to D, etc. The fill is calculated on each of those sections separately. So if the bus leaves from A full, and people leave at C, then user can buy ticket at C.
We calculate it this way, that each route has ID, and each section belongs to this route ID. Then if user buys ticket for more than one section, then each section is marked. Then for the next passenger system checks if all sections along the way are available.

Need to sort postal addresses in a database table in sequence

I'm developing an Android app that has postal address routing in it.
We would like to sort each address in a database table in such a way where each address can be printed out for our driver. We don't want to spend time sorting addresses ourselves when a database table would be ideal for that.
I was thinking somehow to create a primary key in the table on a number that we can easily sort in ascending order. That would make it easy to create a manifest for the driver.
I was researching and found that there was something called WOEID (Where on Earth ID) which I think represents a region but I'm not sure if it would be good for us since 2 postal address in the same neighbourhood could use the same WOEID.
For example these 3 addresses have the same WOEID:
100 Bowden St., Lowell, MA
131 Stedman St., Lowell, MA
50 Stromquest Ave., Lowell, MA
The concept of using a single number like WOEID is perfect for us but we need to sort addresses like these in order so we know which ones are closest to each other.
Maybe there is a web site we can use to send out a request from within our app providing them with the postal address and the site will return a single number representing that address in a format our app can parse.
Maybe there is a way to Geocode or convert latitude and longitude numbers into a single number we can use.
Thanks.
So the problems arise when multiple addresses share the same WOEID? I don't know how many resources are available but couldn't it be in idea to fed this data to some API like Google Maps and calculate the distance from your position to there?
If you want to avoid google a really basic approach could be to do the calculation by yourself. (Though this would be more of an estimation.)
However I got the feeling you want to find the best route over total distance so I think your problem is also related to the travelling salesman problem
Given a list of cities and their pairwise distances, the task is to
find the shortest possible tour that visits each city exactly once.
I found a web site that allows me to do a reverse Geocoding. It returns a latitude and longitude.
I experimented a bit and found that I can add the latitude and longitude together to get a single number. Since giving an exact address to the web site will give results not completely what I'm looking for I give it just a street and city and US state to locate. I can then use the latitude and longitude returned and then sort the house addresses in numeric ascending order.
For example we have many streets that are very grid like so 1 number from the adding of the latitude and longitude represent many homes on 1 particular street. On another street a block away I did the same thing. Now all I have to do is just list everything in the database table like this:
City chosen by a dropdown, US state also chosen by a dropdown, then the geocoded number for each street followed by the home numbers.
Here is the web site I used to get the Geocoding along with a sample street address:
http://where.yahooapis.com/geocode?q=stedman+st,+lowell,+ma

Resources