L i*dZddlZddlmZddlmZddlm Z ddl m Z m Z m Z mZddlmZedd Zd ej$_d e_d Zdd ZdZdZddZdZdZdZdZdZdZddZdZ y)z: Method agnostic utility functions for linear programming N)warn)OptimizeWarning)_remove_redundancy_svd_remove_redundancy_pivot_sparse_remove_redundancy_pivot_dense_remove_redundancy_id) namedtuple _LPProblemz+c A_ub b_ub A_eq b_eq bounds x0 integrality)NNNNNNNa Represents a linear-programming problem. Attributes ---------- c : 1D array The coefficients of the linear objective function to be minimized. A_ub : 2D array, optional The inequality constraint matrix. Each row of ``A_ub`` specifies the coefficients of a linear inequality constraint on ``x``. b_ub : 1D array, optional The inequality constraint vector. Each element represents an upper bound on the corresponding value of ``A_ub @ x``. A_eq : 2D array, optional The equality constraint matrix. Each row of ``A_eq`` specifies the coefficients of a linear equality constraint on ``x``. b_eq : 1D array, optional The equality constraint vector. Each element of ``A_eq @ x`` must equal the corresponding element of ``b_eq``. bounds : various valid formats, optional The bounds of ``x``, as ``min`` and ``max`` pairs. If bounds are specified for all N variables separately, valid formats are: * a 2D array (N x 2); * a sequence of N sequences, each with 2 values. If all variables have the same bounds, the bounds can be specified as a 1-D or 2-D array or sequence with 2 scalar values. If all variables have a lower bound of 0 and no upper bound, the bounds parameter can be omitted (or given as None). Absent lower and/or upper bounds can be specified as -numpy.inf (no lower bound), numpy.inf (no upper bound) or None (both). x0 : 1D 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 `x0` represents a basic feasible solution. integrality : 1-D array or int, optional Indicates the type of integrality constraint on each decision variable. ``0`` : Continuous variable; no integrality constraint. ``1`` : Integer variable; decision variable must be an integer within `bounds`. ``2`` : Semi-continuous variable; decision variable must be within `bounds` or take value ``0``. ``3`` : Semi-integer variable; decision variable must be an integer within `bounds` or take value ``0``. By default, all variables are continuous. For mixed integrality constraints, supply an array of shape `c.shape`. To infer a constraint on each decision variable from shorter inputs, the argument will be broadcast to `c.shape` using `np.broadcast_to`. This argument is currently used only by the ``'highs'`` method and ignored otherwise. Notes ----- This namedtuple supports 2 ways of initialization: >>> lp1 = _LPProblem(c=[-1, 4], A_ub=[[-3, 1], [1, 2]], b_ub=[6, 4]) >>> lp2 = _LPProblem([-1, 4], [[-3, 1], [1, 2]], [6, 4]) Note that only ``c`` is a required argument here, whereas all other arguments ``A_ub``, ``b_ub``, ``A_eq``, ``b_eq``, ``bounds``, ``x0`` are optional with default values of None. For example, ``A_eq`` and ``b_eq`` can be set without ``A_ub`` or ``b_ub``: >>> lp3 = _LPProblem(c=[-1, 4], A_eq=[[2, 1]], b_eq=[10]) c|jdd}|r|tj|}|r|tj|}tj|xstj|}hd}ddh}||vr|rt d|d|d|j d d}|s|r|d k(rd |d <t d td |||fS)a  Check the provided ``A_ub`` and ``A_eq`` matrices conform to the specified optional sparsity variables. Parameters ---------- A_ub : 2-D array, optional 2-D array such that ``A_ub @ x`` gives the values of the upper-bound inequality constraints at ``x``. A_eq : 2-D array, optional 2-D array such that ``A_eq @ x`` gives the values of the equality constraints at ``x``. options : dict A dictionary of solver options. All methods accept the following generic options: maxiter : int Maximum number of iterations to perform. disp : bool Set to True to print convergence messages. For method-specific options, see :func:`show_options('linprog')`. method : str, optional The algorithm used to solve the standard form problem. Returns ------- A_ub : 2-D array, optional 2-D array such that ``A_ub @ x`` gives the values of the upper-bound inequality constraints at ``x``. A_eq : 2-D array, optional 2-D array such that ``A_eq @ x`` gives the values of the equality constraints at ``x``. options : dict A dictionary of solver options. All methods accept the following generic options: maxiter : int Maximum number of iterations to perform. disp : bool Set to True to print convergence messages. For method-specific options, see :func:`show_options('linprog')`. _sparse_presolveF>highs-ds highs-ipmhighssimplexzrevised simplexzMethod 'zL' does not support sparse constraint matrices. Please consider using one of .sparsezinterior-pointTz9Sparse constraint matrix detected; setting 'sparse':True. stacklevel)popsps coo_arrayissparse ValueErrorgetrr) optionsmethA_ubA_eqr sparse_constraintpreferred_methods dense_methodsrs b/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/scipy/optimize/_linprog_util.py_check_sparse_inputsr%[s\{{#5u=D,}}T"D,}}T" T*@cll4.@: 12M }!28D6*N-.a12 2[[5 )F 'D4D,D  H  , D$ c|r"tj|d|fn|tdS|tjd|ftStj |tdS)asFormat the left hand side of the constraints to a 2-D array Parameters ---------- A : 2-D array 2-D array such that ``A @ x`` gives the values of the upper-bound (in)equality constraints at ``x``. n_x : int The number of variables in the linear programming problem. sparse_lhs : bool Whether either of `A_ub` or `A_eq` are sparse. If true return a coo_array instead of a numpy array. Returns ------- np.ndarray or sparse.coo_array 2-D array such that ``A @ x`` gives the values of the upper-bound (in)equality constraints at ``x``. rTdtypecopyr))rrfloatnpzerosarray)An_x sparse_lhss r$_format_A_constraintsr3sX*}} QHqD   xxC..xxT22r&c|tjgtStj|tdj}|jdk7r|S|j dS)a|Format the upper bounds of the constraints to a 1-D array Parameters ---------- b : 1-D array 1-D array of values representing the upper-bound of each (in)equality constraint (row) in ``A``. Returns ------- 1-D np.array 1-D array of values representing the upper-bound of each (in)equality constraint (row) in ``A``. r+Tr(r)r-r/r,squeezesizereshape)bs r$_format_b_constraintsr:sR  yxx%(( %d+335A! 1.2.r&c |\}}}}}}}}|t tj|tjdj }|j dk(r|j d}t|} | dk(st|jdk7r tdtj|js tdtj|xstj|} t|| | }|jd} t|jd k7s|jd| k7r td tj|r-tj|jjr8tj|s.tj|js td  t!|}|j| fk7r tdtj|js td t|| | }|jd} t|jd k7s|jd| k7r tdtj|r-tj|jjr8tj|s.tj|js td t!|}|j| fk7r tdtj|js td| tj|t"dj }|j$dk(r|j d}t|dk(s|j$dk7r td|j |j k(s tdtj|js tdtj&| d ft"}|-tj(|gstj(|ggrdtj*f} tj,tj|t"}|j}t|d kDrtdt|dd tj|| d fk(r|}ntj|d!k(stj|d"k(r)|j1}|d|dddf<|d|dddf<n=tj|d | fk(rtd#| dd$| dd%td&|d'tj2|dddf}tj* ||df<tj2|dddf}tj*||df<t5||||||||S#t$r} td | d} ~ wwxYw#t$r} td| d} ~ wwxYw#t$r} td| d} ~ wwxYw#t$r} td| d} ~ wwxYw#t$r} td| d} ~ wwxYw#t$r} td| d} ~ wwxYw#t$r!} td| j.dz| d} ~ wt$r!} td| j.dz| d} ~ wwxYw)(aZ Given user inputs for a linear programming problem, return the objective vector, upper bound constraints, equality constraints, and simple bounds in a preferred format. Parameters ---------- lp : A `scipy.optimize._linprog_util._LPProblem` consisting of the following fields: c : 1D array The coefficients of the linear objective function to be minimized. A_ub : 2D array, optional The inequality constraint matrix. Each row of ``A_ub`` specifies the coefficients of a linear inequality constraint on ``x``. b_ub : 1D array, optional The inequality constraint vector. Each element represents an upper bound on the corresponding value of ``A_ub @ x``. A_eq : 2D array, optional The equality constraint matrix. Each row of ``A_eq`` specifies the coefficients of a linear equality constraint on ``x``. b_eq : 1D array, optional The equality constraint vector. Each element of ``A_eq @ x`` must equal the corresponding element of ``b_eq``. bounds : various valid formats, optional The bounds of ``x``, as ``min`` and ``max`` pairs. If bounds are specified for all N variables separately, valid formats are: * a 2D array (2 x N or N x 2); * a sequence of N sequences, each with 2 values. If all variables have the same bounds, a single pair of values can be specified. Valid formats are: * a sequence with 2 scalar values; * a sequence with a single element containing 2 scalar values. If all variables have a lower bound of 0 and no upper bound, the bounds parameter can be omitted (or given as None). x0 : 1D 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 `x0` represents a basic feasible solution. Returns ------- lp : A `scipy.optimize._linprog_util._LPProblem` consisting of the following fields: c : 1D array The coefficients of the linear objective function to be minimized. A_ub : 2D array, optional The inequality constraint matrix. Each row of ``A_ub`` specifies the coefficients of a linear inequality constraint on ``x``. b_ub : 1D array, optional The inequality constraint vector. Each element represents an upper bound on the corresponding value of ``A_ub @ x``. A_eq : 2D array, optional The equality constraint matrix. Each row of ``A_eq`` specifies the coefficients of a linear equality constraint on ``x``. b_eq : 1D array, optional The equality constraint vector. Each element of ``A_eq @ x`` must equal the corresponding element of ``b_eq``. bounds : 2D array The bounds of ``x``, as ``min`` and ``max`` pairs, one for each of the N elements of ``x``. The N x 2 array contains lower bounds in the first column and upper bounds in the 2nd. Unbounded variables have lower bound -np.inf and/or upper bound np.inf. x0 : 1D 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 `x0` represents a basic feasible solution. NTr(rr5rzhInvalid input for linprog: c must be a 1-D array and must not have more than one non-singleton dimensionzFInvalid input for linprog: c must not contain values inf, nan, or NonezJInvalid input for linprog: c must be a 1-D array of numerical coefficients)r2zInvalid input for linprog: A_ub must have exactly two dimensions, and the number of columns in A_ub must be equal to the size of czIInvalid input for linprog: A_ub must not contain values inf, nan, or NonezGInvalid input for linprog: A_ub must be a 2-D array of numerical valueszInvalid input for linprog: b_ub must be a 1-D array; b_ub must not have more than one non-singleton dimension and the number of rows in A_ub must equal the number of values in b_ubzIInvalid input for linprog: b_ub must not contain values inf, nan, or NonezInvalid input for linprog: b_ub must be a 1-D array of numerical values, each representing the upper bound of an inequality constraint (row) in A_ubzInvalid input for linprog: A_eq must have exactly two dimensions, and the number of columns in A_eq must be equal to the size of czIInvalid input for linprog: A_eq must not contain values inf, nan, or NonezGInvalid input for linprog: A_eq must be a 2-D array of numerical valueszInvalid input for linprog: b_eq must be a 1-D array; b_eq must not have more than one non-singleton dimension and the number of rows in A_eq must equal the number of values in b_eqzIInvalid input for linprog: b_eq must not contain values inf, nan, or NonezInvalid input for linprog: b_eq must be a dense, 1-D array of numerical values, each representing the right hand side of an equality constraint (row) in A_eqzKInvalid input for linprog: x0 must be a 1-D array of numerical coefficientszkInvalid input for linprog: x0 should be a 1-D array; it must not have more than one non-singleton dimensionzNInvalid input for linprog: x0 and c should contain the same number of elementszGInvalid input for linprog: x0 must not contain values inf, nan, or Noner+zTInvalid input for linprog: unable to interpret bounds, check values and dimensions: zAInvalid input for linprog: provide a 2-D array for bounds, not a dz -D array.)r<r)rr<z%Invalid input for linprog: provide a z! x 2 array for bounds, not a 2 x z array.zQInvalid input for linprog: unable to interpret bounds with this dimension tuple: r) TypeErrorr-r/float64r6r7r8lenshaperisfiniteallrrr3datar:r,ndimr. array_equalinf atleast_2dargsflattenisnanr )lpcrb_ubr b_eqboundsx0 integralityr1er2n_ubn_eq bounds_clean bounds_convbsh bounds_flati_nones r$ _clean_inputsr[sN:<6AtT4vr;y% HHQbjjt 4 < < > 66Q; " A!f !8s177|q(AB B{{1~!!#$% %d#9s||D'9J%$T3:F zz!} tzz?a 4::a=C#7)* * LL r{{499'='A'A'C||D)"++d2C2G2G2I$% %%$T* ::$   {{4 $$&$% %%$T3:F zz!} tzz?a 4::a=C#7)* * LL r{{499'='A'A'C||D)"++d2C2G2G2I$% %%$T* ::$   {{4 $$&$% %  ~ 1"E5==?B 77a<BB r7a<277a<FG Gww!&& *+ +{{2""$$% %88S!HE2L~3r~~frd7SRVV @mmBHHV5$AB    C 3x!| XaL  +, , Qx " && 266#-#8!))+ (^ QT(^ QT 3x 3C7;Qw () ) #uA '( (XXl1a4( )F!vvgL XXl1a4( )F ffL atT4r; OOo # !" ##, * "#() **& : 239: ::& * "#() **( 8 0178 88, 1)*/0 1 1N @ ,./ffQi 89>? @ @ ,./ffQi 89>? @@s4W?7X X9$Y< Y3*Z6.Z-? X XX X6% X11X69 Y YY Y0 Y++Y03 Z < ZZ  Z* Z%%Z*- [?6[ [?[::[?c /0|\}}}}}} } } g} d} d}tj|j}d}d}| dddfj}| dddfj}|j\}}|j\}}|.|j dvrdt |zdz}t |tj|r4|j}|j}d }tj}n tj}tj}tj||ksMtj|tjk(s'tj|tj k(rd }d }d }t|||||| | | || |||fStjtj |dk7d dk(j#}tj|rtjtj$|tj&||kDrd }d}d }t|||||| | | || |||fS|tj(|ddf}|tj(|}tjtj |dk7d dk(j#}tj|rtjtj$||| krd }d}d }t|||||| | | || |||fS|tj(|ddf}|tj(|}|||f}|jddkDr\tjtj |dk7d dk(j#}|tj$||dk|tj$||dk<|tj$||dkD|tj$||dkD<tjtj*|rd}d}d }t|||||| | | || |||fS|tj$||dk|tj$||dk<|tj$||dkD|tj$||dkD<tjtj |dk7d dk(j#}||d}|||ddfd}t-|dkDrt/||D]U\}} ||||| fz }!|| |z |!cxkr || |zks"nd }d}d }t|||||| | | || |||fcS|!|| <|!|| <W|tj(|ddf}|tj(|}tjtj |dk7d dk(j#}|||ddfd}||d}t-|dkDrt/||D]s\}} ||||| fz }!||| fdkDr|!|| |z krd }n)|!|| kr!|!|| <n|!|| |zkDrd }n |!|| kDr|!|| <|sWd }d}t|||||| | | || |||fcS|tj(|ddf}|tj(|}tj&||z |k/tj(/}"tj0/r||j3|z }#||j3|z }$|j4dkDrtj|$dks%|j4dkDr4tj6|#dsd }d}d }t|||||| | | || |||fS|}%|}&tj/r| |/j3|/z } ||dd/fj3|/z }||dd/fj3|/z }||"}|/0||"}| | |"} |dd|"f}|dd|"f}||"}&||"}%/0fd}'| j9|'|j4dk(rL|j4dk(rtjtj$|dkD|&tj k(rd}d}nd}d}d }|%|dk||dk<|&|dkD||dkD<|%|dk(}(|%|dk(tj*|(|(tj*|(<d|(tj*|(<|(||dk(<tj:|&ddtj<f|%ddtj<ff} |jd})d}*tj|rg|rM|j4dkDr>t?||}+|+\}}}}|jd|)krtA|*tBd|dk7rd }t|||||| | | || |||fSd},|r/|j4dkDr tjDjG|}-|r|j4dkDr-|jdkrtA|*tBd|jd|-z }.|6|.|,krtK||}+|+\}}}}|.|,kDs|dk(rqtM||}+|+\}}}}n]|j }|dk(rtK||}+|+\}}}}n4|dk(rtM||}+|+\}}}}n|dk(rtO|||-}+|+\}}}}n |jd|-krd}d}|dk7rd }t|||||| | | || |||fS#tH$rd}-YwxYw) aM Given inputs for a linear programming problem in preferred format, presolve the problem: identify trivial infeasibilities, redundancies, and unboundedness, tighten bounds where possible, and eliminate fixed variables. Parameters ---------- lp : A `scipy.optimize._linprog_util._LPProblem` consisting of the following fields: c : 1D array The coefficients of the linear objective function to be minimized. A_ub : 2D array, optional The inequality constraint matrix. Each row of ``A_ub`` specifies the coefficients of a linear inequality constraint on ``x``. b_ub : 1D array, optional The inequality constraint vector. Each element represents an upper bound on the corresponding value of ``A_ub @ x``. A_eq : 2D array, optional The equality constraint matrix. Each row of ``A_eq`` specifies the coefficients of a linear equality constraint on ``x``. b_eq : 1D array, optional The equality constraint vector. Each element of ``A_eq @ x`` must equal the corresponding element of ``b_eq``. bounds : 2D array The bounds of ``x``, as ``min`` and ``max`` pairs, one for each of the N elements of ``x``. The N x 2 array contains lower bounds in the first column and upper bounds in the 2nd. Unbounded variables have lower bound -np.inf and/or upper bound np.inf. x0 : 1D 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 `x0` represents a basic feasible solution. rr : bool If ``True`` attempts to eliminate any redundant rows in ``A_eq``. Set False if ``A_eq`` is known to be of full row rank, or if you are looking for a potential speedup (at the expense of reliability). rr_method : string Method used to identify and remove redundant rows from the equality constraint matrix after presolve. tol : float The tolerance which determines when a solution is "close enough" to zero in Phase 1 to be considered a basic feasible solution or close enough to positive to serve as an optimal solution. Returns ------- lp : A `scipy.optimize._linprog_util._LPProblem` consisting of the following fields: c : 1D array The coefficients of the linear objective function to be minimized. A_ub : 2D array, optional The inequality constraint matrix. Each row of ``A_ub`` specifies the coefficients of a linear inequality constraint on ``x``. b_ub : 1D array, optional The inequality constraint vector. Each element represents an upper bound on the corresponding value of ``A_ub @ x``. A_eq : 2D array, optional The equality constraint matrix. Each row of ``A_eq`` specifies the coefficients of a linear equality constraint on ``x``. b_eq : 1D array, optional The equality constraint vector. Each element of ``A_eq @ x`` must equal the corresponding element of ``b_eq``. bounds : 2D array The bounds of ``x``, as ``min`` and ``max`` pairs, possibly tightened. x0 : 1D 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 `x0` represents a basic feasible solution. c0 : 1D array Constant term in objective function due to fixed (and eliminated) variables. x : 1D array Solution vector (when the solution is trivial and can be determined in presolve) revstack: list of functions the functions in the list reverse the operations of _presolve() the function signature is x_org = f(x_mod), where x_mod is the result of a presolve step and x_org the value at the start of the step (currently, the revstack contains only one function) complete: bool Whether the solution is complete (solved or determined to be infeasible or unbounded in presolve) status : int An integer representing the exit status of the optimization:: 0 : Optimization terminated successfully 1 : Iteration limit reached 2 : Problem appears to be infeasible 3 : Problem appears to be unbounded 4 : Serious numerical difficulties encountered message : str A string descriptor of the exit status of the optimization. References ---------- .. [5] Andersen, Erling D. "Finding all linearly dependent rows in large-scale linear programming." Optimization Methods and Software 6.3 (1995): 219-227. .. [8] Andersen, Erling D., and Knud D. Andersen. "Presolving in linear programming." Mathematical Programming 71.2 (1995): 221-245. rFNr>idsvdpivot'z[' is not a valid option for redundancy removal. Valid options are 'SVD', 'pivot', and 'ID'.c"|jSN)nonzero)r0s r$wherez_presolve..wheress99; r&r<zThe problem is (trivially) infeasible since one or more upper bounds are smaller than the corresponding lower bounds, a lower bound is np.inf or an upper bound is -np.inf.TaxiszThe problem is (trivially) infeasible due to a row of zeros in the equality constraint matrix with a nonzero corresponding constraint value.zThe problem is (trivially) infeasible due to a row of zeros in the equality constraint matrix with a nonzero corresponding constraint value.zIf feasible, the problem is (trivially) unbounded due to a zero column in the constraint matrices. If you wish to check whether the problem is infeasible, turn presolve off.zzThe problem is (trivially) infeasible because a singleton row in the equality constraints is inconsistent with the bounds.z}The problem is (trivially) infeasible because a singleton row in the upper bound constraints is inconsistent with the bounds.zvThe problem is (trivially) infeasible because the bounds fix all variables to values inconsistent with the constraintsctj}t|}tj|}||z }tj|j t |}|Src)r- flatnonzeror@arangeinsertastyper,)x_modiN index_offsetinsert_indicesx_revi_fx_undos r$revz_presolve..rev sS s#AAA99Q6JELr&zPThe solution was determined in presolve as there are no non-trivial constraints.aThe problem is (trivially) unbounded because there are no non-trivial constraints and a) at least one decision variable is unbounded above and its corresponding cost is negative, or b) at least one decision variable is unbounded below and its corresponding cost is positive. zA_eq does not appear to be of full row rank. To improve performance, check the problem formulation for redundant equality constraints.rrr_r`r^zDue to numerical issues, redundant equality constraints could not be removed automatically. Try providing your constraint matrices as sparse matrices to activate sparse presolve, try turning off redundancy removal, or try turning off presolve altogether.)(r-r.rAr*lowerstrrrrtocsrvstackreanyrGr r/sumrJ logical_andabs logical_notisinfr@ziprCdotr7allcloseappendhstacknewaxisrrrlinalg matrix_rank Exceptionrrr )1rLrr rr_methodtolrMrrNr rOrPrQ_revstackc0completexstatusmessagelbubm_eqnm_ubrer{zero_rowr0zero_col singleton_rowrowscolsrowcolvali_nfresidualslackub_modlb_modrvx_zero_cn_rows_Aredundancy_warningrr_ressmall_nullspacerankdim_row_nullspacertrus1 @@r$ _presolversO r02,AtT4vr1H BH A FG 1   B 1   BjjGD!jjGD!!)??Y'+((!! ||Dzz|zz|  vvb2g"&&rvv."&&w2G!1dD$fbAAx67< <xxtqyq1Q67??AH vvh 66 NNt s" $ % FAGHq$dD&"E8Xvw@ @x0!34Dx01Dxxtqyq1Q67??AH vvh 66"..43$;7 8FBGHq$dD&"E8Xvw@ @x0!34Dx01D d|AwwqzA~88BFF162a78@@B-/ NN8QU +.-"..1q5 )*-/ NN8QU +.-"..1q5 )* 66"((1+ F,GHq$dD&"E8Xvw@ @/1 NN8QU +/-2>>(AE *+.0 NN8QU +/-2>>(AE *+ HHRVVDAIA6!;<DDFM   "D dAg  "D 4y1}D$ HCs)d38n,Cc7S=C82c7S=8; "1dD$fbIAx67DD 33  BNN=1145BNN=12HHRVVDAIA6!;<DDFM mQ&' ( +D   "D 4y1}D$ DHCs)d38n,CCH~!C3&#H2c7]!BsGC3&#H2c7]!BsG;#1dD$fbIAx67DD# D&BNN=1145BNN=12 &&b/C C >># D vvc{$((2,&txx|# YY]rvveai0Qr{{8Q'?F)GHq$dD&"E8Xvw@ @F F vvc{ afjjC!!d1c6l&&r#w//d1c6l&&r#w// dGC dG >DBAtG}AtG}DD   yyA~$))q.xx|xx| 66Q;F5GffR^^AE6RVV+;<=ffR^^AE6bffW+<=>FBGF5G!a%=!a%!a%=!a%!q&>'-a1f~bhhx6H'I(#$'((#$!q& YYq"**}-vam/DE FFzz!}H@ T $))a-4T4@F*0 'D$zz!}x''QG{1dD$fbAAx67< < O dii!m 99((.D dii!mtzz!} 4 Q? JJqM$.   O3/d;.4+dFG ?2fk7dC.4+dFG")IE!/d;.4+dFGg%7dC.4+dFGd".tT4@.4+dFG ::a=4 %G F Q;H q$dD&" = 8Xvw 88I D s;o++ o:9o:c|i}|jDcic]\}}|| }}}t|||j|j\}}}t |j ||}||fScc}}w)a Parse the provided linear programming problem ``_parse_linprog`` employs two main steps ``_check_sparse_inputs`` and ``_clean_inputs``. ``_check_sparse_inputs`` checks for sparsity in the provided constraints (``A_ub`` and ``A_eq) and if these match the provided sparsity optional values. ``_clean inputs`` checks of the provided inputs. If no violations are identified the objective vector, upper bound constraints, equality constraints, and simple bounds are returned in the expected format. Parameters ---------- lp : A `scipy.optimize._linprog_util._LPProblem` consisting of the following fields: c : 1D array The coefficients of the linear objective function to be minimized. A_ub : 2D array, optional The inequality constraint matrix. Each row of ``A_ub`` specifies the coefficients of a linear inequality constraint on ``x``. b_ub : 1D array, optional The inequality constraint vector. Each element represents an upper bound on the corresponding value of ``A_ub @ x``. A_eq : 2D array, optional The equality constraint matrix. Each row of ``A_eq`` specifies the coefficients of a linear equality constraint on ``x``. b_eq : 1D array, optional The equality constraint vector. Each element of ``A_eq @ x`` must equal the corresponding element of ``b_eq``. bounds : various valid formats, optional The bounds of ``x``, as ``min`` and ``max`` pairs. If bounds are specified for all N variables separately, valid formats are: * a 2D array (2 x N or N x 2); * a sequence of N sequences, each with 2 values. If all variables have the same bounds, a single pair of values can be specified. Valid formats are: * a sequence with 2 scalar values; * a sequence with a single element containing 2 scalar values. If all variables have a lower bound of 0 and no upper bound, the bounds parameter can be omitted (or given as None). x0 : 1D 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 `x0` represents a basic feasible solution. options : dict A dictionary of solver options. All methods accept the following generic options: maxiter : int Maximum number of iterations to perform. disp : bool Set to True to print convergence messages. For method-specific options, see :func:`show_options('linprog')`. Returns ------- lp : A `scipy.optimize._linprog_util._LPProblem` consisting of the following fields: c : 1D array The coefficients of the linear objective function to be minimized. A_ub : 2D array, optional The inequality constraint matrix. Each row of ``A_ub`` specifies the coefficients of a linear inequality constraint on ``x``. b_ub : 1D array, optional The inequality constraint vector. Each element represents an upper bound on the corresponding value of ``A_ub @ x``. A_eq : 2D array, optional The equality constraint matrix. Each row of ``A_eq`` specifies the coefficients of a linear equality constraint on ``x``. b_eq : 1D array, optional The equality constraint vector. Each element of ``A_eq @ x`` must equal the corresponding element of ``b_eq``. bounds : 2D array The bounds of ``x``, as ``min`` and ``max`` pairs, one for each of the N elements of ``x``. The N x 2 array contains lower bounds in the first column and upper bounds in the 2nd. Unbounded variables have lower bound -np.inf and/or upper bound np.inf. x0 : 1D 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 `x0` represents a basic feasible solution. options : dict, optional A dictionary of solver options. All methods accept the following generic options: maxiter : int Maximum number of iterations to perform. disp : bool Set to True to print convergence messages. For method-specific options, see :func:`show_options('linprog')`. )rr )itemsr%rr r[_replace)rLrrkvsolver_optionsrr s r$_parse_linprogrs|H'.}}7tq!ad7N7!5nd68ggrww"HND$ r{{4{8 9B ~  8s A.c6 |\}}}}}}}} tj|rSd} tj|}tj|}d} d} tj} tj}nBd} tj } tj } tj} tj}t j|d}|dddf}|dddf}|j\}}t j|tj }t j|tj}t j|}t j|}t j||}t j|d}|| || c||<||<t j|tj }t j|tj}t j|}t j|}||xxd zcc<| ||xxd zcc<t!|dkDrF|jddkDr|dd|fxxd zcc<|jddkDr|dd|fxxd zcc<|j\}||}t!|}|dkDr||jdf}| rLt j"||f}| |tjt j$||f| f}nE| |t j|f}d|t j"||jd|f<t j&|t j|f}|||d| ||f}t j&||f}t j&|t j|jdff}|8t j&|t j|jdff}t j||} t j| d}!t!|!}"t j&|t j|"f}|*t j&|t j|"f}| |ddd|f|dd|!f f}||! ||||"z|@||!dk}#||!|# |t j"||jd|#<d||!|#<| ||jd| |jd|jdfg}$| ||$g}%t j|d}&||j)t*}'|t j,|'||&zz }| rC|%j/}%||%dd|&ftj0|'zj-d z}n||%dd|&f|'zj-d z}| ||&xx|'zcc<|%||||fS) a Given a linear programming problem of the form: Minimize:: c @ x Subject to:: A_ub @ x <= b_ub A_eq @ x == b_eq lb <= x <= ub where ``lb = 0`` and ``ub = None`` unless set in ``bounds``. Return the problem in standard form: Minimize:: c @ x Subject to:: A @ x == b x >= 0 by adding slack variables and making variable substitutions as necessary. Parameters ---------- lp : A `scipy.optimize._linprog_util._LPProblem` consisting of the following fields: c : 1D array The coefficients of the linear objective function to be minimized. A_ub : 2D array, optional The inequality constraint matrix. Each row of ``A_ub`` specifies the coefficients of a linear inequality constraint on ``x``. b_ub : 1D array, optional The inequality constraint vector. Each element represents an upper bound on the corresponding value of ``A_ub @ x``. A_eq : 2D array, optional The equality constraint matrix. Each row of ``A_eq`` specifies the coefficients of a linear equality constraint on ``x``. b_eq : 1D array, optional The equality constraint vector. Each element of ``A_eq @ x`` must equal the corresponding element of ``b_eq``. bounds : 2D array The bounds of ``x``, lower bounds in the 1st column, upper bounds in the 2nd column. The bounds are possibly tightened by the presolve procedure. x0 : 1D 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 `x0` represents a basic feasible solution. c0 : float Constant term in objective function due to fixed (and eliminated) variables. Returns ------- A : 2-D array 2-D array such that ``A`` @ ``x``, gives the values of the equality constraints at ``x``. b : 1-D array 1-D array of values representing the RHS of each equality constraint (row) in A (for standard form problem). c : 1-D array Coefficients of the linear objective function to be minimized (for standard form problem). c0 : float Constant term in objective function due to fixed (and eliminated) variables. x0 : 1-D array Starting values of the independent variables, which will be refined by the optimization algorithm References ---------- .. [9] Bertsimas, Dimitris, and J. Tsitsiklis. "Introduction to linear programming." Athena Scientific 1 (1997): 997. Tc0tj|dSNcsr)format)rrblockss r$rz_get_Abc..hstackb::fU3 3r&c0tj|dSr)rr{rs r$r{z_get_Abc..vstackerr&F)r*Nrrr5)rArf)rr csr_array eye_arrayr-rr{r.eyer/rAequalrGrr~rdr@rkones concatenatermr,r}tocsc diags_array)(rLrrMrrNr rOrPrQrRrrr{r.rlbsubsrrTlb_noneub_nonelb_someub_some l_nolb_someubi_nolbi_newubub_newubn_boundsrAidxsA1r9l_freei_freen_free i_free_negA2r0i_shiftlb_shifts( r$_get_Abcrsjj:<6AtT4vr; ||D}}T"}}T" 4 4 mmffXXf4 (F A,C A,CJD$hhsRVVG$GhhsBFF#GnnW%GnnW%GNN7G4M ZZ &q )F ] c-00+C M*hhsRVVG$GhhsBFF#GnnW%GnnW%GfIOI ~ 6 b  6{Q ::a=1  FOr !O ::a=1  FOr !O HG7|H7|H!|4::a=) IIh'1D40A4/H5:"<=>D4%12D<=D4A/8 9~~tRXXh%789TU t B d|$A 288TZZ]$4567A ~ ^^R4::a=*:!;< = ^^GW -F ZZ  "F [F 288F+,-A ~ ^^R&!12 3 AuuH1f9 ~. /BV9*Ad4; ~Z!^ 8:6*;M8N7N299T288A; ' 34!"6*  TZZ]#UDJJqM4::a=+I%JK LBBxAjj!!$G7|""5)H"&&AgJ& ''B GGI a7 mcooh77 < solution to original problem. N)r@r>)rrrrs r$_unscalers@  F Ra5=?  Fs //c^t||dvrtd|dtd|dy)a Print the termination summary of the linear program Parameters ---------- message : str A string descriptor of the exit status of the optimization. status : int An integer representing the exit status of the optimization:: 0 : Optimization terminated successfully 1 : Iteration limit reached 2 : Problem appears to be infeasible 3 : Problem appears to be unbounded 4 : Serious numerical difficulties encountered fun : float Value of the objective function. iteration : iteration The number of iterations performed. )rrz! Current function value: z <12.6fz Iterations: r=N)print)rrfun iterations r$_display_summaryrs7, 'N  1#g?@ !)A /0r&c|d\}}}}}}} } |dd\} } } t|| | }|jd}|s|d}t|D]}\}}|d}|d}|tj k(r-|tjk(r|dz }|||||zdz z ||<Q|tj k(r |||z ||<q||xx|z cc<|d|}t | D] }||} |j |}tjd5||j |z }||j |z }ddd||fS#1swYxYw)a Given solution x to presolved, standard form linear program x, add fixed variables back into the problem and undo the variable substitutions to get solution to original linear program. Also, calculate the objective function value, slack in original upper bound constraints, and residuals in original equality constraints. Parameters ---------- x : 1-D array Solution vector to the standard-form problem. postsolve_args : tuple Data needed by _postsolve to convert the solution to the standard-form problem into the solution to the original problem, including: lp : A `scipy.optimize._linprog_util._LPProblem` consisting of the following fields: c : 1D array The coefficients of the linear objective function to be minimized. A_ub : 2D array, optional The inequality constraint matrix. Each row of ``A_ub`` specifies the coefficients of a linear inequality constraint on ``x``. b_ub : 1D array, optional The inequality constraint vector. Each element represents an upper bound on the corresponding value of ``A_ub @ x``. A_eq : 2D array, optional The equality constraint matrix. Each row of ``A_eq`` specifies the coefficients of a linear equality constraint on ``x``. b_eq : 1D array, optional The equality constraint vector. Each element of ``A_eq @ x`` must equal the corresponding element of ``b_eq``. bounds : 2D array The bounds of ``x``, lower bounds in the 1st column, upper bounds in the 2nd column. The bounds are possibly tightened by the presolve procedure. x0 : 1D 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 `x0` represents a basic feasible solution. revstack: list of functions the functions in the list reverse the operations of _presolve() the function signature is x_org = f(x_mod), where x_mod is the result of a presolve step and x_org the value at the start of the step complete : bool Whether the solution is was determined in presolve (``True`` if so) Returns ------- x : 1-D array Solution vector to original linear programming problem fun: float optimal objective value for original problem slack : 1-D array The (non-negative) slack in the upper bound constraints, that is, ``b_ub - A_ub @ x`` con : 1-D array The (nominally zero) residuals of the equality constraints, that is, ``b - A_eq @ x`` rrNignore)invalid)rrA enumerater-rGreversedrerrstate)rpostsolve_argsrrMrrNr rOrPrQrRrrrr1 n_unboundedrobilbiubirvrrcons r$ _postsolver"sB:H9J6AtT4vr;)!"-HaAwA ,,q/C * v& EArQ%CQ%Crvvg~#-q tak 1A 566!266'>1:AaDaDCKD  $3A ! F %%(C X &!txx{"TXXa[ ! c5#  !!s )EE c tj|dz}| |dk(rd}d}||fStj|jxs_tj|xsHtj|jxs#tj|j} | rd} n|d}||dddf|z k\||dddf|zkz} | |dkDtj|d|zz} tj |  } |d k7xr|| kj} |d k7xr&tj ||kDj}| xs| xs| } |dk(r| sd}d |d zd z}||fS|d k(r| rd}d}||fS)a Check the validity of the provided solution. A valid (optimal) solution satisfies all bounds, all slack variables are negative and all equality constraint residuals are strictly non-zero. Further, the lower-bounds, upper-bounds, slack and residuals contain no nan values. Parameters ---------- x : 1-D array Solution vector to original linear programming problem fun: float optimal objective value for original problem status : int An integer representing the exit status of the optimization:: 0 : Optimization terminated successfully 1 : Iteration limit reached 2 : Problem appears to be infeasible 3 : Problem appears to be unbounded 4 : Serious numerical difficulties encountered slack : 1-D array The (non-negative) slack in the upper bound constraints, that is, ``b_ub - A_ub @ x`` con : 1-D array The (nominally zero) residuals of the equality constraints, that is, ``b - A_eq @ x`` bounds : 2D array The bounds on the original variables ``x`` message : str A string descriptor of the exit status of the optimization. tol : float Termination tolerance; see [1]_ Section 4.5. Returns ------- status : int An integer representing the exit status of the optimization:: 0 : Optimization terminated successfully 1 : Iteration limit reached 2 : Problem appears to be infeasible 3 : Problem appears to be unbounded 4 : Serious numerical difficulties encountered message : str A string descriptor of the exit status of the optimization. Nrrz^The solver did not provide a solution nor did it report a failure. Please submit a bug report.Fr)atolrhzOThe solution does not satisfy the constraints within the required tolerance of z.2Ea,, yet no errors were raised and there is no certificate of infeasibility or unboundedness. Check whether the slack and constraint residuals are acceptable; if not, consider enabling presolve, adjusting the tolerance option(s), and/or using a different method. Please consider submitting a bug report.r<zuThe solution is feasible, but the solver did not report that the solution was optimal. Please try a different method.)r-sqrtrKr|iscloserCr)rrrrrrPrrrR contains_nans is_feasible valid_boundsinvalid_bounds invalid_slack invalid_cons r$ _check_resultrsj ''#, Cy Q;FGGw    88C=  88E?     88C=      KVAqD\C//A19K4KL qBJJq!#,FFF VVL11! <#(:(:(< k?rvvc{S'8&=&=&? )I]IkJ  {;,14S ;>>>" 7? 1 7?r&)F)g& .>)!__doc__numpyr- scipy.sparserrwarningsr _optimizer!scipy.optimize._remove_redundancyrrrr collectionsr r __new__ __defaults__r%r3r:r[rrrrrrrrrr&r$rs&#  EG "- E RBJ3>/,EPPw8t l^AH$##L 18ePgr&