Issue with min cost path - algorithm

I was solving min cost path problem through dynamic approach but suddenly I realised that greedy approach is also working.
I applied greedy like this :
choose the min of bottom, right and diagonal cost and move in the min cost path.
1 2 3
4 8 2
1 5 3
where numbers are cost which will be added to the required cost if we include that point.
path from 1 to 3 is 12 through greedy is 8.
If my approach doesn't follow all examples, the what is that example?

How about a map such as:
1 1 1
2 10 10
1 1 1
Your greedy approach will end up taking 1+1+1+10+1, instead of 1+2+1+1

Greedy algorithms can be 'beaten' by giving them a long meandering path with several small steps:
2 2 e
2 ∞ 0
s 3 0
In this case, going from s to e will require either
The greedy solution: See that 2 is smaller and slog through several three 2's for a total cost of 6
A dynamic solution: See that after the three there is an easy path for a total cost of 3.
Also, I'd take a look at how your two algorithms define length. The optimal path is actually 1 -> 4 -> 2 -> 3 which has a cost of 10. If your dynamic solution isn't returning that, it may indicate that something else is going on.

Related

Triangle max sum using depth first search

I have the classic problem with a triangle and need to get the max path. I am allowed to move from (i,j) to (i,j-1), (i, j+1), (i+1,j)
Example input:
1
1 2 3
1 2 3 4 5
1 2 3 4 5 6 7
and the maximum path is the sum of (1) + (2 + 3) + (4 + 3 + 2 + 1) + (2 + 3 + 4 + 5 + 6 + 7). I am not allowed to move on a node twice
I know how to solve this using DP but this problem has been shown to us at the Artificial Intelligence class and the solution it needs is using DFS/GBFS
How is it possible to solve this problem using DFS? Only recursive came to my mind but it's not near close to DFS.
I am representing the input as a graph, so for the following
1
2 3 4
5 6 7 8 9
I have the following graph
1 -> {3}, 3 -> {2, 4, 7}, 4 -> {3, 8} etc
I was thinking of doing a recursive function, MaxSum(node) and start from node 1 and do something like that
return Max(MaxSum(neighbour_1), MaxSum(neighbour_2), ..., MaxSum(neighbour_n)) + node
where each neighbour_i is unvisited neighbour
But where does the DFS part come in?
Also, how could I solve this problem using GBFS?
I am not interested in code or something, only algorithmic explanations
But where does the DFS part come in?
The graph you have is acyclic and the problem you are trying to solve is longest path problem (after transforming the node weights to edge weights). The general solution requires you to use DFS to find a topological order but in your case you know an easy topogical order, the one just like in your example:
1
2 3 4
5 6 7 8 9
You use this order for second part - the DP solution. So in a way, you are skipping the DFS part.
You could explicitly run DFS for the first path and it could give a different topological order (depending on how you traverse the edges) but it's just a waste of effort.
Also for the second part, instead of performing the DP you could use BFS level by level to update the neighbors of the elements in the current level. But again it doesn't make much sense since using DP would give you the same results and would even be cheaper.

Should ant colony algorithm show best path in 100% cases?

