linp: Linear Programming.

Description Usage Arguments Value Note Author(s) References See Also Examples

View source: R/linp.R

Description

Solves a linear programming problem,

min(sum(Cost_i x_i))

subject to

Ex=f

Gx>=h

x_i>=0

(optional)

This function provides a wrapper around lp (see note) from package lpSolve, written to be consistent with the functions lsei, and ldei.

It allows for the x's to be negative (not standard in lp).

Usage

1
2
linp(E = NULL, F = NULL, G = NULL, H = NULL, Cost,
     ispos = TRUE, int.vec = NULL, verbose = TRUE, ...)

Arguments

E

numeric matrix containing the coefficients of the equality constraints Ex=F; if the columns of E have a names attribute, they will be used to label the output.

F

numeric vector containing the right-hand side of the equality constraints.

G

numeric matrix containing the coefficients of the inequality constraints Gx>=H; if the columns of G have a names attribute, and the columns of E do not, they will be used to label the output.

H

numeric vector containing the right-hand side of the inequality constraints.

Cost

numeric vector containing the coefficients of the cost function; if Cost has a names attribute, and neither the columns of E nor G have a name, they will be used to label the output.

ispos

logical, when TRUE then the unknowns (x) must be positive (this is consistent with the original definition of a linear programming problem).

int.vec

when not NULL, a numeric vector giving the indices of variables that are required to be an integer. The length of this vector will therefore be the number of integer variables.

verbose

logical to print error messages.

...

extra arguments passed to R-function lp.

Value

a list containing:

X

vector containing the solution of the linear programming problem.

residualNorm

scalar, the sum of absolute values of residuals of equalities and violated inequalities. Should be very small or zero for a feasible linear programming problem.

solutionNorm

scalar, the value of the minimised Cost function, i.e. the value of ∑ {Cost_i.x_i}.

IsError

logical, TRUE if an error occurred.

type

the string "linp", such that how the solution was obtained can be traced.

Note

If the requirement of nonnegativity are relaxed, then strictly speaking the problem is not a linear programming problem.

The function lp may fail and terminate R for very small problems that are repeated frequently...

Also note that sometimes multiple solutions exist for the same problem.

Author(s)

Karline Soetaert <[email protected]>

References

Michel Berkelaar and others (2007). lpSolve: Interface to Lpsolve v. 5.5 to solve linear or integer programs. R package version 5.5.8.

See Also

ldei, lsei,

lp the original function from package lpSolve

Blending, a linear programming problem.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#-------------------------------------------------------------------------------
# Linear programming problem 1, not feasible
#-------------------------------------------------------------------------------

# maximise x1 + 3*x2 
# subject to
#-x1 -x2    < -3
#-x1 + x2   <-1
# x1 + 2*x2 < 2
# xi > 0

G    <- matrix(nrow = 3, data = c(-1, -1, 1, -1, 1, 2))
H    <- c(3, -1, 2)
Cost <- c(-1, -3)
(L <- linp(E = NULL, F = NULL, Cost = Cost, G = G, H = H))
L$residualNorm

#-------------------------------------------------------------------------------
# Linear programming problem 2, feasible
#-------------------------------------------------------------------------------

# minimise x1 + 8*x2 + 9*x3 + 2*x4 + 7*x5 + 3*x6  
# subject to:
#-x1            + x4 + x5      = 0
#    - x2       - x4      + x6 = 0
# x1 + x2 + x3                 > 1
#           x3       + x5 + x6 < 1
# xi > 0

E <- matrix(nrow = 2, byrow = TRUE, data = c(-1, 0, 0, 1, 1, 0,
                                              0,-1, 0, -1, 0, 1))
F <- c(0, 0)
G <- matrix(nrow = 2, byrow = TRUE, data = c(1, 1, 1, 0, 0, 0,
                                             0, 0, -1, 0, -1, -1))
H    <- c(1, -1)
Cost <- c(1, 8, 9, 2, 7, 3)
(L <- linp(E = E, F = F, Cost = Cost, G = G, H = H))
L$residualNorm

#-------------------------------------------------------------------------------
# Linear programming problem 3, no positivity
#-------------------------------------------------------------------------------
# minimise x1 + 2x2 -x3 +4 x4
# subject to:
# 3x1 + 2x2 + x3 + x4 = 2
#  x1 +  x2 + x3 + x4 = 2

# 2x1 +  x2 + x3 + x4 >=-1
# -x1 + 3x2 +2x3 + x4 >= 2
# -x1       + x3      >= 1

E <- matrix(ncol = 4, byrow = TRUE,
            data =c(3, 2, 1, 4, 1, 1, 1, 1))
F <- c(2, 2)

G <- matrix(ncol = 4, byrow = TRUE,
            data = c(2, 1, 1, 1, -1, 3, 2, 1, -1, 0, 1, 0))
H <- c(-1, 2, 1)
Cost <- c(1, 2, -1, 4)

linp(E = E, F = F, G = G, H = H, Cost, ispos = FALSE)

Example output

[1] "problem infeasible"
$X
[1] 0 0

$residualNorm
[1] 5

$solutionNorm
[1] 0

$IsError
[1] TRUE

$type
[1] "linp"

[1] 5
$X
[1] 1 0 0 1 0 1

$residualNorm
[1] 2.220446e-16

$solutionNorm
[1] 6

$IsError
[1] FALSE

$type
[1] "linp"

[1] 2.220446e-16
$X
[1] -3.00 -6.75  7.50  4.25

$residualNorm
[1] 7.105427e-15

$solutionNorm
[1] -7

$IsError
[1] FALSE

$type
[1] "linp"

limSolve documentation built on Aug. 14, 2017, 3:01 p.m.