Ant modeling: is it better to create a process / thread for each Ant or something else?

A simple study is: Ant life modeling

I am creating an OO structure that sees a class for Anthill, a class for Ant, and a class for the entire simulator.

Now I'm brainstorming on how to make ants β€œalive” ...

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

Sincerely, I have to do some tests to understand "better", AFAIK threads, in Python, use less memory than Processes.

The β€œants” should do when you start the simulation, simply: move in a random direction if they find food β†’ eat / bring an anthill if they find another Ant from another ant hill that transports food β†’ attack β†’ collect food β†’ do what what needs to be done .... and so on ... this means that I have to "share" information between ants and the entire environment.

so I rewrite: Is it better to create a process / thread for each Ant or something else?

EDIT: In relation to my question β€œwhich is better,” I would support all the smart answers I received, and also add a comment to them. After my tests, I will accept the best answer.

+6
source share
5 answers

I would recommend looking stackless . Stackless introduces chains, which are a king of microflows, which allows you to get the benefits of streaming 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 microflows. However, it may be worth the fact that there are some companies that use stacks with great success (for example, for EVE Online).

Also look at greenlet , which also offers you a kind of micro-threads without replacing the interpreter. However, compared to a free green network, there is a limited set of features.

+4
source

If you are not against the GPL, I suggest using Khronos a simulation scheme that allows you to define each ant as a generator, so you do not need to create threads. The Chronos engine takes care of planning.

I am actually developing a competing project called GarlicSim , which you can also use for your modeling, but Khronos will be better for your business. (If you have no problem with the GPL.)

+3
source

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

I see no reason to model these ants using Threads. It has no advantage in terms of runtime behavior and is not an advantage in terms of elegance (code)!

Perhaps it would be admittedly a little more realistic to use Threads, since real ants are parallel, but for modeling purposes this IMHO is negligible.

+2
source

I agree with @delan - it seems that overkill allocates the whole chain for Ant, especially if you want to scale it to a solid ant hill, in which thousands of creatures work.

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 you need to use - you can even use the pool of these threads to increase your simulation.

Also keep in mind that in CPython GIL prevents multiple native threads from executing simultaneously.

+1
source

I think the flow solution is the best. Even if ants are individuals, they share the medium, exactly what is allowed when using the stream. The technological solution corresponds to reality; it would be difficult to implement a communication system with the environment.

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

For instance:

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

At the same time, this solution is easier to implement.

+1
source

Source: https://habr.com/ru/post/889458/


All Articles