Python or JavaScript scripting for NetLogo?
Asked Answered
S

5

9

NetLogo is excellent for agent-based modeling...except for the language. I always find myself contorting my brain trying to figure out how to do something that should be simple to code (such as implementing a simple case statement) in NetLogo's Logo implementation. Logo is just not a programmer's language (apologies to those infuriated by this assertion).

I saw Abe Gong's Tengolo project that purported to do just this (http://compsocsci.blogspot.com/2012/02/announcing-tengolo-python-alternative.html) but the project appears to have been abandoned. Also another question in stack overflow (agent-based simulation: performance issue: Python vs NetLogo & Repast) seems to indicate that Python would be slower.

Seems like it would be quite possible to use Jython to compile into modules that NetLogo could use, but I wondered if anyone is aware of something that would let me do NetLogo simulations in a sensible language like Python. Thoughts?

Sheritasherj answered 27/8, 2014 at 20:21 Comment(2)
relevant: simulatingcomplexity.wordpress.com/2014/05/26/…Downtime
I recently was upset at python for not having Netlogo's Repeat statement.Shadow
D
7

There have been a bunch of NetLogo-clone type projects over the years, but they tend to founder and die once the creators realize how massively much work is involved in creating and sustaining something like NetLogo over a period of years and even decades. (Work on NetLogo and its predecessors in the StarLogo line stretches back to the late 1980s.)

2015 update: Mesa looks like a promising entrant in the ABM-for-Python space.

For those who like NetLogo's feature set and agent semantics but dislike the language, I think the most promising direction is the NetLogo Web project, by the developers of NetLogo (including myself, though as of 2015, I'm no longer heavily involved). In addition to being backwards compatible with the old NetLogo language, NLW also allows building models in JavaScript or any compile-to-JavaScript language.

NLW hasn't achieved full feature parity with regular NetLogo yet, but it's well on its way. So depending on your needs, it might just be a promising direction rather than a solution you can use today.

You might also take a look at AgentScript — also JavaScript based, with a CoffeeScript bias (2015 update: I think they are switching to ES6?).

2020 update: A recent JavaScript and Python-based entrant (from StackOverflow founder Joel Spolsky!) is HASH: https://hash.ai

Downtime answered 28/8, 2014 at 12:28 Comment(2)
Thanks for the pointer to Tortoise. I'll check it out. Can't say the purist in me is really into JavaScript, but at least there are better control structures.Sheritasherj
If you check MESA, you can realize that it's still on a very basic level.Fritzsche
H
7

As of April 2018, there is a new kid on the block, PyNetLogo, and it is even available in PyPi for simple installation via pip.

Paper @ JASSS: PyNetLogo: Linking NetLogo with Python (March 2018)

Code @ Github

Documentation

Hydrosphere answered 12/4, 2018 at 23:17 Comment(0)
B
6

NetLogo as a language does have some flaws. The lack of a case statement is certainly a minor one; the lack of modularity above the function level is a bigger one for very complex simulations.

But things that should be simple to code usually are simple to code, albeit in a way that you might not be used to. It's all a matter of mindset: the Logo language has its roots in the Lisp family, and the best way to approach a problem in it is almost always from a functional programming point of view. If you try to maintain an imperative (or even "pythonic") mindset, you'll likely have to "contort your brain" constantly.

I would suggest posting questions here when you find yourself struggling with apparently simple things. There is a very good chance that someone will be willing to show you the "NetLogo way" of doing it.

That being said, there are two main ways for NetLogo to interface with other JVM languages, including Jython:

  • The Extensions API allows you to provide new primitives usable inside NetLogo. You could conceivably build the bulk of your model inside Jython, expose your code through extension primitives, and keep the stuff that you have to code inside NetLogo to a minimum.

  • The Controlling API allows you to control NetLogo from an external program. In this scenario, you would code your model in Jython and use NetLogo as an external simulation engine/library.

But if your goal is just to build simulations, I'm not sure that you would gain a lot by taking these roundabout ways. You might as well switch to a different platform like MASON or Repast or just, you know... learn to love NetLogo.

Begorra answered 27/8, 2014 at 21:46 Comment(5)
Yes, the lack of modularity is probably the big problem, but the minor issues crop up and are cumulative. I love using the map and reduce primitives although it's easy to love them for cleverness and parsimony and hate them for clarity. NetLogo as a language just seems so off-the-beaten path.Sheritasherj
Here is a blog post about controlling NetLogo from Python: davidmasad.com/blog/netlogo-from-pythonDowntime
Thanks, Seth. This is an interesting discussion of what it would take to run the NetLogo API via Python.Sheritasherj
The aforementioned link from davidmasand's blog does not seem to work anymore.Fortification
You are total right, negative prejudice with NetLogo and a constant searching - cognitive dissonance - for a more appreciated language like Python, stems from the following points: 1. Logo, Commenius Logo language was taught or used to teach to kids as a first language; 2. the default 'turles' bread, expression. A scientist should not work with turtles, or cats etc :d... Vote up if you think the same.Fritzsche
C
4

The gridworld.py module provides some NetLogo-like functionality.

I absolutely love Python, but I'll put in a word for NetLogo: it is more than adequate for most small scale simulations once you get used to its feature set, especially since the addition of tasks. (As Nicolas suggests, taking a functional programming perspective helps.) For some projects the absence of inheritance in NetLogo in really inconvenient (breeds are not an adequate substitute). Nevertheless, one of my ongoing projects is in NetLogo, and after trying to teach agent-based programming with Python, I switched to NetLogo for the classroom.

One more thing: looking forward, Tortoise looks pretty exciting.

Castleman answered 27/8, 2014 at 22:29 Comment(4)
Yes, scale is an issue for NetLogo. I have a Digital Ants simulation that I can't get above 10,000 patches because of performance issues. But it is made to run on millions of machines. I'll check out Tortoise. Thanks!Sheritasherj
NetLogo Web (aka Tortoise) is noticeably slower than regular NetLogo, so for high performance and/or large numbers of agents, you're better off with regular NetLogo.Downtime
Would you mind amplifying on why you switched from Python to NetLogo for teaching agent-based programming in the classroom?Shrink
@Shrink Using a domain-specific language was easier for students. The NetLogo simulation toolkit is quite good. The NetLogo Model's Library is very helpful. There are good textbooks for teaching ABM with NetLogo.Castleman
P
3

If you are looking to do optimization or sensitivity analysis on NetLogo models from Python you might want to consider using NL4Py. NL4Py is a Python package that lets you control NetLogo models from Python. The package is released on PyPI.org and you can get started with a simple pip install nl4py.

Article Preprint

Repository and Documentation

Example of model calibration with NL4Py and DEAP

Example of sensitivity analysis with NL4Py and SALib

Phototaxis answered 11/9, 2018 at 15:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.