ó
äÊMc           @   s‡  d  Z  d d l m Z d d l m Z m Z m Z d Z d Z d Z	 d Z
 d Z e e	 e
 e Z i d	 e 6d
 e 6d e	 6d e
 6d e 6d e 6Z d Z d Z d Z d Z d Z d Z d Z d Z d Z i	 d e 6d e 6d e 6d e 6d e 6d e 6d e 6d e 6d e 6Z d d l Z e j Z d d! d d d d d e d d d d d d d d d d d d „ Z e d k rƒd  „  Z e ƒ  n  d S("   s´  
TNC: A python interface to the TNC non-linear optimizer

TNC is a non-linear optimizer. To use it, you must provide a function to
minimize. The function must take one argument: the list of coordinates where to
evaluate the function; and it must return either a tuple, whose first element is the
value of the function, and whose second argument is the gradient of the function
(as a list of values); or None, to abort the minimization.
iÿÿÿÿ(   t	   moduleTNC(   t   asarrayt   inft   arrayi    i   i   i   i   s   No messagess   One line per iterations   Informational messagess   Version infos   Exit reasonss   All messagesi   i   i   i   s   Infeasible (low > up)s   Local minima reach (|pg| ~= 0)s   Converged (|f_n-f_(n-1)| ~= 0)s   Converged (|x_n-x_(n-1)| ~= 0)s)   Max. number of function evaluations reachs   Linear search faileds.   All lower bounds are equal to the upper boundss   Unable to progresss"   User requested end of minimizationNg:Œ0âŽyE>c            sV  t  | d t ƒj ƒ  } t | ƒ } | d k r@ d g | } n  t | ƒ | k ra t d ƒ ‚ n  | d k	 r¬ i t d 6t d 6t d 6t	 d 6t
 d 6t d 6j | t ƒ }	 n  | rÊ ‡ ‡ ‡ f d	 †  } n6 ˆ  d k rë ‡ ‡ f d
 †  } n ‡ ‡  ‡ f d †  } d g | } d g | } xŒ t | ƒ D]~ } | | d k rNt t } } q'| | \ } } | d k rxt | | <n
 | | | <| d k r›t | | <q'| | | <q'W| d k r¾g  } n  | d k rÓg  } n  | d k rût d d t | ƒ ƒ } n  t j | | | | | | |	 |
 | | | | | | | | | ƒ \ } } } t | ƒ | | f S(   s¥  
    Minimize a function with variables subject to bounds, using
    gradient information.

    Parameters
    ----------
    func : callable ``func(x, *args)``
        Function to minimize.  Should return f and g, where f is
        the value of the function and g its gradient (a list of
        floats).  If the function returns None, the minimization
        is aborted.
    x0 : list of floats
        Initial estimate of minimum.
    fprime : callable ``fprime(x, *args)``
        Gradient of func. If None, then func must return the
        function value and the gradient (``f,g = func(x, *args)``).
    args : tuple
        Arguments to pass to function.
    approx_grad : bool
        If true, approximate the gradient numerically.
    bounds : list
        (min, max) pairs for each element in x, defining the
        bounds on that parameter. Use None or +/-inf for one of
        min or max when there is no bound in that direction.
    scale : list of floats
        Scaling factors to apply to each variable.  If None, the
        factors are up-low for interval bounded variables and
        1+|x] fo the others.  Defaults to None
    offset : float
        Value to substract from each variable.  If None, the
        offsets are (up+low)/2 for interval bounded variables
        and x for the others.
    messages :
        Bit mask used to select messages display during
        minimization values defined in the MSGS dict.  Defaults to
        MGS_ALL.
    disp : int
        Integer interface to messages.  0 = no message, 5 = all messages
    maxCGit : int
        Maximum number of hessian*vector evaluations per main
        iteration.  If maxCGit == 0, the direction chosen is
        -gradient if maxCGit < 0, maxCGit is set to
        max(1,min(50,n/2)).  Defaults to -1.
    maxfun : int
        Maximum number of function evaluation.  if None, maxfun is
        set to max(100, 10*len(x0)).  Defaults to None.
    eta : float
        Severity of the line search. if < 0 or > 1, set to 0.25.
        Defaults to -1.
    stepmx : float
        Maximum step for the line search.  May be increased during
        call.  If too small, it will be set to 10.0.  Defaults to 0.
    accuracy : float
        Relative precision for finite difference calculations.  If
        <= machine_precision, set to sqrt(machine_precision).
        Defaults to 0.
    fmin : float
        Minimum function value estimate.  Defaults to 0.
    ftol : float
        Precision goal for the value of f in the stoping criterion.
        If ftol < 0.0, ftol is set to 0.0 defaults to -1.
    xtol : float
        Precision goal for the value of x in the stopping
        criterion (after applying x scaling factors).  If xtol <
        0.0, xtol is set to sqrt(machine_precision).  Defaults to
        -1.
    pgtol : float
        Precision goal for the value of the projected gradient in
        the stopping criterion (after applying x scaling factors).
        If pgtol < 0.0, pgtol is set to 1e-2 * sqrt(accuracy).
        Setting it to 0.0 is not recommended.  Defaults to -1.
    rescale : float
        Scaling factor (in log10) used to trigger f value
        rescaling.  If 0, rescale at each iteration.  If a large
        value, never rescale.  If < 0, rescale is set to 1.3.

    Returns
    -------
    x : list of floats
        The solution.
    nfeval : int
        The number of function evaluations.
    rc : int
        Return code as defined in the RCSTRINGS dict.

    t   dtypes    length of x0 != length of boundsi    i   i   i   i   i   c            s@   t  |  ƒ }  ˆ |  ˆ Œ } t |  ˆ ˆ  ˆ Œ } | t | ƒ f S(   N(   R   t   approx_fprimet   list(   t   xt   ft   g(   t   epsilont   argst   func(    s3   C:\Python27\Lib\site-packages\scipy\optimize\tnc.pyt   func_and_grad¹   s    c            s1   t  |  ƒ }  ˆ |  ˆ  Œ \ } } | t | ƒ f S(   N(   R   R   (   R   R   R	   (   R   R   (    s3   C:\Python27\Lib\site-packages\scipy\optimize\tnc.pyR   ¿   s    c            s:   t  |  ƒ }  ˆ |  ˆ  Œ } ˆ |  ˆ  Œ } | t | ƒ f S(   N(   R   R   (   R   R   R	   (   R   t   fprimeR   (    s3   C:\Python27\Lib\site-packages\scipy\optimize\tnc.pyR   Ä   s    id   i
   N(   NN(   R   t   floatt   tolistt   lent   Nonet
   ValueErrort   MSG_NONEt   MSG_ITERt   MSG_INFOt   MSG_VERSt   MSG_EXITt   MSG_ALLt   gett   rangeR   t   maxR    t   minimizeR   (   R   t   x0R   R   t   approx_gradt   boundsR
   t   scalet   offsett   messagest   maxCGitt   maxfunt   etat   stepmxt   accuracyt   fmint   ftolt   xtolt   pgtolt   rescalet   dispt   nR   t   lowt   upt   it   lt   ut   rct   nfR   (    (   R   R
   R   R   s3   C:\Python27\Lib\site-packages\scipy\optimize\tnc.pyt   fmin_tncQ   sH    [  
		t   __main__c          C   sl   d GHd „  }  t  |  d d g d d d g d d g f ƒ\ } } } d	 G| Gd
 Gt | GHd G| GHd GHHd  S(   Nt   Examplec         S   s•   t  |  d d ƒ t  t |  d ƒ d ƒ } d d g } d |  d | d <d t  t |  d ƒ d ƒ | d <|  d d k  r‹ | d | d <n  | | f S(   Ni    g       @i   g      @(   t   powt   abs(   R   R   R	   (    (    s3   C:\Python27\Lib\site-packages\scipy\optimize\tnc.pyt   functionó   s    *!iùÿÿÿi   R    iöÿÿÿi   i
   t   Afters#   function evaluations, TNC returned:s   x =s   exact value = [0, 1](   R7   t	   RCSTRINGS(   R<   R   R6   R5   (    (    s3   C:\Python27\Lib\site-packages\scipy\optimize\tnc.pyt   exampleð   s    	
6	(    (   t   __doc__t   scipy.optimizeR    t   numpyR   R   R   R   R   R   R   R   R   t   MSGSt
   INFEASIBLEt   LOCALMINIMUMt
   FCONVERGEDt
   XCONVERGEDt   MAXFUNt   LSFAILt   CONSTANTt
   NOPROGRESSt	   USERABORTR>   t   optimizeR   R   R7   t   __name__R?   (    (    (    s3   C:\Python27\Lib\site-packages\scipy\optimize\tnc.pyt   <module>!   sX   

		˜	