ó
î&]\c           @` sT  d  Z  d d l m Z m Z m Z d d g Z d d l Z d d l m	 Z	 d d l m
 Z
 m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z d d	 l m Z m Z m Z d
 Z e e e ƒ j ƒ Z d „  Z f  e  f  e  f  e  e  e  f  d d d e  d e e  d „ Z! f  e  e  f  d d d e" e e  d „
 Z# e$ d k rPd d d d d g d „ Z% e e g d e g d g ƒ j& Z' d d g e' d d … d f <d d „ Z( d d „ Z) d d „ Z* d d „ Z+ i d d 6e( d 6e) d 6d f d 6i d  d 6e* d 6e+ d 6d f d 6f Z, e- d! j. d" d# ƒ ƒ e- d$ ƒ e! e% e d% d g ƒ d& e' d' d d( e/ ƒd  \ Z0 Z1 e- d) ƒ e# e% e d% d g ƒ d& e' i e/ d' 6Z2 e- d* j. d" d# ƒ ƒ e- d$ ƒ e! e% e d% d g ƒ d+ e( d, e) d- e* d. e+ d' d d( e/ ƒd  \ Z0 Z1 e- d) ƒ e# e% e d% d g ƒ d/ e, i e/ d' 6Z2 n  d S(0   s  
This module implements the Sequential Least SQuares Programming optimization
algorithm (SLSQP), originally developed by Dieter Kraft.
See http://www.netlib.org/toms/733

Functions
---------
.. autosummary::
   :toctree: generated/

    approx_jacobian
    fmin_slsqp

i    (   t   divisiont   print_functiont   absolute_importt   approx_jacobiant
   fmin_slsqpN(   t   slsqp(   t   zerost   arrayt   linalgt   appendt   asfarrayt   concatenatet   finfot   sqrtt   vstackt   expt   inft   isfinitet
   atleast_1di   (   t   wrap_functiont   OptimizeResultt   _check_unknown_optionss   restructuredtext enc   	      G` s³   t  |  ƒ } t | | f | Œ  ƒ } t t | ƒ t | ƒ g ƒ } t t | ƒ ƒ } xQ t t | ƒ ƒ D]= } | | | <| | | f | Œ  | | | | <d | | <qh W| j ƒ  S(   s“  
    Approximate the Jacobian matrix of a callable function.

    Parameters
    ----------
    x : array_like
        The state vector at which to compute the Jacobian matrix.
    func : callable f(x,*args)
        The vector-valued function.
    epsilon : float
        The perturbation used to determine the partial derivatives.
    args : sequence
        Additional arguments passed to func.

    Returns
    -------
    An array of dimensions ``(lenf, lenx)`` where ``lenf`` is the length
    of the outputs of `func`, and ``lenx`` is the number of elements in
    `x`.

    Notes
    -----
    The approximation is done using forward differences.

    g        (   R
   R   R   t   lent   ranget	   transpose(	   t   xt   funct   epsilont   argst   x0t   f0t   jact   dxt   i(    (    s3   lib/python2.7/site-packages/scipy/optimize/slsqp.pyR      s    
#id   gíµ ÷Æ°>c         ` sU  | d k	 r | } n  i | d 6| d 6| d 6| d k d 6| d 6| d 6} d } | t ‡  f d †  | Dƒ ƒ 7} | t ‡  f d	 †  | Dƒ ƒ 7} | rÃ | i d
 d 6| d 6| d 6ˆ  d 6f 7} n  | rõ | i d d 6| d 6|	 d 6ˆ  d 6f 7} n  t |  | ˆ  d | d | d | | } | rI| d | d | d | d | d f S| d Sd S(   s6  
    Minimize a function using Sequential Least SQuares Programming

    Python interface function for the SLSQP Optimization subroutine
    originally implemented by Dieter Kraft.

    Parameters
    ----------
    func : callable f(x,*args)
        Objective function.  Must return a scalar.
    x0 : 1-D ndarray of float
        Initial guess for the independent variable(s).
    eqcons : list, optional
        A list of functions of length n such that
        eqcons[j](x,*args) == 0.0 in a successfully optimized
        problem.
    f_eqcons : callable f(x,*args), optional
        Returns a 1-D array in which each element must equal 0.0 in a
        successfully optimized problem.  If f_eqcons is specified,
        eqcons is ignored.
    ieqcons : list, optional
        A list of functions of length n such that
        ieqcons[j](x,*args) >= 0.0 in a successfully optimized
        problem.
    f_ieqcons : callable f(x,*args), optional
        Returns a 1-D ndarray in which each element must be greater or
        equal to 0.0 in a successfully optimized problem.  If
        f_ieqcons is specified, ieqcons is ignored.
    bounds : list, optional
        A list of tuples specifying the lower and upper bound
        for each independent variable [(xl0, xu0),(xl1, xu1),...]
        Infinite values will be interpreted as large floating values.
    fprime : callable `f(x,*args)`, optional
        A function that evaluates the partial derivatives of func.
    fprime_eqcons : callable `f(x,*args)`, optional
        A function of the form `f(x, *args)` that returns the m by n
        array of equality constraint normals.  If not provided,
        the normals will be approximated. The array returned by
        fprime_eqcons should be sized as ( len(eqcons), len(x0) ).
    fprime_ieqcons : callable `f(x,*args)`, optional
        A function of the form `f(x, *args)` that returns the m by n
        array of inequality constraint normals.  If not provided,
        the normals will be approximated. The array returned by
        fprime_ieqcons should be sized as ( len(ieqcons), len(x0) ).
    args : sequence, optional
        Additional arguments passed to func and fprime.
    iter : int, optional
        The maximum number of iterations.
    acc : float, optional
        Requested accuracy.
    iprint : int, optional
        The verbosity of fmin_slsqp :

        * iprint <= 0 : Silent operation
        * iprint == 1 : Print summary upon completion (default)
        * iprint >= 2 : Print status of each iterate and summary
    disp : int, optional
        Over-rides the iprint interface (preferred).
    full_output : bool, optional
        If False, return only the minimizer of func (default).
        Otherwise, output final objective function and summary
        information.
    epsilon : float, optional
        The step size for finite-difference derivative estimates.
    callback : callable, optional
        Called after each iteration, as ``callback(x)``, where ``x`` is the
        current parameter vector.

    Returns
    -------
    out : ndarray of float
        The final minimizer of func.
    fx : ndarray of float, if full_output is true
        The final value of the objective function.
    its : int, if full_output is true
        The number of iterations.
    imode : int, if full_output is true
        The exit mode from the optimizer (see below).
    smode : string, if full_output is true
        Message describing the exit mode from the optimizer.

    See also
    --------
    minimize: Interface to minimization algorithms for multivariate
        functions. See the 'SLSQP' `method` in particular.

    Notes
    -----
    Exit modes are defined as follows ::

        -1 : Gradient evaluation required (g & a)
         0 : Optimization terminated successfully.
         1 : Function evaluation required (f & c)
         2 : More equality constraints than independent variables
         3 : More than 3*n iterations in LSQ subproblem
         4 : Inequality constraints incompatible
         5 : Singular matrix E in LSQ subproblem
         6 : Singular matrix C in LSQ subproblem
         7 : Rank-deficient equality constraint subproblem HFTI
         8 : Positive directional derivative for linesearch
         9 : Iteration limit exceeded

    Examples
    --------
    Examples are given :ref:`in the tutorial <tutorial-sqlsp>`.

    t   maxitert   ftolt   iprinti    t   dispt   epst   callbackc         3` s*   |  ]  } i d  d 6| d 6ˆ  d 6Vq d S(   t   eqt   typet   funR   N(    (   t   .0t   c(   R   (    s3   lib/python2.7/site-packages/scipy/optimize/slsqp.pys	   <genexpr>Ã   s    c         3` s*   |  ]  } i d  d 6| d 6ˆ  d 6Vq d S(   t   ineqR)   R*   R   N(    (   R+   R,   (   R   (    s3   lib/python2.7/site-packages/scipy/optimize/slsqp.pys	   <genexpr>Ä   s    R(   R)   R*   R   R   R-   t   boundst   constraintsR   t   nitt   statust   messageN(    (   t   Nonet   tuplet   _minimize_slsqp(   R   R   t   eqconst   f_eqconst   ieqconst	   f_ieqconsR.   t   fprimet   fprime_eqconst   fprime_ieqconsR   t   itert   accR$   R%   t   full_outputR   R'   t   optst   const   res(    (   R   s3   lib/python2.7/site-packages/scipy/optimize/slsqp.pyR   E   s,    p	

  'c   G   "   ` s)  t  | ƒ | } | } | } |
 ‰  |	 s1 d } n  t | t ƒ rL | f } n  i d@ d 6dA d 6} xAt | ƒ D]3\ } } y | d j ƒ  } WnY t k
 r° t d | ƒ ‚ n\ t k
 rÌ t d ƒ ‚ n@ t k
 rè t d ƒ ‚ n$ X| dB k rt d | d ƒ ‚ n  d	 | k r+t d
 | ƒ ‚ n  | j	 d ƒ } | d' k rh‡  f d †  } | | d	 ƒ } n  | | c i | d	 d	 6| d 6| j	 d dC ƒ d 6f 7<qm Wi d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d  6d! d" 6} t |  | ƒ \ } }  | r*t | | ƒ \ } } n t t |  ˆ  f ƒ \ } } t | ƒ j ƒ  } t t t g  | d D]# } t | d	 | | d Œ ƒ ^ qkƒ ƒ } t t t g  | d D]# } t | d	 | | d Œ ƒ ^ q®ƒ ƒ } | | } t d | g ƒ j ƒ  } t | ƒ } | d }  | | |  |  }! d |  | |  d |  | d |! d d |! |  |! |  | d | |  | d | d d | d | d |  d }" |! }# t |" ƒ }$ t |# ƒ }% | d' k sÝt | ƒ d k r*t j | d# t ƒ}& t j | d# t ƒ}' |& j t j ƒ |' j t j ƒ nt | t ƒ }( |( j d | k r[t d$ ƒ ‚ n  t j d% d& ƒ 1 |( d' d' … d f |( d' d' … d f k }) Wd' QX|) j ƒ  rÒt d( d) j d* „  |) Dƒ ƒ ƒ ‚ n  |( d' d' … d f |( d' d' … d f }& }' t  |( ƒ }* t j |& |* d' d' … d f <t j |' |* d' d' … d f <t j  |& ƒ }+ t j! | |+ |& |+ t j" ƒ | |+ <t j  |' ƒ }+ t j! | |+ t j" |' |+ ƒ | |+ <t d t# ƒ }, t | t ƒ } t | t# ƒ }- d }. t d t ƒ }/ t d t ƒ }0 t d t ƒ }1 t d t ƒ }2 t d t ƒ }3 t d t ƒ }4 t d t ƒ }5 t d t ƒ }6 t d t ƒ }7 t d t ƒ }8 t d t# ƒ }9 t d t# ƒ }: t d t# ƒ }; t d t# ƒ }< t d t# ƒ }= t d t# ƒ }  t d t# ƒ }> t d t# ƒ }? | d k rt$ d+ dD ƒ n  xE|, d k s&|, d k r,|  | ƒ }@ y t t j% |@ ƒ ƒ }@ Wn# t t f k
 rpt d0 ƒ ‚ n X| d r¸t& g  | d D]# } t | d	 | | d Œ ƒ ^ q‰ƒ }A n t d ƒ }A | d rt& g  | d D]# } t | d	 | | d Œ ƒ ^ qÜƒ }B n t d ƒ }B t& |A |B f ƒ } n  |, d k sD|, d k rV	t' | | ƒ d1 ƒ }C | d ršt( g  | d D] } | d | | d Œ ^ qqƒ }D n t | | f ƒ }D | d rít( g  | d D] } | d | | d Œ ^ qÄƒ }E n t | | f ƒ }E | d k r 	t | | f ƒ }F n t( |D |E f ƒ }F t& |F t | d g ƒ f d ƒ }F n  t) | | | |& |' |@ | |C |F | |- |, |$ |% |/ |0 |1 |2 |3 |4 |5 |6 |7 |8 |9 |: |; |< |= |  |> |? ƒ  | d' k	 rë	|- |. k rë	| t j* | ƒ ƒ n  | d k r-
