I want to make turtles move around and look for a partner (a turtle that is not already partnered that is on the same patch) , and when they found one I want them to hatch a certain number of turtles and then die. But what happens is that when I start the simulation, all turtles die at the first tick. What am I doing wrong?
to go
ask turtles [
partner-up
]
birth
death
tick
end
to partner-up
if (not partnered?) [
rt (random-float 90 - random-float 90) fd 1
set partner one-of (turtles-at 0 0) with [ not partnered? ]
if partner != nobody [
set partnered? true
ask partner [
set partnered? true
set partner myself
]
]
]
end
to birth
let partnered-turtles turtles with [ partnered? ]
ask partnered-turtles [
calculate-score
hatch (score + 1)
]
end
to death
let partnered-turtles turtles with [ partnered? ]
ask partnered-turtles [
die
]
end
Two things stand out- one is the call to one-of (turtles-at 0 0) with [ not partnered? ]
. This includes ALL turtles on the patch that you indicate (equivalent to turtles-here
), which means including the turtle executing the command. I think you want other turtles-here
or (other turtles-at 0 0)
.
Next, your 'baby turtles' are inheriting all the attributes of their parents, including values in their partnered?
and partner
variables. So, the turtles are being hatched with partnered? = true
, and so when the call to death
occurs in your go
procedure, they qualify as partnered-turtles
and therefore die. To correct this, you can explicitly set the variables for your hatched turtles- for example:
to birth
let partnered-turtles turtles with [ partnered? ]
ask partnered-turtles [
let score 1
hatch (score + 1) [
set partnered? false
set partner nobody
]
]
end
Also, not sure if this is intentional but wanted to point out that both 'partners' will hatch
an offspring. If you want a more standard biological model you may want only one of the parents to hatch.
Revised toy model:
turtles-own [ partnered? partner]
to setup
ca
reset-ticks
ask n-of 10 patches [
sprout 1 [
set partnered? false
set partner nobody
]
]
end
to go
ask turtles [
partner-up
]
birth
death
tick
end
to partner-up
if (not partnered?) [
rt (random-float 90 - random-float 90) fd 1
set partner one-of (other turtles-at 0 0) with [ not partnered? ]
if partner != nobody [
set partnered? true
ask partner [
set partnered? true
set partner myself
]
]
]
end
to birth
let partnered-turtles turtles with [ partnered? ]
ask partnered-turtles [
let score one-of [ 0 1 ]
hatch (score + 1) [
set partnered? false
set partner nobody
]
]
end
to death
let partnered-turtles turtles with [ partnered? ]
ask partnered-turtles [
die
]
end