NetLogo 6 anonymous procedures with no inputs - netlogo

In the programming guide it's mentioned that anonymous procedures can have no inputs:
[[] -> fd 1]
[[] -> count turtles]
My question is what would be the use of such a procedure?

You'll see a few examples in the NetLogo Models Library if you run grep -rl "\[\] ->" from the models folder, namely:
Code Examples/Extensions Examples/nw/NW General Examples.nlogo
Code Examples/State Machine Example.nlogo
3D/Sample Models/Termites 3D.nlogo3d
Sample Models/Biology/BeeSmart Hive Finding.nlogo
Sample Models/Chemistry & Physics/Sandpile.nlogo
Sample Models/Social Science/Hotelling's Law.nlogo
IABM Textbook/chapter 8/Sandpile Simple.nlogo
I would encourage you to take a look at them, but a common use is to store a task that you want a turtle to execute later.
If you take the State Machine Example, you'll see that termites have a next-task variable that gets set to different anonymous procedures depending on what the termite should be doing next. For example, when a termite stumbles upon a pile of chips, its next task will be to put down the chip it is currently holding:
to find-new-pile ;; turtle procedure -- look for yellow patches
if pcolor = yellow
[ set next-task [ [] -> put-down-chip ] ]
end
You don't always need this technique, but it's a powerful tool for modeling certain kind of systems (e.g., state machines).

Related

Netlogo use of myself

With the use of the myself primitive, I want a patch with a certain land use to make buildings of that type in a specific radius, but it's not working. Here is the code:
patches-own [land-use] ;land-use can take many types: "residential", "industrial", "commercial"
...
to grow
ask patches with [land-use = "residential"]
[ask other patches in-radius 10 [
if [land-use = "residential"] of myself [build-house]]]
end
How can this be fixed?
The problem there is not myself but the use of of. Two things to fix:
of requires the reporter to be in square brackets. Only the reporter that of refers to, not the whole condition being tested (as in your case where you are testing a condition).
You should not specify the value of the variable but just its name. Just as in normal language: you wouldn't say "If your hair colour equals my blonde", but you would say "If your hair colour equals my hair colour".
The resulting syntax is:
if (land-use = [land-use] of myself) [
build-house
]
Parentheses surrounding the condition are optional, I use them as a stylistic choice.
PS Regarding point 2 above... of course you could keep the value of the variable (i.e. "residential") instead of the name of the variable (i.e. land-use), and in that case drop of myself.
You would have:
if (land-use = "residential") [
build-house
]
And as of now it would work because the patches starting this process are just those with land-use = "residential"... but this doesn't look like great code, as it is not extendable to other variable's values and it contains more hard-coding than necessary.

How to read formula including turtle values from a input box?

Im having trouble including a formula in the input box into my model and hoped someone could help. In short I want the global variable "subsidy_absolute_threshold" to be read as "100 + m2 / 1600". So that wherever that variable is implemented, that code is implemented instead. The current code reads something like
turtles-own [m2 subsidy_eligible]
globals [subsidy_absolute_threshold]
[...]
set subsidy_absolute_threshold "100 + m2 / 1600"
[...]
ask turtles [
if ambition < read-from-string subsidy_absolute_threshold [set subsidy_eligible true]
]
Technically the global value is an input box set as a string, but it should be functionally the same thing. The error message I get is "Extra characters after literal." which I struggle to understand.
I have tried all the "types" of input boxes, but none work. I have also tried directly writing in the formula, as in replacing subsidy_absolute_threshold with 100 + m2 / 1600, which works.
Use the native command runresult instead of read-from-string.
I'm somewhat unsure exactly why read-from-string does not work, so if someone wants to present an explanation please do.

Deactivating path to a node

