Linear programming problems where a multivariate linear function should be maximized or minimized subject to linear equality or inequality constraints, as well as mixed integer problems, can be solved with the function lpsolve. It takes (at most) four arguments, in the following order :
The return value is in the form [optimum,soln] where optimum is the minimum/maximum value of the objective function and soln is a list of coordinates corresponding to the point at which the optimal value is attained. If there is no feasible solution, an empty list is returned. When the objective function is unbounded, optimum is returned as +infinity (for maximization problems) or -infinity (for minimization problems), while soln is generally meaningless.
If obj is given as constant (for example, zero) then only a feasible point is returned as a list, if one exists. If the problem is infeasible, an empty list is returned. This may be used as a test to check whether a set of linear constraints is feasible or not.
The given objective function is minimized by default. To maximize it, include the option lp_maximize=true or simply lp_maximize.
By default, all variables are considered continuous and unrestricted in sign.
The solver combines the two-phase simplex method and the dual simplex method to find the optimal solution.
For example, to solve the problem specified in (1), input :
Output :
Therefore, the minimum value of f(x,y,z)=2 x+y−z+4 is equal to −4 under the given constraints. The optimal value is attained at point (x,y,z)=(0,5,13) .
Constraints may also take the form expr=a..b for bounded linear expressions.
Input :
Output :
Use the assume=lp_nonnegative option to specify that all variables are nonnegative. That is easier than entering the nonnegativity constraints explicitly.
Input:
Output:
Bounds can be added separately for some variables. They are entered after the list of constraints.
Input :
Output :
Use the assume=integer or assume=lp_integer option to solve integer programming problems. The function lpsolve uses branch and bound method in such cases. The total number of investigated nodes is printed out before the function returns. To limit branch and bound tree depth, use the option :
In that case optimality of the solution can’t be guaranteed.
Input :
Output :
Use the option assume=lp_binary to specify that all variables are binary, i.e. the only allowed values are 0 and 1. Binary variables usually represent true and false values, giving them a certain meaning in logical context.
Input :
Output :
Options
and
are used for specifying mixed integer/binary programming problems. Also, the
option may be used, which overrides integer and binary settings. For example, a linear programming problem with mostly integer variables may be specified using the option assume=integer and specifying continuous variables with lp_variables, which overrides the global integer setting.
Input :
Output:
Use the assume=lp_nonnegint or assume=nonnegint option to get nonnegative integer values.
Input :
Output :
The function lpsolve supports entering linear programming problems in matrix form, which is convenient for problems with relatively large number of variables and/or constraints.
To enter a problem in matrix form, the parameter obj must be a vector of coefficients c and constr, which is mandatory this case, must be a list [A,b,Aeq,beq] , where A,Aeq are matrices and b,beq are vectors of real numbers. Without any other parameters, this minimizes cT x under conditions A x≤b and Aeq x=beq . If a problem does not contain equality constraints, parameters Aeq and beq may be omitted. For a problem that does not contain inequality constraints, empty lists must be passed as A and b . Also, constr may be an empty list.
The parameter bd is entered as a list of two vectors bl and bu of the same length as the vector c such that bl≤x≤bu . For unbounded variables use +infinity or -infinity.
When specifying mixed problems in matrix form, variables are entered as the corresponding indexes, which are 1-based, i.e. the first variable has index 1, the second variable has index 2 and so on. Other options for lpsolve are passed to a problem in matrix form in the same way as if it was in symbolic form.
Input :
Output :
Input :
Output :
Input :
Output :
Input :
Output :