Turtles moving to target when two conditions are met
I want to create a target (breed: shops) with two conditions:
- The shop is open
- The shop that is closest to the turtle (households) location
But I keep getting errors, see line below.
open-nh? is a shop attribute.
Thanks!
let target-nh [min-one-of healthy-shops[distance myself] and open-nh? = true]
That line contains many syntactical errors, all of which can be addressed by simply checking the related keywords in the NetLogo Dictionary:
-
let
(here) expects the value without any square brackets following the name of the local variable. -
min-one-of
(here) requires a space between the agentset and the subsequent square brackets containing the reporter (of course this is always the case in NetLogo). -
AND
(here) requires a boolean before and a boolean after. In your case,open-nh? = true
is what comes after and it is a boolean, butmin-one-of healthy-shops [distance myself]
is what comes before and it is not a boolean, it is an agent. - This is not a syntactic mistake but note that, if
open-nh?
is already a boolean variable (i.e. it can only take the valuesTRUE
orFALSE
), then there is no need to add the equal sign to evaluate it: it already evaluates as eitherTRUE
orFALSE
by itself.
Bringing together all the points above, the correct version of what you are trying to achieve is:
let target-nh min-one-of (healthy-shops with [open-nh?]) [distance myself]
Note that I used parentheses around healthy-shops with [open-nh?]
although they are not necessary for NetLogo: they only serve the purpose of enhancing readability, making it clear that the first parameter of min-one-of
(which needs to be an agentset) is what comes within those parentheses.
In fact, the logic there is to first take all the open shops and use those shops as the input agentset for min-one-of
.
Final note
I would have posted the following in a comment to the question because this is not really part of the answer, but I want to include some code here so I am adding this note to the answer instead:
As it has been noted already for some of your previous questions (e.g. here or here), please do include a minimal and reproducible example of your problem.
This helps other help you, and makes it possible that those who are willing to help you are able to save some time that otherwise needs to be spent recreating your context. In this case it was easy to answer your question, but in more unclear cases I (and others) could have just ignored it because there was not enough material to work on (or would have needed to ask for more detail, which is extra time both for us and you).
Also, as noted in the same page I linked above about reproducible examples, please describe what errors you are getting:
"It doesn't work" isn't descriptive enough to help people understand your problem. Instead, tell other readers what the expected behavior should be. Tell other readers what the exact wording of the error message is [...].
Please see below what a minimal reproducible example (now with correct syntax) of your case could look like:
breed [people person]
breed [healthy-shops healthy-shop]
healthy-shops-own [
open-nh?
]
to setup
clear-all
create-people 1 [
set color lime
]
create-healthy-shops 50 [
setxy random-xcor random-ycor
ifelse (random 2 < 1)
[set open-nh? TRUE
set shape "square"]
[set open-nh? FALSE
set shape "circle"]
set color white
]
end
to go
ask turtle 0 [
let target-nh min-one-of (healthy-shops with [open-nh?]) [distance myself]
ask target-nh [
set color orange
]
]
end