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

5

6

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.

Dominant answered 31/5, 2011 at 14:49 Comment(3)
One whole thread per ant seems overkill. They're cheaper than processes, yeah, but they still have some overhead (if only context switching) and in CPython they can't even run in parallel (and even if/when they can, you're still limited by the no. of cores).Dandiprat
The relative costs of processes and threads will be dependent on the underlying OS as well. I agree with @delnan: a thread per ant is overkill and a process per ant is thermonuclear overkill.Mumford
Thank you both (lol for the thermonuclear overkill), what do you suggest?Dominant
F
4

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.

Floor answered 31/5, 2011 at 17:42 Comment(2)
I am curious, how did it work out for you? Did you have a chance to look at Stackless?Floor
I'll test it this weekend with Stackless =) (no freetime sorry) xDDominant
R
3

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.)

Rosenquist answered 1/6, 2011 at 9:37 Comment(1)
Another great point to start from... as I can read on th Khronos website it's "what I need"... I'll try it as soon as possible too, thanksDominant
H
2

I wrote an ant simulation (for finding a good TSP-solution) and a wouldnt recommend a Thread-Solution. I use a loop to calculate for each ant the next step, so my ants do not really behave concurrently (but synchronize after each step).

I don't see any reason to model those ants with Threads. Its no advantage in terms of run-time behavior nor is it an advantage in terms of elegancy (of the code)!

It might be, admittedly, slightly more realistic to use Threads since real ants are concurrent, but for simulations purposes this is IMHO neglectable.

Hoarse answered 31/5, 2011 at 15:17 Comment(1)
Thanks for sharing your experience, I'll start with simulating an amount of 10 ants with Threads, I want to understand "how much" can cost in system resources, then I'll seeDominant
M
1

I agree with @delan - it seems like overkill to allocate a whole thread per Ant, especially if you are looking to scale this to a whole anthill with thousands of the critters running around.

Instead you might consider using a thread to update many ants in a single "cycle". Depending on how you write it - you need to carefully consider what data needs to be shared - you might even be able to use a pool of these threads to scale up your simulation.

Also keep in mind that in CPython the GIL prevents multiple native threads from executing code at the same time.

Mayan answered 31/5, 2011 at 14:59 Comment(1)
One Thread -> Many Ants ... this could be a real alternative, anyway, as you told, I have to consider the shared data... imagine ants from different anthill that have to "fight" for survival...Dominant
C
1

I think the thread solution is the best. Even if ants are individuals, they share the environment, exactly what is permitted when using thread. Process solution corresponds to the reality, it would be arduous to implements communication system with the environment.

Another solution would be to define that ants act only when a tick happen. Then, there is no need to use thread or process.

For example :

import time
...
while True:
    for ant in ants:
        ant.act()
    time.sleep(tickwait)

Meanwhile, this solution is easier to implement.

Clave answered 31/5, 2011 at 15:2 Comment(1)
Easier is a good thing =) in real the Ant have to act continuously cause have to "find" something that can activate triggers, like food, like other ants... so normally ant.move() will loop itself until another "event" were triggeredDominant

© 2022 - 2024 — McMap. All rights reserved.