Is there a website or way to use a spreadsheet/LibreCalc to find the shortest route between locations but with other variables included? - spreadsheet

From what I can tell this seems to be at least part of the "Traveling Salesman Problem" but maybe some alternative option that I don't know the name for.
If there are "n" number of the SAME things possible that you want to visit. As a random example lets just say 25.
You want to visit exactly 11 out of the 25, within the shortest route possible.
You have x,y coordinates of each location.
But lets say to get to 11 of the same thing you would potentially have to travel a much greater distance for 3 or 4 out of the 11 locations, with 8 within close proximity.
If you instead had a different thing "m" that was nearby that you could visit instead to achieve the 11 locations and make up for the 3 or 4 other spots in a shorter distance.
So how would you calculate not only the shortest route to get the 11 locations, but I guess the "efficiency" of maximizing the amount you could obtain of "n" versus "m" if it meant it was a shorter traveling distance. Basically the comparison of the shortest distance traveled vs the quantity you could obtain if you had to use "filler" locations "m" instead.
The idea would be that there would be way more varied locations to choose from in all the data (m,n,o,p,q,etc.), so the locations would be mixed distances. So how would you find the shortest route to hit "11" of a specific thing, and if you couldn't hit 11 within a reasonable distance what are the alternative things to get instead.
The data I would have directly per location:
Column A = Product type A,B,C,D etc
Column B = X coordinate
Column C = Y coordinate
So if one set of entries in Column A were all "Computers", and one set of entries in Column A were "Cellphones", how would I find the shortest distance to reach 11 Computers OR (X)Computers+(Y)Cellphones = 11 at a shorter distance.
For an example I would expect to output would be something like you can get 11 Computers but travel 200 miles as the shortest distance, or you can get 8 Computers + 3 Cellphones and travel 150 miles as the shortest distance. And finding out which one is more efficient.

Related

Is there an algorithm for near optimal partition of the Travelling salesman problem, creating routes that need the same time to complete?

I have a problem to solve. I need to visit 7962 places with a vehicle. The vehicle travels with 10km/h and each time I visit one place I stay there for 1 minute. I want to divide those 7962 places into subsets that take will take up to 8 hours. So lets say 200 places take 8 hours I visit them and come back the next day to visit another maybe 250 places(the 200 places subsets will require more distance travelled). For the distance I only care for Euclidean Distances no need to take into account the distance through the road network.
A map of the 7962 places
What I have done so far is use the k means clustering algorithm to get good enough subsets and then the Lin Kernighan heuristic (Program Concorde) to find the distance. And then compute times. But my results go from 4 hours to 12 hours. Any idea to make it better? Or a code that does this whole task all together. Propose anything but I am not a programmer I just use Python some times.
Set of coordinates :
http://www.filedropper.com/wholesetofcoordinates
Coordinates subsets(40 clusters produces with the k means algorithm):
http://www.filedropper.com/kmeans40clusters

Extremely slow query using CONNECT BY Oracle 10

I have a table challenge containing about 12000 rows. Every point connects to the four points around it, for example 100 connects to 99 101 11 and 189. I tried this with a small scale table and it worked just fine but as I increased the size of the table the query became exponentially slower and now it won't even finish. Here's my query
SELECT level, origin, destination
FROM challenge
WHERE destination = 2500
START WITH origin = 1
CONNECT BY NOCYCLE PRIOR destination = origin;
Any advice on how to optimize this query would be greatly appreciated.
So you're finding every path from node 1 to node 2500 in a degree-4 graph (rectangular lattice?) of thousands of nodes. I expect there'll be quite a lot of them. Did the challenge just ask you to count them? Because I think the point was that you have to figure out how many there are by doing math, not brute force computation.
For example, if it's a 50x50 rectangular grid with node 1 and node 2500 in opposite corners, then the minimum path length is 100 steps. A path of 100 steps will have 50 of them horizontal and 50 of them vertical, and they can come in any order. Figure out how many ways you can arrange a string of 50 H's and 50 V's and you might find it's a number that even the mighty Oracle will have a bit of a problem with. (Generating the rows, that is. Doing the calculation just requires large integer arithmetic, which Oracle can probably do quite quickly once you tell it the formula.)
And your query is actually worse than that. It doesn't ask only for minimum-length paths. So it will also return all the paths of length 102 that take a step away from the destination somewhere along the way. And paths of length 104 that take 2 backward steps. And paths of length 2498 that visit almost all of the nodes! Counting those paths is more complicated than counting the short paths because you have to exclude the ones that cross themselves.

