
    Ui,                     V    d dl mZ d dlZd dlmc mc mZ d dl	m
Z d dlmZ d Zd ZdS )    )warnN)_highs_options)OptimizeWarningc
                    | j         }
|j         }|duot          j        |          dk    }ddd}t          j                    }|
|_        ||_        |
|j        _        ||j        _        t          j        j	        |j        _
        | |_        ||_        ||_        ||_        ||_        ||j        _        ||j        _        ||j        _        |j         dk    rd |D             |_        t          j                    }t          j                    }t-          j                    }|	                                D ]3\  }}||dv r|                    |          }d|k    r*t5          dt7          ||i           t8          d	           R|d
v r;t;          |t<                    r|rdnd}nt5          d| d| dt8          d	           t          j        |          }tA          |||          \  }}|t          j        j!        k    r5t;          |t<                    s t5          d| d| dt8          d	           |dk    rt5          |t8          d	           "tE          |||           5|#                    |          }|t          j$        j%        k    rQ|&                    |'                                |(                    |'                                          d           |S |)                    |          }|t          j$        j%        k    r>t          j*        j+        }|&                    ||(                    |          d           |S |,                                }|t          j$        j%        k    rQ|&                    |'                                |(                    |'                                          d           |S |'                                }|-                                }|t          j*        j.        t          j*        j/        t          j*        j0        t          j*        j1        fvpH|t          j*        j/        t          j*        j0        t          j*        j1        hv o|j2        t          j3        k    }|t          j*        j.        k    }|r|s|s_|r]|&                    |d|(                    |           d|4                    |j5                   |j6        |j7        |j8        d           |S |9                                }|:                                } t          j;        d|
f          }!| j<        }"|j=        }#t}          |
          D ]S}$|"|$         t          j?        j@        k    r|#|$         |!d|$f<   +|"|$         t          j?        jA        k    r|#|$         |!d|$f<   T|&                    ||(                    |          t          jB        |jC                  ||jD        z
  t          jB        |jE                  |!|j2        |j6        |j7        |j8        d
           |r(|&                    |jF        |jG        |jH        d           |S )a
  Solve linear programs using HiGHS [1]_.

    Assume problems of the form:

        MIN c.T @ x
        s.t. lhs <= A @ x <= rhs
             lb <= x <= ub

    Parameters
    ----------
    c : 1-D array, (n,)
        Array of objective value coefficients.
    astart : 1-D array
        CSC format index array.
    aindex : 1-D array
        CSC format index array.
    avalue : 1-D array
        Data array of the matrix.
    lhs : 1-D array (or None), (m,)
        Array of left hand side values of the inequality constraints.
        If ``lhs=None``, then an array of ``-inf`` is assumed.
    rhs : 1-D array, (m,)
        Array of right hand side values of the inequality constraints.
    lb : 1-D array (or None), (n,)
        Lower bounds on solution variables x.  If ``lb=None``, then an
        array of all `0` is assumed.
    ub : 1-D array (or None), (n,)
        Upper bounds on solution variables x.  If ``ub=None``, then an
        array of ``inf`` is assumed.
    options : dict
        A dictionary of solver options

    Returns
    -------
    res : dict

        If model_status is one of kOptimal,
        kObjectiveBound, kTimeLimit,
        kIterationLimit:

            - ``status`` : HighsModelStatus
                Model status code.

            - ``message`` : str
                Message corresponding to model status code.

            - ``x`` : list
                Solution variables.

            - ``slack`` : list
                Slack variables.

            - ``lambda`` : list
                Lagrange multipliers associated with the constraints
                Ax = b.

            - ``s`` : list
                Lagrange multipliers associated with the constraints
                x >= 0.

            - ``fun``
                Final objective value.

            - ``simplex_nit`` : int
                Number of iterations accomplished by the simplex
                solver.

            - ``ipm_nit`` : int
                Number of iterations accomplished by the interior-
                point solver.

        If model_status is not one of the above:

            - ``status`` : HighsModelStatus
                Model status code.

            - ``message`` : str
                Message corresponding to model status code.

    Notes
    -----
    If ``options['write_solution_to_file']`` is ``True`` but
    ``options['solution_file']`` is unset or ``''``, then the solution
    will be printed to ``stdout``.

    If any iteration limit is reached, no solution will be
    available.

    ``OptimizeWarning`` will be raised if any option value set by
    the user is found to be incorrect.

    References
    ----------
    .. [1] https://highs.dev/
    .. [2] https://www.maths.ed.ac.uk/hall/HiGHS/HighsOptions.html
    Nr   )xfunc                 6    g | ]}t          j        |          S  )_hHighsVarType).0is     ~/var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/scipy/optimize/_highspy/_highs_wrapper.py