I developed ant colony algorithm. It is working quite good at the moment.
In some moot points it can show not best path, but close to best one.
For example, I have this graph:
Matrix is:
1 2 3 4 5 6 7
1 0 6 5 0 0 2 0
2 6 0 3 2 1 5 0
3 5 3 0 2 5 0 0
4 0 2 2 0 3 0 0
5 0 1 5 3 0 6 0
6 2 5 0 0 6 0 2
7 0 0 0 0 0 2 0
First col and first row are vertex names.
So possible paths are (path - length of the path):
1. 1-2-5 with length 7
2. 1-6-2-5 with length 8
3. 1-6-5 with length 8
My programm is choosing 1st path in 1/10 starts, 2nd path in 7/10 starts and 3rd path in 2/10 start of the programm.
Is it working correct?
Explanation for this is ants has their own eyes (vision, they look at edge length) and also they can detect pheromone level. Own eyes shows for them, that 1-2 edge is rather long and longer then edge 1-6, so in generally they will choose edge 1-6 instead of choosing 1-2. Same for 6-5 and 6-2: 6-2 is more attractive, because it is shorter.
Am I right with my assumption?
According to this: http://en.wikipedia.org/wiki/Ant_colony_optimization_algorithms#Summary , I can see 2 problems in your approach:
ants (initially) wander randomly; it has nothing to do with vision or the adjacent edge length
do you model those pheromone trails at all?
Answering the question: Should ant colony algorithm show best path in 100% cases? No, it doesn't need to show the best path at all.
In ant colony optimization algorithms, the ants have probabilities for each possible step while walking through the graph. Tipically, this probability is based on two factors: a local and a global measure of quality.
The global measure is usually associated with the pheromone deposit in an edge, since pheromone is added to each edge used in the path followed by an ant and the amount added is somehow related to the quality of the solution created by such ant.
The local measure is usually related to the quality of a particular step: the cost of an edge, in the example provided.
Therefore, if your ants are taking only greedy actions, it is possible that the probability function you are using is giving too much weight to local quality. Finding a probability function that exhibit a good compromise between local and global search is a fundamental aspect of a successfully applied ACO strategy.
Why are you using ant colony for shortest path? If you are searching shortest path you don't need optimization algorithm, best solution can be achieved with polynomial time with A* algorithm (with optimal heuristic function). Ant colony is better when you are using it for TSP problem.
And the answer is: no - keep in mind that algorithm is probabilistic so it may not lead to best solution but to local minimum

What is a good individual representation for a closed path planning task using genetic algorithm?

There is a n*n grid and in one of the cells of the grid lies an agent A.
A can travel T number of cells.
Each cell in the grid has some weight and the path for A has to maximize that weight.
A also has to return to its starting position within its traveling range T.
What can be a good individual representation to represent the paths?
Methods I have tried:
Chromosome is a list of coordinates.
Chromosome is a list of directions. Each gene is a direction like up, down, up-right, etc. Path never breaks in the middle.
Problems with both methods is that crossing-over almost always generates invalid paths. Paths become broken in the middle. They don't form a closed path. I can't seem to figure out a good way to represent the individual solution and an appropriate crossing-over method. Please help.
First of all, I would say that this problem is a better fit for other approaches, such as maybe ant colony optimization, greedy approaches that give good enough solutions etc. GAs might not work so well for the exact reason you describe.
However, if you must use GAs, here are two possible models that might be worth investigating:
Severely punish invalid paths by giving invalid moves a cost of -infinity. For example, if your chromosome says go from a cell x to an unreachable cell y, consider the cost of y -infinity. This might be worth combining with a low probability of crossover happening, something like 5% maybe.
Don't do crossover, just do some form of more involved mutation of the offspring.
If you want to get even fancier, this is somewhat similar to the travelling salesman problem, which has a lot of research in relation to genetic algorithms:
http://www.lalena.com/AI/Tsp/
http://www.math.hmc.edu/seniorthesis/archives/2001/kbryant/kbryant-2001-thesis.pdf
You could encode the path as a reference list:
Assume these are your locations (1 2 3 4 5 6 7 8 9)
A subset route of (1 2 3 4 8) could be encoded (1 1 2 1 4).
Now take two parents
p1 = (1 1 2 1 | 4 1 3 1 1)
p2 = (5 1 5 5 | 5 3 3 2 1)
which will produce
o1 = (1 1 2 1 5 3 3 2 1)
o2 = (5 1 5 5 4 1 3 1 1)
which will be decoded into these location routes
o1 = 1 – 2 – 4 – 3 – 9 – 7 – 8 – 6 – 5
o2 = 5 – 1 – 7 – 8 – 6 – 2 – 9 – 3 – 4
This way, a crossover will always yield valid results (whether this representation will help you solving your problem better is a different question).
Some additional information can be found here.

Is there better way than a Dijkstra algorithm for finding fastest path that do not exceed specified cost