|- |. k r-
t$ d2 |- | d |@ t+ j, |C ƒ f ƒ n  t- |, ƒ d k rC
Pn  t# |- ƒ }. qW| d k rÄ
t$ | t# |, ƒ d3 t. |, ƒ d4 ƒ t$ d5 |@ ƒ t$ d6 |- ƒ t$ d7 | d ƒ t$ d8 | d ƒ n  t/ d9 | d	 |@ d |C d  d: t# |- ƒ d; | d d< | d d= t# |, ƒ d> | t# |, ƒ d? |, d k ƒ 	S(E   së  
    Minimize a scalar function of one or more variables using Sequential
    Least SQuares Programming (SLSQP).

    Options
    -------
    ftol : float
        Precision goal for the value of f in the stopping criterion.
    eps : float
        Step size used for numerical approximation of the Jacobian.
    disp : bool
        Set to True to print convergence messages. If False,
        `verbosity` is ignored and set to 0.
    maxiter : int
        Maximum number of iterations.

    i    R(   R-   R)   s"   Constraint %d has no type defined.s/   Constraints must be defined using a dictionary.s#   Constraint's type must be a string.s   Unknown constraint type '%s'.R*   s&   Constraint %d has no function defined.R   c         ` s   ‡ ‡  f d †  } | S(   Nc         ` s   t  |  ˆ ˆ  | Œ S(   N(   R   (   R   R   (   R   R*   (    s3   lib/python2.7/site-packages/scipy/optimize/slsqp.pyt   cjac  s    (    (   R*   RC   (   R   (   R*   s3   lib/python2.7/site-packages/scipy/optimize/slsqp.pyt   cjac_factory  s    R   s$   Gradient evaluation required (g & a)iÿÿÿÿs%   Optimization terminated successfully.s$   Function evaluation required (f & c)i   s4   More equality constraints than independent variablesi   s*   More than 3*n iterations in LSQ subproblemi   s#   Inequality constraints incompatiblei   s#   Singular matrix E in LSQ subproblemi   s#   Singular matrix C in LSQ subproblemi   s2   Rank-deficient equality constraint subproblem HFTIi   s.   Positive directional derivative for linesearchi   s   Iteration limit exceededi	   t   dtypesD   SLSQP Error: the length of bounds is not compatible with that of x0.t   invalidt   ignoreNs"   SLSQP Error: lb > ub in bounds %s.s   , c         s` s   |  ] } t  | ƒ Vq d  S(   N(   t   str(   R+   t   b(    (    s3   lib/python2.7/site-packages/scipy/optimize/slsqp.pys	   <genexpr>]  s    s   %5s %5s %16s %16st   NITt   FCt   OBJFUNt   GNORMs'   Objective function must return a scalarg        s   %5i %5i % 16.6E % 16.6Es       (Exit mode t   )s#               Current function value:s               Iterations:s!               Function evaluations:s!               Gradient evaluations:R   R0   t   nfevt   njevR1   R2   t   success(    (    (   R(   R-   (    (   RJ   RK   RL   RM   (0   R   t
   isinstancet   dictt	   enumeratet   lowert   KeyErrort	   TypeErrort   AttributeErrort
   ValueErrort   getR3   R   R   R
   t   flattent   sumt   mapR   R   R   t   maxR   t   npt   emptyt   floatt   fillt   nant   shapet
   IndexErrort   errstatet   anyt   joinR   t   clipR   t   intt   printt   asarrayR   R	   R   R   t   copyR   t   normt   absRH   R   (G   R   R   R   R   R.   R/   R"   R#   R$   R%   R&   R'   t   unknown_optionsR:   R=   R>   RA   t   ict   cont   ctypeRC   RD   t
   exit_modest   fevalt   gevalR   R,   t   meqt   mieqt   mt   lat   nt   n1t   mineqt   len_wt   len_jwt   wt   jwt   xlt   xut   bndst   bnderrt   infbndt
   have_boundt   modet   majitert   majiter_prevt   alphaR   t   gst   h1t   h2t   h3t   h4t   tt   t0t   tolt   iexactt   inconst   iresett   itermxt   linet   n2t   n3t   fxt   c_eqt   c_ieqt   gt   a_eqt   a_ieqt   a(    (   R   s3   lib/python2.7/site-packages/scipy/optimize/slsqp.pyR5   ×   s6   
	

77

z2 -$%
7
7
1
1$-&( t   __main__i   i   c         C` sf   t  |  d ƒ | d |  d d | d |  d d | d |  d |  d | d |  d | d S(   s    Objective function i    i   i   i   i   (   R   (   R   t   r(    (    s3   lib/python2.7/site-packages/scipy/optimize/slsqp.pyR*   ã  s    Ogš™™™™™¹?gš™™™™™É?c         C` s!   t  |  d d |  d | g ƒ S(   s    Equality constraint i    i   i   (   R   (   R   RI   (    (    s3   lib/python2.7/site-packages/scipy/optimize/slsqp.pyt   feqconî  s    c         C` s   t  d |  d d g g ƒ S(   s!    Jacobian of equality constraint i   i    i   (   R   (   R   RI   (    (    s3   lib/python2.7/site-packages/scipy/optimize/slsqp.pyt   jeqconò  s    i
   c         C` s   t  |  d |  d | g ƒ S(   s    Inequality constraint i    i   (   R   (   R   R,   (    (    s3   lib/python2.7/site-packages/scipy/optimize/slsqp.pyt   fieqconö  s    c         C` s   t  d d g g ƒ S(   s#    Jacobian of Inequality constraint i   (   R   (   R   R,   (    (    s3   lib/python2.7/site-packages/scipy/optimize/slsqp.pyt   jieqconú  s    R(   R)   R*   R   R   R-   s    Bounds constraints iH   t   -s    * fmin_slsqpiÿÿÿÿR.   R%   R?   s    * _minimize_slsqps%    Equality and inequality constraints R7   R;   R9   R<   R/   (3   t   __doc__t
   __future__R    R   R   t   __all__t   numpyR_   t   scipy.optimize._slsqpR   R   R   R   R	   R
   R   R   R   R   R   R   R   R   t   optimizeR   R   R   t   __docformat__Ra   R&   t   _epsilonR   R3   R   t   FalseR5   t   __name__R*   t   TR„   R¤   R¥   R¦   R§   RA   Rk   t   centert   TrueR   t   fRB   (    (    (    s3   lib/python2.7/site-packages/scipy/optimize/slsqp.pyt   <module>   sZ   X	&	Ž	ÿ $"(
$


