scipy 1.8.0 Pypi GitHub Homepage
Other Docs
NotesParametersReturnsBackRef
linprog(c, A_ub=None, b_ub=None, A_eq=None, b_eq=None, bounds=None, method='interior-point', callback=None, options=None, x0=None)

Linear programming solves problems of the following form:

$$\min_x \ & c^T x \\ \mbox{such that} \ & A_{ub} x \leq b_{ub},\\ & A_{eq} x = b_{eq},\\ & l \leq x \leq u ,$$

where $x$ is a vector of decision variables; $c$ , $b_{ub}$ , $b_{eq}$ , $l$ , and $u$ are vectors; and $A_{ub}$ and $A_{eq}$ are matrices.

Alternatively, that's:

minimize:

c @ x

such that:

A_ub @ x <= b_ub
A_eq @ x == b_eq
lb <= x <= ub

Note that by default lb = 0 and ub = None unless specified with bounds .

Notes

This section describes the available solvers that can be selected by the 'method' parameter.

:None:None:`'highs-ds'` and :None:None:`'highs-ipm'` are interfaces to the HiGHS simplex and interior-point method solvers , respectively. :None:None:`'highs'` chooses between the two automatically. These are the fastest linear programming solvers in SciPy, especially for large, sparse problems; which of these two is faster is problem-dependent. :None:None:`'interior-point'` is the default as it was the fastest and most robust method before the recent addition of the HiGHS solvers. :None:None:`'revised simplex'` is more accurate than interior-point for the problems it solves. :None:None:`'simplex'` is the legacy method and is included for backwards compatibility and educational purposes.

Method highs-ds is a wrapper of the C++ high performance dual revised simplex implementation (HSOL) , . Method highs-ipm is a wrapper of a C++ implementation of an i\ nterior-\ p\ oint m\ ethod ; it features a crossover routine, so it is as accurate as a simplex solver. Method highs chooses between the two automatically. For new code involving linprog , we recommend explicitly choosing one of these three method values.

versionadded

Method interior-point uses the primal-dual path following algorithm as outlined in . This algorithm supports sparse constraint matrices and is typically faster than the simplex methods, especially for large, sparse problems. Note, however, that the solution returned may be slightly less accurate than those of the simplex methods and will not, in general, correspond with a vertex of the polytope defined by the constraints.

versionadded

Method revised simplex uses the revised simplex method as described in , except that a factorization of the basis matrix, rather than its inverse, is efficiently maintained and used to solve the linear systems at each iteration of the algorithm.

versionadded

Method simplex uses a traditional, full-tableau implementation of Dantzig's simplex algorithm , (not the Nelder-Mead simplex). This algorithm is included for backwards compatibility and educational purposes.

versionadded

Before applying interior-point, revised simplex, or simplex, a presolve procedure based on attempts to identify trivial infeasibilities, trivial unboundedness, and potential problem simplifications. Specifically, it checks for:

If presolve reveals that the problem is unbounded (e.g. an unconstrained and unbounded variable has negative cost) or infeasible (e.g., a row of zeros in A_eq corresponds with a nonzero in b_eq ), the solver terminates with the appropriate status code. Note that presolve terminates as soon as any sign of unboundedness is detected; consequently, a problem may be reported as unbounded when in reality the problem is infeasible (but infeasibility has not been detected yet). Therefore, if it is important to know whether the problem is actually infeasible, solve the problem again with option presolve=False .

If neither infeasibility nor unboundedness are detected in a single pass of the presolve, bounds are tightened where possible and fixed variables are removed from the problem. Then, linearly dependent rows of the A_eq matrix are removed, (unless they represent an infeasibility) to avoid numerical difficulties in the primary solve routine. Note that rows that are nearly linearly dependent (within a prescribed tolerance) may also be removed, which can change the optimal solution in rare cases. If this is a concern, eliminate redundancy from your problem formulation and run with option rr=False or presolve=False .

Several potential improvements can be made here: additional presolve checks outlined in should be implemented, the presolve routine should be run multiple times (until no further simplifications can be made), and more of the efficiency improvements from should be implemented in the redundancy removal routines.

After presolve, the problem is transformed to standard form by converting the (tightened) simple bounds to upper bound constraints, introducing non-negative slack variables for inequality constraints, and expressing unbounded variables as the difference between two non-negative variables. Optionally, the problem is automatically scaled via equilibration . The selected algorithm solves the standard form problem, and a postprocessing routine converts the result to a solution to the original problem.

Parameters

c : 1-D array

The coefficients of the linear objective function to be minimized.

A_ub : 2-D array, optional

The inequality constraint matrix. Each row of A_ub specifies the coefficients of a linear inequality constraint on x .

b_ub : 1-D array, optional

The inequality constraint vector. Each element represents an upper bound on the corresponding value of A_ub @ x .

