L ijZdZddlZddlmZddlmZddlmZddl m Z ddl m Z dd l m Z dd lmZd Zd Zd ZdZdZddZdZdZ ddZ ddZy)a[Revised simplex method for linear programming The *revised simplex* method uses the method described in [1]_, except that a factorization [2]_ 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:: 1.3.0 References ---------- .. [1] Bertsimas, Dimitris, and J. Tsitsiklis. "Introduction to linear programming." Athena Scientific 1 (1997): 997. .. [2] Bartels, Richard H. "A stabilization of the simplex method." Journal in Numerische Mathematik 16.5 (1971): 414-434. N) LinAlgError)solve)_check_unknown_options)LU)BGLU) _postsolve)OptimizeResultc \|j\} } d} t||||\}}}}}} | dk(r|j|}d}|||||| |fS| }d}t||||||||||| | ||\}}} }|j|}| dk(r||kDrd} t j | t }||| k\D]}|dd|f} t jt||}t j|dd|f}t j | t }d|||| k<t j|d}t j|ddd| f||f}||}|||f|krd||<n||||k(<||d| f}||}|d| }|jd} |||||| |fS#t$rd} YwxYw)a{ The purpose of phase one is to find an initial basic feasible solution (BFS) to the original problem. Generates an auxiliary problem with a trivial BFS and an objective that minimizes infeasibility of the original problem. Solves the auxiliary problem using the main simplex routine (phase two). This either yields a BFS to the original problem or determines that the original problem is infeasible. If feasible, phase one detects redundant rows in the original constraint matrix and removes them, then chooses additional indices as necessary to complete a basis/BFS for the original problem. rdtypeNF) shape_generate_auxiliary_problemdot _phase_twonponesboolabsrargmaxwherer)Abx0callbackpostsolve_argsmaxitertoldisp maxupdatemastpivotmnstatuscbasisxresidualiter_k phase_one_n keep_rows basis_columnB basis_finder pertinent_roweligible_columnseligible_column_indicesindexnew_basis_columns `/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/scipy/optimize/_linprog_rs.py _phase_oner9s* 77DAq F!>@AE6u= M+;;>5), --cd}|j\}}||}ntj|}|||zz }||dk ||dk<||dk ||dk<||dkxxdzcc<|tj|} ntj||kDd} tjtj ||kDd} t | dk(r7t | |kr)tj|} t|| } ||| | ||fSt | |t | z kDstj|dkrtj|} d}||| | ||fSt||\} } tj| | }tjtj| | }tj||}| |} | |} | tjtj| | }t |}|tj|z}tj| |f}tj| |f}tj|tj||ff}d|||f<tj|tj|f}|||||fz ||<tj||z} d| |<tj| |f} t|| } ||| | ||fS)a Modifies original problem to create an auxiliary problem with a trivial initial basic feasible solution and an objective that minimizes infeasibility in the original problem. Conceptually, this is done by stacking an identity matrix on the right of the original constraint matrix, adding artificial variables to correspond with each of these new columns, and generating a cost vector that is all zeros except for ones corresponding with each of the new variables. A initial basic feasible solution is trivial: all variables are zero except for the artificial variables, which are set equal to the corresponding element of the right hand side `b`. Running the simplex method on this auxiliary problem drives all of the artificial variables - and thus the cost - to zero if the original problem is feasible. The original problem is declared infeasible otherwise. Much of the complexity below is to improve efficiency by using singleton columns in the original problem where possible, thus generating artificial variables only as necessary, and using an initial 'guess' basic feasible solution. rr r)rrr<r;rrr=rLany_select_singleton_columnsisin logical_not logical_andrEhstack)rrrr!r(r&r'r+rnonzero_constraintsr*r)colsrowsi_tofix i_notinbasisi_fix_without_auxarowsn_auxacolsbasis_ng basis_ng_rowss r8rrs0F 77DAq ~  HHQK AaCA!a%yAa!eH!a%yAa!eHa!eHNH  z iil hhq3w/2 HHRVVAY_ %a (E 1$Uq HHQK'51!Qq&(( ! "QU^ 3 &&Q- HHQK!Qq&((+1a0JD$ggd/0G>>"''$"67Lw = ! "D ! "D  "(;T B!D EE JE  % E~~tUm,HNND%=1M 1bhh5z*+,AAeUlO 288E?+,AM"1]H%<#==AhK AAeH NNE8, -E #Au -E aE1f $$rMctjtjtj|dk7ddk(d}|dd|f}tjt |t }tj|\}}|||<|||f||zdk\}||ddd}||ddd}tj|d\}} || |fS) a\ Finds singleton columns for which the singleton entry is of the same sign as the right-hand side; these columns are eligible for inclusion in an initial basis. Determines the rows in which the singleton entries are located. For each of these rows, returns the indices of the one singleton column and its corresponding row. raxisrNrrOT) return_index)rnonzerosumrr<r=intunique) rrcolumn_indicescolumns row_indices nonzero_rowsnonzero_columns same_signunique_row_indices first_columnss r8rQrQsZZrvvayA~A >! CDQGN>!"G((3~.c:K$&JJw$7!L/#/K +~-.q~=BI#I.tt4Ni(2.K)+ +?C)E% - (*< <rr<rr9rr)r)c0rrrrrr r!r"r#r$r%unknown_optionsmessagesr+r*r,r(rs r8 _linprog_rsrsn?+H( vv{xx !Xa[!33 1aX~Cy$ ?0AuaHfi{&0Aq%1?18#t1:D%1: '<#5&) fhv&--hrsp&$-%%%C4L .Fd%N=@+'I,9.AEP#h/405[HrM