I'm fairly new to Netlogo and struggling with how to set up a somewhat complicated if statement. The statement is for turtles and the condition is that other turtles live in the same region and have a house.
I've tried the iterations of the following, but have not yet been successful:
if (one-of other turtles with [region = [region] of myself and house? = True]) []
if (other turtles with [region = [region] of myself and house? = True]) []
Thank you for any insights!
If you need to insert code in a question, check out the "Code Sample" button in the toolbar. You can highlight your code and click the button- super handy.
Your second try is extremely close. The quick fix is to add the any? primitive in order to tell Netlogo that you want to evaluate the agentset "other turtles" in this case. As it was, you weren't actually evaluating anything with if- kind of like saying "If the turtles in my region who have a house," as opposed to "If there are any turtles in my region who have a house."
to check-region
ask one-of turtles [
if any? other turtles with [ region = [region] of myself and house? = true ] [
set color white
]
]
end
If you need to evaluate more specific numbers, you can use something like count to set a threshold - for example:
to check-region-count
ask one-of turtles [
if count other turtles with [ region = [region] of myself and house? = true ] > 3 [
set color white
]
]
end
Related
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)
I am trying to get the real color of my global variable.
Here my code:
breed [players player]
globals [
INITIAL-POSSESSION ;
]
to setup
clear-all
reset-ticks
set-initial-possession
end
to go
ticks
ask players [
decision
]
end
to-report initial-possession
report random 2
end
to set-initial-possession
ifelse initial-possession = 1
[
set INITIAL-POSSESSION black]
[
set INITIAL-POSSESSION white]
end
to decision
if ([color] of INITIAL-POSSESSION) = black
[] ;;do something
if ([color] of INITIAL-POSSESSION) = white
[];;do something
end
But I get this error:
OF expected input to be a turtle agentset or link agentset or turtle
or link but got the number 0 instead.
So I change it with (and it works):
to decision
if INITIAL-POSSESSION = 0
[]
if INITIAL-POSSESSION = 9.9
[]
end
But there is any other way to do that? (I am using netlogo 6.0)
I think there may be some code missing so I can't confirm, but it looks like you may not have set BALL-OWNER up as a turtle or patch, and instead assigned a value directly to that variable. of queries a variable from an agent (or a list of variables from an agentset), so if BALL-OWNER is set to a value, NetLogo gets confused. If you do assign an agent to BALL-OWNER, however, your approach should work fine. For example, try running the code below:
to setup
ca
crt 10 [
setxy random-xcor random-ycor
set color one-of [ red blue ]
]
reset-ticks
end
to go
let ball-owner one-of turtles
ifelse [color] of ball-owner = red [
print "red team has possession"
] [
print "blue team has possession"
]
end
Edit: You can definitely use a global to pick a color just as you did in your second code block- I just wanted to point out that of is specifically tied to agents. If you want to store a color in a global variable for comparison, that's possible, it's just that your comparison is simpler than using of:
globals [ initial-possession ]
to setup
ca
crt 3
set-initial-possession
reset-ticks
end
to go
ask turtles [
decision
]
end
to set-initial-possession
set initial-possession ifelse-value ( random 2 = 1 ) [black] [white]
end
to decision
ifelse initial-possession = black [
print "I see that black has possession"
] [
print "I see that white has possession"
]
end
I'm not sure if that helps, it may depend on your purpose of storing a color in the global in the first place!
I'm modeling territory selection in NetLogo, and would like my turtles to calculate the perimeter of their territory once established. I've been trying to come up with ideas for how to do this, but haven't found a good means yet. Any ideas?
patches-own
[ owner ] ;; patches know who owns them
turtles-own
[ territory ;; agentset of patches I own
food ;; food acquired in my territory
threshold ] ;; food required, will build territory until meet this
to go
tick
ask turtles [ build-territory ]
end
to build-territory
if food > threshold [ calculate-perimeter ] ;; stop building when enough food
pick-patch ;; keep picking patches until meet threshold.
end
to calculate-perimeter
;; what could I use to add up the perimeter of the territory?
end
Thanks in advance for any suggestions!
A modification of my last answer to you:
to setup
ca
ask patches with [pxcor > 0 ] [
set pcolor white
]
crt 1
end
to go
ask turtles [
let blacklist patches with [ pcolor = black ]
let northpatches patches with [ pycor > 0 ]
let northred ( northpatches with [ member? self blacklist = false ] )
ask northred [ set pcolor red ]
let border northred with [ any? neighbors4 with [ pcolor != red ] ]
ask border [
set pcolor blue
]
print count border
]
end
You can designate border/perimeter patches as any of you territory patches with neighbors that are not territory. For you it might look something like:
ask turtles [
print count territory with [ any? neighbors4 with [owner != myself ]
]
]
Again, I can't test it without your setup so you would have to modify.
Edited below
To count the edges of patches that are on the border, you could have them count their neighbors4 that belong to another turtle. Then, they can add them to that turtle's perimeter length. For example:
to assess-perimeter ;;; must be called by a turtle
print ("Assessing perimeter")
let current-turtle who
let temp-per-len 0
let border-patches patches with [ owner = current-turtle and any? neighbors4 with [ owner != current-turtle ] ]
show (word "I have " count border-patches " border patches")
ask border-patches [
;; One way to get each border patch to check each of its neighbors
let nobodies 4 - count neighbors4 ;; if any patches are on the edge of the world, returns the number of those edges
let non-territory-edges count neighbors4 with [ owner != current-turtle ]
let border-edges nobodies + non-territory-edges
set temp-per-len temp-per-len + border-edges
]
show (word "My perimeter length: " temp-per-len )
set perimeter-length temp-per-len
end
If that is called after all turtles have chosen their entire home range, the idea is that each turtle assesses the border of its home range. Then, it has each of those border patches count its neighbors4 that have a different owner. I used "temp-per-len" as a summing variable within the loop, which is then used to set the turtles-own "perimeter-length". Full model code, including setup and definitions, here. Note- you'll have to download or copy the code, the model is too bulky to run well in the HTML format.
Also, I didn't actually count to make sure this worked perfectly- I did a quick version and crossed my fingers, but I think the idea makes sense and hopefully gets you started.
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!
I have a netlogo problem for which I can't seem to find a solution, but yet it feels very basic.
I have two types of breeds:
breed [individuals individual]
breed [cars car]
I want to create a link from one individual to one car. So, its a one-one relation. I use this code to do that:
to setup-individuals
create-individuals individuals-number [
set ID 2
set shape "person"
set color yellow
setxy random-xcor random-ycor
set activity ""
set activity_time 0
let rand random 2
ifelse rand = 0
[
set owns-car false
]
[
set owns-car true
create-link-to one-of cars ;; here is the issue
]
]
end
The problem is that if i use "create-link-to one-of cars" there are more than one individuals linked to one car, but I want each individual to have a distinct car. When trying the following command: "create-link-to one-of cars with [my-in-links = 0]" its giving me the following ERROR: "CREATE-LINK-TO expected input to be a turtle but got NOBODY instead." I tried many variations of this command, but its not working.
Your attempted solution of create-link-to one-of cars with [my-in-links = 0] is on the correct path. However, if you look at the NetLogo dictionary, you will see that my-in-links returns an agentset, not an integer giving the number of members of that agentset. So you need to compare to empty rather than compare to the number 0.
This is the code that is syntactically closest to what you have: create-link-to one-of cars with [count my-in-links = 0].
What you really want though is something more like create-link-to one-of cars with [not any? my-in-links]