A_eq : 2-D array, optional

The equality constraint matrix. Each row of A_eq specifies the coefficients of a linear equality constraint on x .

b_eq : 1-D array, optional

The equality constraint vector. Each element of A_eq @ x must equal the corresponding element of b_eq .

bounds : sequence, optional

A sequence of (min, max) pairs for each element in x , defining the minimum and maximum values of that decision variable. Use None to indicate that there is no bound. By default, bounds are (0, None) (all decision variables are non-negative). If a single tuple (min, max) is provided, then min and max will serve as bounds for all decision variables.

method : str, optional

The algorithm used to solve the standard form problem. 'highs-ds' <optimize.linprog-highs-ds> , 'highs-ipm' <optimize.linprog-highs-ipm> , 'highs' <optimize.linprog-highs> , 'interior-point' <optimize.linprog-interior-point> (default), 'revised simplex' <optimize.linprog-revised_simplex> , and 'simplex' <optimize.linprog-simplex> (legacy) are supported.

callback : callable, optional

If a callback function is provided, it will be called at least once per iteration of the algorithm. The callback function must accept a single scipy.optimize.OptimizeResult consisting of the following fields:

x

x

fun

fun

success

success

slack

slack

con

con

phase

phase

status

status

Callback functions are not currently supported by the HiGHS methods.

options : dict, optional

A dictionary of solver options. All methods accept the following options:

maxiter

maxiter

disp

disp

presolve

presolve

All methods except the HiGHS solvers also accept:

tol

tol

autoscale

autoscale

rr

rr

rr_method

rr_method

For method-specific options, see show_options('linprog') <show_options> .

x0 : 1-D array, optional

Guess values of the decision variables, which will be refined by the optimization algorithm. This argument is currently used only by the 'revised simplex' method, and can only be used if :None:None:`x0` represents a basic feasible solution.

Returns

res : OptimizeResult

A scipy.optimize.OptimizeResult consisting of the fields:

Linear programming: minimize a linear objective function subject to linear equality and inequality constraints.

See Also

show_options

Additional options accepted by the solvers.

Examples

Consider the following problem:

$$$$

\min_{x_0, x_1} \ -x_0 + 4x_1 & \\ \mbox{such that} \ -3x_0 + x_1 & \leq 6,\\ -x_0 - 2x_1 & \geq -4,\\ x_1 & \geq -3.

The problem is not presented in the form accepted by linprog . This is easily remedied by converting the "greater than" inequality constraint to a "less than" inequality constraint by multiplying both sides by a factor of $-1$ . Note also that the last constraint is really the simple bound $-3 \leq x_1 \leq \infty$ . Finally, since there are no bounds on $x_0$ , we must explicitly specify the bounds $-\infty \leq x_0 \leq \infty$ , as the default is for variables to be non-negative. After collecting coeffecients into arrays and tuples, the input for this problem is:

>>> c = [-1, 4]
... A = [[-3, 1], [1, 2]]
... b = [6, 4]
... x0_bounds = (None, None)
... x1_bounds = (-3, None)
... from scipy.optimize import linprog
... res = linprog(c, A_ub=A, b_ub=b, bounds=[x0_bounds, x1_bounds])

Note that the default method for linprog is 'interior-point', which is approximate by nature.

>>> print(res)
     con: array([], dtype=float64)
     fun: -21.99999984082494 # may vary
 message: 'Optimization terminated successfully.'
     nit: 6 # may vary
   slack: array([3.89999997e+01, 8.46872439e-08] # may vary
  status: 0
 success: True
       x: array([ 9.99999989, -2.99999999]) # may vary

If you need greater accuracy, try 'revised simplex'.

>>> res = linprog(c, A_ub=A, b_ub=b, bounds=[x0_bounds, x1_bounds], method='revised simplex')
... print(res) con: array([], dtype=float64) fun: -22.0 # may vary message: 'Optimization terminated successfully.' nit: 1 # may vary slack: array([39., 0.]) # may vary status: 0 success: True x: array([10., -3.]) # may vary

You can use the options parameter, e.g., to restrict the maximum number of iterations.

>>> res = linprog(c, A_ub=A, b_ub=b, bounds=[x0_bounds, x1_bounds],
...  options={'maxiter': 4})
... print(res) con: array([], dtype=float64) fun: -21.35207150630407 # may vary message: 'The iteration limit was reached before the algorithm converged.' nit: 4 slack: array([37.19406046, 0.5727398 ]) status: 1 success: False x: array([ 9.4021973 , -2.98746855])
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

scipy.optimize._linprog.linprog scipy.optimize scipy.optimize._optimize.show_options scipy.spatial._qhull.HalfspaceIntersection

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /scipy/optimize/_linprog.py#169
type: <class 'function'>
Commit: