如何构建一个矩阵乘以的简单优化模型?

时间:2012-05-29 20:15:40

标签: excel r optimization model

我使用标准的Solver在Excel中创建了一个优化模型,现在想在R中创建一个类似的模型,因为这样我就可以制作更大的模型。不幸的是,我有点难以找到可以为我的概念建模的好例子。因此,我想问你是否有人能够给我一些关于如何在R中制作类似模型的提示。

我已将Excel表格上传到http://dl.dropbox.com/u/9641130/R/Positioning%20Optimization%20R.xlsx

基本思想是通过将范围E10:L19中的最多8个单元改变为1来使单元B3最大化。 B3细胞包括范围E10:L19的sumproduct()和许多相似的范围。

我期待看到有关如何在R中构建类似模型的一些提示。

谢谢! Jochem

======

按照Chase的建议更新

我想用一些可重复代码的R代码澄清我的问题。这与您在上面的Excel代码中找到的模型大致相同。

初始矩阵集:

A <- as.matrix(structure(list(X0 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), X0.1 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), X0.2 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), X0.3 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), X0.4 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), X0.5 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), X0.6 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), X0.7 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("X0", "X0.1", "X0.2", "X0.3", "X0.4", "X0.5", "X0.6", "X0.7"), class = "data.frame", row.names = c(NA, -9L)))
B <- as.matrix(structure(list(X1 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), X1.1 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), X1.2 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), X1.3 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), X.100000 = c(-100000L, -100000L, -100000L, -100000L, 1L, 1L, 1L, 1L, 1L), X.100000.1 = c(-100000L, -100000L, -100000L, -100000L, 1L, 1L, 1L, 1L, 1L), X.100000.2 = c(-100000L, -100000L, -100000L, -100000L, 1L, 1L, 1L, 1L, 1L), X.100000.3 = c(-100000L, -100000L, -100000L, -100000L, 1L, 1L, 1L, 1L, 1L)), .Names = c("X1", "X1.1", "X1.2", "X1.3", "X.100000", "X.100000.1", "X.100000.2", "X.100000.3"), class = "data.frame", row.names = c(NA, -9L)))
C <- as.matrix(structure(list(X1 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), X1.1 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), X1.2 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), X1.3 = c(1L, 1L, 1L, 1L, 1L, -100000L, 1L, 1L, 1L), X1.4 = c(1L, 1L, 1L, 1L, 1L, -100000L, 1L, 1L, 1L), X1.5 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), X1.6 = c(1L, 1L, 1L, 1L, 1L, -100000L, 1L, 1L, -100000L), X1.7 = c(1L, 1L, 1L, 1L, -100000L, -100000L, 1L, 1L, -100000L)), .Names = c("X1", "X1.1", "X1.2", "X1.3", "X1.4", "X1.5", "X1.6", "X1.7"), class = "data.frame", row.names = c(NA, -9L)))
D <- as.matrix(structure(list(X775 = c(385L, 1233L, 1067L, 5L, 730L, 1123L, 837L, 5L, 3087L), X704 = c(625L, 1338L, 804L, 110L, 659L, 1363L, 942L, -165L, 3350L), X704.1 = c(625L, 1338L, 804L, 110L, 659L, 1363L, 942L, -165L, 3350L), X944 = c(625L, 1263L, 898L, 35L, 899L, 1363L, 867L, -65L, 3110L), X775.1 = c(385L, 1233L, 1067L, 5L, 730L, 1123L, 837L, 5L, 3087L), X775.2 = c(385L, 1233L, 1067L, 5L, 730L, 1123L, 837L, 5L, 3087L), X944.1 = c(625L, 1263L, 898L, 35L, 899L, 1363L, 867L, -65L, 3110L), X944.2 = c(625L, 1263L, 898L, 35L, 899L, 1363L, 867L, -65L, 3110L)), .Names = c("X775", "X704", "X704.1", "X944", "X775.1", "X775.2", "X944.1", "X944.2"), class = "data.frame", row.names = c(NA, -9L)))

函数sum(A * B * C * D)的结果当前为0.这是合乎逻辑的,因为在矩阵A中,所有单元格的值都为0.但是,我想知道什么公式我可以最大化函数和的值(A * B * C * D)。

sum(A*B*C*D)
[1] 0

我想通过将Matrix A中的值从0更改为1来实现此目的。此外,应考虑以下约束。 1.每行只能包含一个值为1的单元格。 2.每列只能包含一个值为1的单元格;这意味着我们最多可以在矩阵A中放置值1的八倍。

有人对如何做到这一点有建议吗?

1 个答案:

答案 0 :(得分:1)

您的决策变量(A中的单元格值)是布尔值(0或1),您的目标和约束是这些变量的线性函数,这使您进入一类称为混合整数线性的优化问题程序设计。例如,可以使用Rglpk包解决这些问题。这是我的解决方案:

n1 <- nrow(A)
n2 <- ncol(A)

# objective coefficients
obj <- as.vector(B*C*D) # objective

# matrix of constraints weights
mat <- matrix(0, n1+n2, n1*n2)
for (i in 1:n1) {
   mat[i, ] <- as.numeric(row(A) == i)
}
for (j in 1:n2) {
   mat[n1+j, ] <- as.numeric(col(A) == j)
}

dir   <- rep("<=", n1+n2) # constraint directions
rhs   <- rep(1, n1+n2)    # constraints upper-bounds
types <- rep("B", n1*n2)  # variable types (boolean)

library(Rglpk)
opt <- Rglpk_solve_LP(obj, mat, dir, rhs, types,
                      max = TRUE, verbose = TRUE)
opt

# $optimum
# [1] 9950

# $solution
#  [1] 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
# [39] 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
# [77] 0 1 0 0

# $status
# [1] 0

opt.A <- matrix(opt$solution, n1, n2)
opt.A

#       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
#  [1,]    0    0    0    1    0    0    0    0
#  [2,]    0    0    1    0    0    0    0    0
#  [3,]    0    1    0    0    0    0    0    0
#  [4,]    1    0    0    0    0    0    0    0
#  [5,]    0    0    0    0    0    0    0    0
#  [6,]    0    0    0    0    0    0    1    0
#  [7,]    0    0    0    0    0    1    0    0
#  [8,]    0    0    0    0    0    0    0    1
#  [9,]    0    0    0    0    0    0    0    0
# [10,]    0    0    0    0    1    0    0    0

9950sum(A*B*C*D)的最佳值吗? (我在这台电脑上没有Excel ......)