Pick shortest route with simpleXML - simplexml

First post so bear with me.
I have hacked together an module for Oscommerce that figures the delivery cost for a pizza delivery.
I used the google distance matrix and got it working only to discover that it picks the fastest route and not the shortest. and there was no option to tell it to pick the shortest.
Oh well.. I poked arround and found that i could use the direction api instead
http://maps.googleapis.com/maps/api/directions/xml?origin=Svendborgvej%20323,%205260%20Odense%20S,%20Denmark&destination=Albanivej%2027,%205792%20%C3%85rslev&alternatives=true
now i have 2 routes in that XML and i need
pick the shortest one
extract the distance from it
my code so far: http://pastebin.com/t3KY5Qzn
i'm not a very good programmer and a bit stuck here... anyone that could help me out?
i used an online xml viewer to get a more human readable format and i can see the 2 blocks with 1 route in it and also the total distance in each..

its a simple as: echo min($result)
from there its simple to mod my module so it picks the shortest route of those google might give

Related

Algorithm to find grid spaces in between two cells

I am making a grid based game which has "line of sight" targeting. Often times a game engine would use Raycast for this but I don't want to use an engine so I am trying to "roll my own" solution.
So basically, given P1,P2 pairs I want to find all those spaces between them (marked X).
I am having a big of a hard time figuring out how to do this. Somehow I have to find out which sides are closest together and use those as my starting points for "raycast". Then I guess I could take "samples" at cell-size increments and compare those with the indexes of the cells.
Unfortunately, I don't have any code yet ... I was hoping some could help with some pseudocode just to get the algorithm. I think if I could figure out how to get the start and end points for each of the pink lines then I could use that to find the orange squares.
Apparently, Bresenham algorithm is a good choice here.
I wish I could post some content in addition to the link but it wouldn't help me give a complete context. So, better to visit the link and use the information there. Don't miss out on the comments section. There are good insights there as well.
Please check this as well Elegant/Clean (special case) Straight-line Grid Traversal Algorithm?.

Grouping web site users by routes they make

I’m writing a simple website. I want to be able to group users by routes they make on my sites. For example I have this site tree, but the final product will be more complicated. Lets say I have three users.
User one route is A->B->C->D
User two route is A->B->C->E
User three route is J->K
We can say that users one and two belongs to same group and user three belongs to some other group.
My question is: what algorithm or maybe more than one I should use to accomplish that> Also what data do I need to collect?
I have some ideas, however, I want to confront it with someone who might have more experience than me.
I’m looking for suggestions rather than an exact solution for my problem. Also, if there are any ready-made solution which I can read, I will appreciate it also.
You could also consider common subpaths. A pointer in this direction and general web usage analysis is: http://pdf.aminer.org/000/473/298/improving_the_effectiveness_of_a_web_site_with_web_usage.pdf
As a first cut, it seems reasonable to divide the problem into (1) defining a similarity score for two traces and (2) using the similarity scores to cluster. One possibility for (1) is Levenshtein distance. Two possibilities for (2) are farthest-point clustering and k-modes.

What is a solution for a TSP with multiple salesmen and no return but known vertices and end points?

I don't know if I worded that correctly and I am not sure if it is even a TSP problem but here is the scenario.
I am designing and trying to optimize a route planner for a delivery service. I have multiple drivers (salesmen) who are all picking up packages at a central depot (origin) and delivering them on their way home. Their home locations (end-points) are known and all the delivery destinations (vertices) on the map are also known. After finishing the deliveries, the drivers go home instead of coming back to the depot.
What kind of problem is this and what sort of solutions should I be looking into? I have been treating it as a multi-TSP without return but still can't determine any near optimum tours. I have also tried shortest length Hamiltonian path but i'm quickly running into a bloc once i introduce a second driver.
Any resources, algorithms and heuristics suggestions are also welcome.
Geoffrey is right. It is a Vehicle Routing Problem. However, it is not the classical capacitated (CVRP) one with a single depot, since your drivers probably start and end at home rather than at the depot. Therefore your problem becomes a bit more difficult and turns to a pickup and delivery problem (VRPPD).
In short: if your drivers just start and end at the depot, it is a CVRP. If they start and end at home, it is a VRPPD.
For the CVRP you can find a number of open source algorithms for example OptaPlanner which is written in Java (Geoffrey knows more about it) or VRPH which is a C++ lib. When it comes to VRPPD the number of available open source algorithms shrink. Probably you can do it with OptaPlanner (I am not hundred percent sure). But you can surely solve it with jsprit which I implemented in Java.
If your problem is large and you need fast response times (computation times), you might be better off turning your VRPPD into CVRP by assuming drivers to ride from home to depot first and finally from depot to home again. But this way you ll certainly loose optimization potential.
This is called the Vehicle Routing Problem (VRP).
There are many resources available on that topic, such as video's (capacitated and/or time windows) and docs.
The VRP web offers a good explanation of the different variants.
This article seems to consider exactly the same problem as you want, and calls it: "K-vehicles Windy Rural Postman Problem".
Authors: Benavent, Corber, Sanchis and Plana.
A few articles like this one call the no return condition variant with one salesman Open VRP (OVRP).
Authors: D Aksen, Z Özyurt and N Aras2.
2013 paper on VRP without return: http://www.hindawi.com/journals/tswj/2013/874349/
Authors: Tantikorn Pichpibul Ruengsak Kawtummachai.

