• Create Account

## Predator-Prey simulation

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

10 replies to this topic

### #1sooner123  Members

269
Like
0Likes
Like

Posted 16 December 2012 - 07:43 AM

I want to write a simplistic predator-prey simulation in which predators and prey move about on a grid, avoiding/devouring each other.

For the first iteration, I want an Entity base class, a Creature class which inherits from Entity. (later on a Food class which inherits from Entity as well), and Predator and Prey classes which inherit from Entity.

Predator and Prey will move about on the grid randomly (for now) and if a predator happens to stumble on a prey's square, said prey gets deleted and said predator's fitness is augmented.

I'm having some trouble coming up with a good design for the logic of everything.

Basically my first idea was to have 'grid' be a 2d array of pointers to type Entity and 'entities' be an array of pointers to type Entity. This way I can loop through the grid to output the state to the screen and loop through entities to perform their actions AND have access to the state of a specific square via 'grid' in order to determine the result of a member of 'entities' moving to its target square.

This seems redundant and runs into the following issue: what if two predators or two prey occupy the same square. An element of 'grid' is a pointer to only one Entity. Is the solution to make 'grid' be a 2d array of pointers to type ListOfEntities? I can't decide how to handle this issue because the other solution seems to be to have Entities being aware of their x,y location, and then when I'm outputting the grid, I need to scan through every entity, for every square, in order to determine where everything is. And in the logic of predator/prey moving around, I have to scan through every entity to see which entities are at the target square.

Also my thinking is that an Entity should not be aware of its x and y location and that that logic should be handled fully by the indexing of the 'grid' array. Does this seem like good design of the data structures?

Edited by sooner123, 16 December 2012 - 07:47 AM.

### #2Álvaro  Members

20272
Like
1Likes
Like

Posted 16 December 2012 - 12:19 PM

I would avoid the class hierarchy if at all possible. I don't think you'll get much from it.

I don't know enough details of what you are trying to do to suggest a design. In particular, will the animals have internal state? (hit points, hunger level, age...)

### #3eppo  Members

4601
Like
1Likes
Like

Posted 16 December 2012 - 01:46 PM

Storing creatures in two separate structures (a list and a grid of lists) is a practical idea. You can iterate over the items in the list to perform general creature actions and use the grid to sample for neighboring animals.

Removing an item from one set means having to remove it from the other as well, though. To avoid having to let entities keep a reference to their current grid-cell, you could in a first pass perform the preying/eating/etc., flag killed animals for deletion, and then later sweep over the grid to remove flagged items.

Using a class hierarchy seems needlessly complicated. What are the similarities between prey and predator to have them both derive from a single base-class? And how would you (virtually) distinct between items when you grab an 'Entity'-item from a list?

Edited by eppo, 16 December 2012 - 01:47 PM.

### #4sooner123  Members

269
Like
0Likes
Like

Posted 16 December 2012 - 02:46 PM

Was going to have a member of the class called 'designation' that would be equal to "predator" or "prey." Seemed like a bad way to do it, but it also seemed better than messing with typeinfo stuff.

I'll ditch the class hierarchy. It wasn't benefiting me at all.

I was going to do it the way you mentioned (with the pass over the creature list, then the sweep over the grid) Hadn't thought about how I needed to delete both though. The flagging seems like a perfect way to handle it.

As for internal state of animals, for now the only information creatures will store is a fitness value for predators. Later on there will be reproduction counters and some genetic data that hopefully I can map to behavior (probably a series of simplistic weights on vectors of proximity to food/predator/prey)

### #5sooner123  Members

269
Like
0Likes
Like

Posted 16 December 2012 - 03:54 PM

One problem with that two pass method is that the consequences of actions of an entity can depend on an action taken by an entity encountered earlier in the same pass.

So I'm back to not being sure how to handle this without entities being explicitly aware of their location which just seems wrong to me.

Example: a prey moves off a square, while I'm cycling through all the creatures. i need to remove this creature from the list of creatures for that square. in order to do that i need to know which square the creature was on.

Is it wrong of me to think that entities being aware of their location is wrong? I'm not sure why it strikes me that way but I'm trying to look at it from the point of view of the creature, who doesn't understand their x/y location. Only their location relative to other creatures.

