Learn R Programming

lpSolveAPI (version 5.5.0.12-3)

add.constraint: Add Constraint

Description

Add a constraint to an lpSolve linear program model object.

Usage

add.constraint(lprec, xt, type = c("<=", "=", ">="), rhs, indices = 1:n, lhs)

Arguments

lprec
an lpSolve linear program model object.
xt
a numeric vector containing the constraint coefficients (only the nonzero coefficients if indices is also given). The length of xt must be equal to the number of decision variables in lprec unless indices
type
a numeric or character value from the set {1 = "<=", 2=">=" 3="=" ,="" }<="" code=""> specifying the type of the constraint.
rhs
a single numeric value specifying the right-hand-side of the constraint.
indices
a numeric vector the same length as xt of unique values from the set {1, ..., n} where n is the number of decision variables in lprec; xt[j] becomes the constraint coefficient for variable indices[j]. Th
lhs
optional. A single numeric value specifying the left-hand-side of the constraint.

Value

  • a logical value is invisibly returned: TRUE indicates that the operation was successful and FALSE indicates that an error occurred.

Details

Specifying the objective function before adding constraints will improve the performance of this function.

The use of this function should be avoided when possible. Building a model column-by-column rather than row-by-row will be on the order of 50 times faster (building the model - not solving the model).

References

http://lpsolve.sourceforge.net/5.5/index.htm

Examples

Run this code
lps.model <- make.lp(0, 4)
set.objfn(lps.model, rep(1, 4))

xt <- c(6,2,4,9)
add.constraint(lps.model, xt, "<=", 50)

yt <- c(3,1,5)
ind <- c(1,2,4)
add.constraint(lps.model, yt, 2, 75, ind)

Run the code above in your browser using DataLab