Connecting Tech Pros Worldwide Help | Site Map

Need a help about how to create simulations

Newbie
 
Join Date: Dec 2006
Posts: 1
#1: Dec 2 '06
Ps i need help about how to create a simulation of doodlebugs life

Problem: anaylysis

The goal for this problem is to create a simple two-dimensional predator-prey simulation. In the simulation the prey are ants and the predators are doodlebugs. These critters live in a world composed of a 20x20 grid of cells. Only one critter may occupy a cell at a time. The grid is enclosed, so a critter is not allowed to move off the edges of the world. Time is simulated in time steps. Each critter performs some action every time step.

The ants behave according to the following model.
• Move. Every time step randomly try to move up, down, left, or right. If the neighbouring cell in the selected direction is occupied or would move the ant off the grid, then the ant stays in the current cell.
• Breed. If an ant survives for three time steps, then at the end of the time step (that is; after moving) the ant will breed. This is simulated by creating a new ant in adjacent (up, down, left, or right) cell that is empty. If there is no empty cell available, then no breeding occurs. Once an offspring is produced an ant cannot produce an offspring until three more steps have elapsed.

The doodlebugs behave according to the following model:
• Move. Every time step, if there is an adjacent ant (up, down, left, or right), then the doodlebug will move to that cell and eat the ant. Otherwise, the doodlebug moves according to the same rules as the ant. Note that a doodlebug cannot eat other doodlebugs.
• Breed. If a doodlebug survives for eight-time step, then at the end of time step it will spawn off a new doodlebug in the same manner as the ant.
• Starve. If a doodlebug has not eaten an ant within the last three time steps, then at the end of the third time step it will starve and die. The doodlebug should then be removed from the grid of cells.

During one turn, the doodlebugs should move before the ants do.
This has to be done using C++
Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,428
#2: Dec 2 '06

re: Need a help about how to create simulations


OK, what have you done so far?
Familiar Sight
 
Join Date: Aug 2005
Location: Houston, TX
Posts: 143
#3: Dec 6 '06

re: Need a help about how to create simulations


I'd make two classes: Doodlebug and Ant.

Each class should have a linked list structure. (doubly-linked would be better suited to this problem.) Whenever a new ant is born, you insert a new ant into the linked list. Whenever an ant dies, you likewise delete the ant from the linked list. (This is where the double-linking comes in handy.)

Each class should have integer variables like "StepsUntilDeath" and "StepsUntilBreeding". You decrement each at the end of a time step, or reset them after successful breeding or eating.

A good structure might be something like this:

Expand|Select|Wrap|Line Numbers
  1. class Doodlebug{
  2. private:
  3.  Doodlebug* pPreviousDoodleBug;
  4.  Doodlebug* pNextDoodleBug;
  5.  
  6.  int StepsUntilBreeding;
  7.  int StepsUntilDeath;
  8. public:
  9.  int PositionX;
  10.  int PositionY;
  11.  
  12.  Doodlebug();
  13.  ~Doodlebug();
  14.  bool MoveDoodlebug();
  15.  bool BreedDoodlebug();
  16.  bool UpdateGridWithPosition();
  17.  bool UpdateDoodlebug();
  18. }
  19.  
I'd use the "UpdateDoodlebug()" function to do everything: call the "move" function and try to move, try to eat, try to breed, and update the grid. (remove any markers from the previous cell, and add a marker to the new cell.)

You could update every ant and every doodlebug by running through both linked lists. One caution, though: running through the linked list in the same order every time can introduce bias that you'll observe in the results! It might be better to update in a random order. (This problem has been observed in cellular automata and agent-based scientific modeling/computation.)

It looks like you'll want an array to keep track of what's occupying each square. You could just as well make it a character array with entries "E" (empty), "A" (ant), or "D" (doodlebug). Or you could make your 2D array a bitmap image and save it as every time step, so you could easily view your simulation and create animations later. Of course, more complex structures are possible. (e.g., an array of Ant memory addresses, and an array of Doodlebug memory addresses. If there's no ant, it's NULL, and otherwise, it has a pointer to the precise ant occupying that cell. This would make it easier to determine which ant is eaten by a Doodlebug.)

Simpler structures are possible, too, of course. You could just go with an integer array. 0-99 denotes an ant, and 100-199 denotes a doodlebug. Any negative number is unoccupied. Then, different values in that range can encode both the time until the next breeding and the time until death.

Have fun with your project--it sounds like a blast!! -- Paul
Reply