K ỉ$dZddlmZddlmZddlmZmZmZddl m Z ddl m Z ddl mZddlmZdd lmZmZdd lmZdd lmZdd lmZGd deZGddeZdZdZd dZd!dZ d"dZ!d#dZ"dZ#dZ$dZ%dZ&dZ'dZ(d$dZ)d$dZ*y)%aTools for optimizing a linear function for a given simplex. For the linear objective function ``f`` with linear constraints expressed using `Le`, `Ge` or `Eq` can be found with ``lpmin`` or ``lpmax``. The symbols are **unbounded** unless specifically constrained. As an alternative, the matrices describing the objective and the constraints, and an optional list of bounds can be passed to ``linprog`` which will solve for the minimization of ``C*x`` under constraints ``A*x <= b`` and/or ``Aeq*x = beq``, and individual bounds for variables given as ``(lo, hi)``. The values returned are **nonnegative** unless bounds are provided that indicate otherwise. Errors that might be raised are UnboundedLPError when there is no finite solution for the system or InfeasibleLPError when the constraints represent impossible conditions (i.e. a non-existent simplex). Here is a simple 1-D system: minimize `x` given that ``x >= 1``. >>> from sympy.solvers.simplex import lpmin, linprog >>> from sympy.abc import x The function and a list with the constraint is passed directly to `lpmin`: >>> lpmin(x, [x >= 1]) (1, {x: 1}) For `linprog` the matrix for the objective is `[1]` and the uivariate constraint can be passed as a bound with None acting as infinity: >>> linprog([1], bounds=(1, None)) (1, [1]) Or the matrices, corresponding to ``x >= 1`` expressed as ``-x <= -1`` as required by the routine, can be passed: >>> linprog([1], [-1], [-1]) (1, [1]) If there is no limit for the objective, an error is raised. In this case there is a valid region of interest (simplex) but no limit to how small ``x`` can be: >>> lpmin(x, []) Traceback (most recent call last): ... sympy.solvers.simplex.UnboundedLPError: Objective function can assume arbitrarily large values! An error is raised if there is no possible solution: >>> lpmin(x,[x<=1,x>=2]) Traceback (most recent call last): ... sympy.solvers.simplex.InfeasibleLPError: Inconsistent/False constraint )sympify) factor_terms)LeGeEq)S)Dummy)ordered)sign)Matrixzeros)linear_eq_to_matrixnumbered_symbols) filldedentceZdZdZy)UnboundedLPErrora A linear programming problem is said to be unbounded if its objective function can assume arbitrarily large values. Example ======= Suppose you want to maximize 2x subject to x >= 0 There's no upper limit that 2x can take. N__name__ __module__ __qualname____doc__[/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/solvers/simplex.pyrrMs   rrceZdZdZy)InfeasibleLPErrorak A linear programming problem is considered infeasible if its constraint set is empty. That is, if the set of all vectors satisfying the constraints is empty, then the problem is infeasible. Example ======= Suppose you want to maximize x subject to x >= 10 x <= 9 No x can satisfy those constraints. Nrrrrrr`s " rrc||ddf|dd|f|||f}}}|dk(r td||||z zz }||z ||ddf<| |z |dd|f<d|z |||f<|S)a The pivot element `M[i, j]` is inverted and the rest of the matrix modified and returned as a new matrix; original is left unmodified. Example ======= >>> from sympy.matrices.dense import Matrix >>> from sympy.solvers.simplex import _pivot >>> from sympy import var >>> Matrix(3, 3, var('a:i')) Matrix([ [a, b, c], [d, e, f], [g, h, i]]) >>> _pivot(_, 1, 0) Matrix([ [-a/d, -a*e/d + b, -a*f/d + c], [ 1/d, e/d, f/d], [-g/d, h - e*g/d, i - f*g/d]]) Nrz'Tried to pivot about zero-valued entry.)ZeroDivisionError)MijMiMjMijAs r_pivotr(us,AqD'1QT7AadGCB ax 57 7 B"s(OA3hAadGcCiAadG#gAadG Hrc0t|fdS)Nc(||fz |fSNr)r"r'BY pivot_cols rz#_choose_pivot_row..s!adQq)|_.Dad-Kr)key)min)r'r,candidate_rowsr.r-s`` ``r_choose_pivot_rowr3s ~#K LLrNc|||xsdgfDcgc] }t|c}\}}}|r8t|j |jj| \}}}| ||fS|rrt|g||gg} n|sr tdt||gg} | jdz } | j dz } t d| Dsttdt| D cgc]} d| fc} t| Dcgc]}d|f} }d} | dd d f| dd dd f}t fd tj Drntj D] }|dks n t|jDcgc]}||fdks|}}|sttd tfd |D\}}t|j Dcgc]}|||fdkDs|dkDs|}}|jt|||| }||f|k(rd}n$||f}t| ||} | ||c|<| |<@ | dd d f| dd dd f}| d dd f}t| Dcgc] }||dks |}}|sn~tfd |D\}}t| Dcgc]}|||fdkDs|}}|sttdt|||| }t| ||} | ||c|<| |<dg| z}dg| z}t!D]\}\}} |dk(rd|| <| d |f|| <t!| D]\}\}} |dk(rd|| <| |d f|| <|r)t d||zDsttd| d ||fScc}wcc} wcc}wcc}wcc}wcc}wcc}w)aPReturn ``(o, x, y)`` obtained from the two-phase simplex method using Bland's rule: ``o`` is the minimum value of primal, ``Cx - D``, under constraints ``Ax <= B`` (with ``x >= 0``) and the maximum of the dual, ``y^{T}B - D``, under constraints ``A^{T}*y >= C^{T}`` (with ``y >= 0``). To compute the dual of the system, pass `dual=True` and ``(o, y, x)`` will be returned. Note: the nonnegative constraints for ``x`` and ``y`` supercede any values of ``A`` and ``B`` that are inconsistent with that assumption, so if a constraint of ``x >= -1`` is represented in ``A`` and ``B``, no value will be obtained that is negative; if a constraint of ``x <= -1`` is represented, an error will be raised since no solution is possible. This routine relies on the ability of determining whether an expression is 0 or not. This is guaranteed if the input contains only Float or Rational entries. It will raise a TypeError if a relationship does not evaluate to True or False. Examples ======== >>> from sympy.solvers.simplex import _simplex >>> from sympy import Matrix Consider the simple minimization of ``f = x + y + 1`` under the constraint that ``y + 2*x >= 4``. This is the "standard form" of a minimization. In the nonnegative quadrant, this inequality describes a area above a triangle with vertices at (0, 4), (0, 0) and (2, 0). The minimum of ``f`` occurs at (2, 0). Define A, B, C, D for the standard minimization: >>> A = Matrix([[2, 1]]) >>> B = Matrix([4]) >>> C = Matrix([[1, 1]]) >>> D = Matrix([-1]) Confirm that this is the system of interest: >>> from sympy.abc import x, y >>> X = Matrix([x, y]) >>> (C*X - D)[0] x + y + 1 >>> [i >= j for i, j in zip(A*X, B)] [2*x + y >= 4] Since `_simplex` will do a minimization for constraints given as ``A*x <= B``, the signs of ``A`` and ``B`` must be negated since the currently correspond to a greater-than inequality: >>> _simplex(-A, -B, C, D) (3, [2, 0], [1/2]) The dual of minimizing ``f`` is maximizing ``F = c*y - d`` for ``a*y <= b`` where ``a``, ``b``, ``c``, ``d`` are derived from the transpose of the matrix representation of the standard minimization: >>> tr = lambda a, b, c, d: [i.T for i in (a, c, b, d)] >>> a, b, c, d = tr(A, B, C, D) This time ``a*x <= b`` is the expected inequality for the `_simplex` method, but to maximize ``F``, the sign of ``c`` and ``d`` must be changed (so that minimizing the negative will give the negative of the maximum of ``F``): >>> _simplex(a, b, -c, -d) (-3, [1/2], [2, 0]) The negative of ``F`` and the min of ``f`` are the same. The dual point `[1/2]` is the value of ``y`` that minimized ``F = c*y - d`` under constraints a*x <= b``: >>> y = Matrix(['y']) >>> (c*y - d)[0] 4*y + 1 >>> [i <= j for i, j in zip(a*y,b)] [2*y <= 1, y <= 1] In this 1-dimensional dual system, the more restrictive constraint is the first which limits ``y`` between 0 and 1/2 and the maximum of ``F`` is attained at the nonzero value, hence is ``4*(1/2) + 1 = 3``. In this case the values for ``x`` and ``y`` were the same when the dual representation was solved. This is not always the case (though the value of the function will be the same). >>> l = [[1, 1], [-1, 1], [0, 1], [-1, 0]], [5, 1, 2, -1], [[1, 1]], [-1] >>> A, B, C, D = [Matrix(i) for i in l] >>> _simplex(A, B, -C, -D) (-6, [3, 2], [1, 0, 0, 0]) >>> _simplex(A, B, -C, -D, dual=True) # [5, 0] != [3, 2] (-6, [1, 0, 0, 0], [5, 0]) In both cases the function has the same value: >>> Matrix(C)*Matrix([3, 2]) == Matrix(C)*Matrix([5, 0]) True See Also ======== _lp - poses min/max problem in form compatible with _simplex lpmin - minimization which calls _lp lpmax - maximimzation which calls _lp References ========== .. [1] Thomas S. Ferguson, LINEAR PROGRAMMING: A Concise Introduction web.tecnico.ulisboa.pt/mcasquilho/acad/or/ftp/FergusonUCLA_lp.pdf rzmust give A and Brc3PK|]}|jxs |j ywr+)is_Float is_Rational.0r"s r z_simplex..s 6qqzz*Q]]*6s$&z@ Only rationals and floats are allowed. FTNc3.K|] }|dk\ywrNr)r9r"r,s rr:z_simplex..3s0Qqtqy0sz- The constraint set is empty!c3,K|] }||f ywr+rr9r"Xs rr:z_simplex..C/AaD!9/c3,K|] }||f ywr+rr?s rr:z_simplex..mrArBzX Objective function can assume arbitrarily large values!c3&K|] }|dk\ ywr=rr8s rr:z_simplex..s=1Q=sz Oscillating system led to invalid solution. If you believe there was a valid solution, please report this as a bug.r;r;)r _simplexT ValueErrorcolsrowsall TypeErrorrrangerr1appendr3r(r enumerate)r'r,CDdualr"_odpr!nmr#r-lastk_piv_colscpiv_rowsrargmax argmin_dualvr@s ` @rrFrFsd'(AqxQC%89&)9JAq!Q QSSD!##qssQB/AqsAqyQ QFQF# $ 01 1 QF8   A  A 6A6 6 $  #1X&%&A!!H%q$%A% D  crc2gJ crc3B3hK 0%-0 0 qvv Atax   %QVV}"D !t 1aOqT1Q4 !adc h  crc2gJ crc3B3hK b#2#gJ %Qx4!1Q4!8A44 /h//1 %Qx7!1QT7Q;A77":/-$./ / aHa 3 1aOqT1Q4 !ad) ,VaZF&1*Kq\& 6Aq :F1Ir1uXKN & q\! 6Aq 9KN!R%F1I !  C=(<== ,%!&' ' fI:v{ **A:4 '%,=JH5 8sGN)( N. N3 N8N8&N=6N=?N=> O O9O Ocd}|ddddf|dddf|dddf|ddddff}|s|St|Dcgc] }|| c}Scc}w)aEreturn parts of M as matrices or lists Examples ======== >>> from sympy import Matrix >>> from sympy.solvers.simplex import _abcd >>> m = Matrix(3, 3, range(9)); m Matrix([ [0, 1, 2], [3, 4, 5], [6, 7, 8]]) >>> a, b, c, d = _abcd(m) >>> a Matrix([ [0, 1], [3, 4]]) >>> b Matrix([ [2], [5]]) >>> c Matrix([[6, 7]]) >>> d Matrix([[8]]) The matrices can be returned as compact lists, too: >>> L = a, b, c, d = _abcd(m, list=True); L ([[0, 1], [3, 4]], [2, 5], [[6, 7]], [8]) cx|j}t|ddk(r|Dcgc]}|d c}S|Scc}w)Nrr)tolistlen)r"ls raslistz_abcd..aslists; HHJ qt9>"#$QAaD$ $%s 7Nr;)tuple)r!listrgrWr"s r_abcdrjsqD #2#ss( QssBwZ2ss7QrsBCx[8A  Q'&)' (('sA c|||||xsdgfDcgc] }t|c}\}}}}t||g||ggScc}w)areturn Matrix([[a, b], [c, d]]) from matrices in Matrix or list form. Examples ======== >>> from sympy import Matrix >>> from sympy.solvers.simplex import _abcd, _m >>> m = Matrix(3, 3, range(9)) >>> L = _abcd(m, list=True); L ([[0, 1], [3, 4]], [2, 5], [[6, 7]], [8]) >>> _abcd(m) (Matrix([ [0, 1], [3, 4]]), Matrix([ [2], [5]]), Matrix([[6, 7]]), Matrix([[8]])) >>> assert m == _m(*L) == _m(*_) r)r )abr\rTr"s r_mrnsL('(AqxQC%89&)9JAq!Q Aq6Aq6" ##:s9c~|sdgfdgffSt|ds t|dvr tdt|}|jDcgc]}|dz  c}\}}t |\}}}}|d}d} t t| dt|D cgc]\}} | c} }} t t| d t|D cgc]\}} | c} }j} fd } d }||| z|}|| |z|}|| || z|tf|| | |z|tffScc}wcc} }wcc} }w) a/ return primal and dual function and constraints assuming that ``M = Matrix([[A, b], [c, d]])`` and the function ``c*x - d`` is being minimized with ``Ax >= b`` for nonnegative values of ``x``. The dual and its constraints will be for maximizing `b.T*y - d` subject to ``A.T*y <= c.T``. Examples ======== >>> from sympy.solvers.simplex import _primal_dual, lpmin, lpmax >>> from sympy import Matrix The following matrix represents the primal task of minimizing x + y + 7 for y >= x + 1 and y >= -2*x + 3. The dual task seeks to maximize x + 3*y + 7 with 2*y - x <= 1 and and x + y <= 1: >>> M = Matrix([ ... [-1, 1, 1], ... [ 2, 1, 3], ... [ 1, 1, -7]]) >>> p, d = _primal_dual(M) The minimum of the primal and maximum of the dual are the same (though they occur at different points): >>> lpmin(*p) (28/3, {x1: 2/3, x2: 5/3}) >>> lpmax(*d) (28/3, {y1: 1/3, y2: 2/3}) If the equivalent (but canonical) inequalities are desired, leave `factor=True`, otherwise the unmodified inequalities for M will be returned. >>> m = Matrix([ ... [-3, -2, 4, -2], ... [ 2, 0, 0, -2], ... [ 0, 1, -3, 0]]) >>> _primal_dual(m, False) # last condition is 2*x1 >= -2 ((x2 - 3*x3, [-3*x1 - 2*x2 + 4*x3 >= -2, 2*x1 >= -2]), (-2*y1 - 2*y2, [-3*y1 + 2*y2 <= 0, -2*y1 <= 1, 4*y1 <= -3])) >>> _primal_dual(m) # condition now x1 >= -1 ((x2 - 3*x3, [-3*x1 - 2*x2 + 4*x3 >= -2, x1 >= -1]), (-2*y1 - 2*y2, [-3*y1 + 2*y2 <= 0, -2*y1 <= 1, 4*y1 <= -3])) If you pass the transpose of the matrix, the primal will be identified as the standard minimization problem and the dual as the standard maximization: >>> _primal_dual(m.T) ((-2*x1 - 2*x2, [-3*x1 + 2*x2 >= 0, -2*x1 >= 1, 4*x1 >= -3]), (y2 - 3*y3, [-3*y1 - 2*y2 + 4*y3 <= -2, y1 <= -1])) A matrix must have some size or else None will be returned for the functions: >>> _primal_dual(Matrix([[1, 2]])) ((x1 - 2, []), (-2, [])) >>> _primal_dual(Matrix([])) ((None, []), (None, [])) References ========== .. [1] David Galvin, Relations between Primal and Dual www3.nd.edu/~dgalvin1/30210/30210_F07/presentations/dual_opt.pdf Nshape)z expecting Matrix or 3 or 4 listsrrct|dS)Nr)startr)xs rr/z_primal_dual..2s"1A.rruycNg}fdt||DD]}|dk(r |dk(rdgcSrt|}|jjr|j|jj dzdk(rt |jj dk(sJ|j|jj d}|jt||jj dz|jt|z}|j| |S)Nc36K|]\}}||ywr+r)r9r"r#ops rr:z-_primal_dual..ineq..8s1tq!"Q(1sTFrr) ziprlhsis_Mulrhsargsrefuncr absrN)Lr^ryrvfrYfactors ` rineqz_primal_dual..ineq6s 1s1ay1 ADyew O55<quuzz?a/66/ 1 AtAwA6QHA IIaL  rc|r|d|z S| SNrr)rurTs rr/z_primal_dual..Fs!adQh!r) hasattrrerHrnrprjr r{rMrGrr)r!rr"rWrVr'rmr\rTrZr#ruyTreqFrs ` r _primal_dualrs>^ bzD":%% 1g  q6 ?@ @ F77 #aAE #DAqqJAq!Q !A.Ac!C&%(34da45A s1S65845tq!5 6 8 8B ,B 1q5! A 261 A tAE1b! "QR!VQ(;$< <<5 $55s D. D3 D9 ci}g}g}tddt}i}g}t|}|D]!}|dk(r |dk(ry|jtj tj r tdt|ttfr|j|jz }|j} | |z rtd| |z zt| dkDr|j|| rJ| j!} | |vrt|d d j#} | |vr| || <|| xx| zcc<|s yt%t'd |z|D]} |j)| d}|sy|dk(r|j| 0|j*|j,} } | j.r%t1|}| |z || <|j|y| j.r(| r%t1|}| |z|| <|j|t1|}|j||| z|| <|j|| | z z |D]&} t1|}|| z || <|j|(|||fS) areturn `(np, d, aux)` where `np` is a list of nonpositive expressions that represent the given constraints (possibly rewritten in terms of auxilliary variables) expressible with nonnegative symbols, and `d` is a dictionary mapping a given symbols to an expression with an auxilliary variable. In some cases a symbol will be used as part of the change of variables, e.g. x: x - z1 instead of x: z1 - z2. If any constraint is False/empty, return None. All variables in ``constr`` are assumed to be unbounded unless explicitly indicated otherwise with a univariate constraint, e.g. ``x >= 0`` will restrict ``x`` to nonnegative values. The ``syms`` must be included so all symbols can be given an unbounded assumption if they are not otherwise bound with univariate conditions like ``x <= 3``. Examples ======== >>> from sympy.solvers.simplex import _rel_as_nonpos >>> from sympy.abc import x, y >>> _rel_as_nonpos([x >= y, x >= 0, y >= 0], (x, y)) ([-x + y], {}, []) >>> _rel_as_nonpos([x >= 3, x <= 5], [x]) ([_z1 - 2], {x: _z1 + 3}, [_z1]) >>> _rel_as_nonpos([x <= 5], [x]) ([], {x: 5 - _z1}, [_z1]) >>> _rel_as_nonpos([x >= 1], [x]) ([], {x: _z1 + 1}, [_z1]) zr)rtclsTFNz only finite bounds are permittedz&unexpected symbol(s) in constraint: %sr)evaluatez only equalities like Eq(x, y) or non-strict inequalities like x >= y are allowed in lp, not %s)rr sethasrInfinityNegativeInfinityrH isinstancerrltsgts free_symbolsrerNpopas_setrLrgetinfsup is_infinitenext)constrsymsr^npauxui univariateunboundr"freeiruivlrlrmus r_rel_as_nonposrLsx@ A B C #QE 2BJG t9DL 9  :  55Q// 0?@ @ a"b " ANNEt| < M5zA~ ! IIK<A.557J&$'JqMqMS(MJ(FHI(JKL L9LD# NN1d # 9 NN1  uuaee1 ==RAq5AaD JJqM ]]H1u! 1 RA JJqMq5AaD IIa1q5k "9#> H1u! 1  q#:rct|}|Dcgc] }t|}}tjg|g|zDcgc]}|jc}t}t t |D]]}t ||ts||j||jz dk||<|j||j dk_t||}|ttd|\}}}|j|}|Dcgc]}|j|}}tt!||z} t#|| \} } t#|g| \} } | | | | || |fScc}wcc}wcc}w)aDreturn A, B, C, D, r, x+X, X for maximizing objective = Cx - D with constraints Ax <= B, introducing introducing auxilliary variables, X, as necessary to make replacements of symbols as given in r, {xi: expression with Xj}, so all variables in x+X will take on nonnegative values. Every univariate condition creates a semi-infinite condition, e.g. a single ``x <= 3`` creates the interval ``[-oo, 3]`` while ``x <= 3`` and ``x >= 2`` create an interval ``[2, 3]``. Variables not in a univariate expression will take on nonnegative values. rz* Inconsistent/False constraint)rrunionrrMrerrr|r~rNrrrxreplacerir r) objective constraintsrr"rrrZr^rxxr'r,rPrQs r _lp_matricesrsp  A) *'!* *B * 99 @sRx8!q~~8 @#% @D3r7^' beR qEII1 )Q.BqE IIr!uyyjAo &' r4 Ay ,-!./ /JB3 1 A!# $A!**Q- $B $ gdm s "B r2 &DAq sB 'DAq aAq"c !!1 +8" %sE)E. E3ct||\}}}}}}} t|j} d| vrt||| | \} } } | }n"d| vrt||||\}} } n t dt t || } |rd|jDcic]\}}||j| }}}| j|t| Dcic] }|| vs|| |} }|| fScc}}wcc}w)aXReturn the optimization (min or max) of ``f`` with the given constraints. All variables are unbounded unless constrained. If `min_max` is 'max' then the results corresponding to the maximization of ``f`` will be returned, else the minimization. The constraints can be given as Le, Ge or Eq expressions. Examples ======== >>> from sympy.solvers.simplex import _lp as lp >>> from sympy import Eq >>> from sympy.abc import x, y, z >>> f = x + y - 2*z >>> c = [7*x + 4*y - 7*z <= 3, 3*x - y + 10*z <= 6] >>> c += [i >= 0 for i in (x, y, z)] >>> lp(min, f, c) (-6/5, {x: 0, y: 0, z: 3/5}) By passing max, the maximum value for f under the constraints is returned (if possible): >>> lp(max, f, c) (3/4, {x: 0, y: 3/4, z: 0}) Constraints that are equalities will require that the solution also satisfy them: >>> lp(max, f, c + [Eq(y - 9*x, 1)]) (5/7, {x: 0, y: 1, z: 1/7}) All symbols are reported, even if they are not in the objective function: >>> lp(min, x, [y + x >= 3, x >= 0]) (0, {x: 0, y: 3}) maxr1zexpecting min or max) rstrlowerrFrHdictr{itemsrupdater )min_maxrrr'r,rPrQr^rrhowrSrUrTorYras r_lprs P*!V4Aq!Q2s g,   C |Aq1"qb)Aq C #1aA&1a/00 SQZA*+'') 4$!QQ 1  4 4  %aj 9ASLQ!W 9 9 a4K 5 :sC+ C1C1c$tt||S)areturn minimum of linear equation ``f`` under linear constraints expressed using Ge, Le or Eq. All variables are unbounded unless constrained. Examples ======== >>> from sympy.solvers.simplex import lpmin >>> from sympy import Eq >>> from sympy.abc import x, y >>> lpmin(x, [2*x - 3*y >= -1, Eq(x + 3*y, 2), x <= 2*y]) (1/3, {x: 1/3, y: 5/9}) Negative values for variables are permitted unless explicitly excluding, so minimizing ``x`` for ``x <= 3`` is an unbounded problem while the following has a bounded solution: >>> lpmin(x, [x >= 0, x <= 3]) (0, {x: 0}) Without indicating that ``x`` is nonnegative, there is no minimum for this objective: >>> lpmin(x, [x <= 3]) Traceback (most recent call last): ... sympy.solvers.simplex.UnboundedLPError: Objective function can assume arbitrarily large values! See Also ======== linprog, lpmax )rr1rrs rlpminr.sF sAv rc$tt||S)a#return maximum of linear equation ``f`` under linear constraints expressed using Ge, Le or Eq. All variables are unbounded unless constrained. Examples ======== >>> from sympy.solvers.simplex import lpmax >>> from sympy import Eq >>> from sympy.abc import x, y >>> lpmax(x, [2*x - 3*y >= -1, Eq(x+ 3*y,2), x <= 2*y]) (4/5, {x: 4/5, y: 2/5}) Negative values for variables are permitted unless explicitly excluding: >>> lpmax(x, [x <= -1]) (-1, {x: -1}) If a non-negative constraint is added for x, there is no possible solution: >>> lpmax(x, [x <= -1, x >= 0]) Traceback (most recent call last): ... sympy.solvers.simplex.InfeasibleLPError: inconsistent/False constraint See Also ======== linprog, lpmin )rrrs rlpmaxrTsB sAv rc dtfd}g}g}g}t|}t|D]?\}\}}|||j| |T|dz }|j|||dfdg|j|||dfdg|j |g| ggv|W|rT|dz }|j|||dfdg|j|||dfdg|j |g| ggΌ|dz }|j|||dfdg|j|||dfdg|j||dg|j |g| g||z ggB|D]V}|dz }|j|||dfddg|j|||dfdd g|j d gd ggXt |D cgc]} | d g|t| z zzc} t |fScc} w) Nlengthc0dg|z}|D] \}}|||< |Srr)rindex_value_pairsliidxvals r _make_listz"_handle_bounds.._make_list|s.S6\) HCBsG  rr)r;rr;rErz)r;)rrr)intrerOrNextendr ) boundsrrrowrow2b_lenrurlrmr^s r_handle_boundsrxs23 G C D KEv&. 6Aq 9 NN1  Y QJE JJz%1a&'):; < JJz%1b'8)<= > KK!rd $ Y  :eq!fh-?@A :eq"gw-?@A aSA2$K( QJE JJz%1a&();< = JJz%1b'7);< = JJz%'3 4 KK!rdQUG, -5.:    :eq!fgx%@AB :eq"gx%ABC aS1#J   S91sECFN++9 :F4L HH9sG<ct|}|jdk7r|jdk(r |j}|jdk7r t d|s:|r t dt d|jt |jd}}n||fDcgc] }t|c}\}}|j|jk7r t d| |ot d||fDcgc] }t|c}\}}|j |}|j | }|j |}|j | }|U|ik(sO|dk(sIt|turt|d k(r|g|jz}nt||jk(rtd |Drnot|turPtd |jDr0|}dg|jz}|r*|j\}} | ||<|rnt d |zt|\} } | j|jz } |r6t|t |j| g| gg}|j | }n| }| }|jt d| }n |j } t!|||\} }}| |d| fScc}wcc}w) aReturn the minimization of ``c*x`` with the given constraints ``A*x <= b`` and ``A_eq*x = b_eq``. Unless bounds are given, variables will have nonnegative values in the solution. If ``A`` is not given, then the dimension of the system will be determined by the length of ``C``. By default, all variables will be nonnegative. If ``bounds`` is given as a single tuple, ``(lo, hi)``, then all variables will be constrained to be between ``lo`` and ``hi``. Use None for a ``lo`` or ``hi`` if it is unconstrained in the negative or positive direction, respectively, e.g. ``(None, 0)`` indicates nonpositive values. To set individual ranges, pass a list with length equal to the number of columns in ``A``, each element being a tuple; if only a few variables take on non-default values they can be passed as a dictionary with keys giving the corresponding column to which the variable is assigned, e.g. ``bounds={2: (1, 4)}`` would limit the 3rd variable to have a value in range ``[1, 4]``. Examples ======== >>> from sympy.solvers.simplex import linprog >>> from sympy import symbols, Eq, linear_eq_to_matrix as M, Matrix >>> x = x1, x2, x3, x4 = symbols('x1:5') >>> X = Matrix(x) >>> c, d = M(5*x2 + x3 + 4*x4 - x1, x) >>> a, b = M([5*x2 + 2*x3 + 5*x4 - (x1 + 5)], x) >>> aeq, beq = M([Eq(3*x2 + x4, 2), Eq(-x1 + x3 + 2*x4, 1)], x) >>> constr = [i <= j for i,j in zip(a*X, b)] >>> constr += [Eq(i, j) for i,j in zip(aeq*X, beq)] >>> linprog(c, a, b, aeq, beq) (9/2, [0, 1/2, 0, 1/2]) >>> assert all(i.subs(dict(zip(x, _[1]))) for i in constr) See Also ======== lpmin, lpmax rC must be a single row.A and b must both be givenr'number of columns in A and C must matchN A_eq and b_eq must both be givenr=rzc3^K|]%}t|tuxrt|dk('ywrzNtyperhrer8s rr:zlinprog..0+B56Q5 0SVq[0+B+-c3^K|]%}t|tuxrt|dk('ywrrr8s rr:zlinprog..1**Q5 0SVq[0**runexpected bounds %s)r rJrIrGrHr col_joinrrhrerKrvaluespopitemrrow_joinrF)r\r'rmA_eqb_eqrrPr"dbr#A_b_rrrUrTs rlinprogrsX q Avv{qvv{ CCvv{233 9: :Qqvvq!11$%q6*aq *1vvBCC ||?@ @*.6AfQi6 d JJt  JJu  JJt  JJu  Nflf .A <5 S[A%5X&F [AFF "s+B:@+B(B  &\T !c****'*B[166)Fzz|1q 3f<= ='Bgg E!&&#./"67A 2AAA JJuQ} %vvgq!QGAq! a#h;a+7s J?#Kc jddlm}t|}|jdk7r|jdk(r |j }|jdk7r t d|s:|r t dtd|jt|jd}}n||fDcgc] }t|c}\}}|j|jk7r t d| |)t d||fDcgc] }t|c}\}}||ik(s|dk(st|turt|d k(r|g|jz}nt||jk(rtd |Drnot|turPtd |jDr0|} dg|jz}| r*| j\}} | ||<| rnt d |zt|d |jdzz} || zdt|| z|D cgc] \}} || k c} }t|| z|D cgc]\}} t!|| c} }z}} t#|D];\}\} }| |j%| || k\|%|j%| ||k=| |fScc}wcc}wcc} }wcc} }w)Nr)symbolsrrrrrr=rzc3^K|]%}t|tuxrt|dk('ywrrr8s rr:zshow_linprog..:rrc3^K|]%}t|tuxrt|dk('ywrrr8s rr:zshow_linprog..=rrrzx1:%s)sympyrr rJrIrGrHr rrhrerKrrrr{rrOrN)r\r'rmrrrrrPr"rr#rurlohis r show_linprogrsq Avv{qvv{ CCvv{233 9: :Qqvvq!11$%q6*aq *1vvBCC ||?@ @*.6AfQi6 d Nflf .A <5 S[A%5X&F [AFF "s+B:@+B(B  &\T !c****'*B[166)Fzz|1q 3f<= =ww!&&(+,-A Q3(S1a[1caQT1DQRFSWHX4Y1R!W4YYaA ( 8B > HHQqT2X  > HHQqT2X   Q3JM+7,24YsJ)J$J)5J/ )NF)Fr+)T)NNNNN)+r sympy.corersympy.core.exprtoolsrsympy.core.relationalrrrsympy.core.singletonrsympy.core.symbolr sympy.core.sortingr $sympy.functions.elementary.complexesr sympy.matrices.denser r sympy.solvers.solvesetrsympy.utilities.iterablesrsympy.utilities.miscr Exceptionrrr(r3rFrjrnrrrrrrrrrrrrrs=~-,,"#&5.66+ y &  * BM r+p+)\$0o=dod("VBJ#L!H2IjjX7r