B
    dY                 @   s   d Z ddlZddlZddlZddlmZ dddgZdZe	e
ejZdZd	ZG d
d dejdZG dd deZG dd deZdS )z>
Optimization algorithms used in `~astropy.modeling.fitting`.
    N)AstropyUserWarningOptimizationSLSQPSimplexd   gHz>)l J)l    J)c               @   s   e Zd ZdZg Zdd Zedd Zejdd Zedd Z	e	jd	d Z	ed
d Z
e
jdd Z
dd Zedd Zejdd ZdS )r   aE  
    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)_opt_methodDEFAULT_MAXITER_maxiterDEFAULT_EPS_epsDEFAULT_ACC_acc)self
opt_method r   h/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/astropy/modeling/optimizers.py__init__.   s    zOptimization.__init__c             C   s   | j S )zMaximum number of iterations)r	   )r   r   r   r   maxiter4   s    zOptimization.maxiterc             C   s
   || _ dS )zSet maxiterN)r	   )r   valr   r   r   r   9   s    c             C   s   | j S )z=Step for the forward difference approximation of the Jacobian)r   )r   r   r   r   eps>   s    zOptimization.epsc             C   s
   || _ dS )zSet eps valueN)r   )r   r   r   r   r   r   C   s    c             C   s   | j S )zRequested accuracy)r   )r   r   r   r   accH   s    zOptimization.accc             C   s
   || _ dS )zSet accuracyN)r   )r   r   r   r   r   r   M   s    c             C   s   | j j d}|S )Nz())	__class____name__)r   fmtr   r   r   __repr__R   s    zOptimization.__repr__c             C   s   | j S )z  Return the optimization method.)r   )r   r   r   r   r   V   s    zOptimization.opt_methodc             C   s   t dd S )Nz'Subclasses should implement this method)NotImplementedError)r   r   r   r   __call__[   s    zOptimization.__call__N)r   
__module____qualname____doc__supported_constraintsr   propertyr   setterr   r   r   r   abcabstractmethodr   r   r   r   r   r      s   )	metaclassc                   s6   e Zd ZdZdddddgZ fddZd	d
 Z  ZS )r   a)  
    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
    boundseqconsineqconsfixedtiedc                s,   ddl m} t | d d d d d| _d S )Nr   )
fmin_slsqp)final_func_valnumiter	exit_modemessage)scipy.optimizer+   superr   fit_info)r   r+   )r   r   r   r   n   s    zSLSQP.__init__c          	      sN  | d| j|d< d|kr$| j|d< d|kr6| j|d< | dd}|d   fdd	 jD }d
d	 |D }t|}x<|D ]4}|d dkrtd |d< |d dkr|td |d< q|W tj|td}t	 j
}	t	 j}
| j||f|d|||	|
d|\}}}}}|| jd< || jd< || jd< || jd< |dkrDtdt || jfS )a  
        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

        r   iterepsilonr   	verblevelNr   c                s   g | ]}t  |qS r   )getattr).0name)modelr   r   
<listcomp>   s    z"SLSQP.__call__.<locals>.<listcomp>c             S   s   g | ]}|j s|js|jqS r   )r)   r*   r&   )r7   parr   r   r   r:      s       )ZdtypeT)argsfull_outputdispr&   r'   Zieqconsr,   r-   r.   r/   zLThe fit may be unsuccessful; check fit_info['message'] for more information.)popr	   r   r   Zparam_namesnpZasarrayDEFAULT_BOUNDSfloatarrayr'   r(   r   r2   warningswarnr   )r   objfuncinitvalfargskwargsr?   Zparsr&   ir'   r(   	fitparamsr,   r-   r.   Zmessr   )r9   r   r   x   s<    









zSLSQP.__call__)r   r   r   r   r    r   r   __classcell__r   r   )r   r   r   `   s   
c                   s2   e Zd ZdZdddgZ fddZdd Z  ZS )	r   a=  
    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
    r&   r)   r*   c                s,   ddl m} t | d d d d d| _d S )Nr   )fmin)r,   r-   r.   num_function_calls)r0   rN   r1   r   r2   )r   Zsimplex)r   r   r   r      s    zSimplex.__init__c             K   s   d|kr| j |d< d|kr.|d | _|d d|krJ|d | _|d |dd}| j||f|| j|dd|\}}}}	}
|| jd< || jd	< |
| jd
< |	| jd< | jd
 dkrtdt | jd
 dkrtdt || jfS )a  
        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

        r   r   xtolr5   NT)r=   rP   r?   r>   r,   r-   r.   rO   r<   zLThe fit may be unsuccessful; Maximum number of function evaluations reached.   zBThe fit may be unsuccessful; Maximum number of iterations reached.)r	   r   r@   r   r2   rE   rF   r   )r   rG   rH   rI   rJ   r?   rL   r,   r-   Zfuncallsr.   r   r   r   r      s.    








zSimplex.__call__)r   r   r   r   r    r   r   rM   r   r   )r   r   r      s   

)r   rE   r#   numpyrA   Zastropy.utils.exceptionsr   __all__r   sqrtZfinforC   r   r
   r   rB   ABCMetar   r   r   r   r   r   r   <module>   s   
EQ