473,503 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need a help about how to create simulations

1 New Member
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++
Dec 2 '06 #1
2 4175
Ganon11
3,652 Recognized Expert Specialist
OK, what have you done so far?
Dec 2 '06 #2
macklin01
145 New Member
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
Dec 6 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

12
3102
by: rhmd | last post by:
Just found Python and I love it. What an elegant language! I would like to use it for various applications, but the mathematical calculations are way too slow (a million sines 8 seconds in Python...
7
3287
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
2
4420
by: Keith Kowalski | last post by:
I anm opening up a text file reading the lines of the file that refer to a tif image in that file, If the tif image does not exist I need it to send an email stating that the file doesn't exist...
15
4564
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
2
1183
by: skididdy | last post by:
Help!! A company has 20 machines which run 8 hrs a day. 4 personnel are on duty during the 8 hrs. When a mach. goes down, only 1 person can work them at a time. At times, more than 4 mach. are...
20
4222
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site...
28
2631
by: galathaea | last post by:
On Mar 2, 11:29 pm, galath...@veawb.coop (galathaea) wrote: still being very naive about this whole crackpot / crank thing i accidentally let the engineer inside think too hard about this ...
7
1782
by: apollonius2 | last post by:
Greetings, I have been working on a little project today to help me better understand classes in Python (I really like Python). I am a self taught programmer and consider myself to fall in the...
5
1862
by: ram.rachum | last post by:
Hello! I am currently working on writing a simulation engine for special relativity physics. I'm writing it in Python, of course. I'm doing fine with the engine, but I want a GUI framework in which...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.