Netlogo expected agentset error - netlogo

I'm trying to create links between an "active-turtle" and a group of other turtles (which are currently stored in a list).
I want to make use of the create-links-with function, but am unable to without getting an error: expected input to be an agent-set but got a list...
This is my code ask active-turtle [create-links-with like-list]
I have a working solution, but I find it ugly so am looking for a better alternative
foreach like-list [ask ? [create-link-with active-turtle]]

The very handy turtle-set primitive will turn your list into an agentset!
ask active-turtle [ create-links-with turtle-set like-list ]

Related

On NetLogo, problem to use an ifelse statement inside a Monitor

On NetLogo, I am trying to create a Monitor that will show a string.
The code I am trying is:
(word "The current value is " (ifelse myvalue <= max_value [myvalue][max_value]) " in this model.")
myvalue and max_value are both global variables and integers.
Ideally, I would want the result to be something like:
The current value is 12 in this model.
But I always get the error Expected reporter and the ifelse statement highlighted, as if the problem were there.
I understand that one way to solve this would be to create another variable that would store the result of this ifelse elsewhere, and then just call here this new variable. However, if possible, I would prefer not to create a new variable, just to use the conditional inside the monitor itsel.
Does anyone knows why I am having this problem with Monitor?
ifelse is used to conditionally execute commands, ifelse-value is used to conditionally report values.
When you use word, NetLogo expects reporters but then finds ifelse, which NetLogo expects to store a command instead; therefore that error message and ifelse being highlighted.
This will work:
(word "The current value is " (ifelse-value myvalue <= max_value [myvalue][max_value]) " in this model.")

How does agent death surely affect the `while` loop that the agent is running?

As the following test illustrates, if a turtle is running a while when it dies, this breaks out of the while (without any error). Is this behavior guaranteed by NetLogo?
to test
ca
let age 0
crt 1
ask turtle 0 [
while [true] [
set age (1 + age)
print (word "age is " age)
if (random-float 1 < 0.1) [die] ;death ends the while
]
]
end
Yes- whenever a turtle executes "die" it immediately stops execution of any commands. In your case, the while loop (and "ask turtle 0") is terminated. The turtle is also automatically removed from all agentsets.
If you used "[die show "I'm dead!"]" instead of just "[die]", the code would never show "I'm dead!" because it stops as soon as it executes "die".
(If you have ever tried to program a model that removes agents in another programming language, you should appreciate how magical NetLogo's "die" primitive is. In other languages, doing the same thing can be a complicated nightmare.)

Netlogo not writing results to file correctly

I’m running simulations in Netlogo, with coding to send results automatically to a spreadsheet. This occurs at the end of a simulation in order to save each individual’s variables (e.g., identity, home range size, etc.). The spreadsheet results look fine normally, but when using BehaviorSpace, occasionally data from an individual's variables are not printed correctly (e.g., data in wrong columns or missing, see screenshot below).
I’m wondering if during parallel runs in BehaviorSpace, when simulations happen to end at the same time this results in simultaneous writing to the file. Is that likely what’s happening? Should I be reasonably confident that remaining results were printed correctly to the table, aside from these noticeable issues? Most importantly, how can I avoid these misprints to the file? Sample code follows.
Bonus question [SOLVED]: I thought printing date-and-time would identify unique simulation runs, but this is not the case since each turtle's variables take more than an instant to print to the file. Is there a quick way to add a unique identifier for each simulation run?
Thanks!
to start-output-file ;; Observer procedure, called from setup.
set-current-directory "C:\\...
file-open "Table_results.csv"
;; Define the names of the variables:
file-type "Run,"
file-type "Landscape,"
file-type "Individual,"
file-type "Home range size,"
;; ... and so on, ~60 results saved for each individual
file-print ""
file-close ]
end
to end-simulation ;; Observer procedure, called from go.
write-outputs
file-close
end
to write-outputs ;; Observer procedure, called from end-simulation.
set-current-directory "C:\\...
file-open "Table_results.csv"
ask turtles
[ file-type (word date-and-time ", ") ;;<---Would like a unique run identifier here instead if possible.
file-type (word Landscape ", ")
file-type (word Who ", ")
file-type (word Home-range-size ", ")
;; ...
file-print "" ]]
end
Update/Solution: My solution was to have each model run write to a separate CSV. This allowed me to save results of each agent's variables. Files will each have a unique identifier so won't get overwritten or added to later. All the CSVs can then easily be imported and combined using Program R.
Example Code: Define a CSV file name during setup, and ask turtles to write their results to the file:
extensions [ csv ]
globals [ filename ] ;; Will define as the date/time and BehaviorSpace run #.
to setup
clear-all
let run-ID (remove-item 6 (remove-item 7 (remove-item 8 (remove "-"(remove " "(remove "." (remove ":" date-and-time)))))))
set filename (word "Results, " run-ID ", " word behaviorspace-run-number".csv")
file-open filename
start-output-file
;; Etc.
end
to start-output-file ;; Called from setup.
file-open filename
file-type "Turtle identity,"
file-type "Home range size,"
;; Etc., rest of column headers.
file-print ""
end
to end-simulation ;; Observer procedure, called from go.
ask turtles [ write-outputs ]
file-close
end
to write-outputs ;; Called from end-simulation.
file-type (word Who ", ")
file-type (word home-range-size ", ") ;; (Turtle variable for home range size.)
;; Etc., i.e., each turtle variable you are writing to file.
file-print ""
end
Hope this helps someone else looking to do something similar, or noticing similar oddities in their CSV output from BehaviorSpace.

Purpose of "stop" in NetLogo

I read a document about stop and some example code with stop in it. However, I'm still unsure about the usage of stop.
The following is how stop is used in the Ants Simple example in the model library. Could anyone help me understand the use of stop here?
to look-for-food ;; turtle procedure
;; turtle can access its own patch's property, in this line is `food`
ifelse food > 0 ;; if turtle's own patch has food on it
[ set carrying-food? true ;; pick up food --> change this turtle's carrying-food property to true
set food food - 1 ;; let's this patch losing one food
rt 180 ;; let turtle turn around
stop ;; no idea what is the use of stop here?????????????
]
[ ;; go in the direction where the pheromone smell is strongest
uphill-pheromone
]
stop can also be used to stop a BehaviorSpace model run. If the go commands directly call a procedure, then when that procedure calls stop, the run ends.
stop can also be used to stop a forever button.

Netlogo: Ask expected this input to be a command block, but got a number instead

I'm getting this error "Ask expected this input to be a command block, but got a number instead" inside my setup procedure...
to setup
....
....
....
....
[ask color gray + 1.5]
Anyone have any ideas?
ask takes an agent or agentset as its first argument and a command block (something like [ ... ] that has commands in it) as its second. So, in your case, you probably want something like:
ask turtles [ set color gray + 1.5 ]