Edited by sooner123, 16 December 2012 - 03:57 PM.

### #6Álvaro  Members

20272
Like
1Likes
Like

Posted 16 December 2012 - 04:17 PM

Making the location part of the entity doesn't fell wrong to me at all. If you insist on not having it, you'll have to pass the position to the function that allows a prey to move [and many other functions].

EDIT: You could also make the function return what the creature wants to do, and the code that asked the creature (which knows about the location) is now responsible for executing the action.

Edited by Álvaro, 16 December 2012 - 04:19 PM.

### #7eppo  Members

4601
Like
1Likes
Like

Posted 16 December 2012 - 04:44 PM

If a creature object has no concept of if or where it is in a list, you'll have to do a linear search over all the list's elements until you find it, before you can remove it. If you have a lot of creatures, this might be too expensive. If you quickly and immediately need to remove an object, the following isn't too bad:

class Grid{

class Cel{ List<Animal> animals; }

Cel cels[][];
}

class Animal{

Grid::Cel *cel;

setPos(Coord pos){ cel->remove(this); (cel = grid->celGet(pos))->add(this); }
}


Unless even a single cel-list becomes too long to traverse, in which case you could make the animal point directly to a container object that encapsulates a pointer to itself.

Edited by eppo, 16 December 2012 - 04:46 PM.

### #8sooner123  Members

269
Like
1Likes
Like

Posted 16 December 2012 - 10:10 PM

Wow. A lot of that syntax is totally different from anything I ever knew would work. I had to do some googling to understand it all. I wasn't even aware that assignment returned a reference to the lefthand operand. That's awesome. Love the way that single line of code reassigns the cel reference for that Animal and simultaneously adds the Animal to that cel in the grid. Very clever. I have tons to learn.

I really appreciate both your help. Thanks a lot

### #9LorenzoGatti  Members

4094
Like
0Likes
Like

Posted 17 December 2012 - 04:24 AM

If you are worried by race conditions which cause the simulation results to depend on the order in which entities are processed within an update, you are trying to implement incorrect and inconsistent rules. There are at least two rather incompatible fixes:
• Process an event queue. After an initial random assignment of priorities to the starting population, you process one entity at a time, putting it back into the priority queue to schedule its next move. No conflicts, and you can support different durations for different actions. You can purge dead entities when they come to the top of the queue, no need to search whenever someone is eaten. Downsides: probably, low cache coherence (successive scheduled entities aren't close in the map).
• Process simultaneous actions, keeping two separate state representations for each update: "before" (read-only) and "after" (write-only). If entities can only affect adjacent cells (by moving there, for example) you could split the map into 3 or 9 subsets of cells that cannot possibly interfere with each other, and process them in a round-robin fashion; or you can more simply choose tie-breaking rules (e.g. if multiple entities try to enter a square, all but the eldest fail and skip a turn)

Omae Wa Mou Shindeiru

### #10sooner123  Members

269
Like
0Likes
Like

Posted 17 December 2012 - 01:42 PM

Yep I am very concerned about simultaneity issues. An issue that also plagued an implementation of a neural network simulation I helped someone write a few years ago.

I'm not sure what you mean by the "two separate state representations for each update" though. Example, prey on A1, predator on C1, prey wants to move to B1 and predator wants to move to B1 in same round, which I might ideally want to result in prey being eaten, regardless of the order in which I process their actions. How would your second suggestion apply to this scenario?

1065
Like
1Likes
Like

Posted 17 December 2012 - 02:43 PM

I'm not entirely certain if this is what Lorenzo was suggesting, but if you have an array indicating where everything would be after their moves on a given turn, you could populate it by looping through all of your entities and assigning movements to them. Then, after they have all decided where to move, check which areas contain two or more entities (you wouldn't need to check every tile, only ones with at least one entity) and process their actions that way (if one is a prey and the other is a predator, just kill the prey; or, if both are predators, move one of them to an adjacent empty tile, etc.).

I think that would give you your desired effect.

Inspiration from my tea:

"Never wish life were easier. Wish that you were better" -Jim Rohn

soundcloud.com/herwrathmustbedragons

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.