o
    h+                     @   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}!t<|
D ]+}"| j=|" tj>j?kr|j@|" |!d|"f< q| j=|" tj>jAkr|j@|" |!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  )_hHighsVarType).0ir   r   Z/var/www/vscode/kcb/lib/python3.10/site-packages/scipy/optimize/_highspy/_highs_wrapper.py
<listcomp>   s    z"_highs_wrapper.<locals>.<listcomp>)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range
col_statusHighsBasisStatuskLowercol_dual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   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.)getOptionTyper7   r8   r   rC   kOkr>   r@   r=   kIntintkDoublefloatkStringstrgetcheck_string_optioncheck_double_optioncheck_int_optiongetOptionValue)	
highs_instoptionvaluer   option_typerv   valid_typesexpected_typecurrent_valuer   r   r   r?   0  s2   r?   )warningsr   numpyr#   scipy.optimize._highspy._coreoptimize_highspy_corer   scipy.optimize._highspyr   r7   scipy.optimizer   r   r?   r   r   r   r   <module>   s      )