o
    i,                     @   sX   d dl mZ d dlZd dlm  m  mZ d dl	m
Z d dlmZ dd Zdd ZdS )	    )warnN)_highs_options)OptimizeWarningc
           %      C   s  | j }
|j }|duot|dk}ddd}t }|
|_||_|
|j_||j_tjj	|j_
| |_||_||_||_||_||j_||j_||j_|j dkrVdd |D |_t }t }t }|	 D ]}\}}|du sr|dv rsqf||}d|krtdt||i td	d
 qf|dv rt|tr|rdnd}ntd| d| dtd	d
 qft|}t |||\}}|tjj!krt|tstd| d| dtd	d
 qf|dkrt|td	d
 qft"||| qf|#|}|tj$j%kr|&|' |(|' d |S |)|}|tj$j%krtj*j+}|&||(|d |S |, }|tj$j%kr:|&|' |(|' d |S |' }|- }|tj*j.tj*j/tj*j0tj*j1fvpf|tj*j/tj*j0tj*j1hv of|j2tj3k}|tj*j.k}|rs|sy|s|r|&|d|(| d|4|j5 |j6|j7|j8d |S |9 }|: } t;d	|
f}!| j<}"|j=}#t>|
D ]'}$|"|$ tj?j@kr|#|$ |!d|$f< q|"|$ tj?jAkr|#|$ |!d|$f< q|&||(|tB|jC||jD tB|jE|!|j2|j6|j7|j8d
 |r|&|jF|jG|jHd |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                 S   s   g | ]}t |qS  )_hZHighsVarType).0ir   r   `/home/kim/smarthome/.venv/lib/python3.10/site-packages/scipy/optimize/_highspy/_highs_wrapper.py
<listcomp>   s    z"_highs_wrapper.<locals>.<listcomp>)ZsensezUnrecognized options detected:    )
stacklevel)ZpresolveZ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   Zslacklambda	marg_bndsr   r   r   r   )mip_node_countmip_dual_boundmip_gap)Isizenpsumr   ZHighsLpZnum_col_Znum_row_Z	a_matrix_ZMatrixFormatZkColwiseZformat_Z	col_cost_Z
col_lower_Z
col_upper_Z
row_lower_Z
row_upper_Zstart_Zindex_Zvalue_Zintegrality_Z_HighsZHighsOptionshoptHighsOptionsManageritemsZget_option_typer   dictr   
isinstanceboolHighsOptionTypecheck_optionkBoolsetattrZpassOptionsHighsStatusZkErrorupdateZgetModelStatusZmodelStatusToStringZ	passModelZHighsModelStatusZkModelErrorrunZgetInfoZkOptimalZ
kTimeLimitZkIterationLimitZkSolutionLimitZobjective_function_valueZ	kHighsInfZsolutionStatusToStringZprimal_solution_statusZsimplex_iteration_countZipm_iteration_countZcrossover_iteration_countZgetSolutionZgetBasisZzerosZ
col_statusZcol_dualrangeZHighsBasisStatusZkLowerZkUpperarrayZ	col_valueZ	row_valueZrow_dualr   r   r   )%cZindptrindicesdatalhsrhsZlbZubZintegralityoptionsZnumcolZnumrowZisMipresZlpZhighsZhighs_optionshoptmanagerkeyvalZopt_typer   msgZ
opt_statusZinit_statusZerr_model_statusZ
run_statusZmodel_statusinfoZmipFailConditionZlpFailConditionZsolutionZbasisr   Zbasis_col_statusZsolution_col_dualiir   r   r   _highs_wrapper	   s$  a










r<   c           	      C   s   |  |\}}t }|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 r7|||s7dS |tu rC|||sCdS |t
u rO|||sOdS |d u rUdS | |\}}|tjjkrdd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.)ZgetOptionTyper    r!   r   r*   ZkOkr&   r(   r%   ZkIntintZkDoublefloatZkStringstrgetZcheck_string_optionZcheck_double_optionZcheck_int_optionZgetOptionValue)	Z
highs_instoptionvaluer   Zoption_typer6   Zvalid_typesexpected_typecurrent_valuer   r   r   r'   2  s2   r'   )warningsr   numpyr   Zscipy.optimize._highspy._coreoptimizeZ_highspyZ_corer   Zscipy.optimize._highspyr   r    Zscipy.optimizer   r<   r'   r   r   r   r   <module>   s      +