Click here to Skip to main content
Click here to Skip to main content

Insect Life Simulation

By , 2 Jan 2004
 

Sample screenshot

Introduction

This program simulates Insect (Bugs or Virus) life cycle. All Insects have some life cycle. They are born, then live for their life period, or die before their life period because of some reason. Here is one Insect life cycle simulation program using some life rules. The rules are listed in the rule section. The program is implemented in C#.

How it works

Once you will run demo program and press Insect>>Draw menu, it will create all possible green boxes on screen. Each box represents Insects. Green color indicates Insect is dead and Red color indicates Insect is alive. (I chose red color for live status because initially I decided the title "Virus Life simulation", and virus are not good whenever they are live, but then I changed the title thinking that the word "Virus" always makes the impression of computer virus for programmers.) You can change the state of an Insect by clicking on it. If you make some Insects live which are near of each other it will generate a pattern followed by life cycle rule.

Life cycle rule.

  1. Insect will always die on end of life. End of life is 10 second (hard coded to get good pattern).
  2. If two Insects are live then it will result in new Insect birth.
  3. If three or more Insects live near, then one Insect will die because of over crowd.

Memory requirement.

This demo program draws 16*16 pixel possible box for each Insect. Each Insect is an instance of the InsectControl class and it holds memory. In my computer, I have 1152*864 screen resolution, which allows me to create ~ 4000 Insects and takes 22MB memory. So, memory requirement depends on screen resolution for this application.

Code description.

I created a user control called InsectControl. This control has a timer with 1 sec interval to update the status of the Insect. Each Insect holds information about its children. Each Insect will have maximum 8 children (see in figure). Insects which are on the corner will have less children. Every second, each Insect will check their child status and follow the above rules to decide its own status.

Sample screenshot

Known Issues:

  1. Cannot accept any command until all Insect creation is finished.
  2. On close, it is not clearing instance of application, so clean it using task manager.

Enjoy simulation.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

ashvin
Web Developer
United States United States
Member
Senior Software Er.
Aspect Communication
San Jose USA CA
 

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
AnswerTry Thismembernkka213 Mar '08 - 0:41 
"On close, it is not clearing instance of application, so clean it using task manager."
 
Try this:
 
insectcontrol.dispose
OR
insectcontrol = nothing
GeneralGranted.memberKinlan4 Jan '04 - 11:59 
Hi,
 
Granted the article may need a little extra work, and although I haven't looked at the code, you can't say that the person who wrote the article can't program. The reason why I state this is because it seems that this aritcle could be the grounding for a agent based simulation (www.agentsheets.com is one I used in university), take the following point, how could a bit array hold state information about every agent in the program? And you "critics" call yourself programmers Smile | :) . Basing each ant/wasp whatever you want to test hypothosis for swarm based systems/agent based systems around an "object" which can hold state information is the correct method, I think.
 
I can't say I have seen the code or if the implemention is "efficient" but could you keep your comments constructive, especially since I can see great promise from this article if given time to let the project take it's course.
 
Paul Kinlan.
GeneralRe: Granted.memberdog_spawn4 Jan '04 - 12:07 
Kinlan wrote:
I haven't looked at the code
 
You really should...
 
Kinlan wrote:
how could a bit array hold state information about every agent in the program?
 
Sigh... that is just an example of the minimum possible footprint. Please look at the code in more detail. I predict you will retract your comment...
 
Kinlan wrote:
And you "critics" call yourself programmers
 
How dare you call me a critic Smile | :)
 
I always thought you could do a "game of life" algo with only a tiny amount of data for each square. Correct me if I'm wrong... but every site I've been to or book I've read says the same thing. I kind of thought that was the whole point.
GeneralRe: Granted.memberKinlan4 Jan '04 - 12:13 
dog_spawn wrote:
Sigh... that is just an example of the minimum possible footprint. Please look at the code in more detail. I predict you will retract your comment...
 
I probably am not going to look at the code, because I think the article needs more work, i.e motivations, conclusions. Agentsheets uses a class per agent (ant for instance) and can have several agents on top of each other. I think that this "idea" would be a good starting ground for a great article.
GeneralRe: Granted.memberKinlan4 Jan '04 - 12:18 
I actually missed out what I was going to say.
 
Really the minimum footprint requires more definate requirements. If it was a simple game of life then yes your assumption is correct. Anything more complex may require a none bit array (integer array). Any intelligence will require an object array at the bare minimum.
 
regards
Paul
GeneralRe: Granted.memberdog_spawn4 Jan '04 - 12:23 
Kinlan wrote:
Any intelligence will require an object array
 
Of course Smile | :) The thing here is how and what type of objects are used. Believe me, the devil is in the detail with this article.
 
