简单线性规划
简单线性规划
library(Rglpk)
obj<-c(2,4,3)
mat<-matrix(c(3,2,1,4,1,3,2,1,2),nrow=3)
dir<-c("<=","<=","<=")
rhs<-c(60,40,80)
Rglpk_solve_LP(obj,mat,dir,rhs,max=T)
Rglpk_solve_LP(obj,mat,dir,rhs,max=T)
$optimum #为目标函数的最大值
[1] 90
$solution #表示决策变量的最优解
[1] 0 0 30
$status # 表示最优解已经找到,非0时失败
[1] 0
$solution_dual
[1] -2.5 -2.0 0.0
$auxiliary
$auxiliary$primal
[1] 60 30 60
$auxiliary$dual
[1] 1.5 0.0 0.0
obj<-c(3,1,3)
mat<-matrix(c(-1,0,1,2,4,-3,1,-3,2),nrow=3)
dir<-rep("<=",3)
rhs<-c(4,2,3)
types<-c("I","C","I")
Rglpk_solve_LP(obj,mat,dir,rhs,types=types,max=TRUE)
$optimum
[1] 26.75
$solution
[1] 5.00 2.75 3.00
$status
[1] 0
$solution_dual
[1] NA
$auxiliary
$auxiliary$primal
[1] 3.50 2.00 2.75
$auxiliary$dual
[1] NA
- 2.2 专题:lpSolve包和运输问题
install.packages("lpSolve")
library(lpSolve)
costs <- matrix(c(4,2,8,12,10,5,4,3,11,11,9,6),nrow=3) #运费矩阵
row.signs <- rep("=", 3) #各家造纸厂的产量恰好可以售完,故都取等号
row.rhs <- c(16,10,22) #销量约束值
col.signs <- rep("=", 4) #各家客户需求量恰好可以满足,故都取等号
col.rhs <- c(8,14,12,14) #需求约束值
res <- lp.transport(costs,"min",row.signs,row.rhs,col.signs,col.rhs)
res #输出最小运费
res$solution
Success: the objective function is 244
res$solution
[,1] [,2] [,3] [,4]
[1,] 4 0 12 0
[2,] 4 0 0 6
[3,] 0 14 0 8
costs <- matrix (c(3,11,6,12,2,7,3,5,1,4,9,5),nrow=3); ##运费矩阵
row.signs <- rep ("<=", 3) #总产量大于总销量,故各家销量小于等于产量
row.rhs <- c(8,5,9) #销量约束值
col.signs <- rep ("=", 4) #各家客户需求量都可以满足,故都取等号
col.rhs <- c(4,3,5,6) #需求约束值
res <- lp.transport(costs,"min",row.signs,row.rhs,col.signs,col.rhs)
res #输出最小运费
res$solution
Success: the objective function is 49
res$solution
[,1] [,2] [,3] [,4]
[1,] 4 0 0 4
[2,] 0 3 0 0
[3,] 0 0 5 2
2.3 专题: lpSolve包和指派问题(成本计算)
library(lpSolve)
x=matrix(c(4,7,6,6,6,8,9,9,7,9,7,17,12,14,12,15,14,
8,6,10,12,10,7,10,6),ncol=5)
x
lp.assign(x)
lp.assign(x)$solution
> x
[,1] [,2] [,3] [,4] [,5]
[1,] 4 8 7 15 12
[2,] 7 9 17 14 10
[3,] 6 9 12 8 7
[4,] 6 7 14 6 10
[5,] 6 9 12 10 6
> lp.assign(x)
Success: the objective function is 34
> lp.assign(x)$solution
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 1 0 0
[2,] 0 1 0 0 0
[3,] 1 0 0 0 0
[4,] 0 0 0 1 0
[5,] 0 0 0 0 1
library(goalprog)
coefficients=matrix(c(5,1,4,6,10,-2,4,8),4)
targets=c(60,0,36,48)
achievements=data.frame(objective=1:4,priority=c(1,2,3,4),
p=c(1,0,1,0),n=c(0,1,0,1))
soln=llgp(coefficients, targets, achievements)
soln$converged
soln$out
coefficients=matrix(c(50,1,0,2,1,0,30,0,1,1,3,0),6)
coefficients
targets<-c(4600,50,80,120,150,20)
achievements<-data.frame(objective=1:6,priority=c(1,2,3,4,4,5),
p=c(1,0,0,4,1,1),n=c(0,1,1,0,0,0))
achievements
soln<-llgp(coefficients,targets,achievements)
soln
soln$out
```
专注数据分析
欢迎转载并注明出处
```