Algorithm choice for gaining intelligence from messages

What I'm trying to do is find an algorithm that can I can implement to generate 'intelligent' suggestions to people, by comparing messages they send to messages sent by their peers.
For example, Person A sends a message to Person B talking about Obj1. If Person C sends a message to Person D about Obj1, it will notice they are talking about the same things, and may suggest Person A talks to person C.
I have implemented collecting the statistics to capture the mentions people have in common but do not know which algorithm to use to analyse this.
Any suggestions?
(I hope this makes enough sense)
take a look at clustering algorithms
and k-means or
k-nearest neighbours for a quick start
How much data you've got? The more the better.
There are lots of approaches to this problem. You may for example take that all users, to some degree, are similar to each other and what you want to do is to find for each user the most similar ones.Vector space, cosine similarity, will give you quick results.
Give some more information on what you want to achieve.
This is exactly the same problem Twitter is battling with. You might end up with a job there if you crack this ;)
On serious note coming back, one could use some crude measures (i.e. heuristic based) to do something like this, but it has a big error percentage. As delnan said in the comment.
NLP is a sure bet. Note that using NLP too has some error %, but it's far more accurate than any heuristic you would use. If you are using python I would suggest this toolkit, I use it now and then - NLP.
For other languages I am sure there are packages which will help you in this regard.
UPDATE1: If you have a way for the users to tag their messages (like stackoverflow does), you could approach this problem barring NLP. Then you could simply take the intersection of the tags of both the messages to see if there is any commonality & suggest some top items for the common items.
But there are other issues you'll have to deal with - make tags a mandatory, plus you need to be sure that the users are actually entering correct tags etc... But nevertheless this greatly simplifies your problem.
UPDATE2: As the Q has been updated - Since you have some specific keywords/phrases only which you are interested in. This kind of simplifies it. You would need to get each of your message, split it into words, then stem each word. After stemming, intersect this set with the set of keywords you have. You'll get a set(S1). Do the same with the second message, you'll get a set(S2). Intersect S1, S2. If you find something is common, bingo! Some theme is common between message1, message2. else nothing.

Comparing a "path" (or GPS trail) of a vehicle

I have a bit of a difficult algorithm question, I can't find any suitable algorithm from a lot of searching, so I am hoping that someone here on stackoverflow might know the answer.
I have a set of x,y coordinates for a vehicle as it moves through a 2D space, the coordinates are recorded at "decision points" in the time period (i.e. they have stopped and made a determination of where to move next).
What I want to do is find a mechanism for comparing these trails efficiently (i.e. not going through each point individually). Compounding this is that I am interested in the "pattern" of their movement, not necessarily the individual points they went to. This means that the "path" is considered the same if you reflect it around an axis, or if you rotate it by 90,180 or 270 degrees.
Basically I am trying to distil some sort of "behaviour" to the way they move through the space, then examine the different "behaviours" for classification purposes.
Cheers,
Aidan
This may be way more complicated than you're looking for, but it sounds like what the guys did at astrometry.net may be similar to what you're looking for. Essentially, you can upload a picture of some stars, and it will figure out the position in the sky it belongs, along with rotation, you may be able to use similar pattern matching in what you're looking for.
They have a great pdf explaining how it works here, and apparently you can email them and they'll send you the source code (details are in the pdf).
Edit: apparently you can download the code directly here.
Hope it helps.
there are several approaches you could make:
Using vector paths and translation matricies together with two algorithms, The A* (a star) algorithm ( to locate best routes from what are called greedy functions ), and the "nearest neighbour" algorithm --- these are both commonly used for comparing path efficiencies for routes.
you may not know it but the issue you have is known as the "travelling salesman" problem and has many many approaches.
so look up
traveling salesman problem
A*
Nearest neighbour
also look at
Random walk algorithm - for the most basic approach
for a learned behaviour approach try neural networks "ANN" or genetic algorithms
the mathematics for this type of problem are covered under what is called "graph theory"
It seems that basically what is needed is some metric to compare two(N in general) paths and choose the best one?
If that's the case then I'd suggest plain statistics. I'd start with heading(orientation) histogram, relative(relative to previous heading) heading histogram and so on. Other thing comes to mind - distance/orientation between points covariance. Or just simply make up some kind of "statistics"(number of turns, etc.) and compare those paths using that.

Resources