r中的二进制选择变量的整数优化

发布于 2025-02-10 01:47:03 字数 564 浏览 6 评论 0原文

我正在处理一个优化问题,其中我的选择变量(是否构建)是二进制变量。所附的照片显示了最佳的构建选择(在Excel中使用求解器解决)。

我试图在R中复制此问题。将预期价值(EV)的总和限制为≤30,并将选择作为选择变量。

问题在于,EV随构建决策的给定组合而变化。它的一般公式是:

ev =火的概率(p)*损坏(发生火灾时) +(1-p)*损坏(无火) +成本(k)。

没有火灾时损坏为0。当有火灾并且地块上没有塔时,地块上没有塔楼时,地块上没有塔楼时,高损坏。

so ev = p*损坏(火状态) +成本

。 (大小或其他属性无关紧要)。 H:高伤害。仅当没有建立决定时,高伤害。 L:损坏低。如果我们选择在给定的地块中建造塔楼,那么低伤害。 P:火的概率。 K:建筑观察塔的成本。 给定的b配置文件:b是二进制变量。 b = 1表示我们选择在给定图中构建。 EV(B = 1):如果我们选择构建,则期望值。 EV(B = 0):如果我们不构建,则期望值。 EV:最终期望值。如果我们构建,EV = EV(B = 1)ERSE EV = EV(B = 0)。预算约束:如果我们建造,它等于建筑物的成本,否则为0。

I am working on an optimization problem in which my choice variable (to build or not) is a binary variable. The photo attached shows the optimal build choices (solved using Solver in Excel).

I am trying to replicate this problem in R. Minimizing the sum of expected value (EV) with the budget constraint ≤ 30 and build choices as the choice variable.

The problem is that EV changes with the given combination of build decisions. The general formula for it is:

EV = probability of fire (p)*Damage(when there is a fire) + (1-p)*Damage (no fire) + Cost (K).

Damage is 0 when there is no fire. High damage when there is a fire and the plot does not have a tower and low damage when the plot has a tower built on it.

So EV = p*Damage(fire state) + Cost

Excel sheet of problem in hand.

Plot i: Ten plots of land (size or other attributes do not matter).
H: High damage. High damage only if there is no build decision.
L: Low damage. Low damage if we chose to build a tower in a given plot.
p: Probability of fire.
K: Cost of building watch tower.
Given B profile: B is a binary variable. B=1 signifies we chose to built in the given plot. EV(B=1): Expected value if we chose to build. EV(B=0): Expected value if we do not build. EV: Final expected value. If we build, EV = EV(B=1) else EV = EV(B=0). Budget constraint: If we build, it is equal to the cost of building, else it is 0.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喜爱皱眉﹌ 2025-02-17 01:47:03

您可以通过对每行有两个变量来表达您的问题作为二进制线性。那些变量对以下方式连接:

  • 它们的成本为EV1,EV0
  • 的预算为k,0
  • 的总和正好是1

data

data <- data.frame(
  H = c(50, 55, 58, 98, 60, 78, 88, 69, 78, 63),
  L = c(20, 11, 5, 12, 15, 22, 32, 15, 8, 17),
  p = c(0.1, 0.2, 0.05, 0.07, 0.5, 0.15, 0.09, 0.02, 0.01, 0.15),
  K = c(6, 8, 6, 5, 4, 8, 9, 7, 4, 2)
)

data$EV1 <- data$L * data$p + data$K
data$EV0 <- data$H * data$p

library(lpSolve)

n <- nrow(data)

total_budget <- 30

res <- lp(
  direction = "min",
  objective = c(data$EV1, data$EV0),
  const.mat = rbind(c(data$K, rep(0, n)), cbind(diag(n), diag(n))),
  const.dir = c("<=", rep("=", n)),
  const.rhs = c(total_budget, rep(1, n)),
  all.bin = TRUE
)

res$solution[seq_len(n)]
[1] 0 1 0 1 1 1 0 0 0 1
res$objval
[1] 61.37
sum(data$K[res$solution[seq_len(n)] == 1])
[1] 27

其中total_budget 是您预算的上限。

You can express your problem as binary linear one by having two variables for each row. Those pair of varibles are connected in following way:

  • their costs are EV1 and EV0
  • their budgets are K and 0
  • their sum is exactly 1

data

data <- data.frame(
  H = c(50, 55, 58, 98, 60, 78, 88, 69, 78, 63),
  L = c(20, 11, 5, 12, 15, 22, 32, 15, 8, 17),
  p = c(0.1, 0.2, 0.05, 0.07, 0.5, 0.15, 0.09, 0.02, 0.01, 0.15),
  K = c(6, 8, 6, 5, 4, 8, 9, 7, 4, 2)
)

data$EV1 <- data$L * data$p + data$K
data$EV0 <- data$H * data$p

solution

library(lpSolve)

n <- nrow(data)

total_budget <- 30

res <- lp(
  direction = "min",
  objective = c(data$EV1, data$EV0),
  const.mat = rbind(c(data$K, rep(0, n)), cbind(diag(n), diag(n))),
  const.dir = c("<=", rep("=", n)),
  const.rhs = c(total_budget, rep(1, n)),
  all.bin = TRUE
)

res$solution[seq_len(n)]
[1] 0 1 0 1 1 1 0 0 0 1
res$objval
[1] 61.37
sum(data$K[res$solution[seq_len(n)] == 1])
[1] 27

where total_budget is upper limit on your budget.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文