Please edit your question rather than provide follow up as answers.
The problem is that you have got your satisfaction values confused. You state that 0 means satisfied in the question text, but then assign 1 if nearest != nobody
(that is, there is another agent). So your code jumps to the distance calculation block when the nearest != nobody
is false or equivalently when the nearest is actually nobody. All of this can be avoided by using true
and false
, which would make your code much easier to read and less susceptible to errors. It's not essential, but NetLogo convention has a ? at the end of variable names that are used for boolean (true/false) variables.
So, rewriting your code so it can stand alone and ditching the 0/1, dumping the negative test (which is confusing) and reversing the final assignment, it looks like this:
turtles-own [satisfied?]
to testme
clear-all
create-turtles 20
[ setxy random-xcor random-ycor
]
ask turtles
[ if any? other turtles
[ let nearest min-one-of (other turtles) [distance myself]
ifelse nearest = nobody
[ set satisfied? true ]
[ ifelse distance nearest < 8
[ set satisfied? false ]
[ set satisfied? true ]
]
]
]
type "satisfied turtles: " print count turtles with [satisfied?]
end
I have also reformatted the code so you can see where your ifelse
structures operate. It is now also clear that no value is assigned to satisfied? if there's only one turtle, so the value will stay as the default of 0.
So a better version would look like:
ask turtles
[ ifelse any? other turtles
[ let nearest min-one-of (other turtles) [distance myself]
ifelse distance nearest < 8
[ set satisfied? false ]
[ set satisfied? true ]
]
[ set satisfied? true
]
]
That can be done in a single statement (and I have expressed it positively instead of negatively because that is more natural to read, so also see the new not
statement):
to testme
clear-all
create-turtles 1
[ setxy random-xcor random-ycor
]
ask turtles
[ ifelse not any? other turtles or
distance min-one-of (other turtles) [distance myself] > 8
[ set satisfied? true ]
[ set satisfied? false ]
]
type "satisfied turtles: " print count turtles with [satisfied?]
end
You need to have the clauses ordered so that it checks the any?
first. This should have been achieved by the nested ifelse
in your attempted solution except that you had the test reversed.