I'm having a problem with finding fastest path that do not exceed specified cost.
There's similar question to this one, however there's a big difference between them.
Here, the only records that can appear in the data are the ones, that lead from a lower point to a higher point (eg. 1 -> 3 might appear but 3 -> 1 might not) (see below). Without knowing that, I'd use Dijkstra. That additional information, might let it do in a time faster than Dijkstras algorithm.
What do you think about it?
Let's say I've got specified maximum cost, and 4 records.
// specified cost
10
// end point
5
//(start point) (finish point) (time) (cost)
2 5 50 5
3 5 20 9
1 2 30 5
1 3 30 7
// all the records lead from a lower to a higher point no.
I have to decide, whether It's possible to get from point (1) to (5) (its impossible when theres no path that costs <= than we've got or when theres no connection between 1-5) and if so, what would be the fastest way to get in there.
The output for such data would be:
80 // fastest time
3 1 // number of points that (1 -> 2) -> (2 -> 5)
Keep in mind, that if there's a record saying you can move 1->2
1 2 30 5
It doesnt allow you to move 2<-1.
For each node at depth n, the minimum cost of path to it is n/2 * (minimal first edge at the path + minimal edge connecting to the node) - sum of arithmetic series. If this computation exceeds the required maximum, no need to check the nodes that follow. Cut these nodes off and apply Dijkstra on the rest.

Adding waypoints to A* graph search

I have the ability to calculate the best route between a start and end point using A*. Right now, I am including waypoints between my start and end points by applying A* to the pairs in all permutations of my points.
Example:
I want to get from point 1 to point 4. Additionally, I want to pass through points 2 and 3.
I calculate the permutations of (1, 2, 3, 4):
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 2 3
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 1 3
2 4 3 1
3 1 2 4
3 1 4 2
3 2 1 4
3 2 4 1
3 4 1 2
3 4 2 1
4 1 2 3
4 1 3 2
4 2 1 3
4 2 3 1
4 3 1 2
4 3 2 1
Then, for each permutation, I calculate the A* route from the first to the second, then append it to the route from the second to the third, then the third to the fourth.
When I have this calculated for each permutation, I sort the routes by distance and return the shortest.
Obviously, this works but involves a lot of calculation and totally collapses when I have 6 waypoints (permutations of 8 items is 40320 :-))
Is there a better way to do this?
First of all, you should store all intermediate calculations. Once you calculated the route from 1 to 2, you should never recalculate it again, just look up in a table.
Second, if your graph is undirected, a route from 2 to 1 has exactly the same distance as a route from 1 to 2, so you should not recalculate it either.
And finally, in any case you will have an algorithm that is exponential to the number of points you need to pass. This is very similar to the traveling salesman problem, and it will be exactly this problem if you include all available points. The problem is NP-complete, i.e. it has complexity, exponential to the number of waypoints.
So if you have a lot of points that you must pass, exponential collapse is inevitable.
As a previous answer mentioned, this problem is the NP-complete Traveling Salesperson Problem.
There is a better method than the one you use. The state-of-the-art TSP solver is due to Georgia Tech's Concorde solver. If you can't simply use their freely available program in your own or use their API, I can describe the basic techniques they use.
To solve the TSP, they start with a greedy heuristic called the Lin-Kernighan heuristic to generate an upper bound. Then they use branch-and-cut on a mixed integer programming formulation of the TSP. This means they write a series of linear and integer constraints which, when solved, gives you the optimal path of the TSP. Their inner loop calls a linear programming solver such as Qsopt or Cplex to get a lower bound.
As I mentioned, this is the state-of-the-art so if you're looking for a better way to solve the TSP than what you're doing, here is the best. They can handle over 10,000 cities in a few seconds, especially on the symmmetric, planar TSP (which I suspect is the variant you're working on).
If the number of waypoints you need to eventually handle is small, say on the order of 10 to 15, then you may be able to do a branch-and-bound search using the minimum spanning tree heuristic. This is a textbook exercise in many introductory AI courses. More waypoints than that you will probably outlive the actual running time of the algorithm, and you will have to use Concorde instead.

Resources