Collects the constraints of a linear program in long form and hands them to
lpSolve::lp() in triplet form, which keeps memory linear in the number of non-zero
coefficients. Constraints are tagged with the block they belong to, so a program can be
assembled once and solved over a subset of its blocks.
Details
Two tables describe a program.
variables has one row per decision variable: an id_var giving its column in the
program, a block naming the group it belongs to, and whatever further key columns the
caller needs to identify it. Nothing about the meaning of a variable is known here; the
subclass supplies it.
constraints has one row per non-zero coefficient: block, id_row, id_var,
coefficient, plus the dir and rhs of the row it belongs to, which are constant
within an id_row. Coefficients are summed over duplicated (id_row, id_var) pairs and
exact zeroes are dropped, since lpSolve::lp() matches constraint rows to dir/rhs by
the order of the row indices it is given, and cannot represent an empty row.
All variables are non-negative: lpSolve::lp() has no notion of variable bounds, so a
quantity that may take either sign has to be split into two variables.
lpSolve is a suggested dependency, since most of the package does not solve linear
programs. It has to be installed before a program can be constructed.
Active bindings
variablesThe decision variables, one row each.
constraintsThe constraint coefficients, one row per non-zero entry.
block_summaryRows and coefficients per constraint block.
n_varNumber of decision variables.
n_rowNumber of constraint rows.
statusThe
lpSolve::lp()status of the last solve; 0 is success.objectiveThe objective value of the last solve.
valuesThe solved value of every variable, joined to its keys.
Methods
lp_problem$new()
Initialize a program over a fixed set of decision variables. Fails if
the suggested lpSolve package is not installed, since nothing could be solved.
Usage
lp_problem$new(variables)lp_problem$add_constraints()
Add one block of constraint rows.
lp_problem$solve()
Solve the program and store the solution.