Question

I'm trying to implement a model of predator-prey. It is agent-based model. Every few milliseconds is a new move. On the field there are two types of creatures: predator and prey. The behavior of each of them is given by the following rules:

Prey:

  1. Just moved to an unoccupied cell
  2. Every few steps creates offspring to his old cell
  3. Life expectancy is limited by the number of moves

Predator:

  1. Predator moves to the cell with prey. If such cells are not, in any free neighboring cell
  2. Same
  3. Same

I have a problem with the choice of prey move. Prey

For example, I have preys in cells 5 and 9. Each of them can move to cell 6. How can I resolve this conflict? Thanks

Was it helpful?

Solution

Use asynchronous updating. Iterate through the prey in random order, having them decide in turn to which cell they should move.

This is a common approach in simulations. It has an additional benefit in that it eliminates limit cycles in the dynamics.

OTHER TIPS

How long does 'moving' take? If you move one, then after the prey has moved, you move the next one, there is no conflict. The prey will simply see the space is already occupied and move elsewhere.

If moving takes time you might say the prey keep an eye on each other and see if some other prey is trying to move somewhere (like people watch cars in traffic). Then you would change the status of the target field to 'reserved for 5' when prey from 5 is trying to move there. Then prey from 9 can see this and decide if they want to collide with 5 (could be intresting :P) or avoid 5.

Depends on game logic. If preys can be on the same cell, so simply use indicator that show preys count. If you are using 2D array for representing current field state you can use such codes:

-1 - predator
n - preys

n >= 0, (n = 0 - cell is empty, n = 1 cell contains 1 prey and so on).

Otherwise (if preys can't appear on the same cell) use turn-based strategy. Save all your preys in array or give number to each prey. In that case preys' moves represents by simple loop (pseudocode):

for each prey in preys
    move(prey)
end

where move logic describes algorithm how your prey moves.

Quite a few ways, depending on if you're deciding & moving as two steps or one, etc:

  1. Keep track of each prey's intended moves, and prevent other prey from occupying those.
  2. Check if another prey is already occupying the destination, and do nothing if so.
  3. Remove one of the preys at random if they both try to occupy the same location.
  4. Re-evaluate the move options if the destination is occupied.

There's not really a 'right' way to do it.

See this related question and my answer. It describes a good collision detection mechanism.

Avoid O(n^2) complexity for collision detection

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top