-
Notifications
You must be signed in to change notification settings - Fork 0
/
biologically_constrained_feasibility_domain.R
165 lines (126 loc) · 3.71 KB
/
biologically_constrained_feasibility_domain.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
require(here)
# defines some functions used below
source(here("code/lib/feasibility_utils.R"))
pdf(file=here("figures/feasibility_domain.pdf"),width = 10, height = 3)
#xlab=expression(italic(r[i]))
#ylab=expression(italic(r[j]))
make_figure_area <- function(label){
plot(NA,NA,
xlim=c(-3, 3),
ylim=c(-3, 3),
type='n',
xlab="",
ylab="",
cex.lab=1.5
)
# title(main = label, adj=0, line = 0.5, font.main=1, cex.main = 1.5)
abline(h=0,lty='dashed',lwd=1.5)
abline(v=0,lty='dashed',lwd=1.5)
mtext(expression("Vital rate,"~italic(nu[i])), side = 1, line = 3,cex = 1.1)
}
layout.matrix <- matrix(c(1,2,3,4),
nrow = 1, ncol = 4, byrow = T)
layout(mat = layout.matrix, heights=c(1,1,1), widths = c(1,1))
# biologically constrained feasibility domain ------------------------------------------------------
alpha<- diag(2)
alpha[1,2]<- 0.25
alpha[2,1]<- 0.25
inv_alpha <- solve(alpha)
rconstraints <- list(
lower = c(-Inf, -Inf),
upper = c(Inf, Inf)
)
gN_max <- c(10, 10)
npts <- 5000
# sample feasible equilibria
N_feas <- sapply(gN_max, function(x,npts) runif(npts,0,x), npts=npts)
# determine the growth rates the correspond to these
r_feas <- N_feas %*% t(alpha)
# mathematical feasibility
make_figure_area()
mtext("Mathematical", side = 3, line=2)
mtext("feasibility domain", side = 3, line=0.7)
# mtext("Feasibility domain", side = 3, line=1.4)
mtext(" - ", side = 3, line=1.2, cex=2)
# add in the feasible growth rates
points(
r_feas[,1],
r_feas[,2],
pch=20,
col=scales::alpha("mediumseagreen", alpha=1)
)
# abundance constraints
make_figure_area()
mtext("Abundance constraints", side = 3, line=1.4)
mtext(" -", side = 3, line=1.2, cex=2)
# polygon(
# c(-4,-4,-1,-1,4,4,-4),
# c(-4,4,4,-1,-1,-4,-4),
# border = NA,
# lty=0,
# col = scales::alpha("#1f00c6", alpha = 1)
# )
# impose new abundance constraints
gN_max <- c(1,2)
outside_abundance_constraints <- apply(
N_feas,
1,
function(x,gN_max){any(x>gN_max)},
gN_max=gN_max
)
# plot points outside the abundance constraints
points(
r_feas[outside_abundance_constraints,1],
r_feas[outside_abundance_constraints,2],
pch=20,
col=scales::alpha("#c60044", alpha=1)
)
# model-based constraints
rconstraints <- list(
lower = c(-1, -1),
upper = c(Inf, Inf)
)
make_figure_area()
mtext("Model-based constraints", side = 3, line=1.4)
r_null <- cbind(runif(npts,-5,5),runif(npts,-5,5))
r_bad <- r_null[apply(r_null, 1, function(x) x[1] <= -1 || x[2] <= -1),]
# plot points outside the vital rate constraints
points(
r_bad[,1],
r_bad[,2],
pch=20,
col=scales::alpha("#1f00c6", alpha=1)
)
# bcfd in the house!
make_figure_area()
mtext("Biologically constrained", side = 3, line=2)
mtext("feasibility domain", side = 3, line=0.7)
mtext("= ", side = 3, line=1.2, cex=2)
# sample feasible equilibria
r_feas <- integrate_area(
alpha=alpha,
rconstraints=rconstraints,
gN_max=gN_max,
npts
)$coords
bcfd_hull <- grDevices::chull(r_feas)
bcfd_hull <- c(bcfd_hull, bcfd_hull[1])
polygon(
r_feas[bcfd_hull,],
border = 'black',
lty=0,
col = scales::alpha("mediumseagreen", alpha = 1)
)
# bcfd <- apply(feasible_pts, 1, within_N_boundaries, inv_alpha=inv_alpha, gN_max=gN_max)
# bcfd_pts <- feasible_pts[bcfd,]
# bcfd_hull <- grDevices::chull(bcfd_pts)
# bcfd_hull <- c(bcfd_hull, bcfd_hull[1])
# polygon(
# bcfd_pts[bcfd_hull,],
# border = NA,
# lty=0,
# col = scales::alpha("mediumseagreen", alpha = 1)
# )
# mtext(expression("Vital rate,"~italic(r[i])), side = 1, outer = TRUE, line = -2,cex = 1.1)
mtext(expression("Vital rate,"~italic(nu[j])), side = 2, outer = TRUE, line = -2., cex = 1.1)
dev.off()