〚
Simulation test
〛
Density-
dependent dispersal promotes
female-biased sex allocation in
viscous populations
Chedhawat Chokechaipaisarn* and Andy Gardner
School of Biology, University of St. Andrews, St Andrews, Fife,
KY16 9TH, United Kingdom.
Fixed Parameter :
- Mutation rate
μ
= 0.01,
- Mutation increment standard deviation
σ
= 0.1,
- Patch number P = 1000,
- Fecundity K = 10,
- Generation G = 100,000
Varying Parameter:
- Number of foundresses
n
= 1, 2, 5, and 10
- Cost of dispersal
c
= 0.2, 0.4, 0.6, and 0.8
Haploidy
Model code
haploidModel
[
n
_
,
μ
_
,
σ
_
, c
_
, P
_
Integer, K
_
Integer, G
_
Integer
]
:
=
Module
{}
,
(*
Table for storing variable
*)
SexAllocation
=
Table
[
Null,
{
G
}]
;
DispersalRate
=
Table
[
Null,
{
G
}]
;
MaleCount
=
Table
[
0,
{
G
}]
;
FemaleCount
=
Table
[
0,
{
G
}]
;
DisperserCount
=
Table
[
0,
{
G
}]
;
ActualSexRatio
=
Table
[
Null,
{
G
}]
;
ActualDispersal
=
Table
[
Null,
{
G
}]
;
d0
=
Table
[
Null,
{
G
}]
;
d1
=
Table
[
Null,
{
G
}]
;
(*
rounding value
*)
SAgene
=
Round
n
-
1
2 n
, 0.01
;
Disgene
=
Round
2
1
+
2 c n
+
1
+
4 c
2
n
(
n
-
1
)
, 0.01
;
(*
Create mother P patches n each;
genes employ the optimal strategy value from analytical results
*)
Mother
=
Table
[{
SAgene, SAgene, Disgene, Disgene, Disgene, Disgene
}
, P, n
]
;
Do
(*
create list of juveniles
*)
JuM
=
Table
[{}
,
{
P
}]
;
JuF
=
Table
[{}
,
{
P
}]
;
(*<<
Mutation Phase
>>*)
totalgene
=
Length
[
Flatten
[
Mother
]]
;
mutationHappen
=
RandomInteger
[
PoissonDistribution
[
μ
totalgene
]]
;
(*
number of mutation occurrence
*)
(*
specify location
*)
randompatch
=
RandomInteger
[{
1, P
}
, mutationHappen
]
;
randommother
=
RandomInteger
[{
1, n
}
, mutationHappen
]
;
randomlocus
=
RandomInteger
[{
1, 6
}
, mutationHappen
]
;
(*
mutation
*)
Do
[
Mother
〚
randompatch
〚
x
〛
, randommother
〚
x
〛
, randomlocus
〚
x
〛〛
=
Mother
〚
randompatch
〚
x
〛
, randommother
〚
x
〛
, randomlocus
〚
x
〛〛
+
RandomReal
[
NormalDistribution
[
0,
σ
]]
,
{
x, mutationHappen
}]
;
(*<<
Sex Allocation Phase
>>*)
(*
Ensure that there is 1 male and 1 female per patch
*)
Do
[
i
=
Random
[
DiscreteUniformDistribution
[{
1, Length
[
Mother
〚
x
〛
]}]]
;
j
=
Random
[
DiscreteUniformDistribution
[{
1, Length
[
Mother
〚
x
〛
]}]]
;
Do
[
JuM
〚
x
〛
={{
Mother
〚
x, i, RandomInteger
[{
1, 2
}]
〛
,
Mother
〚
x, i, RandomInteger
[{
3, 4
}]
〛
, Mother
〚
x, i, RandomInteger
[{
5, 6
}]
〛
}}
;
MaleCount
〚
g
〛
=
MaleCount
〚
g
〛
+
1;
JuF
〚
x
〛
={{
Mother
〚
x, j, RandomInteger
[{
1, 2
}]
〛
,
Mother
〚
x, j, RandomInteger
[{
3, 4
}]
〛
, Mother
〚
x, j, RandomInteger
[{
5, 6
}]
〛
}}
;
FemaleCount
〚
g
〛
=
FemaleCount
〚
g
〛
+
1, 1
]
,
{
x, P
}]
;
Do
[
(*
create gene randomization
*)
gene1
=
RandomInteger
[{
1, 2
}]
;
gene2
=
RandomInteger
[{
3, 4
}]
;
gene3
=
RandomInteger
[{
5, 6
}]
;
2
ChokechaipaisarnGardner-SimulationTest-Code.nb
(*
individual offspring genotype
*)
offspring
={
Mother
〚
x, y, gene1
〛
, Mother
〚
x, y, gene2
〛
, Mother
〚
x, y, gene3
〛
}
;
(*
sex allocation process
*)
SAstrategy
=
Mother
〚
x, y, 1
〛
;
If
[
RandomReal
[]<
SAstrategy, JuM
〚
x
〛
=
Insert
[
JuM
〚
x
〛
, offspring, 1
]
;
MaleCount
〚
g
〛
=
MaleCount
〚
g
〛
+
1,
JuF
〚
x
〛
=
Insert
[
JuF
〚
x
〛
, offspring, 1
]
;
FemaleCount
〚
g
〛
=
FemaleCount
〚
g
〛
+
1
]
;
,
{
x, P
}
,
{
y, n
}
,
{
K
}]
;
(*<<
Mating Phase
>>*)
MatedF
=
Table
[
Table
[
Null,
{
Length
[
JuF
〚
x
〛
]}]
,
{
x, P
}]
;
Do
[
Do
[
indivmale
=
Random
[
DiscreteUniformDistribution
[{
1, Length
[
JuM
〚
x
〛
]}]]
;
MatedF
〚
x, f
〛
={
JuF
〚
x, f, 1
〛
, JuM
〚
x, indivmale, 1
〛
, JuF
〚
x, f, 2
〛
, JuM
〚
x, indivmale,
2
〛
, JuF
〚
x, f, 3
〛
, JuM
〚
x, indivmale, 3
〛
}
,
{
f, Length
[
JuF
〚
x
〛
]}]
,
{
x, P
}]
;
(*<<
Dispersal Phase
>>*)
(*
Ensure no empty patch
*)
DispersedF
=
Table
[{
MatedF
〚
x, 1
〛
}
,
{
x, P
}]
;
(*
Create list for individual dispersal rate
*)
d
=
Table
[
Table
[
, 1
]
, P
]
;
Do
cal1
=
MatedF
〚
x, 1, 3
〛
+(
MatedF
〚
x, 1, 5
〛
-
MatedF
〚
x, 1, 3
〛
)
Length
[
MatedF
〚
x
〛
]
n K
+
2
;
If
[
cal1
<
0, cal1
=
0 , If
[
cal1
>
1, cal1
=
1, cal1
=
cal1
]]
;
d
〚
x
〛
={
cal1
}
,
{
x, P
}
;
(*
Note: Dispersal probability
d
=
x
0
+(
x
1
-
x
0
)(
relative density
)
;
x
0
& x
1
=
average value of self dispersal locus;
Every dispersers with probability c die
enroute
*)
Do
Do
cal
=
MatedF
〚
x, m, 3
〛
+(
MatedF
〚
x, m, 5
〛
-
MatedF
〚
x, m, 3
〛
)
Length
[
MatedF
〚
x
〛
]
n K
+
2
;
Do
[
If
[
cal
<
0, cal
=
0, If
[
cal
>
1, cal
=
1, cal
=
cal
]]
, 1
]
;
d
〚
x
〛
=
Insert
[
d
〚
x
〛
, cal,
-
1
]
;
If
[
RandomReal
[]<
d
〚
x, m
〛
, z
=
Random
[
DiscreteUniformDistribution
[{
1, P
}]]
;
DisperserCount
〚
g
〛
=
DisperserCount
〚
g
〛
+
1;
If
[
RandomReal
[]>
c,
DispersedF
〚
z
〛
=
Insert
[
DispersedF
〚
z
〛
, MatedF
〚
x, m
〛
, 1
]
, Null
]
, DispersedF
〚
x
〛
=
ChokechaipaisarnGardner-SimulationTest-Code.nb
3
Insert
[
DispersedF
〚
x
〛
, MatedF
〚
x, m
〛
, 1
]]
,
{
m, 2, Length
[
MatedF
〚
x
〛
]}
,
{
x, P
}
;
(*<<
Competing Phase
>>*)
Mother
=
Table
[
Table
[
DispersedF
〚
x, Random
[
DiscreteUniformDistribution
[{
1, Length
[
DispersedF
〚
x
〛
]}]]
〛
, n
]
,
{
x, P
}]
;
(*
Store Optimal sex ratio of the population
*)
motherSAgene
=
Table
[
Mother
〚
x, y, 1
〛
,
{
x, P
}
,
{
y, n
}]
;
SexAllocation
〚
g
〛
=
1
P n
Apply
[
Plus, Flatten
[
motherSAgene
]]
;
ActualSexRatio
〚
g
〛
=
N
[
MaleCount
〚
g
〛
/(
FemaleCount
〚
g
〛
+
MaleCount
〚
g
〛
)]
;
(*
Store optimal dispersal rate of the population
*)
DispersalRate
〚
g
〛
=
Mean
[
Flatten
[
d
]]
;
motherd0gene
=
Table
[
Mother
〚
x, y, 3
〛
,
{
x, P
}
,
{
y, n
}]
;
motherd1gene
=
Table
[
Mother
〚
x, y, 5
〛
,
{
x, P
}
,
{
y, n
}]
;
d0
〚
g
〛
=
Mean
[
Flatten
[
motherd0gene
]]
;
d1
〚
g
〛
=
Mean
[
Flatten
[
motherd1gene
]]
;
ActualDispersal
〚
g
〛
=
N
[
DisperserCount
〚
g
〛
/
FemaleCount
〚
g
〛
]
;
,
{
g, G
}
;
(*
Average value over last 50,000 generations
*)
SAlast50k
=
Take
[
SexAllocation,
-
Round
[
G
/
2
]]
;
SRlast50k
=
Take
[
ActualSexRatio,
-
Round
[
G
/
2
]]
;
DThrlast50k
=
Take
[
DispersalRate,
-
Round
[
G
/
2
]]
;
ADRlast50k
=
Take
[
ActualDispersal,
-
Round
[
G
/
2
]]
;
Print
[
"Average value over the last 50,000 generation"
]
;
Print
[
"Sex allocation strategy
=
", Mean
[
SAlast50k
]]
;
Print
[
"Sex ratio
=
", Mean
[
SRlast50k
]]
;
Print
[
"Dispersal Threshold
=
", Mean
[
DThrlast50k
]]
;
Print
[
"Actual Dispersal rate
=
", Mean
[
ADRlast50k
]]
;
(*
plotting graphs
*)
Do
[
SA
=
ListPlot
[
SexAllocation, Joined
True, PlotRange
{{
0, G
}
, Automatic
}
,
AspectRatio
1, ImageSize
Small, Frame
True,
PlotStyle
Orange, PlotLabel
"Sex allocation strategy"
]
;
SR
=
ListPlot
[
ActualSexRatio, Joined
True, PlotRange
{{
0, G
}
, Automatic
}
,
AspectRatio
1, ImageSize
Small, Frame
True,
PlotStyle
GrayLevel
[
0.3
]
, PlotLabel
"Actual Sex ratio"
]
;
DpR
=
ListPlot
[{
DispersalRate, d0, d1
}
, Joined
True, PlotRange
{{
0, G
}
, Automatic
}
,
AspectRatio
1, ImageSize
Small, Frame
True,
PlotStyle
{{
RGBColor
[
0.43, 0.24, 0.
]}
,
{
RGBColor
[
0.78, 0.32, 0.38, 0.9
]}
,
{
RGBColor
[
0.24, 0.43, 0.71, 0.9
]}}
, PlotLabel
"Dispersal threshold"
]
;
4
ChokechaipaisarnGardner-SimulationTest-Code.nb
ADpR
=
ListPlot
[
ActualDispersal, Joined
True, PlotRange
{{
0, G
}
, Automatic
}
,
AspectRatio
1, ImageSize
Small, Frame
True,
PlotStyle
GrayLevel
[
0.3
]
, PlotLabel
"Actual dispersal"
]
;
, 1
]
;
GraphicsGrid
[{{
SA, SR
}
,
{
DpR, ADpR
}}
, AspectRatio
1
]
;
Analytical prediction
haploidmodelPrediction
[
n
_
, c
_]
:
=
Module
{
sexallo, disperse
}
,
Do
sexallo
=
1
-
2 n
+
√
1
+
4 c
2
(-
1
+
n
)
n
4
-
1
+
c
2
n
;
disperse
=
2
1
+
2 c n
+
√
1
+
4 c
2
n
(
n
-
1
)
;
Print
[
"Sex allocation prediction
=
", sexallo
]
;
Print
[
"Dispersal prediction
=
", disperse
]
, 1
;
Diploidy
Model code
DiploidModel
[
n
_
,
μ
_
,
σ
_
, c
_
, P
_
Integer, K
_
Integer, G
_
Integer
]
:
=
Module
{}
,
(*
Table for storing variable
*)
SexAllocation
=
Table
[
Null,
{
G
}]
;
DispersalRate
=
Table
[
Null,
{
G
}]
;
MaleCount
=
Table
[
0,
{
G
}]
;
FemaleCount
=
Table
[
0,
{
G
}]
;
DisperserCount
=
Table
[
0,
{
G
}]
;
ActualSexRatio
=
Table
[
Null,
{
G
}]
;
ActualDispersal
=
Table
[
Null,
{
G
}]
;
d0
=
Table
[
Null,
{
G
}]
;
d1
=
Table
[
Null,
{
G
}]
;
(*
rounding value
*)
SAgene
=
Round
n
-
1
2 n
, 0.01
;
Disgene
=
Round
2
1
+
2 c n
+
1
+
4 c
2
n
(
n
-
1
)
, 0.01
;
ChokechaipaisarnGardner-SimulationTest-Code.nb
5
(*
Create mother P patches n each;
genes employ the optimal strategy value from analytical results
*)
Mother
=
Table
[{{
SAgene, SAgene
}
,
{
SAgene, SAgene
}
,
{
Disgene, Disgene
}
,
{
Disgene, Disgene
}
,
{
Disgene, Disgene
}
,
{
Disgene, Disgene
}}
, P, n
]
;
Do
[
(*
create list of juveniles
*)
JuM
=
Table
[{}
,
{
P
}]
;
JuF
=
Table
[{}
,
{
P
}]
;
(*<<
Mutation Phase
>>*)
totalgene
=
Length
[
Flatten
[
Mother
]]
;
mutationHappen
=
RandomInteger
[
PoissonDistribution
[
μ
totalgene
]]
;
(*
number of mutation occurrence
*)
(*
specify location
*)
randompatch
=
RandomInteger
[{
1, P
}
, mutationHappen
]
;
randommother
=
RandomInteger
[{
1, n
}
, mutationHappen
]
;
randomlocus
=
RandomInteger
[{
1, 6
}
, mutationHappen
]
;
randomgene
=
RandomInteger
[{
1, 2
}
, mutationHappen
]
;
(*
mutation
*)
Do
[
Mother
〚
randompatch
〚
x
〛
, randommother
〚
x
〛
, randomlocus
〚
x
〛
, randomgene
〚
x
〛〛
=
Mother
〚
randompatch
〚
x
〛
, randommother
〚
x
〛
, randomlocus
〚
x
〛
, randomgene
〚
x
〛〛
+
RandomReal
[
NormalDistribution
[
0,
σ
]]
,
{
x, mutationHappen
}]
;
(*<<
Sex Allocation Phase
>>*)
(*
Ensure at least 1 male and 1 female per patch
*)
Do
[
i
=
Random
[
DiscreteUniformDistribution
[{
1, Length
[
Mother
〚
x
〛
]}]]
;
j
=
Random
[
DiscreteUniformDistribution
[{
1, Length
[
Mother
〚
x
〛
]}]]
;
Do
[
JuM
〚
x
〛
={{{
Mother
〚
x, i, 1, RandomInteger
[{
1, 2
}]
〛
, Mother
〚
x, i, 2,
RandomInteger
[{
1, 2
}]
〛
}
,
{
Mother
〚
x, i, 3, RandomInteger
[{
1, 2
}]
〛
,
Mother
〚
x, i, 4, RandomInteger
[{
1, 2
}]
〛
}
,
{
Mother
〚
x, i, 5,
RandomInteger
[{
1, 2
}]
〛
, Mother
〚
x, i, 6, RandomInteger
[{
1, 2
}]
〛
}}}
;
MaleCount
〚
g
〛
=
MaleCount
〚
g
〛
+
1;
JuF
〚
x
〛
={{{
Mother
〚
x, j, 1, RandomInteger
[{
1, 2
}]
〛
, Mother
〚
x, j, 2,
RandomInteger
[{
1, 2
}]
〛
}
,
{
Mother
〚
x, j, 3, RandomInteger
[{
1, 2
}]
〛
,
Mother
〚
x, j, 4, RandomInteger
[{
1, 2
}]
〛
}
,
{
Mother
〚
x, j, 5,
RandomInteger
[{
1, 2
}]
〛
, Mother
〚
x, j, 6, RandomInteger
[{
1, 2
}]
〛
}}}
;
FemaleCount
〚
g
〛
=
FemaleCount
〚
g
〛
+
1, 1
]
,
{
x, P
}]
;
Do
[
(*
create gene randomization
*)
gene1
=
RandomInteger
[{
1, 2
}]
;
gene2
=
RandomInteger
[{
1, 2
}]
;
gene3
=
RandomInteger
[{
1, 2
}]
;
gene4
=
RandomInteger
[{
1, 2
}]
;
gene5
=
RandomInteger
[{
1, 2
}]
;
6
ChokechaipaisarnGardner-SimulationTest-Code.nb
gene6
=
RandomInteger
[{
1, 2
}]
;
(*
individual offspring genotype
*)
offspring
={{
Mother
〚
x, y, 1, gene1
〛
, Mother
〚
x, y, 2, gene2
〛
}
,
{
Mother
〚
x, y, 3, gene3
〛
, Mother
〚
x, y, 4, gene4
〛
}
,
{
Mother
〚
x, y, 5, gene5
〛
, Mother
〚
x, y, 6, gene6
〛
}}
;
(*
sex allocation process
*)
SAstrategy
=
Mean
[
Flatten
[
Mother
〚
x, y, 1
〛
]]
;
If
[
RandomReal
[]<
SAstrategy, JuM
〚
x
〛
=
Insert
[
JuM
〚
x
〛
, offspring, 1
]
;
MaleCount
〚
g
〛
=
MaleCount
〚
g
〛
+
1,
JuF
〚
x
〛
=
Insert
[
JuF
〚
x
〛
, offspring, 1
]
;
FemaleCount
〚
g
〛
=
FemaleCount
〚
g
〛
+
1
]
;
,
{
x, P
}
,
{
y, n
}
,
{
K
}]
;
(*<<
Mating Phase
>>*)
MatedF
=
Table
[
Table
[
Null,
{
Length
[
JuF
〚
x
〛
]}]
,
{
x, P
}]
;
Do
[
Do
[
indivmale
=
Random
[
DiscreteUniformDistribution
[{
1, Length
[
JuM
〚
x
〛
]}]]
;
MatedF
〚
x, f
〛
={
JuF
〚
x, f, 1
〛
, JuM
〚
x, indivmale, 1
〛
, JuF
〚
x, f, 2
〛
, JuM
〚
x, indivmale,
2
〛
, JuF
〚
x, f, 3
〛
, JuM
〚
x, indivmale, 3
〛
}
,
{
f, Length
[
JuF
〚
x
〛
]}]
,
{
x, P
}]
;
(*<<
Dispersal Phase
>>*)
(*
Ensure no empty patch
*)
DispersedF
=
Table
[{
MatedF
〚
x, 1
〛
}
,
{
x, P
}]
;
(*
Create list for individual dispersal rate
*)
d
=
Table
[
Table
[
, 1
]
, P
]
;
Do
[
cal1
=
Mean
[
Flatten
[
MatedF
〚
x, 1, 3
〛
]]+(
Mean
[
Flatten
[
MatedF
〚
x, 1, 5
〛
]]-
Mean
[
Flatten
[
MatedF
〚
x, 1, 3
〛
]])(
Length
[
MatedF
〚
x
〛
]/(
n K
+
2
))
;
If
[
cal1
<
0, cal1
=
0 , If
[
cal1
>
1, cal1
=
1, cal1
=
cal1
]]
;
d
〚
x
〛
={
cal1
}
;
,
{
x, P
}]
;
(*
Note: Dispersal probability d
=
x
0
+(
x
1
-
x
0
)(
relative density
)
;
x
0
& x
1
=
average value of self dispersal locus
Every dispersers with probability c die
enroute
*)
Do
[
Do
[
cal
=
Mean
[
Flatten
[
MatedF
〚
x, m, 3
〛
]]+(
Mean
[
Flatten
[
MatedF
〚
x, m, 5
〛
]]-
Mean
[
Flatten
[
MatedF
〚
x, m, 3
〛
]])(
Length
[
MatedF
〚
x
〛
]/(
n K
+
2
))
;
Do
[
If
[
cal
<
0, cal
=
0, If
[
cal
>
1, cal
=
1, cal
=
cal
]]
, 1
]
;
d
〚
x
〛
=
Insert
[
d
〚
x
〛
, cal,
-
1
]
;
If
[
RandomReal
[]<
d
〚
x, m
〛
, z
=
Random
[
DiscreteUniformDistribution
[{
1, P
}]]
;
DisperserCount
〚
g
〛
=
DisperserCount
〚
g
〛
+
1;
If
[
RandomReal
[]>
c,
ChokechaipaisarnGardner-SimulationTest-Code.nb
7
DispersedF
〚
z
〛
=
Insert
[
DispersedF
〚
z
〛
, MatedF
〚
x, m
〛
, 1
]
, Null
]
, DispersedF
〚
x
〛
=
Insert
[
DispersedF
〚
x
〛
, MatedF
〚
x, m
〛
, 1
]]
,
{
m, 2, Length
[
MatedF
〚
x
〛
]}]
,
{
x, P
}]
;
(*<<
Competing Phase
>>*)
Mother
=
Table
[
Table
[
DispersedF
〚
x, Random
[
DiscreteUniformDistribution
[{
1, Length
[
DispersedF
〚
x
〛
]}]]
〛
, n
]
,
{
x, P
}]
;
(*
Store Optimal sex ratio of the population
*)
motherSAgene
=
Table
[
Mother
〚
x, y, 1
〛
,
{
x, P
}
,
{
y, n
}]
;
SexAllocation
〚
g
〛
=(
1
/(
2 P n
))
Apply
[
Plus, Flatten
[
motherSAgene
]]
;
ActualSexRatio
〚
g
〛
=
N
[
MaleCount
〚
g
〛
/(
FemaleCount
〚
g
〛
+
MaleCount
〚
g
〛
)]
;
(*
Store optimal dispersal rate of the population
*)
DispersalRate
〚
g
〛
=
Mean
[
Flatten
[
d
]]
;
motherd0gene
=
Table
[
Mother
〚
x, y, 3
〛
,
{
x, P
}
,
{
y, n
}]
;
motherd1gene
=
Table
[
Mother
〚
x, y, 5
〛
,
{
x, P
}
,
{
y, n
}]
;
d0
〚
g
〛
=
Mean
[
Flatten
[
motherd0gene
]]
;
d1
〚
g
〛
=
Mean
[
Flatten
[
motherd1gene
]]
;
ActualDispersal
〚
g
〛
=
N
[
DisperserCount
〚
g
〛
/
FemaleCount
〚
g
〛
]
;
,
{
g, G
}]
;
(*
Average value over last 50,000 generations
*)
SAlast50k
=
Take
[
SexAllocation,
-
50 000
]
;
SRlast50k
=
Take
[
ActualSexRatio,
-
50 000
]
;
DThrlast50k
=
Take
[
DispersalRate,
-
50 000
]
;
ADRlast50k
=
Take
[
ActualDispersal,
-
50 000
]
;
Print
[
"Average value over the last 50,000 generation"
]
;
Print
[
"Sex allocation strategy
=
", Mean
[
SAlast50k
]]
;
Print
[
"Sex ratio
=
", Mean
[
SRlast50k
]]
;
Print
[
"Dispersal Threshold
=
", Mean
[
DThrlast50k
]]
;
Print
[
"Actual Dispersal rate
=
", Mean
[
ADRlast50k
]]
;
(*
plotting graphs
*)
Do
[
SA
=
ListPlot
[
SexAllocation, Joined
True, PlotRange
{{
0, G
}
, Automatic
}
,
AspectRatio
1, ImageSize
Small, Frame
True,
PlotStyle
Orange, PlotLabel
"Sex allocation strategy"
]
;
SR
=
ListPlot
[
ActualSexRatio, Joined
True, PlotRange
{{
0, G
}
, Automatic
}
,
AspectRatio
1, ImageSize
Small, Frame
True,
PlotStyle
GrayLevel
[
0.3
]
, PlotLabel
"Actual Sex ratio"
]
;
DpR
=
ListPlot
[{
DispersalRate, d0, d1
}
, Joined
True, PlotRange
{{
0, G
}
, Automatic
}
,
AspectRatio
1, ImageSize
Small, Frame
True,
PlotStyle
{{
RGBColor
[
0.43, 0.24, 0.
]}
,
{
RGBColor
[
0.78, 0.32, 0.38, 0.9
]}
,
{
RGBColor
[
0.24, 0.43, 0.71, 0.9
]}}
, PlotLabel
"Dispersal threshold"
]
;
ADpR
=
ListPlot
[
ActualDispersal, Joined
True, PlotRange
{{
0, G
}
, Automatic
}
,
8
ChokechaipaisarnGardner-SimulationTest-Code.nb
AspectRatio
1, ImageSize
Small, Frame
True,
PlotStyle
GrayLevel
[
0.3
]
, PlotLabel
"Actual dispersal"
]
;
, 1
]
;
GraphicsGrid
[{{
SA, SR
}
,
{
DpR, ADpR
}}
, AspectRatio
1
]
;
Analytical prediction
diploidmodelPrediction
[
n
_
, c
_]
:
=
Module
{
SA, disperse
}
,
Do
SA
=
1
-
2 n
+
√
1
+
4 c
2
(-
1
+
n
)
n
4
-
1
+
c
2
n
;
disperse
=
2
1
+
2 c n
+
√
1
+
4 c
2
n
(
n
-
1
)
;
Print
[
"Sex allocation prediction
=
", SA
]
;
Print
[
"Dispersal prediction
=
", disperse
]
, 1
;
Haplodiploidy
Model code
haplodiploidModel
[
n
_
,
μ
_
,
σ
_
, c
_
, P
_
Integer, K
_
Integer, G
_
Integer
]
:
=
Module
{}
,
(*
Table for storing variable
*)
SexAllocation
=
Table
[
Null,
{
G
}]
;
DispersalRate
=
Table
[
Null,
{
G
}]
;
MaleCount
=
Table
[
0,
{
G
}]
;
FemaleCount
=
Table
[
0,
{
G
}]
;
DisperserCount
=
Table
[
0,
{
G
}]
;
ActualSexRatio
=
Table
[
Null,
{
G
}]
;
ActualDispersal
=
Table
[
Null,
{
G
}]
;
d0
=
Table
[
Null,
{
G
}]
;
d1
=
Table
[
Null,
{
G
}]
;
(*
rounding value
*)
SAgene
=
Round
n
-
1
2 n
8 n
-
2 c
2
(
n
-
1
)(
3 n
-
2
)(
3 n
-
1
)-
1
+
4 n
(
n
-
1
)
c
2
-
3
8 n
-
2 c
2
(
n
-
1
)(
3 n
-
1
)
2
-
2
, 0.01
;
Disgene
=
Round
2
1
+
2 c n
+
1
+
4 c
2
n
(
n
-
1
)
, 0.01
;
ChokechaipaisarnGardner-SimulationTest-Code.nb
9
(*
Create mother P patches n each;
genes employ the optimal strategy value from analytical results
*)
Mother
=
Table
[{{
SAgene, SAgene
}
, SAgene,
{
Disgene, Disgene
}
, Disgene,
{
Disgene, Disgene
}
, Disgene
}
, P, n
]
;
Do
(*
create list of juveniles
*)
JuM
=
Table
[{}
,
{
P
}]
;
JuF
=
Table
[{}
,
{
P
}]
;
(*<<
Mutation Phase
>>*)
totalgene
=
Length
[
Flatten
[
Mother
]]
;
mutationHappen
=
RandomInteger
[
PoissonDistribution
[
μ
totalgene
]]
;
(*
number of mutation occurrence
*)
(*
specify location
*)
randompatch
=
RandomInteger
[{
1, P
}
, mutationHappen
]
;
randommother
=
RandomInteger
[{
1, n
}
, mutationHappen
]
;
randomlocus
=
RandomInteger
[{
1, 6
}
, mutationHappen
]
;
randomgene
=
RandomInteger
[{
1, 2
}
, mutationHappen
]
;
(*
mutation
*)
Do
[
If
[
EvenQ
[
randomlocus
〚
x
〛
]
,
Mother
〚
randompatch
〚
x
〛
, randommother
〚
x
〛
, randomlocus
〚
x
〛〛
=
Mother
〚
randompatch
〚
x
〛
, randommother
〚
x
〛
, randomlocus
〚
x
〛〛
+
RandomReal
[
NormalDistribution
[
0,
σ
]]
,
Mother
〚
randompatch
〚
x
〛
, randommother
〚
x
〛
, randomlocus
〚
x
〛
, randomgene
〚
x
〛〛
=
Mother
〚
randompatch
〚
x
〛
, randommother
〚
x
〛
, randomlocus
〚
x
〛
, randomgene
〚
x
〛〛
+
RandomReal
[
NormalDistribution
[
0,
σ
]]]
,
{
x, mutationHappen
}]
;
(*<<
Sex Allocation Phase
>>*)
(*
Ensure that there is 1 male and 1 female per patch
*)
Do
[
i
=
Random
[
DiscreteUniformDistribution
[{
1, Length
[
Mother
〚
x
〛
]}]]
;
j
=
Random
[
DiscreteUniformDistribution
[{
1, Length
[
Mother
〚
x
〛
]}]]
;
Do
[
JuM
〚
x
〛
={{
Mother
〚
x, i, 1, RandomInteger
[{
1, 2
}]
〛
, Mother
〚
x, i, 3,
RandomInteger
[{
1, 2
}]
〛
, Mother
〚
x, i, 5, RandomInteger
[{
1, 2
}]
〛
}}
;
MaleCount
〚
g
〛
=
MaleCount
〚
g
〛
+
1;
JuF
〚
x
〛
={{{
Mother
〚
x, j, 1, RandomInteger
[{
1, 2
}]
〛
, Mother
〚
x, j, 2
〛
}
,
{
Mother
〚
x, j, 3, RandomInteger
[{
1, 2
}]
〛
, Mother
〚
x, j, 4
〛
}
,
{
Mother
〚
x, j, 5, RandomInteger
[{
1, 2
}]
〛
, Mother
〚
x, j, 6
〛
}}}
;
FemaleCount
〚
g
〛
=
FemaleCount
〚
g
〛
+
1, 1
]
,
{
x, P
}]
;
Do
[
(*
create gene randomization
*)
gene1
=
RandomInteger
[{
1, 2
}]
;
gene3
=
RandomInteger
[{
1, 2
}]
;
gene5
=
RandomInteger
[{
1, 2
}]
;
(*
individual offspring genotype
*)
10
ChokechaipaisarnGardner-SimulationTest-Code.nb
offspringMale
=
{
Mother
〚
x, y, 1, gene1
〛
, Mother
〚
x, y, 3, gene3
〛
, Mother
〚
x, y, 5, gene5
〛
}
;
offspringFemale
={{
Mother
〚
x, y, 1, RandomInteger
[{
1, 2
}]
〛
, Mother
〚
x, y, 2
〛
}
,
{
Mother
〚
x, y, 3, RandomInteger
[{
1, 2
}]
〛
, Mother
〚
x, y, 4
〛
}
,
{
Mother
〚
x, y, 5, RandomInteger
[{
1, 2
}]
〛
, Mother
〚
x, y, 6
〛
}}
;
(*
sex allocation process
*)
SAstrategy
=(
2
/
3
)(
Apply
[
Plus, Flatten
[
Mother
〚
x, y, 1
〛
]])
;
If
[
RandomReal
[]<
SAstrategy, JuM
〚
x
〛
=
Insert
[
JuM
〚
x
〛
, offspringMale, 1
]
;
MaleCount
〚
g
〛
=
MaleCount
〚
g
〛
+
1,
JuF
〚
x
〛
=
Insert
[
JuF
〚
x
〛
, offspringFemale, 1
]
;
FemaleCount
〚
g
〛
=
FemaleCount
〚
g
〛
+
1
]
;
,
{
x, P
}
,
{
y, n
}
,
{
K
}]
;
(*<<
Mating Phase
>>*)
MatedF
=
Table
[
Table
[
Null,
{
Length
[
JuF
〚
x
〛
]}]
,
{
x, P
}]
;
Do
[
Do
[
indivmale
=
Random
[
DiscreteUniformDistribution
[{
1, Length
[
JuM
〚
x
〛
]}]]
;
MatedF
〚
x, f
〛
={
JuF
〚
x, f, 1
〛
, JuM
〚
x, indivmale, 1
〛
, JuF
〚
x, f, 2
〛
, JuM
〚
x, indivmale,
2
〛
, JuF
〚
x, f, 3
〛
, JuM
〚
x, indivmale, 3
〛
}
,
{
f, Length
[
JuF
〚
x
〛
]}]
,
{
x, P
}]
;
(*<<
Dispersal Phase
>>*)
(*
Ensure no empty patch
*)
DispersedF
=
Table
[{
MatedF
〚
x, 1
〛
}
,
{
x, P
}]
;
(*
Create list for individual dispersal rate
*)
d
=
Table
[
Table
[
, 1
]
, P
]
;
Do
[
cal1
=
Mean
[
Flatten
[
MatedF
〚
x, 1, 3
〛
]]+(
Mean
[
Flatten
[
MatedF
〚
x, 1, 5
〛
]]-
Mean
[
Flatten
[
MatedF
〚
x, 1, 3
〛
]])(
Length
[
MatedF
〚
x
〛
]/(
n K
+
2
))
;
If
[
cal1
<
0, cal1
=
0 , If
[
cal1
>
1, cal1
=
1, cal1
=
cal1
]]
;
d
〚
x
〛
={
cal1
}
;
,
{
x, P
}]
;
(*
Note: Dispersal probability d
=
x
0
+(
x
1
-
x
0
)(
relative density
)
;
x
0
& x
1
=
average value of self dispersal locus
Every dispersers with probability c die
enroute
*)
Do
[
Do
[
cal
=
Mean
[
Flatten
[
MatedF
〚
x, m, 3
〛
]]+(
Mean
[
Flatten
[
MatedF
〚
x, m, 5
〛
]]-
Mean
[
Flatten
[
MatedF
〚
x, m, 3
〛
]])(
Length
[
MatedF
〚
x
〛
]/(
n K
+
2
))
;
Do
[
If
[
cal
<
0, cal
=
0, If
[
cal
>
1, cal
=
1, cal
=
cal
]]
, 1
]
;
d
〚
x
〛
=
Insert
[
d
〚
x
〛
, cal,
-
1
]
;
If
[
RandomReal
[]<
d
〚
x, m
〛
, z
=
Random
[
DiscreteUniformDistribution
[{
1, P
}]]
;
DisperserCount
〚
g
〛
=
DisperserCount
〚
g
〛
+
1;
If
[
RandomReal
[]>
c,
DispersedF
〚
z
〛
=
Insert
[
DispersedF
〚
z
〛
, MatedF
〚
x, m
〛
, 1
]
, Null
]
, DispersedF
〚
x
〛
=
ChokechaipaisarnGardner-SimulationTest-Code.nb
11
Insert
[
DispersedF
〚
x
〛
, MatedF
〚
x, m
〛
, 1
]]
,
{
m, 2, Length
[
MatedF
〚
x
〛
]}]
,
{
x, P
}]
;
(*<<
Competing Phase
>>*)
Mother
=
Table
[
Table
[
DispersedF
〚
x, Random
[
DiscreteUniformDistribution
[{
1, Length
[
DispersedF
〚
x
〛
]}]]
〛
, n
]
,
{
x, P
}]
;
(*
Store Optimal sex ratio of the population
*)
motherSAgene
=
Table
[
Mother
〚
x, y, 1
〛
,
{
x, P
}
,
{
y, n
}]
;
SexAllocation
〚
g
〛
=
2
3 P n
Apply
[
Plus, Flatten
[
motherSAgene
]]
;
ActualSexRatio
〚
g
〛
=
N
[
MaleCount
〚
g
〛
/(
FemaleCount
〚
g
〛
+
MaleCount
〚
g
〛
)]
;
(*
Store optimal dispersal rate of the population
*)
DispersalRate
〚
g
〛
=
Mean
[
Flatten
[
d
]]
;
motherd0gene
=
Table
[
Mother
〚
x, y, 3
〛
,
{
x, P
}
,
{
y, n
}]
;
motherd1gene
=
Table
[
Mother
〚
x, y, 5
〛
,
{
x, P
}
,
{
y, n
}]
;
d0
〚
g
〛
=
Mean
[
Flatten
[
motherd0gene
]]
;
d1
〚
g
〛
=
Mean
[
Flatten
[
motherd1gene
]]
;
ActualDispersal
〚
g
〛
=
N
[
DisperserCount
〚
g
〛
/
FemaleCount
〚
g
〛
]
;
,
{
g, G
}
;
(*
Average value over last 50,000 generations
*)
SAlast50k
=
Take
[
SexAllocation,
-
Round
[
G
/
2
]]
;
SRlast50k
=
Take
[
ActualSexRatio,
-
Round
[
G
/
2
]]
;
DThrlast50k
=
Take
[
DispersalRate,
-
Round
[
G
/
2
]]
;
ADRlast50k
=
Take
[
ActualDispersal,
-
Round
[
G
/
2
]]
;
Print
[
"Average value over the last 50,000 generation"
]
;
Print
[
"Sex allocation strategy
=
", Mean
[
SAlast50k
]]
;
Print
[
"Sex ratio
=
", Mean
[
SRlast50k
]]
;
Print
[
"Dispersal Threshold
=
", Mean
[
DThrlast50k
]]
;
Print
[
"Actual Dispersal rate
=
", Mean
[
ADRlast50k
]]
;
(*
plotting graphs
*)
Do
[
SA
=
ListPlot
[
SexAllocation, Joined
True, PlotRange
{{
0, G
}
, Automatic
}
,
AspectRatio
1, ImageSize
Small, Frame
True,
PlotStyle
Orange, PlotLabel
"Sex allocation strategy"
]
;
SR
=
ListPlot
[
ActualSexRatio, Joined
True, PlotRange
{{
0, G
}
, Automatic
}
,
AspectRatio
1, ImageSize
Small, Frame
True,
PlotStyle
GrayLevel
[
0.3
]
, PlotLabel
"Actual Sex ratio"
]
;
DpR
=
ListPlot
[{
DispersalRate, d0, d1
}
, Joined
True, PlotRange
{{
0, G
}
, Automatic
}
,
AspectRatio
1, ImageSize
Small, Frame
True,
PlotStyle
{{
RGBColor
[
0.43, 0.24, 0.
]}
,
{
RGBColor
[
0.78, 0.32, 0.38, 0.9
]}
,
{
RGBColor
[
0.24, 0.43, 0.71, 0.9
]}}
, PlotLabel
"Dispersal threshold"
]
;
ADpR
=
ListPlot
[
ActualDispersal, Joined
True, PlotRange
{{
0, G
}
, Automatic
}
,
12
ChokechaipaisarnGardner-SimulationTest-Code.nb
AspectRatio
1, ImageSize
Small, Frame
True,
PlotStyle
GrayLevel
[
0.3
]
, PlotLabel
"Actual dispersal"
]
;
, 1
]
;
GraphicsGrid
[{{
SA, SR
}
,
{
DpR, ADpR
}}
, AspectRatio
1
]
;
Analytical prediction
haplodiploidmodelPrediction
[
n
_
, c
_]
:
=
Module
{
SA, disperse, H
}
,
Do
H
=
1
+
4 c
2
n
(
n
-
1
)
;
SA
=
n
(
8 n
-
7
-
5 H
)+
2
(
1
+
H
)+
c
2
(
n
-
1
)(
2
(
1
+
H
)-
n
(
9 n
(
2 n
-
3
-
H
)+
9 H
+
11
))
4 n
c
2
-
1
1
+
c
2
(
3 n
-
1
)
2
(
n
-
1
)-
4 n
; disperse
=
2
1
+
2 c n
+
1
+
4 c
2
n
(
n
-
1
)
;
Print
[
"Sex allocation prediction
=
", SA
]
;
Print
[
"Dispersal prediction
=
", disperse
]
, 1
;
Statistical analysis
Align data
In[
]:=
alignData
[
dHap
_
List, dDip
_
List, sHap
_
List, sDip
_
List
]
:
=
Module
[{
realdata, n
}
,
realdata
=
Table
[{}
, 3
]
;
n
=
1;
Do
[
Do
[
If
[
EvenQ
[
n
]
, realdata
〚
group
〛
=
Insert
[
realdata
〚
group
〛
,
{
dDip
〚
group, i
〛
, sDip
〚
group, i
〛
}
,
-
1
]
, realdata
〚
group
〛
=
Insert
[
realdata
〚
group
〛
,
{
dHap
〚
group, i
〛
, sHap
〚
group, i
〛
}
,
-
1
]]
;
n
=
n
+
1;
,
{
i, 4
}
, 2
]
,
{
group, 3
}]
;
Return
[
realdata
]
;
]
ChokechaipaisarnGardner-SimulationTest-Code.nb
13
Randomization code
permutation
[
data
_
List
]
:
=
Module
[{
yList, shuffle, permutedList
}
,
(
yList
=
Table
[{}
, 3
]
;
permutedList
=
Table
[{}
, 3
]
;
(*
Obtain y value
(
Sex ratio
)
from data
*)
Do
[
yList
〚
group
〛
=
Insert
[
yList
〚
group
〛
, data
〚
group, coordinate, 2
〛
,
-
1
]
,
{
group, 3
}
,
{
coordinate, 8
}]
;
(*
Shuffle y value
*)
shuffle
=
Table
[
Permute
[
yList
〚
group
〛
, RandomPermutation
[
8
]]
,
{
group, 3
}]
;
(*
align x and y
*)
Do
[
permutedList
〚
group
〛
=
Insert
[
permutedList
〚
group
〛
,
{
data
〚
group, coordinate, 1
〛
, shuffle
〚
group, coordinate
〛
}
,
-
1
]
,
{
group, 3
}
,
{
coordinate, 8
}]
;
Return
[
permutedList
]
)]
Nested linear regression code
regressionCoeff
[
data
_
List
]
:
=
Module
{
sumsquare, dsumsdm, dsumsdc1, dsumsdc2, dsumsdc3, sol, coefficient
}
,
sumsquare
=
group
=
1
3
i
=
1
8
data
〚
group, i, 2
〛
-
m data
〚
group, i, 1
〛
+
c
group
2
;
dsumsdm
=
D
[
sumsquare, m
]
;
dsumsdc1
=
D
[
sumsquare, c
1
]
;
dsumsdc2
=
D
[
sumsquare, c
2
]
;
dsumsdc3
=
D
[
sumsquare, c
3
]
;
sol
=
Solve
[{
dsumsdm
0, dsumsdc1
0, dsumsdc2
0, dsumsdc3
0
}
,
{
m, c
1
, c
2
, c
3
}]
;
coefficient
=
m
/
. sol;
Return
[
coefficient
]
14
ChokechaipaisarnGardner-SimulationTest-Code.nb