You agree with the original comment that it is silly to have a control/object per drawn square? Clearly the author should have coded a custom control to draw the model.
GeneralRe: Granted.memberKinlan4 Jan '04 - 12:31 
Well..... I would have created a container control, that each "Ant/Virus" would know that it has to draw into, but I can see why you would use a UserControl for each square (errrr... If you wanted a smooth refresh of the controls, then only the control that is redrawn has to handle the OnPaint event). It is a trade off for ease of use I think, each "Ant/Virus" can handle it's own destiny so to speak directly on the form. But then again I have not seen the code.
GeneralRe: Granted.memberdzCepheus5 Jan '04 - 12:12 
Ehh...
 
No more 'ease of use' is involved with implementing UserControls than it would be implementing a container control...
 
As for your argument about the insects having intelligence -- there are only three "Life cycle rule" that the simulation must follow... No intelligence is assumed.
 
Thank you, dog_spawn -- minimum memory footprint was exactly what I was trying to explain.
 
Also, I'm not trying to discredit the guy, really -- I mean, he did have the guts to post this to CodeProject -- I have yet to do that myself. :P
 
Skydive -- Testing gravity, one jump at a time.
GeneralRe: Granted.memberKinlan5 Jan '04 - 13:07 
dzCepheus wrote:
As for your argument about the insects having intelligence -- there are only three "Life cycle rule" that the simulation must follow... No intelligence is assumed.
 
Hmm, I think I might have come across wrong, I was trying to imply extenisibility futher than what the three rule suggest, be they emergent properties or not.
 
Ahh well. Smile | :)
GeneralHmm...memberdzCepheus3 Jan '04 - 17:23 
A UserControl for each insect?
 
Wouldn't it be easier and more efficient just to store an two dimensional array of booleans, have the simulation act on that data, then create an in-memory bitmap every pass to render the display to, then copy that in-memory bitmap to the form when the paint event is received?
 
This way, you'd be dealing with far fewer objects for the garbage collector to clean up, plus a lot less memory overhead from all those objects, not to mention the GDI resources they use... Please correct me if I'm wrong, but aren't UserControls considered windows by the operating system?
 
Skydive -- Testing gravity, one jump at a time.
GeneralRe: Hmm...memberdog_spawn4 Jan '04 - 5:10 
Exactly. "4000 Insects and takes 22MB memory" is a joke.
GeneralRe: Hmm...memberdzCepheus4 Jan '04 - 10:24 
4000 insects should only require 500 bytes, if you use an array of bitarray objects, plus any overhead from bitarrays. *sigh*
 
Oh well. Poke tongue | ;-P
 
Skydive -- Testing gravity, one jump at a time.
GeneralRe: Hmm...memberashvin4 Jan '04 - 10:57 
I wrote same program with so many diff combination. One of them is what ever you suggested. But I want to write true object oriented. In this example each object is seperate and holds it own state. each object has timer which check induvidual next status. I know it is wrong idea to create 4000 timer. it is just overhead on system to run 4000 message loops as well. But I just created for Fun. I belive there will be so many good idea to solve same problem. Wink | ;)
 
Ashvin Patel
GeneralIdiotmemberdog_spawn4 Jan '04 - 11:03 
Learn to program idiot Mad | :mad:
GeneralRe: Hmm...memberdzCepheus4 Jan '04 - 11:25 
"Senior Software Er.
Aspect Communication
San Jose USA CA"
 
No offense, Ashvin, but if I were a Senior Software Engineer(?), I would have spent a little more time working up a proper Code Project article. Perhaps something my boss would be proud of...
 
Wish I had your job...
 
Skydive -- Testing gravity, one jump at a time.
GeneralDelete this articlememberdog_spawn4 Jan '04 - 11:09 
Of course. The author has completely missed the point and is clearly of sub-normal intelligence.
 
His profile says he is a "Senior Software Er.". OMG talk about promoted to your level of incompetence.
 
It is a good example of the lengths idiots will go to write massively stupid code.
 
This article should be deleted ASAP.
GeneralRe: Delete this articlememberschor26 Jan '04 - 19:58 
Now that is not an acceptable comment. As the author said, this WAS an exercise in objects that know their own state. WHY the hell does everyone bitch about the design, when anyone can see that this example is illustrative and descriptive of a real solution. Everyone of you so-called experts is griping about the solution but NO one offers concrere illustrative improvement? Go to jump of a cliff. We NEED more concept models, not less. OF COURSE for real world application you would choose different options and implementations methods, BUT that is NOT the point of this example. Smile | :)
GeneralI agree, butmemberdog_spawn27 Jan '04 - 5:57 
What you say is fine, but when you look at the actual implementation you will change your mind. You have to always bear in mind people use this site when learning a programming language. That is why it is so important to make sure the implementation is practical. I totally agree with the spirit of what you are saying, I just don't think it applies to this case. Does that make sense? Big Grin | :-D

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 3 Jan 2004
Article Copyright 2004 by ashvin
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid