线性规划与整数规划—R实现

2023-05-12,,

线性规划的R语言实现

R语言在针对各类优化模型时都能快速方便的求解,对运输问题、生产计划问题、产销问题和旅行商问题等都有专门的R包来解决。线性规划整数规划的区别主要在于对决策变量的取值约束有所不同。线性规划的决策变量为正实数,而整数规划则要求决策变量为正整数。在R语言中,有众多相关的R包可以解决这两类问题,例如stat包中的optim、optimize函数,这里给大家推荐lpsolve包,lpsolve包用法简单,平移性好,核心函数调用方便,对解决大型的线性规划和整数规划问题十分好用。

1. R包lpSolve概述

lpSolve包专为求解线性规划问题的R包,与以前大家学习过的LINGO与MATLAB求解相仿。其核心函数为lp函数,其用法如下:

lp (direction = "min", objective.in, const.mat, const.dir, const.rhs,transpose.constraints = TRUE, int.vec, presolve=0, compute.sens=0, binary.vec, all.int=FALSE, all.bin=FALSE, scale = 196, dense.const, num.bin.solns=1, use.rw=FALSE)

参数Argument 释义 中文解释
direction Character string giving direction of optimization: "min" (default) or "max." 目标函数 "min" (缺省) or "max."
objective.in Numeric vector of coefficients of objective function 目标函数系数
const.mat Matrix of numeric constraint coefficients, one row per constraint, one column per variable (unless transpose.constraints = FALSE; see below) 系数矩阵
const.dir Vector of character strings giving the direction of the constraint: each value should be one of "<," "<=," "=," "==," ">," or ">=". (In each pair the two values are identical.) 约束方向"<=" "=" ">="
const.rhs Vector of numeric values for the right-hand sides of the constraints 常数项
int.vec Numeric vector giving the indices of variables that are required to be integer. The length of this vector will therefore be the number of integer variables. 整数变量设置
compute.sens Numeric: compute sensitivity? Default 0 (no); any non-zero value means "yes." 灵敏度分析设置
binary.vec Numeric vector like int.vec giving the indices of variables that are required to be binary 0-1变量设置
all.int Logical: should all variables be integer? Default: FALSE 可设全部变量都取整数
all.bin Logical: should all variables be binary? Default: FALSE 可设全部变量都是0-1变量

2. 线性规划的R计算

2.1 线性规划示例

例1:一家公司希望最大化两种产品A和B的利润,分别以25美元和20美元的价格出售。每天有1800个资源单位,产品A需要20个单位,而B需要12个单位。这两种产品都需要15分钟的生产时间,并且可用的总工作时间为每天8小时。每种产品的生产数量应该是什么才能使利润最大化。

产品A 产品B 资源
资源1 20 12 1800
时间2 15 15 4800
利润 25 20

解:设生产两种产品的数量为\(x_1\),\(x_2\)

上述问题的目标函数是:

\(max(销售额)=max\) (25 \(x_1\) + 20 \(x_2\))

问题中的约束(资源和时间):

20\(x_1\) + 12 \(x_2\) <= 1800 (资源约束)

15\(x_1\) + 15 \(x_2\)<=4800 (时间约束)

数学模型(LP)为:

\begin{array}{l} \max z = 25{x_1} + 20{x_2} \\ s.t.\left\{ {\begin{array}{*{20}{c}} {20{x_1} + 12{x_2} \le 1800}\\ {15{x_1} + 15{x_2} \le 4800}\\\ {{x_1} \ge 0,{x_2} \ge 0} \end{array}} \right. \end{array}

例2:线性规划

#Set up problem: maximize
x1 + 9 x2 + x3
#subject to
x1 + 2 x2 + 3 x3 <= 9
3 x1 + 2 x2 + 2 x3 <= 15
x1>=0, x2>=0

2.2线性规划R求解

例1求解
library(lpSolve)
f.obj <- c(25, 20)
f.con <- matrix (c(20,12, 15,15), nrow=2, byrow=TRUE)
f.dir <- c("<=", "<=")
f.rhs <- c(1800, 4800)
lp ("max", f.obj, f.con, f.dir, f.rhs)
lp1<-lp ("max", f.obj, f.con, f.dir, f.rhs)
lp1$solution
lp1$objval
lp ("max", f.obj, f.con, f.dir, f.rhs)
Success: the objective function is 3000
lp1$solution
[1] 0 150 #最优解
lp1$objval
[1] 3000 #最优值
例2求解
library(lpSolve)
f.obj <- c(1, 9, 1)
f.con <- matrix (c(1, 2, 3, 3, 2, 2), nrow=2, byrow=TRUE)
f.dir <- c("<=", "<=")
f.rhs <- c(9, 15)
lp ("max", f.obj, f.con, f.dir, f.rhs)
lp2<-lp ("max", f.obj, f.con, f.dir, f.rhs)
lp2$solution
lp2$objval
lp ("max", f.obj, f.con, f.dir, f.rhs)
Success: the objective function is 40.5
lp2$solution
[1] 0.0 4.5 0.0 #最优解
lp2$objval
[1] 40.5 #最优值

3. 整数规划R计算

例3:使用lpSolve求解整数规划最大值

\begin{array}{l} \max z = 5{x_1} + 7{x_2} \\ s.t.\left\{ {\begin{array}{*{20}{c}} {{x_1} + 2{x_2} \le 16}\\ {2{x_1} + 3{x_2} \le 9}\\{{x_1} + {x_2} \le 8}\ \\{{x_1} \ge 0,{x_2} \ge 0} 且都是整数\end{array}} \right. \end{array}

library(lpSolve)
f.obj <- c(5,7)
f.con <- matrix(c(1,2,2,3,1,1), nrow=3,byrow=TRUE)
f.dir <- c('<=', '<=', '<=')
f.rhs <- c(16,9,8)
lp('max', f.obj, f.con,f.dir,f.rhs,all.int=TRUE)

求解结果如下

lp('max', f.obj, f.con,f.dir,f.rhs,all.int=TRUE)
Success: the objective function is 22
lp3<-lp('max', f.obj, f.con,f.dir,f.rhs,all.int=TRUE)
lp3$solution
[1] 3 1 #最优解都是整数
lp3$objval
[1] 22 #最优值

不加整数约束的解

lp('max', f.obj, f.con,f.dir,f.rhs)
Success: the objective function is 22.5
lp3<-lp('max', f.obj, f.con,f.dir,f.rhs)
lp3$solution
[1] 4.5 0.0
lp3$objval
[1] 22.5

参考文献

(【R语言在最优化中的应用】lpSolve包解决 指派问题和指派问题)[https://cloud.tencent.com/developer/article/1411788]

线性规划与整数规划—R实现的相关教程结束。

《线性规划与整数规划—R实现.doc》

下载本文的Word格式文档,以方便收藏与打印。