其他分享
首页 > 其他分享> > 线性规划与整数规划—R实现

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

作者:互联网

R包Lpsolve概述

Usage
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)

Arguments
direction
Character string giving direction of optimization: "min" (default) 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.

transpose.constraints
By default each constraint occupies a row of const.mat, and that matrix needs to be transposed before being passed to the optimizing code. For very large constraint matrices it may be wiser to construct the constraints in a matrix column-by-column. In that case set transpose.constraints to FALSE.

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.

presolve
Numeric: presolve? Default 0 (no); any non-zero value means "yes." Currently ignored.

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.

all.int
Logical: should all variables be integer? Default: FALSE.

all.bin
Logical: should all variables be binary? Default: FALSE.

scale
Integer: value for lpSolve scaling. Details can be found in the lpSolve documentation. Set to 0 for no scaling. Default: 196

dense.const
Three column dense constraint array. This is ignored if const.mat is supplied. Otherwise the columns are constraint number, column number, and value; there should be one row for each non-zero entry in the constraint matrix.

num.bin.solns
Integer: if all.bin=TRUE, the user can request up to num.bin.solns optimal solutions to be returned.

use.rw
Logical: if TRUE and num.bin.solns > 1, write the lp out to a file and read it back in for each solution after the first. This is just to defeat a bug somewhere. Although the default is FALSE, we recommend you set this to TRUE if you need num.bin.solns > 1, until the bug is found.

线性规划

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

上述问题的目标函数是:

max(销售额)=max(25 x 1 + 20 x 2)

其中,

x 1是产品A的单位产生的

x 2是产品B的单位产生的

x 1和x 2也称为决策变量

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

20x 1 + 12 x 2 <= 1800(资源约束)
4x 1 + 4x 2 <= 8 * 60(时间约束)

设生产A,B,C分别为\(x_1\),\(x_2\),\(x_3\)个单位,数学模型为(LP):
\begin{array}{l} \max z = 12{x_1} + 18{x_2} + 5{x_3}\\ s.t.\left\{ {\begin{array}{*{20}{c}} {6{x_1} + 9{x_2} + 5{x_3} \le 200}\\ {12{x_1} + 16{x_2} + 17{x_3} \le 360}\\ {25{x_1} + 20{x_2} + 12{x_3} \le 780}\\ {{x_1} \ge 0,{x_2} \ge 0,{x_3} \ge 0} \end{array}} \right. \end{array}

Set up problem: maximize

x1 + 9 x2 + x3 subject to

x1 + 2 x2 + 3 x3 <= 9

3 x1 + 2 x2 + 2 x3 <= 15

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)

Now run.

lp ("max", f.obj, f.con, f.dir, f.rhs)

Not run: Success: the objective function is 40.5

lp ("max", f.obj, f.con, f.dir, f.rhs)$solution

Not run: [1] 0.0 4.5 0.0

The same problem using the dense constraint approach:

f.con.d <- matrix (c(rep (1:2,each=3), rep (1:3, 2), t(f.con)), ncol=3)
lp ("max", f.obj, , f.dir, f.rhs, dense.const=f.con.d)

Not run: Success: the objective function is 40.5

整数规划

lpSolve

原理同Excel的solve规划求解

案例: 使用lpSolve规划求解最大值

约束1: [公式]

约束2: [公式]

约束3: [公式]

求解 [公式] 的最大值

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

求解结果如下

> lp('max', f.obj, f.con,f.dir,f.rhs)
Success: the objective function is 41.6 
> lp('max', f.obj, f.con,f.dir,f.rhs)$solution
[1] 1.6 4.8

参考文献

()[]

标签:bin,const,constraint,线性规划,max,整数,FALSE,规划,dir
来源: https://www.cnblogs.com/haohai9309/p/16218804.html