我有以下简单的线性编程模型,我用R中的linprog
包解决了这个问题:
install.packages("linprog")
library(linprog)
function_opt <- c(8, 13, 9, 8, 9, 11, 12, 10, 7, 8, 10, 9)
names(function_opt) <- c("F1A1","F1A2","F1A3","F1A4","F2A1","F2A2","F2A3","F2A4","F3A1","F3A2","F3A3","F3A4")
##Order: 3 factory capacities, 4 customer demands
cons_indep_term <- c(60, 70, 80, 75, 45, 40, 50)
names(cons_indep_term) <- c("F1","F2","F3","A1","A2","A3","A4")
r1 <- c(1,1,1,1,0,0,0,0,0,0,0,0)
r2 <- c(0,0,0,0,1,1,1,1,0,0,0,0)
r3 <- c(0,0,0,0,0,0,0,0,1,1,1,1)
r4 <- c(1,0,0,0,1,0,0,0,1,0,0,0)
r5 <- c(0,1,0,0,0,1,0,0,0,1,0,0)
r6 <- c(0,0,1,0,0,0,1,0,0,0,1,0)
r7 <- c(0,0,0,1,0,0,0,1,0,0,0,1)
cons_coef <- rbind(r1,r2,r3,r4,r5,r6,r7)
res <- solveLP(function_opt, cons_indep_term, cons_coef, maximum=FALSE, const.dir = c("<=","<=","<=",">=",">=",">=",">="))
print (res)
第六个约束条件要求FxA3变量之和必须至少为40.但是,解决方案如下:
Results of Linear Programming / Linear Optimization
Objective function (Minimum): 1355
Iterations in phase 1: 6
Iterations in phase 2: 3
Solution
opt
F1A1 10
F1A2 0
F1A3 0
F1A4 50
F2A1 30
F2A2 0
F2A3 0
F2A4 0
F3A1 35
F3A2 45
F3A3 0
F3A4 0
Basic Variables
opt
F1A1 10
F1A4 50
F2A1 30
F3A1 35
F3A2 45
S F2 40
S A3 40
Constraints
actual dir bvec free dual dual.reg
F1 60 <= 60 0 1 10
F2 30 <= 70 40 0 40
F3 80 <= 80 0 2 35
A1 75 >= 75 0 9 40
A2 45 >= 45 0 10 35
A3 80 >= 40 40 0 40
A4 50 >= 50 0 9 10
All Variables (including slack variables)
opt cvec min.c max.c marg marg.reg
F1A1 10 8 -9 9 NA NA
F1A2 0 13 99 77 4 10
F1A3 0 9 99 77 10 10
F1A4 50 8 -17 9 NA NA
F2A1 30 9 -10 10 NA NA
F2A2 0 11 99 77 1 30
F2A3 0 12 99 77 12 40
F2A4 0 10 99 77 1 30
F3A1 35 7 -8 9 NA NA
F3A2 45 8 -18 9 NA NA
F3A3 0 10 99 77 12 35
F3A4 0 9 99 77 2 35
S F1 0 0 -1 Inf 1 10
S F2 40 0 NA 1 0 NA
S F3 0 0 -2 Inf 2 35
S A1 0 0 -9 Inf 9 40
S A2 0 0 -10 Inf 10 35
S A3 40 0 NA 10 0 NA
S A4 0 0 -9 Inf 9 10
所有三个FxA3变量都设置为0,这意味着违反了第六个约束。问题是什么?我已经三次检查了一切,但仍然不知道。
答案 0 :(得分:0)
这是......非常奇怪,我无法在您的代码中发现任何问题。由于这是一个如此简单的LP,你可能会考虑向软件包维护者提交一个bug。
话虽如此,您应该能够通过使用lpSolve
包来解锁,该包具有几乎相同的界面(实际上,您所询问的linprog
包使用了该包) :
library(lpSolve)
mod2 = lp(direction = "min",
objective.in = function_opt,
const.mat = cons_coef,
const.dir = c("<=","<=","<=",">=",">=",">=",">="),
const.rhs = cons_indep_term)
setNames(mod2$solution, names(function_opt))
# F1A1 F1A2 F1A3 F1A4 F2A1 F2A2 F2A3 F2A4 F3A1 F3A2 F3A3 F3A4
# 0 0 40 20 40 0 0 30 35 45 0 0
mod2$objval
# [1] 1785
在最佳解决方案中,我们有F1A3 = 40
,F2A3 = 0
和F3A3 = 0
,因此符合第六个约束条件。