Manhattan distance with n dimensions in oracle

I have a table with around 5 millions rows and each row has 10 columns representing 10 dimensions.
I would like to be able when a new input is coming to perform a search in the table to return the closest rows using Manhattan distances.
The distance is the sum of abs(Ai-Aj)+abs(Bi-Bj)...
The problem is that for the moment if I do a query, it does a full scan of the entire table, to calculate the distances from every rows, and then sort them to find the top X.
Is there a way to speed the process and make the query more efficient?
I looked at the distance function online for the SDO_GEOMETRY, but I couldn't find it for more than 4 dimensions.
Thank you
If you are inserting a point A and you want to look for points that are within a neighbourhood of radius r (i.e., are less than r away, on any metric), you can do a really simply query:
select x1, x2, ..., xn
from points
where x1 between a1 - r and a1 + r
and x2 between a2 - r and a2 + r
...
and xn between an - r and an + r
...where A = (a1, a2, ..., an), to find a bound. If you have an index over all x1, ..., xn fields of points, then this query shouldn't require a full scan. Now, this result may include points that are outside the neighbourhood (i.e., the bits in the corners), but is an easy win to find an appropriate subset: you can now check against the records in this subquery, rather than checking against every point in your table.
You may be able to refine this query further because, with the Manhattan metric, a neighbourhood will be square shaped (although at 45 degrees to the above) and squares are relatively easy to work with! (Even in 10 dimensions.) However, the more complicated logic required may be more of an overhead than an optimisation, ultimately.
I suggest using function based index. You need this distance calculated, therefore pre calculate it using function based index.
You may want to read following question and it links. Function based index creates hidden column for you. This hidden column will hold manhanttan distance , therefore sorting will be easier.
Thanks for #Xophmeister's comment. Function based index will not help you for arbitrary point. I do not know any sql function to help you here. But if you are willing to use machine learning data mining algorithm.
I suggest cluster your 5 million rows using k-means clustering. Lets say 1000 cluster center you found. Put this cluster centers to another table.
By definition clustering , your points will be assigned to cluster centers. Because of this you know which points are nearest to this cluster center, say
cluster (1) contains 20.000 points, ... cluster ( 987) contains 10.000 points ...
Your arbitrary point will be near to one cluster. You find that your point is nearest to cluster 987. Run your sql , using only points which belongs to this cluster center, that 10.000 points.
You need to add several tables/columns to your schema to make this effective. If your 5.000.000 rows changes continuously, you need to run k-means clustering again as they change. But if they are fairly constant values, one clustering per week or per month will be enough.

Is there a formula to change a latitude and longitude into a single number?

