Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ant simulation: it's better to create a Process/Thread for each Ant or something else?

The simple study is: Ant life simulation

I'm creating an OO structure that see a Class for the Anthill, a Class for the Ant and a Class for the whole simulator.

Now I'm brainstorming on "how to" make Ants 'live'...

I know that there are projects like this just started but I'm brainstorming, I'm not looking for a just-ready-to-eat-dish.

Sincerely I have to make some tests for understand on "what is better", AFAIK Threads, in Python, use less memory than Processes.

What "Ants" have to do when you start the simulation is just: moving around with random direction, if they found food ->eat/bring to the anthill, if they found another ant from another anthill that is transporting food -> attack -> collect food -> do what have to do.... and so on...that means that I have to "share" information across ants and across the whole enviroment.

so I rewrite: It's better to create a Process/Thread for each Ant or something else?

EDIT: In cause of my question "what is better", I'd upvoted all the smart answers that I received, and I also put a comment on them. After my tests, I'll accept the best answer.

like image 249
MiPnamic Avatar asked May 31 '11 14:05

MiPnamic


2 Answers

I would recommend to have a look at stackless. Stackless introduces tasklets which are akind of microthreads which allows to get the benefits of thread-based programming without the performance and complexity problems associated with conventional threads

A possible problem with stackless is, that as far as i know you need to use a modified interpreter or pypy to use the microthreads. However it might be worth it, as there are a some companies that use stackless with great success (eg. for it is used for EVE Online)

Also have a look at greenlet which also offers you a kind of microthreads, without replacing the interpreter. However compared to stackless greenlet only offers a limited featureset.

like image 143
circus Avatar answered Sep 27 '22 18:09

circus


If you don't mind GPL, I suggest you use the Khronos simulation framework, which allows you to define each ant as a generator so you don't need to create threads. The Khronos engine takes care of the scheduling.

I'm actually developing a competing project called GarlicSim which you can also use for your simulation, but for your case Khronos would be better. (Unless you have a problem with GPL.)

like image 28
Ram Rachum Avatar answered Sep 27 '22 20:09

Ram Rachum