# R语言-条件约束最优化_整数规划、线性规划求解（Rglpk）

### Rglpk包简介

Rglpk包中主要的函数有：
Rglpk_solve_LP((obj, mat, dir, rhs, bounds = NULL, types = NULL, max = FALSE,control = list(), ...))

obj 规划目标系数
mat 约束向量矩阵
dir 约束方向向量，有’>’、’<’、’=’构成
rhs 约束值
bounds 上下限的约束，默认0到INF
type 限定目标变量的类型,’B’指的是0-1规划,’C’代表连续,’I’代表整数，默认是’C’
control 包含四个参数verbose、presolve、tm_limit、canonicalize_status。

### Example

#### Example1

min(z=2x1+4x2+3x3)3x1+4x2+2x3<=602x1+x2+2x3<=40x1+3x2+2x3<=80

obj <- c(2, 4, 3)
mat <- matrix(c(3, 2, 1, 4, 1, 3, 2, 2, 2), nrow = 3)
#       [,1] [,2] [,3]
# [1,]    3    4    2
# [2,]    2    1    2
# [3,]    1    3    2
dir <- c("<=", "<=", "<=")
rhs <- c(60, 40, 80)
max <- TRUE

Rglpk_solve_LP(obj, mat, dir, rhs, max = max)
# $optimum # [1] 76.66667 #$solution
# [1]  0.000000  6.666667 16.666667
# $status # [1] 0 #$solution_dual
# [1] -1.833333  0.000000  0.000000
# $auxiliary #$auxiliary$primal # [1] 60.00000 40.00000 53.33333 #$auxiliary$dual # [1] 0.8333333 0.6666667 0.0000000 #### Example2 min(z=3x1+1x2+3x3)1x1+2x2+x3<=44x23x3<=2x13x2+2x3<=3 其中x1、x3是非负整数数，x2为非负实数 obj <- c(3, 1, 3) mat <- matrix(c(-1, 0, 1, 2, 4, -3, 1, -3, 2), nrow = 3) # [,1] [,2] [,3] # [1,] -1 2 1 # [2,] 0 4 -3 # [3,] 1 -3 2 dir <- c("<=", "<=", "<=") rhs <- c(4, 2, 3) types <- c("I", "C", "I") max <- TRUE Rglpk_solve_LP(obj, mat, dir, rhs, types = types, max = max) #$optimum
# [1] 26.75
# \$solution
# [1] 5.00 2.75 3.00