7

How can I include the procedures from one Netlogo file into another? Basically, I want to separate the code of a genetic algorithm from my (quite complicated) fitness function, but, obviously, I want the fitness reporter, which will reside in "fitness.nlogo", to be available in the genetic algorithm code, probably "genetic.nlogo".

If it can be done, how are the procedures imported, and the code executed? Is it like Python, where importing a module pretty much executes everything in the module, or like C/C++, where the file is blindly "joined"?

This may be a stupid question, but I couldn't find anything on Google. The Netlogo documentation says something about __includes, an experimental keyword that may do the trick, but there's not much explained there. No example either.

Any hints? Should I go with __includes? How does it work?

Seth Tisue
  • 29,985
  • 11
  • 82
  • 149
CamilB
  • 1,377
  • 1
  • 12
  • 27
  • 1
    Found my answers by experimenting. The file to be included must be named "something.nls", and it is included like this, in the main file: `__includes ["something.nls"]`. I have no idea what paths it explores to find it, but it works if the included file is in the same folder as the main file. The namespaces seem to be joined, like in C++. Also, `turtles-own [new-property]` is honoured if found in the included file. – CamilB Apr 29 '11 at 10:14

2 Answers2

14

To include a file you use

__includes["libfile.nls"]

After adding this and pressing the “Check” button, a new button will appear next to the Procedures drop-down menu. There you can create and manage multiple source files.

The libfile.nls is just a text file that contains NetLogo code. It is not a netlogo model, which always end in .nlogo, as a NetLogo model contains a lot of other information besides the NetLogo code.

Including a file is the equivalent of just inserting all its contents at that point. In order to make it work in a way like reusable library files, one should create procedures which use agentsets and parameters as input variables to be independent of global definitions or interface settings.

The feature is documented in the NetLogo User Manual at http://ccl.northwestern.edu/netlogo/docs/programming.html#includes.

Seth Tisue
  • 29,985
  • 11
  • 82
  • 149
Jose M Vidal
  • 8,816
  • 6
  • 43
  • 48
  • 1
    You gave a shorter, yet more clear explanation of what I found while experimenting with __includes. I hope people read your answer before my comment above. – CamilB Apr 29 '11 at 10:42
  • In `code` tab I click on `includes` drop down, then a `New source file`. Now a new tab opens with `New File`. How do I name this file? – Abhishek Bhatia Jun 29 '15 at 20:09
0

You can create a file libfile.nls and in the same folder create your main model model.nlogo.

After that, go to your model.nlogo and write:

__includes["libfile.nls"]  

This file contains your reporters and procedures that you can call in your model.

Daniel Puiu
  • 962
  • 6
  • 21
  • 29