应用 lpSolveAPI

问题描述 投票:0回答:1

我有许多变量表示我可以更换的设备。更换后,它们会改善影响指标,

[I]
。每个还具有相关的年度成本节省
[S]
和更换成本
[C]

n <- 1000 # variable count

# impact
# negative for use in minimization function
I <- -rnorm(n, mean=20000, sd=8000)
# cost savings
s <- rnorm(n, mean=2500, sd=1000)
# replacement cost
c <- rnorm(n, mean=15000, sd=5000)

我想选择要替换的组件,以在预算范围内最大限度地发挥全面影响,同时仍然确保整个项目(作为一个整体)满足简单的投资回收目标。

payback_goal <- 3
budget <- 1000000

这个问题由下面的方程描述。

我正在努力使用

lpSolveAPI
进行设置。具体来说,我不知道如何合并方程式。 3.

library(lpSolveAPI)
m <- 2 # number of constraints, disregarding binary constraint set by type
my.lp <- make.lp(m, n)
set.row(my.lp, 1, c)
# i don't think this is the correct way to set up the calculation
set.row(my.lp, 2, c/s) # cost divided by savings

# create obj fn coefficients
set.objfn(my.lp, I)
set.type(my.lp, 1:n, "binary")

eq <- c("<=", "<=")
set.constr.type(my.lp, eq)
set.rhs(my.lp, c(budget, payback_goal))

solve(my.lp)
soln1 <- get.variables(my.lp)

# total cost
s1_cost <- sum(soln1 * c)
# total impact
s1_impact <- -get.objective(my.lp)
# total simple payback
s1_pb <- sum(soln1*c) / sum(soln1*s) 

我尝试了一种有点hacky的解决方法,假设总成本将相当接近预算,这使得第三个方程

但这只是一个近似值,我想知道如何在 R 代码中更准确地实现它。

r mathematical-optimization linear-programming lpsolve
1个回答
3
投票

您可以使用您的符号将 (3) 线性化如下:

 sum([X]*[C]) - payback*sum([X]*[S]) <= 0

可以重写为;

 sum([X]*([C]-payback*[S])) <= 0

只要

sum([X]*[S])>0
就有效。

© www.soinside.com 2019 - 2024. All rights reserved.