How do you "reference" a particular turtle within as ask turtles block? - netlogo

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.

Related

Getting breed type

In my go procedure, I want to check the breed of my turtles. But I cannot find a way to do it simply. I want to execute different code depending upon the breed. How should I do this?
I define breeds as:
breed [late-adopters late-adopter]
and in go
to go
ask turtles [
if is-late-adopter [do something...]
]
end
You just need to check the breed turtle variable against the breed agentset itself. The Bug Hunt Predators model is a good example of this. Here is the relevant code:
to go
; ...
ask turtles [
;; we need invaders and bugs to eat at the same time
;; so one breed doesn't get all the tasty grass before
;; the others get a chance at it.
(ifelse breed = bugs [ bugs-live reproduce-bugs ]
breed = invaders [ invaders-live reproduce-invaders ]
breed = birds [ birds-live reproduce-birds ]
[ ] ; anyone else doesn't do anything
)
]
; ...
end

Copy the link breed variable in the patch below

I have a network of nodes and links. This figure
is a capture of the world. The graph represents streets of a city. I have imported a shapefile with the gis extension. The gray lines are links, black dots are nodes and red dots represent people. The people move heading to the next node. In a street corner, the red dot chooses next street by examining the variable popularity owned by the link.
The links breed has a variable, popularity, whose value I would like to copy in the patches that are below.
If I try, for example, something like this to access patches under links will produce an error
ask links [show [(list pxcor pycor)] of patch-here]
Another approach can be to access links variable popularity from patches, but I do not know how to do it.
The reason why I want this is because I want to write in a file a matrix of popularity values and its position in the matrix should correspond with the position of the link in the world. Thus, the patches below the links would give me the matrix form. I have a procedure that for each patch writes the value of the patch in a file. However, I do not know how to pass the popularityvalue from the link to the patch below it.
Is there any way to copy a link owned variable to its patch?
Regards
If someone has a better way of doing this (or can simplify my code), feel free. Here is a complete working example. Copy it into an empty NetLogo model and run it to see it work.
The setup procedure just creates some nodes and links with appropriate test values and then calls the transfer-link-values procedure, which does what I think you want. The setup procedure then puts the values into the patch labels to display them and see the results.
The way the transfer-link-values procedure works is to create a turtle at one end of the link, and that turtle moves toward the other end of the link transferring the value as it goes. When it gets to the other end, the turtle dies.
patches-own [patch-popularity]
links-own [link-popularity]
to setup
clear-all
create-turtles 10 [ setxy random-xcor random-ycor]
while [ any? turtles with [not any? my-links] ]
[ let to-pair turtles with [not any? my-links]
let thisNode one-of to-pair
ask thisNode
[ create-link-with one-of other to-pair
[ set link-popularity 5 + random 5 ]
]
]
transfer-link-values
ask patches [ if patch-popularity != 0 [set plabel patch-popularity ] ]
end
to transfer-link-values
ask links
[ let start-node one-of both-ends
let this-link self
let end-node nobody
ask start-node [ set end-node [other-end] of this-link ]
let transfer-value link-popularity
ask start-node
[ hatch 1
[ face end-node
if transfer-value > patch-popularity
[ ask patch-here [ set patch-popularity transfer-value ] ]
while [ not member? end-node turtles-here ]
[ forward 1
if transfer-value > patch-popularity
[ ask patch-here [ set patch-popularity transfer-value ] ]
]
if transfer-value > patch-popularity
[ ask patch-here [ set patch-popularity transfer-value ] ]
die
]
]
]
end

Netlogo - identifying a subset of an agentset

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

How to create links in netlogo and ask link-neighbors to execute commands according to link-length?

I've been trying to link turtles from BREED1 (still) to turtles from breed2 (mobile) who are on neighbors of BREED1. I want to do so in order to change a variable according the link-length between BREED1 and breed2.
(you can say that BREED1 represent houses and breed2 represents people, I would like to change the fact that the people are "protected" or not, according to the distance that separates them from their house (BREED1 that they are linked to))
I don't know if this is the best way to do it, but here's my code, I know it's not working because the "protected" variable is always false by default.
to protect
ask n-of total-number-BREED1 BREED1
[ if any? breed2-on neighbors
[ create-link-with [who] of breed2-on neighbors]
ask link-neighbors
[ set protected true]
]
I would also like to add a part concerning the link's length
ask link-neighbors
[ ifelse link-length < 2
[set protected true]
[set protected false]]
Thank you for your help !
Try this to create links with the breed2-on the neighboring patches:
ask BREED1
[
if any? breed2-on neighbors [ create-links-with breed2-on neighbors]
ask link-neighbors [ set protected true]
]
and this, which gets the link-length between the breed1 and it's neighbors
ask BREED1
[
ask link-neighbors
[
if [link-length] of link-with myself < 2 [ do something]
]
]
Note: link-length is called from a link's perspective, so you need to get the link that's connecting two things.

NetLogo: create a dynamic number of breeds

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".