to check all 8-neighboring patches with turtles on it? NetLogo - netlogo

I need to check if all the neighboring patches have turtles on them. The code I tried gave the error of "expected reporter" with set primitive.
My code is
if all? other (people-on neighbors) with [fear?] [set unable-move? true ]
people is the breed, fear is an attribute variable (people-own variable) and unable-move? is a global variable.
At some point I want to stop the person(turtle) if all the 8-patches including the center patch have a person(turtle) on it and are in fear.

The all? primitive requires you to give:
an agentset for which you want to test a condition (in your case, neighbors).
a reporter for testing that condition on each agent (in your case, the neighboring patches): any? people-here with [ fear? ].
All together:
if all? neighbors [ any? people-here with [ fear? ] ] [
set unable-move? true
]
That's all there is to it!

This should work as well:
if (not any? neighbors with [count people-here with [fear?] = 0])
[ set unable-move? true ]

something like :
if sum [count people-here with [fear?]] of neighbors >= 8 [
set unable-move? true
]
I hope it works!

Related

Linking turtles to another turtle with highest value in NetLogo. Limit the number of links

I am a beginner with NetLogo and I am trying to ask some turtles (students from different social classes) to link to other turtles (schools). What I would like is for the working class students to look for the school with the highest achievement which is at the same not expensive and has not reached the max number of links allowed. If the desired school has reached the max number of links allowed I want the student to look for the next school with the highest achievement which has not reached the max number of links allowed and so on.
This is the code. I get the following message "ANY? expected input to be an agentset but got the turtle (school 1) instead."
breed [schools school]
breed [upperclass upperclass-student]
breed [workingclass workingclass-student]
upperclass-own [achievement enrolled? target]
workingclass-own [achievement enrolled? target]
schools-own [schoolachievement expensive? ]
to setup
clear-all
set-default-shape schools "house"
set-default-shape upperclass "person"
set-default-shape workingclass "person"
ask patches [ set pcolor 8 ]
create-schools num-of-schools [ setxy random-xcor random-ycor set schoolachievement random-normal 5 1
set expensive? one-of [ true false ] ]
create-upperclass num-of-upperclass [ set color white setxy random-xcor random-ycor set achievement
random-normal 5 1 ] ;Students from upper class have higher achievement
create-workingclass num-of-workingclass [ set color red setxy random-xcor random-ycor set achievement
random-normal 4 1 ]
end
to go
ask workingclass [
choose-school ]
end
to choose-school
if breed = workingclass [
set target one-of schools with-max [ schoolachievement ] with [ expensive? = false ] ]
if any? target with [ count link-neighbors < max-link-count ] [
create-link-with target ]
set enrolled? TRUE
end
Your problem is the difference between an agent and an agentset, which is a somewhat subtle problem. The with-max returns an agentset - a set of agents (in this case turtles). That agentset can have 0 members, 1 member, 2+ members but is a set even if it is empty. However, the one-of selects one agent from the agentset and returns it as an agent, not an agentset. That is, NetLogo knows anything that is returned by one-of must be exactly one agent. At this point, primitives that are for agentsets (like any?) will throw an error unless they can also be used on individual agents.
So, back to your code. I like the readability of checking whether there are viable schools and then selecting one, which is what I think you meant to do. That would be:
to choose-school
if breed = workingclass
[ set targets schools with-max [ schoolachievement ] with [ expensive? = false ]
set candidates targets with [ count link-neighbors < max-link-count ]
if any? candidates
[ create-link-with one-of candidates
set enrolled? TRUE
]
]
end
Note that I also changed to targets instead of target, which is one way to keep track of whether something is an agent or an agentset.
The other way you could do this and keep it as an agent is:
to choose-school
if breed = workingclass [
set target one-of schools with-max [ schoolachievement ] with [ expensive? = false ] ]
if target != nobody and [count link-neighbors] of target < max-link-count [
create-link-with target ]
set enrolled? TRUE
end
So you can use nobody instead of any? but you can't also use with in that line because the with is really a filter on a set.
I also think you have a bracketing issue - I assume you want set enrolled? TRUE inside the brackets. I left it in the second fix, but changed in the first error (as well as changing bracket position convention to make the code block structure more visible)

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: How to compare the ID and then flag in this case?

I would like to put a flag called "min-id" for the turtle with the smallest ID. And I want to flag other turtles as "not-min-id". However the following sample syntax has errors. The error message is as follows.
" error while turtle 0 running >
called by procedure GO
called by button 'go' "
I probably to need your advice. Thank you.
globals [ min-id not-min-id count-up ID ]
to go
reset-ticks
ask patch 0 0
[
sprout 1 ;;This model needs to use sprout.
]
ask (turtles-on patch 0 0)
[
set ID who
setxy min-pxcor 0
set heading 90
]
if (count turtles > 0)
[
ask min-one-of turtles [who]
[
set min-id TRUE
]
]
if (count turtles > 0)
[
ask (turtles-on patch 0 0)
[
if ID > min-one-of turtles [who] ;;This syntax has errors.
[
set not-min-id TRUE
]
]
]
ask (turtles-on patch 0 0) with [not-min-id]
[
set count-up count-up + 1
]
if (count turtles > 0) [
ask (turtles-on patch 0 0) with [min-id]
[
die
]
tick
end
You have some confusion in your code. From your description, I believe you want each turtle to have a flag for whether or not it has the minimum who number. This means you need a flag for each turtle. However, you have set up min-id as a global variable instead of a turtle variable. Furthermore, you only need the flag variable once (that is, you need min-id but not not-min-id) and you set it to TRUE or FALSE.
Replace
globals [ min-id not-min-id count-up ID ]
with
globals [ count-up ID ]
turtles-own [ min-id ]
and see if that fixes it. Also initialise min-id to FALSE as part of the sprout.
Having said all that, I strongly agree with Alan, if you ever use the who variable for anything except print statements in debug, you probably need to rethink your code. In your case, what is special about the turtle with the lowest who number that makes you want to keep track of it? Do you simply want a random turtle that happens to be at a particular location? Then select a random turtle at that location to do the TRUE/FALSE without going through who.

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.

how to sprout around a turtle in netlogo?

I have been looking to spread around turtles and I don't get it.
The idea is that I have an insect population (a type of turtle) and this insect population check around if there's a nesting patch available. If there's one and there's no other insect population I would like this patch to generate a new insect population. So far I have come with this idea :
ask insect-populations
[
ask patches in-radius 2
[
if lay? = 1
[
if not any? insect-populations [ask self [sprout-insect-populations 1]]
]
]
]
Thanks in advance for any tip
ask insect-populations
[
ask patches in-radius 2 with [lay? = 1 and not any? insect-populations-here]
[sprout-insect-populations 1]
]
should be what you want if I understand your intent correctly.
The trick is in the [with]. It takes a true/false block. So any agent for which the boolean statement inside the square brackets is included in the set.