ó
šßÈ[c           @` s  d  Z  d d l m Z m Z m Z m Z d d l Z d d l Z d d l Z	 d d l
 m Z d d l m Z d d d	 g Z d
 Z e	 j e	 j e ƒ j ƒ Z d Z d d f Z e j e j ƒ d e f d „  ƒ  Yƒ Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   u>   
Optimization algorithms used in `~astropy.modeling.fitting`.
i    (   t   absolute_importt   unicode_literalst   divisiont   print_functionNi   (   t   six(   t   AstropyUserWarningu   Optimizationu   SLSQPu   Simplexid   gH¯¼šò×z>i
   i   t   Optimizationc           B` sª   e  Z d  Z g  Z d „  Z e d „  ƒ Z e j d „  ƒ Z e d „  ƒ Z e j d „  ƒ Z e d „  ƒ Z	 e	 j d „  ƒ Z	 d „  Z
 e d	 „  ƒ Z e j d
 „  ƒ Z RS(   uE  
    Base class for optimizers.

    Parameters
    ----------
    opt_method : callable
        Implements optimization method

    Notes
    -----
    The base Optimizer does not support any constraints by default; individual
    optimizers should explicitly set this list to the specific constraints
    it supports.

    c         C` s(   | |  _  t |  _ t |  _ t |  _ d  S(   N(   t   _opt_methodt   DEFAULT_MAXITERt   _maxitert   DEFAULT_EPSt   _epst   DEFAULT_ACCt   _acc(   t   selft
   opt_method(    (    s:   lib/python2.7/site-packages/astropy/modeling/optimizers.pyt   __init__1   s    			c         C` s   |  j  S(   u   Maximum number of iterations(   R	   (   R   (    (    s:   lib/python2.7/site-packages/astropy/modeling/optimizers.pyt   maxiter7   s    c         C` s   | |  _  d S(   u   Set maxiterN(   R	   (   R   t   val(    (    s:   lib/python2.7/site-packages/astropy/modeling/optimizers.pyR   <   s    c         C` s   |  j  S(   u=   Step for the forward difference approximation of the Jacobian(   R   (   R   (    (    s:   lib/python2.7/site-packages/astropy/modeling/optimizers.pyt   epsA   s    c         C` s   | |  _  d S(   u   Set eps valueN(   R   (   R   R   (    (    s:   lib/python2.7/site-packages/astropy/modeling/optimizers.pyR   F   s    c         C` s   |  j  S(   u   Requested accuracy(   R   (   R   (    (    s:   lib/python2.7/site-packages/astropy/modeling/optimizers.pyt   accK   s    c         C` s   | |  _  d S(   u   Set accuracyN(   R   (   R   R   (    (    s:   lib/python2.7/site-packages/astropy/modeling/optimizers.pyR   P   s    c         C` s   d j  |  j j ƒ } | S(   Nu   {0}()(   t   formatt	   __class__t   __name__(   R   t   fmt(    (    s:   lib/python2.7/site-packages/astropy/modeling/optimizers.pyt   __repr__U   s    c         C` s   |  j  S(   N(   R   (   R   (    (    s:   lib/python2.7/site-packages/astropy/modeling/optimizers.pyR   Y   s    c         C` s   t  d ƒ ‚ d  S(   Nu'   Subclasses should implement this method(   t   NotImplementedError(   R   (    (    s:   lib/python2.7/site-packages/astropy/modeling/optimizers.pyt   __call__]   s    (   R   t
   __module__t   __doc__t   supported_constraintsR   t   propertyR   t   setterR   R   R   R   t   abct   abstractmethodR   (    (    (    s:   lib/python2.7/site-packages/astropy/modeling/optimizers.pyR      s   		t   SLSQPc           B` s5   e  Z d  Z d d d d d g Z d „  Z d „  Z RS(   u)  
    Sequential Least Squares Programming optimization algorithm.

    The algorithm is described in [1]_. It supports tied and fixed
    parameters, as well as bounded constraints. Uses
    `scipy.optimize.fmin_slsqp`.

    References
    ----------
    .. [1] http://www.netlib.org/toms/733
    u   boundsu   eqconsu   ineqconsu   fixedu   tiedc         C` sO   d d l  m } t t |  ƒ j | ƒ i d  d 6d  d 6d  d 6d  d 6|  _ d  S(   Ni    (   t
   fmin_slsqpu   final_func_valu   numiteru	   exit_modeu   message(   t   scipy.optimizeR$   t   superR#   R   t   Nonet   fit_info(   R   R$   (    (    s:   lib/python2.7/site-packages/astropy/modeling/optimizers.pyR   p   s    c         K` s  | j  d |  j ƒ | d <d | k r5 |  j | d <n  d | k rQ |  j | d <n  | j  d d ƒ } | d } g  | j D] } t | | ƒ ^ qw } g  | D]! }	 |	 j p® |	 j s™ |	 j	 ^ q™ }
 t
 j |
 ƒ }
 xP |
 D]H } | d d k rý t d | d <n  | d d k rÖ t d | d <qÖ qÖ Wt
 j |
 d t
 j ƒ}
 t
 j | j ƒ } t
 j | j ƒ } |  j | | d	 | d
 t d | d |
 d | d | | \ } } } } } | |  j d <| |  j d <| |  j d <| |  j d <| d k rùt j d t ƒ n  | |  j f S(   u  
        Run the solver.

        Parameters
        ----------
        objfunc : callable
            objection function
        initval : iterable
            initial guess for the parameter values
        fargs : tuple
            other arguments to be passed to the statistic function
        kwargs : dict
            other keyword arguments to be passed to the solver

        u   maxiteru   iteru   epsilonu   accu	   verbleveli    i   t   dtypet   argst   full_outputt   dispt   boundst   eqconst   ieqconsu   final_func_valu   numiteru	   exit_modeu   messageuL   The fit may be unsuccessful; check fit_info['message'] for more information.N(   t   popR	   R   R   R'   t   param_namest   getattrt   fixedt   tiedR-   t   npt   asarrayt   DEFAULT_BOUNDSt   floatt   arrayR.   t   ineqconsR   t   TrueR(   t   warningst   warnR   (   R   t   objfunct   initvalt   fargst   kwargsR,   t   modelt   namet   parst   parR-   t   iR.   R:   t	   fitparamst   final_func_valt   numitert	   exit_modet   mess(    (    s:   lib/python2.7/site-packages/astropy/modeling/optimizers.pyR   z   s<    
%.	
(   R   R   R   R   R   R   (    (    (    s:   lib/python2.7/site-packages/astropy/modeling/optimizers.pyR#   b   s   	
t   Simplexc           B` s/   e  Z d  Z d d d g Z d „  Z d „  Z RS(   u=  
    Neald-Mead (downhill simplex) algorithm.

    This algorithm [1]_ only uses function values, not derivatives.
    Uses `scipy.optimize.fmin`.

    References
    ----------
    .. [1] Nelder, J.A. and Mead, R. (1965), "A simplex method for function
       minimization", The Computer Journal, 7, pp. 308-313
    u   boundsu   fixedu   tiedc         C` sO   d d l  m } t t |  ƒ j | ƒ i d  d 6d  d 6d  d 6d  d 6|  _ d  S(   Ni    (   t   fminu   final_func_valu   numiteru	   exit_modeu   num_function_calls(   R%   RM   R&   RL   R   R'   R(   (   R   t   simplex(    (    s:   lib/python2.7/site-packages/astropy/modeling/optimizers.pyR   Â   s    c         K` sL  d | k r |  j  | d <n  d | k rE | d |  _ | j d ƒ n  d | k rn | d |  _ | j d ƒ n  | j d d ƒ } |  j | | d | d |  j d | d t | \ } } } }	 }
 | |  j d	 <| |  j d
 <|
 |  j d <|	 |  j d <|  j d d k rt j d t	 ƒ n  |  j d d k r?t j d t	 ƒ n  | |  j f S(   u  
        Run the solver.

        Parameters
        ----------
        objfunc : callable
            objection function
        initval : iterable
            initial guess for the parameter values
        fargs : tuple
            other arguments to be passed to the statistic function
        kwargs : dict
            other keyword arguments to be passed to the solver

        u   maxiteru   accu   xtolu	   verblevelR*   t   xtolR,   R+   u   final_func_valu   numiteru	   exit_modeu   num_function_callsi   uL   The fit may be unsuccessful; Maximum number of function evaluations reached.i   uB   The fit may be unsuccessful; Maximum number of iterations reached.N(
   R	   R   R0   R'   R   R;   R(   R<   R=   R   (   R   R>   R?   R@   RA   R,   RG   RH   RI   t   funcallsRJ   (    (    s:   lib/python2.7/site-packages/astropy/modeling/optimizers.pyR   Ì   s.    	
	
(   R   R   R   R   R   R   (    (    (    s:   lib/python2.7/site-packages/astropy/modeling/optimizers.pyRL   ³   s   	
I ¥Ôè   I ðZ+ÿÿÿI ¥Ôè   (   R   t
   __future__R    R   R   R   R<   R!   t   numpyR5   t   externR   t   utils.exceptionsR   t   __all__R   t   sqrtt   finfoR8   R   R
   R   R7   t   add_metaclasst   ABCMetat   objectR   R#   RL   (    (    (    s:   lib/python2.7/site-packages/astropy/modeling/optimizers.pyt   <module>   s   "DQ