簡體   English   中英

LpSolve R 條件約束

[英]LpSolve R conditional constraint

我正在嘗試回答以下 ILP,其目標是最大限度地增加手術患者的類型,而最多只能手術 2 種不同類型。

max 310x1 + 400x2 + 500x3 + 500x4 + 650x5 + 800x6 + 850x7
subject to
1.8x1 + 2.8x2 + 3.0x3 + 3.6x4 + 3.8x5 + 4.6x6 + 5.0x7 <= 25
250x1 + 300x2 + 500x3 + 400x4 + 550x5 + 800x6 + 750x7 >= 4000 
xj <= dj
d1 + d2 + d3 + d4 + d5 + d6 + d7 <= 2
xj >= 0 and integer

寫在 R package lpSolve 我有以下代碼:

# Set coefficients of the objective function
f.obj <- c(310, 400, 500, 500, 650, 800, 850, 0, 0, 0, 0, 0, 0, 0)

# Set matrix corresponding to coefficients of constraints by rows
f.con <- matrix(c(1.8, 2.8, 3, 3.6, 3.8, 4.6, 5, 0, 0, 0, 0, 0, 0, 0,
                  250, 300, 500, 400, 550, 800, 750, 0, 0, 0, 0, 0, 0, 0,
                  0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1,
                  1, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0,
                  0, 1, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0,
                  0, 0, 1, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0,
                  0, 0, 0, 1, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0,
                  0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, -1, 0, 0,
                  0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, -1, 0,
                  0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, -1), nrow = 10, byrow = TRUE)

# Set unequality/equality signs
f.dir <- c("<=",
           "<=",
           "<=",
           "<=",
           "<=",
           "<=",
           "<=",
           "<=",
           "<=",
           "<=")

# Set right hand side coefficients
f.rhs <- c(25, 4000, 2,0, 0, 0, 0, 0, 0,0)

# Final value (z)
lp("max", f.obj, f.con, f.dir, f.rhs, int.vec = 1:7, binary.vec = 8:14)

# Variables final values
lp("max", f.obj, f.con, f.dir, f.rhs, int.vec = 1:7, binary.vec = 8:14)$solution

但是,現在 x 不會 go 高於 1,因為 d 是二進制的。

有誰知道我如何正確編寫這些約束?

您有 7 種不同的患者類型,x1 到 x7,x 是整數。 您最多可以將 select 2 x 設為非零。 您可以通過為每個 x 添加二進制變量 b1 到 b7 並為每個 x 添加兩個約束來做到這一點。

x >= -U + U*b
x <= U*b

其中 U 是最大 x 值的某個上限。

library(lpSolve)

# Set coefficients of the objective function
f.obj <- c(310, 400, 500, 500, 650, 800, 850, 0, 0, 0, 0, 0, 0, 0, 0)

U=999

# Set matrix corresponding to coefficients of constraints by rows
f.con <- matrix(c(1.8, 2.8, 3, 3.6, 3.8, 4.6, 5, 0, 0, 0, 0, 0, 0, 0, 0,
                  250, 300, 500, 400, 550, 800, 750, 0, 0, 0, 0, 0, 0, 0, 0,
                  0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0,
                  1, 0, 0, 0, 0, 0, 0, -U, 0, 0, 0, 0, 0, 0, U,
                  1, 0, 0, 0, 0, 0, 0, -U, 0, 0, 0, 0, 0, 0, 0,
                  0, 1, 0, 0, 0, 0, 0, 0, -U, 0, 0, 0, 0, 0, U,
                  0, 1, 0, 0, 0, 0, 0, 0, -U, 0, 0, 0, 0, 0, 0,
                  0, 0, 1, 0, 0, 0, 0, 0, 0, -U, 0, 0, 0, 0, U,
                  0, 0, 1, 0, 0, 0, 0, 0, 0, -U, 0, 0, 0, 0, 0,
                  0, 0, 0, 1, 0, 0, 0, 0, 0, 0, -U, 0, 0, 0, U,
                  0, 0, 0, 1, 0, 0, 0, 0, 0, 0, -U, 0, 0, 0, 0,
                  0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, -U, 0, 0, U,
                  0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, -U, 0, 0, 0,
                  0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, -U, 0, U,
                  0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, -U, 0, 0,
                  0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, -U, U,
                  0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, -U, 0), nrow = 17, byrow = TRUE)

# Set unequality/equality signs
f.dir <- c("<=","<=","<=",rep(c(">=","<="),7))

# Set right hand side coefficients
f.rhs <- c(25, 4000, 2, rep(0,14))

# Final value (z)
res=lp("max", f.obj, f.con, f.dir, f.rhs, int.vec = 1:7, binary.vec = 8:14)

結果

> res$objval
[1] 4260

> res$solution
 [1] 11.000000  0.000000  0.000000  0.000000  0.000000  0.000000  1.000000  1.000000  0.000000  0.000000
[11]  0.000000  0.000000  0.000000  1.000000  0.998999

因此選擇了第一和第七種患者類型,x1 中的 11 種,x7 中的 1 種。 我們可以檢查約束

> sum(c(1.8, 2.8, 3, 3.6, 3.8, 4.6, 5)*c(11,0,0,0,0,0,1))
[1] 24.8
> sum(c(250, 300, 500, 400, 550, 800, 750)*c(11,0,0,0,0,0,1))
[1] 3500

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM