[英]Optimization function applied to table of values in R
`values <- matrix(c(0.174,0.349,1.075,3.1424,0.173,0.346,1.038,3.114,0.171,0.343,1.03,3.09,0.17,0.34,1.02,3.06),ncol=4) `
I am attempting to maximize the total value for the dataset taking only one value from each row, and with associated costs for each column我试图最大化数据集的总价值,每行只取一个值,并且每列都有相关成本
subject to:受:
One value column used per row.每行使用一个值列。
cost of each use of column 1 is 4每次使用第 1 列的成本是 4
cost of each use of column 2 is 3每次使用第 2 列的成本是 3
cost of each use of column 3 is 2每次使用第 3 列的成本是 2
cost of each use of column 4 is 1每次使用第 4 列的成本是 1
total cost <= 11总成本 <= 11
These are stand in values for a larger dataset.这些代表更大数据集的值。 I need to be able to apply it directly to all the rows of a dataset.我需要能够将它直接应用于数据集的所有行。
I have been trying to use the lpSolve package, with no success.我一直在尝试使用 lpSolve package,但没有成功。
`f.obj <- values
f.con <- c(4,3,2,1)
f.dir <- "<="
f.rhs <- 11
lp("max", f.obj, f.con, f.dir, f.rhs)`
I am getting a solution of "0"我得到“0”的解决方案
I do not know how to model this in a way that chooses one value per row and then uses a different value in calculating the constraints.我不知道 model 如何以每行选择一个值然后在计算约束时使用不同的值的方式进行此操作。
Looks like the problem is as follows:看起来问题如下:
We have a matrix a[i,j] with values, and a vector c[j] with costs.我们有一个带有值的矩阵 a[i,j] 和一个带有成本的向量 c[j] 。
We want to select one value for each row such that:我们希望 select 为每一行一个值,这样:
a. A。 total cost <= 11总成本 <= 11
b. b. total value is maximized总价值最大化
To develop a mathematical model, we introduce binary variables x[i,j] ∈ {0,1}
.为了开发数学 model,我们引入了二进制变量x[i,j] ∈ {0,1}
。 With this, we can write:有了这个,我们可以写:
max sum((i,j), a[i,j]*x[i,j]) subject to sum((i,j), c[j]*x[i,j]) <= 11 sum(j, x[i,j]) = 1 ∀ix[i,j] ∈ {0,1}
Implement in R. I use here CVXR.在R实现。我这里用的是CVXR。
# # data # A: values # C: cost # A <- matrix(c(0.174,0.349,1.075,3.1424,0.173,0.346,1.038,3.114,0.171,0.343,1.03,3.09,0.17,0.34,1.02,3.06),ncol=4) C <- c(4,3,2,1) maxcost <- 11 # # form a matrix cmat[i,j] indicating the cost of element i,j # cmat <- matrix(C,nrow=dim(A)[1],ncol=dim(A)[2],byrow=T) # # problem: # pick one value from each row # such that total value of selected cells is maximized # and cost of selected cells is limited to maxcost # # model: # min sum((i,j), a[i,j]*x[i,j]) # subject to # sum((i,j), c[j]*x[i,j]) <= maxcost # sum(j,x[i,j]) = 1 ∀i # x[i,j] ∈ {0,1} # # library(CVXR) x = Variable(dim(A), name="x", boolean=T) p <- Problem(Maximize(sum_entries(A*x)), constraints=list( sum_entries(cmat*x) <= maxcost, sum_entries(x,axis=1) == 1 )) res <- solve(p,verbose=T) res$status res$value res$getValue(x)*A
The output looks like: output 看起来像:
> res$status
[1] "optimal"
> res$value
[1] 4.7304
> res$getValue(x)*A
[,1] [,2] [,3] [,4]
[1,] 0.0000 0 0.000 0.17
[2,] 0.0000 0 0.343 0.00
[3,] 1.0750 0 0.000 0.00
[4,] 3.1424 0 0.000 0.00
The description in the original post is not very precise.原帖中的描述不是很准确。 For instance, I assumed that we need to select precisely one cell from each row.例如,我假设我们需要 select 正好是每一行中的一个单元格。 If we just want "select at most one cell from each row", then replace如果我们只想“从每一行中选择最多一个单元格”,那么替换
sum(j, x[i,j]) = 1 ∀i
by经过
sum(j, x[i,j]) <= 1 ∀i
As mentioned by Steve, the lpSolve
package expects a single objective function not a matrix.正如史蒂夫所提到的, lpSolve
package 需要一个目标 function 而不是矩阵。 You could reformulate as maximize(sum(RowSums(values*xij)) given constraint
您可以重新表述为maximize(sum(RowSums(values*xij)) given constraint
Eg, change the matrix to a vector, and change the problem to a integer optimization problem例如,将矩阵变为向量,将问题变为integer优化问题
obj <- as.vector(values)
f.con <- rep(f.con, each = 4)
r <- lp('max', obj, matrix(f.con, nrow = 1), f.dir, f.rhs, int.vec = seq_along(obj))
#' Success: the objective function is 9.899925
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.