I'm trying to make an application to calculate a daily route to visit my clients. I can solve whole way by using Genetic Algorithms so far. But I need to limit solution by distance. When I just "cut" the solution path at some point, it becomes a bad solution. Is there a special algorithm for this instance? I'm trying to find and fit one but no luck.
Someone used to do this can give me a recommendetion? I can use vb.NET, c#, php or JAVA.
Thanks.
If you're limiting the distance traveled, then I'm assuming that you're okay with not visiting ALL of your clients every day. If you need to visit ALL of your clients AND you have a maximum distance you want to travel, then all you can do is keep running your TSP algorithm until it (hopefully) produces a solution you're happy with.
If you only want to visit clients within a certain distance of the starting point, then determine the Euclidean distance of each point from the starting point, and filter out those that are too far away. Then run your TSP algorithm on the remaining points.
I'm assuming you instead want to be able to visit as many clients as possible by traveling a maximum distance d. I recommend using a Hill-climbing approach. Start with a valid solution (e.g. just use a greedy approach of taking the next closest unvisited client and stop when the total distance is d), and then randomly modify n nodes in the solution (this could mean reordering them, or this could mean swapping a node for one that isn't currently in the solution; use a sensible heuristic here, you don't want to swap a node for a node that's on the other side of the map, one possible approach is to use a weighted algorithm that favors swaps with closer nodes over more distant nodes) and test to see if the new solution is valid + better than the previous solution. You can always force the new solution to be valid by stripping off the last few clients from the trip.
Maybe you can adjust the TSP or VRP example in OptaPlanner (open source, java) to do your bidding? There's a video that shows how to customize/tailor the constraints to your specific case.
Related
I'm unsure of which algorithm I should use to accomplish this task. I have a graph of nodes. Some nodes are connected with a weighted line that are required to be traversed. However, every node is connected with a weighted, bi-directional line. Only some of the lines must be traversed while the others are just for navigation. I need to find a path to go over all these required lines (bi-directional), but only go over the lines one time. I know which node I must start with.
The real-world problem is that I have a list of edges that need cut from a CNC pattern. I'm trying to decrease the amount of time the CNC machine spends cutting out this pattern. I know I always want to start at the origin, but I don't care where the pattern ends, just as long as all the little pieces in the pattern are cut out. I know how long each edge of the pieces will take to cut out, and the machine is accurate enough that it can lift up the head and go to any point to start from that position. My graph isn't huge, maybe up to 100 nodes in a general case.
This is unlike the travelling-salesman because I don't have to start and end at the same place, and I'm allowed to (and required) to hit a node multiple times.
Djikstras algorithm doesn't work because I need to traverse all the nodes to get all the edges cut... I'm not just trying to find the fastest way from point A to B.
Bonus, I need this implemented in C#, but even if I just knew what algorithm, I can probably get it programmed.
Here is a sample picture of a pattern I need to cut out. Note, there is one diagonal and one arc I forgot to assign a weight to, which can be 50 for the diagonal, and 75 for the arc:
I believe this can be solved as a case of the route inspection problem.
https://en.wikipedia.org/wiki/Route_inspection_problem
You will need ensure that there is a eulerian circuit for the graph, which may achieved through luck or by joining the odd vertices together.
I think this would still reduce to the traveling salesman problem. TSP does not get any easier by removing the return-to-origin rule or allowing multiple visits.
As such there would be no polonomial solution, and your best bet is probably an approximate solution.
Assume you have written a hill climbing algorithm which is very slow. Your smallest data unit is a Node. You have another class called NodeList which contains a list of Nodes plus some other data. You have a list of NodeLists, and their number or order doesn’t change. Your algorithm is trying to decide which Node should go in which NodeList, so that we end up with the best score. After profiling the algorithm, you find out the score calculation is consuming 95% of the CPU time. Can you think of general ways to speed up the algorithm?
I have tried to google it and learn about the basic concept of hill climbing algorithm. But still can't figure out what should I do to improve the algorithm. Any help will be appreciated. Thanks.
There is no all-purpose improvement because if you already have the best possible code it cannot be improved further.
If the paths along which your code would be climbing are very long (probably rare) then you can save time by trying a number of different starts and then only climbing from the best of them - these will tend to be further along their paths.
If you can find a faster but perhaps less accurate version of your score you could check out a lot of neighbors with the less accurate score and try the best of them with the full score. This should make it quicker to find improvements, which should help if you accept the first improvement you come across.
You talk about a score calculation rather than a score update. Hill-climbing tends to make small changes from the existing answer. If you are calculating the score from scratch for each possibility, can you find a way to save time by updating a score you have already calculated for the existing answer, based on the fact that the neighbor of that answer that you are checking out is very similar to it?
You could look again to see if there are other ways of solving the problem than hill-climbing.
I have a game map represented as a tile map. Currently there are two types of objects that are present on the map, relevant to this problem: gatherable resources (trees, rocks, etc.) and buildings built by the player. Buildings are also connected by roads.
I have a problem figuring out an efficient algorithm that could do the following:
find the closest resource to any relevant building (ie. find the closest tree to lumberjack/tree-gatherer)
find the closest relevant building to any building (ie. find the closest storage to any sawmill)
I separated those two issues because the first one does not need roads, but the second one is supposed to only use roads.
So, the result of this should be a single path to a single object, that is the closest to the one I'm figuring it out from. The path is then used by a worker to gather the resource and bring it back, or let's say, to pick a resource from a sawmill and bring it to the closest storage.
I know how to get the closest path itself (A*, Djikstra or even Floyd-Warshall), but I'm not sure how to optimally proceed with multiples of those and getting the best/closest one, especially if it's going to be run very regularly and the map object collections (roads and buildings) is expected to be changing regularly as well.
I'm doing this in Unity3D/c# but I guess this is not really Unity3D-related issue.
How should I proceed?
Finding the geographical distance between two objects is a cheap (quick) operation - you can afford to perform that many times per game tick. Use it if the option is available.
Finding the shortest path by making use of terrain features such as roads, tracks etc. is a much more complex operation. As you already mentioned in your post the A* search algorithm is probably your best option for it, but it is quite slow.
But generally, you should not need to run it too often - just compute the path every X seconds (for some value of X), and make your worker spend the next few game ticks following this computed path, until you "refresh" it. The more precision you have, and more responsiveness to changes to the game environment (e.g. obstacles appearing in your path), the more CPU time you will use.
Try different amounts of precision, and find one that gives decent precision while not being too expensive in terms of CPU time. (The update interval depends purely on the number of calls you are expected to make. Calculating paths for 100 workers is obviously much harder than for 1.)
I have a database of wireless access points and multiple readings (1-n) of signal strength (RSSI) for each access point. Along with this I record the GPS coordinates of each reading.
Now, what I want to do is given visible access points compute my approx. GPS position but I'm not sure how to go about doing this. I've read that I possibly need trilateration however I'm not maths savvy and a lot of articles on this seem to be that way inclined, can someone break it down for my simple mind with code examples (psuedo or otherwise)?
Many of these pages also talk about distance and I'm unsure how best to compute that. One thought is to infer it from the RSSI. Assuming I have at least two readings for a given access point at decreasing RSSI I should be able to roughly infer distance from that just by computing the distance between the GPS coords? I'm making the assumption the lowest RSSI is the nearest to the actual device and not taking into account signal propagation or anything like that.
Any thoughts, points and links would be most appreciated.
I believe you'll need at least four readings from reasonably separated locations for an access point to get a rough idea of where it is, however this only works even vaguely accurately in a vacuum :) Buildings, trees, and other interference will naturally skew the results, often quite dramatically. More readings in that case would improve the accuracy.
The theory is that at each point in space exists within a shell of radius r from the given access point. Theoretically, the signal strength drops off with the inverse square of the distance r, so you need to take that into account as well.
So say you measure a signal strength at some location - you know that the square root of one over the signal strength is proportional to the distance from you to the access point - but that access point could exist anywhere on the surface of the sphere with that radius from you.
A second reading at a different location produces a second sphere which should intersect with the first - however think about two overlapping beach balls and you'll see that the intersection is a circle, anywhere along which the AP could be located. The third reading will intersect with the circle from the first two at two places so now you have two target points where the access point could be located. The fourth and final reading will tell you which of these two it is.
Again, this is all assuming an ideal situation - far from likely in the real world. You'd need to apply ranges, statistical methods, and a lot more than 4 readings etc to get a good guess as to the location of the AP.
Also, the method used above to conceptualize the method to find the AP may not be the best way to actually do this in practice. You'd also need to ensure the user who is trying to locate the AP walks around a bit to get some decent readings.
Other things to consider: where the readings are taken from matters. If you just walk in a straight line or on a flat surface you'll most likely not be able to determine if the AP is to the left or right of the line or below or above you. Your walk must include some movement in all three dimensions. Also, you'll probably want a smattering of trig and Pythagoras to do the actual translation calcs :)
Anyway, I'd love to have an iPhone app that finds APs and puts them on google maps for me so I can figure out where they are and who they belong to... it'd be awesome. But yeah, I haven't tried any of the above, it just seems to me to be the way it would work. I'm happy to be corrected if this turns out to be inaccurate :D
I am developing a Journey Planner website. There are few things that are simple in this case currently i.e. Right now the website will only be able to plan bus routes, the timings of buses are not currently available. So this means we only have bus routes stored in the db and since bus timings are not available so waiting times for traveler are not relevant as well. What is available is the time and distance covered between two stops for an individual bus.
I think that using an undirected weighted graph storing the time and distance costs of each bus stop for each individual bus would be the way to go. Then I could use Dijkstra algorithm to calculate the shortest path between two locations entered by the user based on either time or distance as per user preference. I would find out whether two or three buses are required through simple C# functions if the bus routes intersect at stops and then using those intersection stops for the traveler to change the bus. But there would be an individual graph for each bus. An alternative (not sure if this is correct) way would be to use a graph containing every bus stop of city as nodes and then using this technique to find out the way to travel between two stops. Which is the correct approach? Should I use A* algorithm in place of Dijkstra algo?
A few general points for the design: I would like the app to be extensible so I could add other transportation means later when the need arises. Moreover the bus times could also be added later if possible without major changes to the website. I have seen quite a few experts here who have worked on much complex projects of transportation. So please help me out with the best way to implement this functionality in the most scalable, modular and extensible fashion.
A graph is going to have to be a directional graph - bus stops on opposite sides of the roads (even in a country like the UK that rarely has medians) are NOT the same stop!
I started a similar application last summer and never finished it, but I do have some advice on this graph, and how to structure your data.
My plan was to have each stop as a node, and a path between each of these nodes for every time a bus went through. For example, if a bus stopped every half hour over a 6 hour period, then there would be 12 paths between the two nodes. Time was the main driver behind "cost" of the path, so typically the soonest path would be the one chosen.
Before starting the application would query the database for all paths in the next 5 hours (adjust as appropriate). It would then crunch with Dijkstra's algorithm.
Other things to factor in cost are the actual money cost of the route, transfers (and their cost), stops with no roofs (if you tend to have bad weather), etc.
This plan worked well for me. I live in an area with 3 bus systems.
Finally, it may do you some good to structure your data in a similar way to the Google Transit Feed Specification, as many agencies produce this type of data that you could import.
I think the most important optimization is separating stations where you can change routes and stations where you can't. Then you just need to consider stations where you can change route as intermediate stations in your graph. This should make the graph so small that Dijkstra is fine.
I'm distinguishing nodes with only two edges by simply cutting them out of the graph and instead connecting their two neighbors with an edge of the added length. Then I do pathfinding on this reduced graph which should be much faster. i.e. only consider stations where one might switch routes.
Maybe you can have some use of paddydubs work for TransportDublin found on github.
I coded such an algorithm for a test application. I had a dictionary for each stop, as source and as destination. The algorithm was recursive. Each step of the recursion was like this: Given source and target, it would generate a list of routes going into target, list of routes leaving source. If there were any common stops, we were done, we report the route. If not, then I generate neighboring stops for source, and recurse. The next recursion generates list of neighboring stops for sink, recurse. Before recursion I recorded the previous path of course, and at the end I would have a list.
I do remember I had to place some cutoff conditions because the recursion would sometimes get stuck in certain "bad" regions.
I also looked at this paper:
www.citeulike.org/user/rchauhan/article/819528
I am interested if you managed to solve this problem in a different way.