ó
î&]\c           @   s  d  Z  d d l Z d d l j Z d d l m Z d d l m	 Z	 d d l
 m Z m Z m Z d „  Z e e e e e d „ Z d	 d
 „ Z d „  Z d e e e e e g  d „ Z d „  Z e e e e e e g  d d „ Z d „  Z e e e e e e g  d d d e e d „ Z d S(   s9   
Method agnostic utility functions for linear progamming
iÿÿÿÿN(   t   warni   (   t   OptimizeWarning(   t   _remove_redundancyt   _remove_redundancy_sparset   _remove_redundancy_densec         C   s¸   |  j  d t ƒ } | r6 | d k	 r6 t j | ƒ } n  | rZ | d k	 rZ t j | ƒ } n  |  j d t ƒ } | r« t j | ƒ s‘ t j | ƒ r« t |  d <t d t	 ƒ n  |  | | f S(   s«  
    Check the provided ``A_ub`` and ``A_eq`` matrices conform to the specified
    optional sparsity variables.

    Parameters
    ----------
    A_ub : 2D array, optional
        2D array such that ``A_ub @ x`` gives the values of the upper-bound
        inequality constraints at ``x``.
    A_eq : 2D array, optional
        2D 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')`.

    Returns
    -------
    A_ub : 2D array, optional
        2D array such that ``A_ub @ x`` gives the values of the upper-bound
        inequality constraints at ``x``.
    A_eq : 2D array, optional
        2D 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')`.
    t   _sparse_presolvet   sparses9   Sparse constraint matrix detected; setting 'sparse':True.N(
   t   popt   Falset   Nonet   spst
   coo_matrixt   gett   issparset   TrueR    R   (   t   optionst   A_ubt   A_eqR   R   (    (    s;   lib/python2.7/site-packages/scipy/optimize/_linprog_util.pyt   _check_sparse_inputs   s    ,%

c         C   sÖ	  yÙ |  d  k r t ‚ n  y% t j |  d t ƒj ƒ  j ƒ  }  Wn t k
 rV t ‚ n X|  j d k rx |  j	 d ƒ }  n  t
 |  ƒ } | d k s¥ t
 |  j ƒ d k r´ t d ƒ ‚ n  t j |  ƒ j ƒ  sØ t d ƒ ‚ n  Wn t k
 rø t d ƒ ‚ n Xyny© t j | ƒ st j | ƒ rb| d  k rDt j d | f d t ƒn t j | d t ƒj ƒ  } nB | d  k r‰t j d | f d t ƒn t j | d t ƒj ƒ  } Wn t k
 r¾t ‚ n X| j d } t
 | j ƒ d k sú| j d t
 |  ƒ k r	t d	 ƒ ‚ n  t j | ƒ r1t j | j ƒ j ƒ  sWt j | ƒ rft j | ƒ j ƒ  rft d
 ƒ ‚ n  Wn t k
 r†t d ƒ ‚ n Xyî yF | d  k r®t j g  d t ƒn t j | d t ƒj ƒ  j ƒ  } Wn t k
 rét ‚ n X| j d k r| j	 d ƒ } n  t
 | j ƒ d k r/t d ƒ ‚ n  t
 | ƒ | k rPt d ƒ ‚ n  t j | ƒ j ƒ  stt d ƒ ‚ n  Wn t k
 r”t d ƒ ‚ n Xyny© t j | ƒ s¹t j | ƒ rþ| d  k ràt j d | f d t ƒn t j | d t ƒj ƒ  } nB | d  k r%t j d | f d t ƒn t j | d t ƒj ƒ  } Wn t k
 rZt ‚ n X| j d } t
 | j ƒ d k s–| j d t
 |  ƒ k r¥t d ƒ ‚ n  t j | ƒ rÍt j | j ƒ j ƒ  sót j | ƒ rt j | ƒ j ƒ  rt d ƒ ‚ n  Wn t k
 r"t d ƒ ‚ n Xyî yF | d  k rJt j g  d t ƒn t j | d t ƒj ƒ  j ƒ  } Wn t k
 r…t ‚ n X| j d k r§| j	 d ƒ } n  t
 | j ƒ d k rËt d ƒ ‚ n  t
 | ƒ | k rìt d ƒ ‚ n  t j | ƒ j ƒ  st d ƒ ‚ n  Wn t k
 r0t d ƒ ‚ n Xy4t | t ƒ rLt ‚ n  | d  k sjt
 | ƒ d k rzd! g | } n_t
 | ƒ d k rÇ| d }	 t
 |	 ƒ d k r·t d ƒ ‚ n  |	 g | } nt
 | ƒ | k rwy t
 | d ƒ Wn, t k
 r| d | d f g | } n Xx¼ t | ƒ D]I \ }
 }	 t
 |	 ƒ d k r't d t |
 ƒ d t |	 ƒ d ƒ ‚ q'q'Wnb t
 | ƒ d k rÍt j | d ƒ rÍt j | d ƒ rÍ| d | d f g | } n t d ƒ ‚ g  } x|t | ƒ D]n\ }
 }	 |	 d d  k	 rW|	 d d  k	 rW|	 d |	 d k rWt d t |
 ƒ d t |	 ƒ d ƒ ‚ n  |	 d t j k r•t d t |
 ƒ d t |	 ƒ d ƒ ‚ n  |	 d t j k rÔt d t |
 ƒ d t |	 ƒ d ƒ ‚ n  |	 d d  k	 r	|	 d t j k r	t |	 d ƒ n d  } |	 d d  k	 rA	|	 d t j k rA	t |	 d ƒ n d  } | j | | f ƒ qìW| } WnX t k
 rœ	} d | j d k r“	t ‚ qÀ	| ‚ n$ t k
 r¿	} | GHt d ƒ ‚ n X|  | | | | | f S("   sÃ  
    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
    ----------
    c : 1D array
        Coefficients of the linear objective function to be minimized.
    A_ub : 2D array, optional
        2D array such that ``A_ub @ x`` gives the values of the upper-bound
        inequality constraints at ``x``.
    b_ub : 1D array, optional
        1D array of values representing the upper-bound of each inequality
        constraint (row) in ``A_ub``.
    A_eq : 2D array, optional
        2D array such that ``A_eq @ x`` gives the values of the equality
        constraints at ``x``.
    b_eq : 1D array, optional
        1D array of values representing the RHS of each equality constraint
        (row) in ``A_eq``.
    bounds : sequence, optional
        ``(min, max)`` pairs for each element in ``x``, defining
        the bounds on that parameter. Use None for one of ``min`` or
        ``max`` when there is no bound in that direction. By default
        bounds are ``(0, None)`` (non-negative).
        If a sequence containing a single tuple is provided, then ``min`` and
        ``max`` will be applied to all variables in the problem.

    Returns
    -------
    c : 1D array
        Coefficients of the linear objective function to be minimized.
    A_ub : 2D array, optional
        2D array such that ``A_ub @ x`` gives the values of the upper-bound
        inequality constraints at ``x``.
    b_ub : 1D array, optional
        1D array of values representing the upper-bound of each inequality
        constraint (row) in ``A_ub``.
    A_eq : 2D array, optional
        2D array such that ``A_eq @ x`` gives the values of the equality
        constraints at ``x``.
    b_eq : 1D array, optional
        1D array of values representing the RHS of each equality constraint
        (row) in ``A_eq``.
    bounds : sequence of tuples
        ``(min, max)`` pairs for each element in ``x``, defining
        the bounds on that parameter. Use None for each of ``min`` or
        ``max`` when there is no bound in that direction. By default
        bounds are ``(0, None)`` (non-negative).

    t   dtypei   iÿÿÿÿi    si   Invalid input for linprog: c should be a 1D array; it must not have more than one non-singleton dimensionsF   Invalid input for linprog: c must not contain values inf, nan, or NonesI   Invalid input for linprog: c must be a 1D array of numerical coefficientsi   sƒ   Invalid 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 c sI   Invalid input for linprog: A_ub must not contain values inf, nan, or Nones|   Invalid input for linprog: A_ub must be a numerical 2D array with each row representing an upper bound inequality constraintsl   Invalid input for linprog: b_ub should be a 1D array; it must not have more than one non-singleton dimensionsc   Invalid input for linprog: The number of rows in A_ub must be equal to the number of values in b_ubsI   Invalid input for linprog: b_ub must not contain values inf, nan, or Nones“   Invalid input for linprog: b_ub must be a 1D array of numerical values, each representing the upper bound of an inequality constraint (row) in A_ubsƒ   Invalid 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 c sI   Invalid input for linprog: A_eq must not contain values inf, nan, or Nonesd   Invalid input for linprog: A_eq must be a 2D array with each row representing an equality constraintsl   Invalid input for linprog: b_eq should be a 1D array; it must not have more than one non-singleton dimensionsc   Invalid input for linprog: the number of rows in A_eq must be equal to the number of values in b_eqsI   Invalid input for linprog: b_eq must not contain values inf, nan, or Nones–   Invalid input for linprog: b_eq must be a 1D array of numerical values, each representing the right hand side of an equality constraints (row) in A_eqsn   Invalid input for linprog: exactly one lower bound and one upper bound must be specified for each element of xs!   Invalid input for linprog, bound t    sU   : exactly one lower bound and one upper bound must be specified for each element of xsK   : a lower bound must be less than or equal to the corresponding upper bounds%   : infinity is not a valid lower bounds.   : negative infinity is not a valid upper bounds!   could not convert string to floatsq   Invalid input for linprog: bounds must be a sequence of (min,max) pairs, each defining bounds on an element of x N(   i    N(   R	   t	   TypeErrort   npt   asarrayt   floatt   copyt   squeezet   BaseExceptiont   sizet   reshapet   lent   shapet
   ValueErrort   isfinitet   allR
   R   R   t   zerost   datat   arrayt
   isinstancet   strt	   enumeratet   isrealt   inft   appendt   args(   t   cR   t   b_ubR   t   b_eqt   boundst   n_xt   n_ubt   n_eqt   bt   it   clean_boundst   lbt   ubt   e(    (    s;   lib/python2.7/site-packages/scipy/optimize/_linprog_util.pyt   _clean_inputsH   s0   6	%
!--
.(&'
--
.(&'
	
%	4:9
		g•Ö&è.>c   ,      C   sn  g  } d }	 t  }
 t j |  j ƒ } d } d } t j | ƒ } | d d … d f } | d d … d f } t j | t j | d ƒ <t j | t j | d ƒ <| j t	 ƒ } | j t	 ƒ } | j t	 ƒ } | j \ } } | j \ } } t
 j | ƒ r%| j ƒ  } | j ƒ  } d „  } t
 j } n t j } t j } t j t j | d k d d ƒd k ƒ j ƒ  } t j | ƒ rt j t j | t j | ƒ | k ƒ ƒ rÚd } d } t }
 |  |	 | | | | | | | |
 | | f S| t j | ƒ d d … f } | t j | ƒ } n  t j t j | d k d d ƒd k ƒ j ƒ  } t j | ƒ rßt j t j | | | k  ƒ ƒ rªd } d	 } t }
 |  |	 | | | | | | | |
 | | f S| t j | ƒ d d … f } | t j | ƒ } n  | | | f ƒ } | j d d k rQt j t j | d k d d ƒd k ƒ j ƒ  } | t j | |  d k  ƒ | t j | |  d k  ƒ <| t j | |  d k ƒ | t j | |  d k ƒ <t j t j | ƒ ƒ rêd
 } d } t }
 |  |	 | | | | | | | |
 | | f S| t j | |  d k  ƒ | t j | |  d k  ƒ <| t j | |  d k ƒ | t j | |  d k ƒ <n  t j t j | d k d d ƒd k ƒ j ƒ  } | | ƒ d } | | | d d … f ƒ d } t | ƒ d k rªx¯ t | | ƒ D]ž \ } } | | | | | f } | | | | k o| | | k n s]d } d } t }
 |  |	 | | | | | | | |
 | | f S| | | <| | | <qÓW| t j | ƒ d d … f } | t j | ƒ } n  t j t j | d k d d ƒd k ƒ j ƒ  } | | | d d … f ƒ d } | | ƒ d } t | ƒ d k rPxü t | | ƒ D]ë \ } } | | | | | f } | | | f d k r£| | | | k  rƒt }
 qÝ| | | k  rÝ| | | <qÝn: | | | | k rÀt }
 n | | | k rÝ| | | <n  |
 r,d } d } |  |	 | | | | | | | |
 | | f Sq,W| t j | ƒ d d … f } | t j | ƒ } n  t j | | ƒ | k  } t j | ƒ } t j | ƒ r0| | j | ƒ }  | | j | ƒ }! | j d k rÑt j |! d k  ƒ só| j d k r0t j |  d ƒ r0d } d } t }
 |  |	 | | | | | | | |
 | | f Sn  | }" | }# t j | ƒ r(	|	 |  | j | | ƒ 7}	 | | d d … | f j | | ƒ } | | d d … | f j | | ƒ } |  | }  | | } | d d … | f } | d d … | f } t j | ƒ d | | g } | | }# | | }" n  | j d k rŒ
