netlogo - How to ask one or two turtles (if any are present) to do an action? -
the following code finds patches there @ least 1 male , 1 female present, on each such patch, has 1 female hatch offspring turtle of random gender.
turtles-own [ gender] setup ask patches [ sprout 1 [set size 0.2 set color pink set gender "female" ]] ask patches [ sprout 1 [set size 0.2 set color blue set gender "male" ]] reset-ticks end to-report parents-here? ;; patch procedure report any? turtles-here [gender = "male"] , any? turtles-here [gender = "female"] end go ask patches [parents-here?] [ ask one-of turtles-here [gender = "female"] [ hatch 1 [ set gender one-of ["male" "female"] ] ] ] tick end
instead of asking 1 female hatch, want ask if 1 female present ask hatch " or " if 2 females present ask them hatch ( minmum 1 , maximum two). tried write it
ask n-of 2 turtles-here ............
but had error said patch has 1 turtles
i tried use ( error) tried write
ask n-of (1 + random 2 )
as minimum , maximum, , wrong.
thank in advance
this simplest solution can think of:
let females turtles-here [gender = "female"] ask n-of (min list 2 count females) females [ hatch 1 [ ... ] ]
why min list 2 count females
? it's little counterintuitive need use primitive called min
when want maximum of 2. result of min list 2 ...
2 or smaller. or if break down cases:
- if
count females
0,min list 2 count females
0. - if
count females
1,min list 2 count females
1. - if
count females
2 or more,min list 2 count females
2.
which, if understand correctly, trying for.
Comments
Post a Comment