
Many complex systems can be investigated only by modeling. For systems consisting of a large number of independent objects, such as crowd behavior, the development of multicellular organisms or military operations, agent simulation is the most appropriate. There are
many systems designed for this, for example, the Russian proprietary
AnyLogic .
I want to talk about the language
NetLogo , well-proven in education, but also suitable for adult tasks.
Syntax
The Logo syntax is minimalistic — a space-separated sequence of names and constants with a rare grouping using [] or (). [] are used to create lists and group commands into a block in most constructions, () - regular brackets for subexpressions. Names refer to built-in or programmer-defined entities — functions, variables.
Commands (procedures) are announced
to [ ] end
The functions in NetLogo are called “reporters” and are announced a little differently:
')
to-reporter [ ] report end
The compiler knows “arity” (“valence”) about each procedure or function and does not require the use of grouping once again. The truth with the functions of higher orders can be wrong - then he needs a hint in the form of parentheses.
Suppose we have defined functions:
to-report inc [x] report x + 1 end to-report add [xy] report x + y end
Then you can write
add add inc 1 inc 2 inc 3
and get a well-deserved nine.
It works the same way.
map inc [1 2 3]
which returns a list [2 3 4]. And for add you will have to write brackets
(map add [1 2 3] [4 5 6])
Agents
Agents are of three types - turtles (turtle - as without them), links (link) and spots (patch - they are places in space). For turtles and relationships, you can specify a user-defined breed (breed). Agents of the same type are combined into the appropriate set (agentset) - turtles, links, and pathes. Representatives of the same breed are also combined into a set. New breed is created by the team
breed [ninjas ninja]
where ninja is the name of the breed, and ninjas is the name of the set that unites all agents of this breed.
There is also a special agent - an observer.
The turtles are created with the create-turtles command (with an argument, the number of turtles to be created), and then they are indexed by the turtle function. The agent is “first class value”, if desired it can be stored in a variable, but this is rarely required.
An agent or set of agents can be the context for a team. Execution of commands in the context of agents is the main mechanism for working with them.
ask turtles [fd 1]
This code will ask all turtles to step forward.
ask patch 17 13 [set pcolor pink]
And this one will paint the field with the coordinates (17,13) in gently pink color.
Breed can be set dynamically
ask turtle 1 [set bread ninjas] ask ninja 1 [set pcolor black]
Variables
In the original Logo, a dynamic scope was implemented, which creates inconvenience and contradicts current trends in the field of programming languages. The NetLogo developers acted severely - banned the creation of variables of the same name, whose scope may intersect. Library developers do not add this convenience, but it is more likely to be useful for schoolchildren. Yes, and sociologists and military experts in the development of models will allow to make fewer mistakes :-).
The scope is static, which simplifies the use of higher-order functions.
Variables can be global, their own for the type of agents or breed, formal arguments and local in a block of code. The same variable name cannot refer to different classes of variables, but local variables in different blocks, including in one function, can be called the same. The name of a local variable cannot coincide with the name of the function argument, where it is defined, or with the name of a global or agent variable.
Global variables are described
global [1 2 ...]
own
turtles-own [3 4 ...] ninjas-own [5 6 ...]
Local variables are created by the command
let 7 _
Higher order functions
Some standard functions, such as map, receive another function as one of the arguments. They can simply specify the name of the function to be passed or write a closure. A closure is a code enclosed in []. The fact that this is a closure, and not a list, the compiler guesses by context. Formal parameters are variables '?', '? 1', '? 2' and so on.
Using their higher-order functions is more difficult. You can assign a value to a variable or transfer a function to another function using the special task function, and call a function from a variable using runresult (or run for commands).
to test1 [f] show (runresult f 1) show (map f [2 3 4]) end test1 (task [? + 2])
Personally, I consider this approach with separation of functions and variables referring to functions to be not successful, but it is implemented, for example, in Common Lisp, and has its supporters.
Interface and Graphics

The program contains three tabs (tabs): "interface", documentation, and code editor. The tab “interface” contains a field of graphic objects (including the image of the world), a text output field and a REPL. In the REPL, you can switch modes that set the context in which the executed commands are executed - “observer”, “turtles”, “spots” and “connections”. REPL limited - to describe variables, rocks, procedures and functions in it is impossible (they are created out of context), you will have to go to the code editor.
In the zone where the field with the world is drawn, you can use the mouse (by pressing the right button in the free space) to create other graphical objects and interface elements. When you create them, you must specify the names by which they can be accessed from the program. Programmatically I did not find a way to generate graphic objects. In the file, these objects are saved in text form after the code, but in a form that is unsuitable for manual editing.
For example, if we create a “plot” named “plot1”, then the command
set-current-plot "Class Histogram" histogram map [position ? [red green blue]] ([color] of turtles)
draw a graph of the distribution of turtles in red, green and blue colors (assuming that others do not occur).
Model library
The extensive
library of models , from educational and entertaining, to research is attached to NetLogo. Some models can communicate with specialized hardware, such as the
GoGo board .
Access to the model to the model through the menu File. As a rule, in the interface of the models there are the Setup and Go buttons and to launch them they must be clicked in this order. Setup initializes the model, after which its settings can be changed via the interface.