<listcomp>z"_highs_wrapper.<locals>.<listcomp>   s"    CCC!2?1--CCC    )sensezUnrecognized options detected:    )
stacklevel)presolveparallelonoffz	Option f"z" is "z4", but only True or False is allowed. Using default.)statusmessagezmodel_status is z; primal_status is )r   r   simplex_nitipm_nitcrossover_nit   )
r   r   r   slacklambda	marg_bndsr   r   r   r   )mip_node_countmip_dual_boundmip_gap)Isizenpsumr   HighsLpnum_col_num_row_	a_matrix_MatrixFormatkColwiseformat_	col_cost_
col_lower_
col_upper_
row_lower_
row_upper_start_index_value_integrality__HighsHighsOptionshoptHighsOptionsManageritemsget_option_typer   dictr   
isinstanceboolHighsOptionTypecheck_optionkBoolsetattrpassOptionsHighsStatuskErrorupdategetModelStatusmodelStatusToString	passModelHighsModelStatuskModelErrorrungetInfokOptimal
kTimeLimitkIterationLimitkSolutionLimitobjective_function_value	kHighsInfsolutionStatusToStringprimal_solution_statussimplex_iteration_countipm_iteration_countcrossover_iteration_countgetSolutiongetBasiszeros
col_statuscol_dualrangeHighsBasisStatuskLowerkUpperarray	col_value	row_valuerow_dualr#   r$   r%   )%cindptrindicesdatalhsrhslbubintegralityoptionsnumcolnumrowisMipreslphighshighs_optionshoptmanagerkeyvalopt_typer   msg
opt_statusinit_statuserr_model_status
run_statusmodel_statusinfomipFailConditionlpFailConditionsolutionbasisr"   basis_col_statussolution_col_dualiis%                                        r   _highs_wrapperr   	   s   B VFXFt#?{(;(;a(?E  C 
BBKBK"BL"BL?3BLBLBMBMBMBM BL!BLBL!CC{CCC IKKEO%%M*,,KMMOO +1 +1S;#++ ..s33>>D$Sz2B2BDD   
 ...c4(( 	"%0$$5CC3C 3 3s 3 3 3'#$	    )(33H&uc377KFC2-333!#t,, 3C 3 3s 3 3 3'#$	     {{S/a88888sC0000""=11JR^***

..00 44U5I5I5K5KLL 	
 	
 	
 
//"%%Kbn+++.:

* 445EFF 	
 	
 	
 
 JR^***

..00 44U5I5I5K5KLL 	
 	
 	
 
 ''))L ==??D $
$
&
+
*	,   	*/.
	
 	< *bl:  #b&9&BBO " E o 

&O,,\::O O //0KLLO O  $;3!%!?	 		
 	
 	
 
   ""HNNE !V%%I' )Fmm 5 5B2#6#===04Iaeb!R%8%???04IaeJJ"00>>(,-- 8--hx011"07/!;	
 	
  &  


"&"5"&"5< 	
 	
 	
 Jr   c                    |                      |          \  }}t          j                    }|t          j        j        k    rdS t          j        j        t          t          j        j	        t          t          j        j        t          t          j        j        t          i}|                    |d           }|t          u r|                    ||          sdS |t          u r|                    ||          sdS |t          u r|                    ||          sdS |dS |                     |          \  }}|t          j        j        k    rdS dS )N)r   zInvalid option name.)r   zInvalid option value.)   zUnknown option type.)   z Failed to validate option value.)r   zCheck option succeeded.)getOptionTyper;   r<   r   rG   kOkrB   rD   rA   kIntintkDoublefloatkStringstrgetcheck_string_optioncheck_double_optioncheck_int_optiongetOptionValue)	
highs_instoptionvaluer   option_typerz   valid_typesexpected_typecurrent_values	            r   rC   rC   2  sC   $226::FK*,,K###)) 	 $

"E
"C	K  OOK66M..vu== 	/....vu== 	/..++FE:: 	/..((&55f==FM###44''r   )warningsr   numpyr'   scipy.optimize._highspy._coreoptimize_highspy_corer   scipy.optimize._highspyr   r;   scipy.optimizer   r   rC   r
   r   r   <module>r      s              * * * * * * * * * * * * : : : : : : * * * * * *f f fR	 (  (  (  (  (r   