| j d k rŒ
t j g  ƒ } t j g  ƒ } |  j d k r‚	d } d } np t j t j |  d k  |" t j k ƒ ƒ s×	t j t j |  d k |# t j k ƒ ƒ ræ	d
 } d } n d } d } t }
 |" |  d k  | |  d k  <|# |  d k | |  d k <|" |  d k }$ |" |  d k t j |$ ƒ |$ t j |$ ƒ <d |$ t j |$ ƒ <|$ | |  d k <n  d | t j | t j ƒ <d | t j | t j ƒ <t j | d d … t j f | d d … t j f f ƒ } | j  ƒ  } xZ t! | ƒ D]L \ }% } x= t! | ƒ D]/ \ }& } t" | ƒ d k r,d | |% |& <q,q,WqW| j d }' d }( t
 j | ƒ r| rð| j d k rðt# | | ƒ \ } } } } | j d |' k  rØt$ |( t% ƒ n  | d k rðt }
 qðn  |  |	 | | | | | | | |
 | | f Sd }) | rf| j d k rfy t j& j' | ƒ }* Wqft( k
 rbd }* qfXn  | rF| j d k rF|* | j d k  rFt$ |( t% ƒ | j d |* }+ |+ |) k rÖt) | | ƒ \ } } } } n  |+ |) k sî| d k rt* | | ƒ \ } } } } n  | j d |* k  r.d } d } n  | d k rFt }
 qFn  |  |	 | | | | | | | |
 | | f S(   sö  
    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
    ----------
    c : 1D array
        Coefficients of the linear objective function to be minimized.
    A_ub : 2D array, optional
        2D array such that ``A_ub @ x`` gives the values of the upper-bound
        inequality constraints at ``x``.
    b_ub : 1D array, optional
        1D array of values representing the upper-bound of each inequality
        constraint (row) in ``A_ub``.
    A_eq : 2D array, optional
        2D array such that ``A_eq @ x`` gives the values of the equality
        constraints at ``x``.
    b_eq : 1D array, optional
        1D array of values representing the RHS of each equality constraint
        (row) in ``A_eq``.
    bounds : sequence of tuples
        ``(min, max)`` pairs for each element in ``x``, defining
        the bounds on that parameter. Use None for each of ``min`` or
        ``max`` when there is no bound in that direction.
    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).
    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
    -------
    c : 1D array
        Coefficients of the linear objective function to be minimized.
    c0 : 1D array
        Constant term in objective function due to fixed (and eliminated)
        variables.
    A_ub : 2D array, optional
        2D array such that ``A_ub @ x`` gives the values of the upper-bound
        inequality constraints at ``x``.
    b_ub : 1D array, optional
        1D array of values representing the upper-bound of each inequality
        constraint (row) in ``A_ub``.
    A_eq : 2D array, optional
        2D array such that ``A_eq @ x`` gives the values of the equality
        constraints at ``x``.
    b_eq : 1D array, optional
        1D array of values representing the RHS of each equality constraint
        (row) in ``A_eq``.
    bounds : sequence of tuples
        ``(min, max)`` pairs for each element in ``x``, defining
        the bounds on that parameter. Use None for each of ``min`` or
        ``max`` when there is no bound in that direction. Bounds have been
        tightened where possible.
    x : 1D array
        Solution vector (when the solution is trivial and can be determined
        in presolve)
    undo: list of tuples
        (index, value) pairs that record the original index and fixed value
        for each variable removed from the problem
    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.

    i    t    Ni   c         S   s
   |  j  ƒ  S(   N(   t   nonzero(   t   A(    (    s;   lib/python2.7/site-packages/scipy/optimize/_linprog_util.pyt   whereÖ  s    t   axisi   sŒ   The problem is (trivially) infeasible due to a row of zeros in the equality constraint matrix with a nonzero corresponding constraint value.s   The problem is (trivially) infeasible due to a row of zeros in the equality constraint matrix with a nonzero corresponding  constraint value.i   s®   If 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.sz   The problem is (trivially) infeasible because a singleton row in the equality constraints is inconsistent with the bounds.s}   The problem is (trivially) infeasible because a singleton row in the upper bound constraints is inconsistent with the bounds.sv   The problem is (trivially) infeasible because the bounds fix all variables to values inconsistent with the constraintssP   The solution was determined in presolve as there are no non-trivial constraints.s  The 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. t   nans†   A_eq does not appear to be of full row rank. To improve performance, check the problem formulation for redundant equality constraints.i   i   sþ   Due 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   R#   R   R%   R*   t   equalR	   t   astypeR   R
   R   t   tolilt   vstackR>   t   sumt   flattent   anyt   logical_andt   absR   t   logical_nott   isinfR   t   zipR"   t   dotR   t   allcloseR<   t   hstackt   newaxist   tolistR(   R'   R   R    R   t   linalgt   matrix_rankt	   ExceptionR   R   (,   R-   R   R.   R   R/   R0   t   rrt   tolt   undot   c0t   completet   xt   statust   messageR7   R8   t   m_eqt   nt   m_ubR>   RD   t   zero_rowR=   t   zero_colt   singleton_rowt   rowst   colst   rowt   colt   valt   i_ft   i_nft   residualt   slackt   ub_modt   lb_modt   x_zero_cR5   t   jt   n_rows_At   redundancy_warningt   small_nullspacet   rankt   dim_row_nullspace(    (    s;   lib/python2.7/site-packages/scipy/optimize/_linprog_util.pyt	   _presolveT  sr   f			00"0///20 ,
0 		$"''


	*+	*;(	c         C   sŒ   | d k r i  } n  d „  | j ƒ  Dƒ } t | | | ƒ \ } } } t |  | | | | | ƒ \ }  } } } } } |  | | | | | | f S(   sz  
    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
    ----------
    c : 1D array
        Coefficients of the linear objective function to be minimized.
    A_ub : 2D array, optional
        2D array such that ``A_ub @ x`` gives the values of the upper-bound
        inequality constraints at ``x``.
    b_ub : 1D array, optional
        1D array of values representing the upper-bound of each inequality
        constraint (row) in ``A_ub``.
    A_eq : 2D array, optional
        2D array such that ``A_eq @ x`` gives the values of the equality
        constraints at ``x``.
    b_eq : 1D array, optional
        1D array of values representing the RHS of each equality constraint
        (row) in ``A_eq``.
    bounds : sequence
        ``(min, max)`` pairs for each element in ``x``, defining
        the bounds on that parameter. Use None for one of ``min`` or
        ``max`` when there is no bound in that direction. By default
        bounds are ``(0, None)`` (non-negative). If a sequence containing a
        single tuple is provided, then ``min`` and ``max`` will be applied to
        all variables in the problem.
    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
    -------
    c : 1D array
        Coefficients of the linear objective function to be minimized.
    A_ub : 2D array, optional
        2D array such that ``A_ub @ x`` gives the values of the upper-bound
        inequality constraints at ``x``.
    b_ub : 1D array, optional
        1D array of values representing the upper-bound of each inequality
        constraint (row) in ``A_ub``.
    A_eq : 2D array, optional
        2D array such that ``A_eq @ x`` gives the values of the equality
        constraints at ``x``.
    b_eq : 1D array, optional
        1D array of values representing the RHS of each equality constraint
        (row) in ``A_eq``.
    bounds : sequence, optional
        ``(min, max)`` pairs for each element in ``x``, defining
        the bounds on that parameter. Use None for one of ``min`` or
        ``max`` when there is no bound in that direction. By default
        bounds are ``(0, None)`` (non-negative).
        If a sequence containing a single tuple is provided, then ``min`` and
        ``max`` will be applied to all variables in the problem.
    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')`.

    c         S   s   i  |  ] \ } } | | “ q S(    (    (   t   .0t   kt   v(    (    s;   lib/python2.7/site-packages/scipy/optimize/_linprog_util.pys
   <dictcomp>)  s   	 N(   R	   t   itemsR   R:   (   R-   R   R.   R   R/   R0   R   t   solver_options(    (    s;   lib/python2.7/site-packages/scipy/optimize/_linprog_util.pyt   _parse_linprogÔ  s    R	*i    c   %      C   sb  t  j | ƒ rZ t } t  j | ƒ } t  j | ƒ } d „  }	 d „  }
 t  j } t  j } n* t } t j }	 t j }
 t j	 } t j } t
 ƒ  } t | ƒ d k r² t
 | d ƒ } n  g  t t | ƒ ƒ D] } | | k rÅ | | ^ qÅ } t j | ƒ } | d d … d f } | d d … d f } | j \ } } t j | d ƒ } t j | d ƒ } t j | ƒ } t j | ƒ } t j | | ƒ } t j | ƒ d } | | | | | | <| | <t j | d ƒ } t j | d ƒ } t j | ƒ } t j | ƒ } |  | c d 9<t | ƒ d k r| j d d k rL| d d … | f c d 9<n  | j d d k r| d d … | f c d 9<qn  t j | ƒ d } | | } t j | ƒ } |
 | | | | j d f ƒ f ƒ } t j | t j	 | ƒ f ƒ } d | t | | j d ƒ | f <| | | )|
 | | f ƒ } t j | | f ƒ } t j |  t j	 | j d f ƒ f ƒ }  t j | | ƒ } t j | ƒ d } t | ƒ }  |	 | | | j d |  f ƒ f ƒ } t j |  t j	 |  ƒ f ƒ }  | d d … | f | d d … t | | j d ƒ f <|  | |  t j | | j d ƒ <|
 | | j d ƒ | | j d | j d f ƒ g ƒ }! |	 | |! g ƒ }" t j | ƒ d }# | | j t ƒ }$ | t j |$ |  |# ƒ 7} | r(| j d d ƒ } |" j ƒ  }" | |" d d … |# f t  j |$ ƒ j d d ƒ 8} | j ƒ  } n* | |" d d … |# f |$ j d d ƒ 8} |" | |  | f S(   s	  
    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
    ----------
    c : 1D array
        Coefficients of the linear objective function to be minimized.
        Components corresponding with fixed variables have been eliminated.
    c0 : float
        Constant term in objective function due to fixed (and eliminated)
        variables.
    A_ub : 2D array, optional
        2D array such that ``A_ub @ x`` gives the values of the upper-bound
        inequality constraints at ``x``.
    b_ub : 1D array, optional
        1D array of values representing the upper-bound of each inequality
        constraint (row) in ``A_ub``.
    A_eq : 2D array, optional
        2D array such that ``A_eq @ x`` gives the values of the equality
        constraints at ``x``.
    b_eq : 1D array, optional
        1D array of values representing the RHS of each equality constraint
        (row) in ``A_eq``.
    bounds : sequence of tuples
        ``(min, max)`` pairs for each element in ``x``, defining
        the bounds on that parameter. Use None for each of ``min`` or
        ``max`` when there is no bound in that direction. Bounds have been
        tightened where possible.
    undo: list of tuples
        (`index`, `value`) pairs that record the original index and fixed value
        for each variable removed from the problem

    Returns
    -------
    A : 2D array
        2D array such that ``A`` @ ``x``, gives the values of the equality
        constraints at ``x``.
    b : 1D array
        1D array of values representing the RHS of each equality constraint
        (row) in A (for standard form problem).
    c : 1D 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.

    References
    ----------
    .. [9] Bertsimas, Dimitris, and J. Tsitsiklis. "Introduction to linear
           programming." Athena Scientific 1 (1997): 997.

    c         S   s   t  j |  d d ƒS(   Nt   formatt   lil(   R
   RO   (   t   blocks(    (    s;   lib/python2.7/site-packages/scipy/optimize/_linprog_util.pyRO   ‡  s    c         S   s   t  j |  d d ƒS(   NR|   R}   (   R
   RD   (   R~   (    (    s;   lib/python2.7/site-packages/scipy/optimize/_linprog_util.pyRD   Š  s    i    Ni   iÿÿÿÿR?   (   R
   R   R   t
   lil_matrixt   eyeR   R   RO   RD   R#   t   setR   t   rangeR%   R   RA   R	   RJ   RH   R<   t   count_nonzerot   concatenatet   arangeRB   R   RE   R   t   tocsct   diagst   ravel(%   R-   RX   R   R.   R   R/   R0   RW   R   RO   RD   R#   R€   t   fixed_xR5   t   lbst   ubsR_   R2   t   lb_nonet   ub_nonet   lb_somet   ub_somet   l_nolb_someubt   i_nolbt   i_newubt   ub_newubt   n_boundst   A1R4   t   l_freet   i_freet   n_freet   A2R=   t   i_shiftt   lb_shift(    (    s;   lib/python2.7/site-packages/scipy/optimize/_linprog_util.pyt   _get_Abc1  s†    Q								5"
% 
(%7"93*c         C   s4   |  GH| d k r" d j  | ƒ GHn  d j  | ƒ GHd S(   s›  
    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.
    i    i   s,            Current function value: {0: <12.6f}s            Iterations: {0:d}N(   i    i   (   R|   (   R\   R[   t   funt	   iteration(    (    s;   lib/python2.7/site-packages/scipy/optimize/_linprog_util.pyt   _display_summaryë  s    g:Œ0âŽyE>c
         C   s'  t  | ƒ }
 t ƒ  } t  | ƒ d k r t | d ƒ } |  j ƒ  }  x5 t | d | d ƒ D] \ } } |  j | | ƒ q[ Wt j |  ƒ }  n  | rX| d k	 rXd } x¯ t | ƒ D]ž \ } } | | k rÑ q³ n  | \ } } | d k r | d k r | d 7} |  | |  |
 | d |  | <q³ | d k rA| |  | |  | <q³ |  | c | 7<q³ Wn  t  | ƒ }
 |  |
  }  |  j	 | ƒ } | | j	 |  ƒ } | | j	 |  ƒ } t j | ƒ } | d d … d f } | d d … d f } t j
 | t j | d ƒ <t j
 | t j | d ƒ <|  | | | | | f S(   s  
    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 : 1D array
        Solution vector to the standard-form problem.
    c : 1D array
        Original coefficients of the linear objective function to be minimized.
    A_ub : 2D array, optional
        2D array such that ``A_ub @ x`` gives the values of the upper-bound
        inequality constraints at ``x``.
    b_ub : 1D array, optional
        1D array of values representing the upper-bound of each inequality
        constraint (row) in ``A_ub``.
    A_eq : 2D array, optional
        2D array such that ``A_eq @ x`` gives the values of the equality
        constraints at ``x``.
    b_eq : 1D array, optional
        1D array of values representing the RHS of each equality constraint
        (row) in ``A_eq``.
    bounds : sequence of tuples
        Bounds, as modified in presolve
    complete : bool
        Whether the solution is was determined in presolve (``True`` if so)
    undo: list of tuples
        (`index`, `value`) pairs that record the original index and fixed value
        for each variable removed from the problem
    tol : float
        Termination tolerance; see [1]_ Section 4.5.

    Returns
    -------
    x : 1D array
        Solution vector to original linear programming problem
    fun: float
        optimal objective value for original problem
    slack : 1D array
        The (non-negative) slack in the upper bound constraints, that is,
        ``b_ub - A_ub @ x``
    con : 1D array
        The (nominally zero) residuals of the equality constraints, that is,
        ``b - A_eq @ x``
    lb : 1D array
        The lower bound constraints on the original variables
    ub: 1D array
        The upper bound constraints on the original variables
    i    i   N(   R   R   RQ   RL   t   insertR   R%   R	   R(   RM   R*   RA   (   RZ   R-   R   R.   R   R/   R0   RY   RW   RV   R1   t	   no_adjustR5   Rg   t   n_unboundedR4   R7   R8   R   Rk   t   con(    (    s;   lib/python2.7/site-packages/scipy/optimize/_linprog_util.pyt
   _postsolve  s>    =	$
!
c	         C   sc  t  j | ƒ d } t  j |  ƒ j ƒ  p^ t  j | ƒ p^ t  j | ƒ j ƒ  p^ t  j | ƒ j ƒ  }	 |	 rp t }
 n… |  | | k  j ƒ  p™ |  | | k j ƒ  } | d k o¸ | | k  j ƒ  } | d k oß t  j | ƒ | k j ƒ  } | pñ | pñ | }
 | d k r|
 rd } d } nB | d k r8|	 r8d } d } n! | d k rY|
 rYt | ƒ ‚ n  | | f S(   s(  
    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 : 1D 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 : 1D array
        The (non-negative) slack in the upper bound constraints, that is,
        ``b_ub - A_ub @ x``
    con : 1D array
        The (nominally zero) residuals of the equality constraints, that is,
        ``b - A_eq @ x``
    lb : 1D array
        The lower bound constraints on the original variables
    ub: 1D array
        The upper bound constraints on the original variables
    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.
    i
   i   i    i   sS  The solution does not satisfy the constraints, yet no errors were raised and there is no certificate of infeasibility or unboundedness. This is known to occur if the `presolve` option is False and the problem is infeasible. If you encounter this under different circumstances, please submit a bug report. Otherwise, please enable presolve.sc  Numerical difficulties were encountered but no errors were raised. This is known to occur if the 'presolve' option is False, 'sparse' is True, and A_eq includes redundant rows. If you encounter this under different circumstances, please submit a bug report. Otherwise, remove linearly dependent equations from your equality constraints or enable presolve.i   (   R   t   sqrtt   isnanRG   R   RI   R    (   RZ   R   R[   Rk   R£   R7   R8   RV   R\   t   contains_nanst   is_feasiblet   invalid_boundst   invalid_slackt   invalid_con(    (    s;   lib/python2.7/site-packages/scipy/optimize/_linprog_util.pyt   _check_resultv  s(    6	,'		R;   c         C   s•   t  |  | | | | | | | | | ƒ
 \ }  } } } } } t |  | |	 | | | | | |
 ƒ	 \ }	 }
 | r t |
 |	 | | ƒ n  |  | | | |	 |
 f S(   sÓ
  
    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 : 1D array
        Solution vector to the standard-form problem.
    c : 1D array
        Original coefficients of the linear objective function to be minimized.
    A_ub : 2D array, optional
        2D array such that ``A_ub @ x`` gives the values of the upper-bound
        inequality constraints at ``x``.
    b_ub : 1D array, optional
        1D array of values representing the upper-bound of each inequality
        constraint (row) in ``A_ub``.
    A_eq : 2D array, optional
        2D array such that ``A_eq @ x`` gives the values of the equality
        constraints at ``x``.
    b_eq : 1D array, optional
        1D array of values representing the RHS of each equality constraint
        (row) in ``A_eq``.
    bounds : sequence of tuples
        Bounds, as modified in presolve
    complete : bool
        Whether the solution is was determined in presolve (``True`` if so)
    undo: list of tuples
        (`index`, `value`) pairs that record the original index and fixed value
        for each variable removed from the 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

    message : str
        A string descriptor of the exit status of the optimization.
    tol : float
        Termination tolerance; see [1]_ Section 4.5.

    Returns
    -------
    x : 1D array
        Solution vector to original linear programming problem
    fun: float
        optimal objective value for original problem
    slack : 1D array
        The (non-negative) slack in the upper bound constraints, that is,
        ``b_ub - A_ub @ x``
    con : 1D array
        The (nominally zero) residuals of the equality constraints, that is,
        ``b - A_eq @ x``
    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.

    (   R¤   R¬   RŸ   (   RZ   R-   R   R.   R   R/   R0   RY   RW   R[   R\   RV   Rž   t   dispR   Rk   R£   R7   R8   (    (    s;   lib/python2.7/site-packages/scipy/optimize/_linprog_util.pyt   _postprocessØ  s    K$(   t   __doc__t   numpyR   t   scipy.sparseR   R
   t   warningsR    t   optimizeR   t!   scipy.optimize._remove_redundancyR   R   R   R   R	   R:   Ru   R{   Rœ   RŸ   R   R¤   R¬   R®   (    (    (    s;   lib/python2.7/site-packages/scipy/optimize/_linprog_util.pyt   <module>   s(   	:ÿ ÿ 	]¹	n	b