views:

119

answers:

4

I am working on an AI bot for the game Defcon. The game has cities, with varying populations, and defensive structures with limited range. I'm trying to work out a good algorithm for placing defence towers.

  • Cities with higher populations are more important to defend
  • Losing a defence tower is a blow, so towers should be placed reasonably close together
  • Towers and cities can only be placed on land

So, with these three rules, we see that the best kind of placement is towers being placed in a ring around the largest population areas (although I don't want an algorithm just to blindly place a ring around the highest area of population, sometime there might be 2 sets of cities far apart, in which case the algorithm should make 2 circles, each one half my total towers).

I'm wondering what kind of algorithms might be used for determining placement of towers?

+1  A: 

Just define a utility function that takes a potential build position as input and returns a "rating" for that position. I imagine it would look something like:

utility(position p) = k1 * population_of_city_at_p +
                      k2 * new_area_covered_if_placed_at_p +
                      k3 * number_of_nearby_defences

(k1, k2, and k3 are arbitrary constants that you'll need to tune)

Then, just randomly sample of bunch of different points, p and choose the one with the highest utility.

Peter Alexander
+2  A: 

I would define a function determines the value of a tower placed at that position. Then search for maxima in that function and place a tower there.

A sketch for the function could look like this:

if water return 0

popsum = sum for all city over (population/distance) // it's better to have towers close by

towersum = - sum for all existing towers (1/distance) // you want you towers spread somewhat evenly

return popsum + towersum*f // f adjusts the relative importance of spreading towers equally and protecting the population centers with many towers 

Should give a reasonable algorithm to start with. For improvement you might change the 1/distance function to something different, to get a faster or slower drop of.

Jens Schauder
+2  A: 

I'd start with implementing a fitness function that calculates the expected protection provided by a set of towers on a given map.

You'd calculate the amount of population inside the "protected" area where areas covered by two towers is rated a bit higher than area covered by only one tower (the exact scaling factor depends a lot on the game mechanics, 'though).

Then you could use a genetic algorithm to experiment with different sets of placements and let that run for several (hundered?) iterations.

If your fitness function is a good fit to the real quality of the placement and your implementation of the genetic algorithm is correct, then you should get a reasonable result.

And once you've done all that you can start developing an attack plan that tries to optimize the casualties for any given set of defense tower placements. Once you have that you can set the two populations against each other and reach even better defense plans this way (that is one of the basic ideas of artificial life).

Joachim Sauer
This sounds like a pretty good idea, although running a GA for defcon would be a little logistically difficult unfortunately. I will look into it however :D
Martin
The game mechanics sound more amenable to simulated annealing than GA; multidimensional continuous valued dimensions are easier in SA than GA's, usually. Both kinds of algorithms get very good approximations to NP-hard problems in reasonable time... perfection isn't always worth the wait.
Andrew McGregor
@McGregor: that may very well be true, I'm simply more familiar with GA than SA ;-)
Joachim Sauer
+1  A: 

I don't know the game but from your description it seems that you need an algorithm similar to the one for solving the (weighted) k-centers problem. Well, unfortunately, this is an NP hard problem so in the best case you'll get an approximation upper bounded by some factor.

Take a look here: http://algo2.iti.kit.edu/vanstee/courses/kcenter.pdf

Victor Hurdugaci
Ooh, this looks interesting, it does look rather a lot like my problem. I'll have a read around on the k-center problem. Thanks
Martin
I don not think it is the same kind of problem. 1. Most games allow placement of pieces only at discrete positions, so a brute force algorithm would be possible and polynomial. 2. I can't see how the k-center problem could possibly result in the ring like structure the OP describes, and which sound plausible.
Jens Schauder
I think Defcon allows units to be placed at floating point positions, so it's not at discrete positions. Think about it this way, we want to minimise the maximum distance from a city to a tower, weighted by population size. Sounds a bit more like the kcenter problem now?
Martin
I still don't think so. In the k-center problem when you have a tower very near to a city, it won't put another there. Also it sounds like you have more towers then cities. Which makes perfectly sense for the game, but in the k-center problem a tower would be placed on each city and the rest spread arbitrarily.
Jens Schauder
I don't want 2 towers near a single city, I have about 6 towers and 20 cities, which is why I want towers to cover the biggest cities (prioritise) and to spread out (to cover a greater area)
Martin