这个问题可以转化为一个有 25 个未知数,5x2 个等式约束,25x2 个不等式约束的欠定线性方程组问题,写好方程以后,使用 limSolve 给出抽样解即可…… 😂
row_sum_idx <- function(x) {
y <- rep(0, 5)
y[x] <- 1
rep(y, 5)
}
col_sum_idx <- function(x) {
y <- rep(0, 25)
y[(((x - 1) * 5 + 1):(x * 5))] <- 1
y
}
E <- matrix(
c(unlist(lapply(1:5, row_sum_idx)), unlist(lapply(1:5, col_sum_idx))),
ncol = 25, byrow = TRUE
)
F <- c(rep(10, 5), c(2, 1, 3, 2.5, 1.5) * 5)
range_idx <- function(x) {
y <- rep(0, 25)
y[x] <- 1
y
}
G <- matrix(
c(unlist(lapply(1:25, range_idx)), unlist(lapply(1:25, range_idx))),
ncol = 25, byrow = TRUE
)
H <- c(rep(0, 25), rep(-10, 25))
library("limSolve")
z <- xsample(E = E, F = F, G = G, H = H)
ex <- matrix(z$X[42, ], ncol = 5)
rowSums(ex)
# [1] 10 10 10 10 10
colSums(ex) / 5
# [1] 2.0 1.0 3.0 2.5 1.5
range(ex)
# [1] 0.1768611 6.2030256