Can you tell me if there is a formula to change a latitude and longitude into a single number?
I plan to use this for a database table in software that provides routing for deliveries. The table row would have that number as well as the postal address. The database table would be sorted in ascending numeric order so the software can figure out which address the truck would need to go to first, second etc.
Please can you respond showing VB or VB.Net syntax so I can understand how it works?
For example I would use the following numbers for the latitude and longitude:
Lat = 40.71412890
Long = -73.96140740
Additional Information:
I'm developing an Android app using Basic4Android. Basic4Android uses a VB or VB.Net syntax with SQLite as the database.
Part of this app will have route planning. I want to use this number as the first column in an SQLite table and the other columns will be for the address. If I do a query within the app that sorts the rows in numerical ascending order, I will be able to figure out which postal address are closest to each other so it will take less time for me to go from house to house.
For example, if the numbers were:
194580, 199300, 178221
I can go to postal address 178221 then to 194580 and finally to 199300 and I won't need to take the long way around town to do my deliveries after they were sorted.
As an alternative, I would be happy if there was an easy way to call a web service that returns maybe a json response that has the single number if I send a postal address to the web site. Basic4Android does have http services that can send requests to a web site.
A latitude an longitude, can both be represented as 4 byte integer, such that the coordinates has an accuracy of 3cm which is sufficent for most applications.
Steps to create one 8 byte value of type long from latitude and longitude:
1) convert lat and lon to int by: int iLat = lat * 1E7;
2) Use a 8 byte long value to store both 4 byte int.
set upper 4byte to latitude, and lower 4 to longitude.
Now you have a 8 byte long representing a point on world up to 3cm accuracy.
There are other, better solutions, such ones that maintain similar numbers for near locations, but these are more complex.
You can add them up, but it makes little sense.
For instance a total of "10" - 8 lat and 2 long would then be the same as "10" - 3 lat and 7 long.
You can concatenate them, maybe with a dash.
But why do either? They are both really bad choices. A delivery system would want real x-y co-ordinates and if planning a route would want them seperate in order to calculate things like Euclidean distances.
Is this a homework question? I doubt a delivery service is designing their service structure on SO. Least hope not.
Based on AlexWien's anwser this is a solution in JavaScript:
pairCoordinates = function(lat, lng) {
return lat * 1e7 << 16 & 0xffff0000 | lng * 1e7 & 0x0000ffff;
}
How about this:
(lat+90)*180+lng
From Tom Clarkson's comment in Geospatial Indexing with Redis & Sinatra for a Facebook App
If you want to treat location as "one thing", the best way to handle this is to create a data structure that contains both values. A Class for OO languages, or a struct otherwise. Combining them into a single scalar value has little value, even for display.
Location is a really rich problem space, and there are dozens of ways to represent it. Lat/Lon is the tip of the iceberg.
As always, the right answer depends on what you're using it for, which you haven't mentioned.
I have created a method of putting the latitude and longitude into one base-36 number which for now I'm calling a geohexa.
The method works by dividing the world into a 36 x 36 grid. The first character is a longitude and the second character is a latitude. The latitude and longitude those two characters represent is the midpoint of that 'rectangle'. You just keep adding characters, alternating between longitude and latitude. Eventually the geohexa, when converted back to a lat and lon will be close enough to your original lat and lon.
Nine characters will typically get you within 5 meters of a randomly generated lat and lon.
The geohexa for London Bridge is hszaounu and for Tower Bridge is hszaqu88.
It is possible to sort the geohexa, and locations that are near each other will tend to be next to each other in a sorted list to some extent. However it by no means solves the travelling salesman problem!
The project, including a full explanation, implementations in Python, Java and JavaScript can be found here: https://github.com/Qarj/geohexa
You can use the Hilbert space filling curve to convert latitude,longitude into a single number: e.g., https://geocode.xyz/40.71413,-73.96141?geoit=xml 2222211311031 and https://geocode.xyz/40.71413,-73.96151?geoit=xml 2222211311026
The source code is here: https://github.com/eruci/geocode
In a nutshell:
Let X,Y be latitude,longitude
Truncate both to the 5th decimal point and convert to integers multiplying by 100000
Let XY = X+Y and YX = X-Y
Convert XY,YX to binary, and merge them into XYX by alternating the bits
Convert XYX to decimal
Add an extra number (1,2,3,4) to indicate when one or both XY,YX are negative numbers.
Now you have a single number that can be converted back to latitude,longitude and which preserves all their positional properties.
I found I can get good results by adding the latitude and longitude of a particular address by not including the house number and sorting the results in the database table by the added number following by a 2nd sort on the house number in ascending order.
I used this url to get the numbers I needed to add together:
http://where.yahooapis.com/geocode?q=stedman+st,+lowell,+ma

How to programmatically solve the 15 (moving numbers) puzzle?