I am developing a casualty rescue model. The idea behind the model is for the rescuers to search and rescue casualties. The rescuers select the casualty they are closest to at the start of their run. Next, the network extension's nw:weighted-distance-to function is used to find the path with the minimum distance between them.
A bit of background behind how I've implemented the model,
I have created a bunch of nodes(breed) and placed casualties(breed) and rescues(breed) on these nodes. The rescuers at the start of the model pick the closest casualty to them and select a path using nw:weighted-distance-to
The problem I am having is that I want to temporarily disable access to a certain path. So that the rescuers will have to get to their respective casualty using an alternative path. What I am thinking is to have a variable called accessible? (nodes-owned) and set this to false to temporarily disable access to that particular route. But I am unsure how to implement this since the network extension does not contain any function that I know that returns alternative paths.
Any Ideas?
extensions [nw]
let dist_list []
foreach rescuer-nodes-list [
rescue-node ->
foreach casualties-nodes-list[
casualty-node ->
ask rescue-node[
let dist-to-casualty-node nw:weighted-distance-to casualty-node netlogo-dist
set dist-list lput dist-to-casualty-node dist-list
]
]
]
The following code iterates over all nodes containing rescue agents and casualty agents and computes the minimum distance between each agent pair (rescue-casualty).

Create an "itinerary" agentset of patches

I'm building a model where turtles "search" a subset of patches for a resource according to different search criteria.
I'm trying to build reports that return a sorted list or agentset of patches that a turtle can then use as an itinerary for it's search.
For some reason I'm having trouble storing the itinerary in a turtle owned variable.
an example reporter is:
to-report availability
let sorted-patches sort-on [ ( (space - occupants) / space ) ] patches with [space > 0]
report sorted-patches
end
when I do show availability in the console, it prints out what I expect, an ordered list of patches.
But if I do
let test-variable availability
show test-variable
it returns
ERROR: Nothing named TEST-VARIABLE has been defined.
is this a problem of scope somehow, can I not use let as an observer?
Is it a problem of type? Can I not store an agentset as a named turtle-owned variable?
Is there a way to do the same thing with a list instead of an agent set?
Thanks
From your description, it's a scoping problem. But the problem is not that you are trying to use let with an observer, it's the scope of let. NetLogo is not really interactive in the sense you are trying to do - the variable created by let is thrown away at the end of the line.
If you type let test 3, hit enter, then type show test, you will get the same error. However if you type let test 3 show test, then it will return 3.
Why are you needing this from the console? If it's for testing, then you can look at it the way you have already found - simply by show availability. If you are using it for turtles while the model is running, then it is not interactive and there's no problem.

Netlogo Agents error

I am implementing an evacuation simulation of a lecture hall.And i have two types of students those who sit on tables and extra students who allocated randomly inside the class.So i created two sliders in order to allocate the students at the desired numbers.The sliders are named extrastudents and standarstudents. When the simulation starts i want all the students (both in tables and extras students) to go to the nearest exit ( i have two exits ) .So i implemented that for only the students that are seating down :
ask standarstudents [
ifelse pycor > 0
[ set target one-of nexits]
[ set target one-of sexits]
face target
]
Nexits is the north exit.
Sexits is the south exit.
The problem is that i get this error and i can move on :
ASK expected input to be an agent or agentset but got the number 3 instead.
error while observer running ASK.(The number 3 derives from the slider that user is pick)
called by procedure SETUP
called by Button 'setup'
org.nlogo.nvm.ArgumentTypeException: ASK expected input to be an agent or agentset but got the number 3 instead.
any ideas?
Well, if you have sliders named extrastudents and standarstudents, those two variables are numeric values representing how many students of each kind you want, not the students themselves. Like the error message says, the ask primitive works with agentsets that represent "who you are asking".
Have you actually created your students? Putting sliders on the interface won't do anything by itself. I would suggest renaming your sliders to something like num-extrastudents and num-standarstudents and then using code like this to initialize your population:
breed [ extrastudents extrastudent ]
breed [ standarstudents standarstudent ]
to setup
create-standarstudents num-standarstudents
create-extrastudents num-extrastudents
end
Then, the code you have posted would work because extrastudents and standarstudents would be breeds of turtles, and thus, agentsets.