12

Suppose there is a grid containing both walls (blocked cells) as well as food items placed in any location on the grid.

Image of example grid

Now suppose we are trying to decide the optimal location to place an ant colony on this grid, such that the ants have to travel the least distance (in any direction to/from the starting point of the colony) to get the maximum amount of food.

So far, the best approach I've come up with is the following:

for each square on the grid
    use a shortest path algorithm to find the distance to/from each food source from this square
    sum these distances to find a number and put the number in that square
select the square with the smallest number

Would this approach even work? Is there a more efficient solution?

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
Jose
  • 123
  • 4
  • 1
    Optimisation would be to keep track of the shortest distance, and stop calculating any `sum of shortest paths` that exceeds. – tofi9 Feb 07 '15 at 06:33
  • 2
    It's not clear what function you're trying to optimize here. Are the food pellets all the same size? Suppose there's a pellet at (0,0) and another at (4,0). Is it better to have the colony at (0,0) (on top of a pellet, and 4 units from the other pellet), or to have the colony at (2,0) (halfway between the two pellets)? If you value a pellet as foodValue/distance, the first is better. If you value a pellet as foodValue - distance, all positions between the pellets are equally good. Can an ant carry an entire pellet back to the colony in one trip? – rob mayoff Feb 07 '15 at 06:48
  • 2
    @robmayoff I think "such that the ants have to travel the least distance" is pretty clear – OP is trying to minimize the sum of the distances between one particular point and all cells containing food. – The Paramagnetic Croissant Feb 07 '15 at 10:45
  • It's really not obvious. If you want to optimize something, you really need to have a mathematical expression to work against. – Novak Feb 07 '15 at 19:15

2 Answers2

2

Yes, your algorithm works but you can optimize it for the case when [number of food packets] << [number of squares in the grid]. eg. In the above graph.

distances = new int[ROWS][COLS];

for each food-packet on the grid
    use a shortest path algorithm to find the distance to/from each square from this food-packet
    accumulate the distances for each square in the 'distances' array

In the end the distances array would contain the amount of work that an ant colony has to do to capture all the food-packets on the grid. Place the ant colony at the square which has the smallest value.

But note that the asymptotic complexity of this approach remains the same as the algorithm you have given in the question.


P.S Another obvious optimization to your algorithm has been given by taoufiq in the comments. ie. stop calculating any sum of shortest paths that exceeds the shortest distance found till now.

Hope this was useful.

Nikunj Banka
  • 11,117
  • 16
  • 74
  • 112
  • 1
    Thanks! I implemented this algorithm (on top of the Lee Algorithm for finding the shortest paths) and it works. – Jose Feb 08 '15 at 04:34
0

Some optimisations based on a brute-force approach:

  • Keep track of the shortest distance, and stop calculating any sum of shortest paths that exceeds

  • If the Manhattan distance (delta(x) + delta(y)) is longer than the shorted distance ever recorded, stop calculating

  • Combined with the Manhattan distance optimisation: start in the centre of the board or the centre the food packets and work yourself inside-out. The optimal location is more likely to be somewhere in the middle

  • Reduce your search domain to the area between the food packets (i.e. from [1,1] to [6,7], rather than [0,0] to [7,7])

  • Nikunj's optimisation

Further, if your board is really huge, an optimisation solver might be able to reduce the number of calculations. However, your problem seems to be a non-convex problem, and many solvers have issues solving those.

Community
  • 1
  • 1
tofi9
  • 5,775
  • 4
  • 29
  • 50