all of you have probably seen the moving number/picture puzzle. The one where you have numbers from 1 to 15 in a 4x4 grid, and are trying to get them from random starting position to
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15
My girlfriend or some of my non-programmer friends can solve this with some mumbo-jumbo magic, that they can't explain to me. I can't solve the puzzle.
The most promising approach I have found out is to solve first row, then I'd get
1 2 3 4
X X X X
X X X X
X X X
then first column without touching solved cells
1 2 3 4
5 X X X
9 X X X
13 X X
then second row to
1 2 3 4
5 6 7 8
9 X X X
13 X X
then second column
1 2 3 4
5 6 7 8
9 10 X X
13 14 X
the problem is, that remaining X (random) tiles are sometimes in unsolvable position and this is where my solution fails. But I feel as if I'm on the right path.
My program does the solving of specified row/column by trying to get number X to specified position without messing up the correct cells, if possible. But it can't do the last 3 tiles on 2x2 grid. What am I missing?
Make sure that your puzzle is solvable first. Not all are.
Otherwise your strategy looks sound.
You're definitely on the right track, but rather than solving by row/column iteratively to the point of being left with a 2x2, solve until you have a minimum 3x3 and then solve just that grid. 3x3 is the smallest size you need to properly re-order the grid (while 2x2 doesn't give you the complete flexibility you may need as you've already discussed). This approach is scalable too - you can solve 5x5, 10x10 etc.
I think The most effective way of solving this, is using additive patterns, with an admisible heuristic, and IDA* algorithm. As described here - http://www.aaai.org/Papers/JAIR/Vol22/JAIR-2209.pdf. (I think Felner told us he found a way which is quite better, but I don't remember exactly what it was (bidirectional A*?), but anyhow this should be sufficient (-: ).
Anyhow this course was long ago, so I recommend reading the article..
HTH. Take care.
This site has a nice explanation about 3x3 grids, you could probably extend it to 4x4 quite easily.
By reduction the only possible case you can't solve must be of the form
1 3
2 X
and you want to get it to
1 2
3 X
by using an additional row and column you can move those to the proper positions with a simple precomputed sequence
The solution strategy described by the original poster will always work for a standard solvable 15-puzzle. If Axarydax can reduce a 15-puzzle to the state s/he described and still be unable to solve it, then it was impossible to begin with. Let me explain.
If we treat the blank space in the puzzle as one of the tiles, then each legal move involves swapping that blank "tile" for an adjacent tile. This allows us to regard motions on the puzzle as permutations on 16 characters. That is, elements of the symmetric group S16. Each primitive move is a "swap" or transposition between only two elements (one of which is the blank).
Because the puzzle begins and ends with the blank tile in the lower right, the blank tile must move an even number of times for the puzzle to be solved. (This is easiest to see by imagining an overlaid checkerboard pattern on top of the puzzle -- after an odd number of moves the blank would be on a different color square.) That means that the solution enacted must be a product of evenly many permutations, so it must be an element of the alternating group A16, which has exactly half of S16. (Of the 16! permutations of S16, 16!/2 permutations are even, and 16!/2 are odd. Moreover even*even=even, even*odd = odd, and odd*odd=even.)
If the necessary correcting permutation happens to be odd, it's not possible to solve the puzzle, no matter what you do. If the necessary correcting permutation is even, and if Axarydax follows the strategy described, then the permutation required for the remaining 2x2 block will necessarily be an even permutation fixing the blank square. The only even permutations of only three elements are the rotations 1->2->3->1 (cycle notation (123)) and 1->3->2->1 (cycle notation (132)). These are easily performed on the remaining four squares without disturbing the others.
Since it's implausible that the Axarydax cannot figure out these trivial solutions of the 2x2 blocks, I suspect that either s/he has been pranked, or the 15-puzzle being attempted is nonstandard in some way.
There are always up to 4 move positions from any given one. I wonder whether the simple algorithm that goes over all options building 2-4 tree will reach the "solved" position or the stack overflow :)