# Would Mathematica be your first choice for agent-based modeling?

**URL:** <https://community.wolfram.com/t/would-mathematica-be-your-first-choice-for-agent-based-modeling/8748>\
**Category:** Questions\
**Tags:** mathematica, modeling\
**Created:** [June 14, 2016, 6:52am UTC](https://community.wolfram.com/t/would-mathematica-be-your-first-choice-for-agent-based-modeling/8748 "2016-06-14T06:52:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahcvankampen](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/ahcvankampen/32/12564_2.png) [@ahcvankampen](https://community.wolfram.com/u/ahcvankampen)\
**Post date:** [June 14, 2016, 6:52am UTC](https://community.wolfram.com/t/would-mathematica-be-your-first-choice-for-agent-based-modeling/8748/1 "2016-06-14T06:52:39Z")

</div>

Hi,  
I am trying to find the best environment for doing Agent Based modelling.

I could program myself (e.g., C, Phyton, …), use the statistical environment R, or dedicated programs like NetLogo or CompuCell3D. Each has it’s cons and pro’s.

What about Mathematica? Is it suitable for doing ABM? (I noticed some of the demo’s). Is it fast? Is it flexible? Any other thoughts?

Thanks,  
Antoine

---

<div class="post-metadata">

**Author:** ![fkampas](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/fkampas/32/41217_2.png) [@fkampas](https://community.wolfram.com/u/fkampas)\
**Post date:** [June 15, 2016, 3:05pm UTC](https://community.wolfram.com/t/would-mathematica-be-your-first-choice-for-agent-based-modeling/8748/2 "2016-06-15T15:05:53Z")

</div>

What are the requirements for doing agent-based modeling? Mathematica is very flexible but not as fast as a low level language.

---

<div class="post-metadata">

**Author:** ![ahcvankampen](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/ahcvankampen/32/12564_2.png) [@ahcvankampen](https://community.wolfram.com/u/ahcvankampen)\
**Post date:** [June 15, 2016, 6:49pm UTC](https://community.wolfram.com/t/would-mathematica-be-your-first-choice-for-agent-based-modeling/8748/3 "2016-06-15T18:49:01Z")

</div>

I am aiming to simulated different types of immune cells (about 15000 in total), The simulation would comprise about 21 days in resolution of 1 hour. The cells can interact. And the will have to respond to a chemokine gradient, which I want to calculated with a pdf (diffusion equation). I assume that ODE/PDE/ABM can easily be integrated within mathematica (one reason for wanting to try M).

I understand that low-level languages are faster (and probably even more flexible) but if I can use pre-existing function from mathematica then this may save time.

---

<div class="post-metadata">

**Author:** ![fkampas](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/fkampas/32/41217_2.png) [@fkampas](https://community.wolfram.com/u/fkampas)\
**Post date:** [June 15, 2016, 7:35pm UTC](https://community.wolfram.com/t/would-mathematica-be-your-first-choice-for-agent-based-modeling/8748/4 "2016-06-15T19:35:09Z")

</div>

do all the cells interact with each other? that may make the problem too big.

---

<div class="post-metadata">

**Author:** ![samcarrettie](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/samcarrettie/32/29668_2.png) [@samcarrettie](https://community.wolfram.com/u/samcarrettie)\
**Post date:** [June 15, 2016, 7:44pm UTC](https://community.wolfram.com/t/would-mathematica-be-your-first-choice-for-agent-based-modeling/8748/5 "2016-06-15T19:44:29Z")

</div>

Slow or fast depends on your code. Single function in Mathematica is basically C code. Things gets slow when you compose many top level functions. But it is still can be fast if it is well architected. Also I expect simple interactions to be simple functions - so you could [**Compile**](http://reference.wolfram.com/language/ref/Compile.html). Also see

[http://demonstrations.wolfram.com/search.html?query=agent](http://demonstrations.wolfram.com/search.html?query=agent)

---

<div class="post-metadata">

**Author:** ![ahcvankampen](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/ahcvankampen/32/12564_2.png) [@ahcvankampen](https://community.wolfram.com/u/ahcvankampen)\
**Post date:** [June 15, 2016, 8:09pm UTC](https://community.wolfram.com/t/would-mathematica-be-your-first-choice-for-agent-based-modeling/8748/6 "2016-06-15T20:09:36Z")

</div>

Hi,  
Only cells close together would interact with each other. But each cell should sense the gradient calculated by the PDE  
a

---

<div class="post-metadata">

**Author:** ![fkampas](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/fkampas/32/41217_2.png) [@fkampas](https://community.wolfram.com/u/fkampas)\
**Post date:** [June 15, 2016, 8:18pm UTC](https://community.wolfram.com/t/would-mathematica-be-your-first-choice-for-agent-based-modeling/8748/7 "2016-06-15T20:18:19Z")

</div>

sounds like it should work

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/szhorvat/32/3531_2.png) [@szhorvat](https://community.wolfram.com/u/szhorvat)\
**Post date:** [June 16, 2016, 6:17pm UTC](https://community.wolfram.com/t/would-mathematica-be-your-first-choice-for-agent-based-modeling/8748/8 "2016-06-16T18:17:58Z")

</div>

I would not use Mathematica, Python, or any other interpreted language for this (especially not R which is probably the slowest of the bunch). Except maybe for prototyping.

Mathematica is very flexible, but not fast, especially for this type of code, which often requires procedural programming. People have done agent-based models in Mathematica. You will find a few posts on this website. But once you’re done with a prototype and know how your simulation needs to work, you might easily get very frustrated with the performance.

I would write the simulation in a low-level language such as C++, C or Java, then control it form Mathematica. Expect _orders of magnitude_ higher performance when programmed carefully. If you go this route and decide to integrate the simulation into Mathematica, take a look at [LibraryLink](https://wolfram.discourse.group/t/1349/1) for C/C++ and J/Link for Java. If you go with LibraryLink, also take a look at my package [LTemplate](http://mathematica.stackexchange.com/a/96128/12). I hope eventually I’ll have time to post some examples about how to easily integrate simulation code into Mathematica.

NetLogo may be worth trying even if it is a high level language. I am not very familiar with its performance characteristics, but it does have some significant advantages:

- it is specifically made for agent based modelling, so it should be quick to set up a prototype
- as I remember it can solve some sorts of simple PDEs, e.g. diffusion
- it can be controlled from Mathematica!
