here is what I would like to write in netlogo, but cannot seem to find the syntax
ask where [ param > threshold] turtles [
]
I want to return the agent-set, but I cannot find the correct syntax to make this work
as Matteo notes in the comments, you appear to be after with, so the syntax would be:
ask turtles with [param > threshold]
[ ; do something
]
Related
NetLogo does not have a native SWITCH, SWITCH-CASE, or SELECT-CASE type of multiple condition, how do I do the same thing without having a giant mess of nested IF-ELSE statements?
Since NetLogo 6.1, NetLogo has supported multi-case ifelse, which can be used similarly to other languages' switch statements. Here is an example from the docs:
ask patches [
let choice random 4
(ifelse
choice = 0 [
set pcolor red
set plabel "r"
]
choice = 1 [
set pcolor blue
set plabel "b"
]
choice = 2 [
set pcolor green
set plabel "g"
]
; elsecommands
[
set pcolor yellow
set plabel "y"
])
]
And here is the example from the other answer rewritten to use it:
;; example of SWITCH style conditional block
(ifelse
(criteria-1) [ action-1 ]
(criteria-2) [ action-2 ]
(criteria-3) [ action-3 ]
[ default-action ]
)
This does not need to have the "multi-close" of the ] on the last line, so it overall seems cleaner to me.
We can simulate a SWITCH statement with a chain of nested IF-ELSE statements, formatted so it's not a big mess, and actually easy to read and edit.
We will format the chain of IF-ELSE statement with the following goals:
avoid excessive indenting,
avoid overly long code lines
make it look more like switch in other languages
rearranging the cases does not break the syntax
there is a default case
A SWITCH Statement
Here's how it looks:
;; example of SWITCH style conditional block
if-else [ criteria 1 ] [ action-1 ][
if-else [ criteria 2 ] [ action-2 ][
if-else [ criteria 3 ] [ action-3 ][
if-else [ true ] [ default-action ][
]]]]
Other considerations
Some programmers may find it offensive to have a default action that tests for true, and leaves the "else block" if the final "IF" empty. You can do what you like. I find making the default case explicit, makes all the actions formatted exactly the same, and avoids an awkward cluster of punctuation at the end.
Avoid overly long lines--it makes it harder to read
Keep conditions short--wrap them in reporter if needed.
Keep actions short--wrap them in procedures if needed.
This helps the code read more like a story and less like code.
Whenever we use the "ask" command for all agents of a particular kind or breed, the Netlogo program goes through each agent one by one in random order. What I want is really simple: I would like to access the turtle whose turn it is at that moment.
I can't seem to find an appropriate command for this.
My code for reference purposes is as follows:
to surfer-visits-source
ask surfers [
if ([quality] of one-of [out-link-st-neighbors] of one-of out-link-ss-neighbors) < expected-quality
[
let temp ([who] of out-link-ss-neighbors)
create-link-ss-to one-of sources with [who != temp]
ask links-ss with [end1 = [who] of surfer][
ask links-ss with [[who] of turtle temp] [
die
]
]
]
]
We can use the self command to do this.
I spent all afternoon trying to work out with a part of my code and I don't seem to be getting anywhere. Basically, I'm trying to create a social network on model setup. Each person in the model starts off with a set of people that are nearby to them people-nearby. It is from this set that people choose who to connect with:
create-people population-size
[
set people-nearby turtle-set other people in-radius neighborhood-radius
]
to create-network
let num-links round (average-node-degree * population-size) / 2
while [ count links < num-links and count people with [length sort people-nearby > 0] > 0 ]
[ ask one-of people
[ *... initiate probabilistic link creation process...*
create-unlink-with chosen-friend
Once person A has connected to someone (ie. person B), person B is removed from person A's people-nearby set. I'm having trouble with this portion of the code where the people-nearby set is updated by excluding all nearby people that are members of the unlink-neighbors set (i.e., those to whom person A is already connected - this set including person B):
ifelse count turtle-set people-nearby > 1
[ let nearby-people-not-linked-to-me ( turtle-set people-nearby with [ not member? self [ turtle-set unlink-neighbors ] of myself ] )
set people-nearby nearby-people-not-linked-to-me ]
[ set people-nearby [ ] ]
For some reason this error keeps popping up:
"WITH expected input to be an agentset but got the list [(person 0) (person 1) (person 3) (person 4)] instead." whenever
people-nearby with [ not member? self [ turtle-set unlink-neighbors ] of myself is called.
I looked up so many posts but can't seem to get the form of the argument right so that it stops showing this error.
Can anyone help me fix this please? (Oh and it's my first post so apologies if I haven't set up the issue properly)
When you submit code, try to submit what is needed to recreate your problem- check out the asking help page, and specifically the section on helping others reproduce your problem. As is, I think your problem comes from using turtle-set. That primitive is mostly used to combine agentsets, not to query them. So in your line:
( turtle-set people-nearby with [ not member? self [ turtle-set unlink-neighbors ] of myself ] )
there is an syntax issue related to turtle-set. The error itself is saying that you have not returned an agentset but a list of agents, which behave differently.
If I understand correctly, you want all people to have a variable that contains all people within a radius of themselves: "people-nearby". Then, you want the people to form a link with one of their "neighbor" turtles. Finally, you want the people to update their "people-nearby" variable to exclude the person to whom they just formed a link. Below is some code with comments where I tried to follow those steps- obviously your variables will be different, but it may get you started. Let me know if I need to clarify anything or if I missed a step.
breed [ people person ]
turtles-own [ people-nearby ]
to setup
ca
reset-ticks
create-people 70 [
setxy (random 30 - 15) (random 30 - 15)
]
; do this after all turtles have spawned
ask people [
set people-nearby other people in-radius 3
]
end
to create-links
let num-links 10
;; Create a temporary agentset out of turtles that have people nearby
let turtles-with-neighbors turtles with [ any? people-nearby ]
; ask some number of the temporary agentset:
ask n-of num-links turtles-with-neighbors [
;; This just makes it easy to identify the turtle that causes the link
ask patches in-radius 3 [
set pcolor white
]
; create a link to one of the nearby people
create-link-to one-of people-nearby
; newly set people-nearby to only include turtles in radius
; that are not linked-to from the currently acting turtle
set people-nearby other people in-radius 3 with [ not member? self [ out-link-neighbors ] of myself ]
ask people-nearby [ set size 0.5 ]
]
end
My first netlogo program was working well, but now is failing since 'tick' in the 'go' method is not in a valid context.
Please see the attached code, line 99, which generates:
You can't use tick in a turtle/patch context, because it is observer only.
Code is here:
http://jpark.us/temp/CSSS.v1.nlogo
Problem solved...
I was trying to 'set number.sparrows...' down in other methods, but not within a proper patches context.
So this works:
if all? patches [ eggs.laid = true ] [
ask patches [ set number.sparrows count sparrows-here ]
whereas this does not:
if all? patches [ eggs.laid = true ] [
set number.sparrows count sparrows-here
how can i create a dynamic number of breeds at runtime?
I want my user to be able to choose the amount of breeds.
I thought about something like
to setup_breeds
let j 1
while[j <= n_groups][
breed[j]
]
end
where n_groups is the number of breeds whick is taken from a slider.
But unfortunatly i cannot use breed inside a method...
Any ideas?
Thanks!
You need to explicitly declare each breed with the breed keyword, so the short answer is: no, you can't have a dynamic number of breeds.
But do you really need actual NetLogo breeds? The main purpose of having different breeds is to have different variables for each breed. If that is not case, perhaps you can get away with having a group-id turtle variable. To create a certain number of turtles for each of n_groups, you could just do something like:
turtles-own [ group-id ]
to setup
clear-all
let n_groups 10
let n-turtles-per-group 5
foreach n-values n_groups [ ? ] [
create-turtles 10 [ set group-id ? ]
]
ask turtles [ set label group-id ]
; do something with only turtles of, e.g., group 2:
ask turtles with [ group-id = 2 ] [
fd 5
]
end
If you think you really need breeds, edit your question to tell us why, and we'll see if we can find a solution for you.
Side note:
I used foreach n-values n_groups [ ? ] to loop through your n groups. That's the equivalent of:
let i 0
while [ i < n_groups ] [
set i i + 1
]
...but arguably more "NetLogo-ish".