ó
W™xTc           @   sª   d  d l  m Z d  d l m Z d  d l m Z d  d l m Z d e f d „  ƒ  YZ	 d e f d „  ƒ  YZ
 d	 e f d
 „  ƒ  YZ d „  Z e d k r¦ e ƒ  d GHn  d S(   iÿÿÿÿ(   t   division(   t   flex(   t   Sorry(   t   approx_equalt   simplex_optc           B   s¡   e  Z d  Z d d d d d d d d „ Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   se  
  Python implementaion of Nelder-Mead Simplex method
  The first publication is By JA Nelder and R. Mead:
    A simplex method for function minimization, computer journal 7(1965), 308-313
  The routine is following the description by Lagarias et al:
    Convergence properties of the nelder-mead simplex method in low dimensions SIAM J. Optim. Vol 9, No. 1, pp. 112-147
  And a previous implementation in (c++) by Adam Gurson and Virginia Torczon can be found at:
    http://www.cs.wm.edu/~va/software/SimplexSearch/AGEssay.html

  User need to specifiy the dimension and an initial 'guessed' simplex (n*(n+1)) matrix
    where n is the dimension

  A minimum is desired for simplex algorithm to work well, as the searching is not bounded.

  !! Target function should be modified to reflect specific purpose

  Problems should be referred to haiguang.liu@gmail.com
  g�íµ ÷Æ°>g    eÍÍAg      ð?g      à?g       @i
   c         C   sí   | |  _  | |  _ | |  _ | |  _ t | ƒ |  j d k sV | d j ƒ  |  j k re t d ƒ ‚ n  x= | d D]1 } | j ƒ  | d j ƒ  k rp t d ƒ ‚ qp qp W| |  _ | |  _ | |  _	 |	 |  _
 |
 |  _ |  j | ƒ |  j ƒ  d  S(   Ni   i    s>   The initial simplex matrix does not match dimensions specifieds/   Vector length in intial simplex do not match up(   t   max_itert	   dimensiont	   tolerancet	   evaluatort   lent   sizeR   t   alphat   betat   gammat   sigmat   monitor_cyclet
   initializet   optimize(   t   selfR   t   matrixR   R   R   R   R   R   R   R   t   vector(    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/simplex.pyt   __init__   s     				2					c         C   sé   t  |  _ t  |  _ t j ƒ  |  _ g  |  _ x= | D]5 } |  j j | j ƒ  ƒ |  j j |  j	 | ƒ ƒ q1 Wt j ƒ  |  _
 t j ƒ  |  _ t j ƒ  |  _ t j ƒ  |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d  S(   Ni    g        (   t   Falset   endt   foundR   t   doublet   simplexValueR   t   appendt	   deep_copyt   functiont   centroidt   reflectionPtt   expansionPtt   contractionPtt   min_indxt   second_indxt   max_indxt   maxPrimePtIdt
   secondHight   maxt   min(   R   R   R   (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/simplex.pyR   8   s$    									c         C   sé   t  } t  } d |  _ d } xÇ | rä | rä |  j ƒ  |  j d 7_ |  j |  j |  _ |  j |  j d k rÆ t | |  j ƒ } | t |  j ƒ |  j |  j } | |  j k  rº t	 } qÆ |  j } n  |  j |  j
 k r t	 } q q Wd  S(   Ni    i   (   R   t   countt   exploreR   R"   t	   min_scoreR   t   absR   t   TrueR   (   R   R   R   t   monitor_scoret   rd(    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/simplex.pyR   N   s     	
!	c         C   sÎ  |  j  ƒ  |  j ƒ  |  j ƒ  |  j |  j |  _ |  j |  j |  j k r² |  j ƒ  |  j |  j	 k rŒ |  j
 |  j ƒ |  j	 |  j |  j <qÊ|  j
 |  j ƒ |  j |  j |  j <n|  j |  j k r|  j |  j |  j k r|  j
 |  j ƒ |  j |  j |  j <nÇ |  j |  j k rÊ|  j ƒ  |  j d k rs|  j |  j k rM|  j ƒ  qÇ|  j
 |  j ƒ |  j |  j |  j <qÊ|  j d k rÊ|  j |  j k r¡|  j ƒ  qÇ|  j
 |  j ƒ |  j |  j |  j <qÊn  d  S(   Ni    i   (   t   FindMinMaxIndicest   FindCentroidPtt   FindReflectionPtR   R#   R&   R"   t   reflectionPtValuet   FindExpansionPtt   expansionPtValuet   ReplaceSimplexPointR    R$   R   t   FindContractionPtR%   t   contractionPtValuet   maxPrimePtValuet   ShrinkSimplexR!   (   R   (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/simplex.pyR*   c   s6    



+
c         C   s   |  j  d |  _ |  _ } d |  _ d |  _ d |  _ xÃ t d |  j d ƒ D]« } |  j  | |  j k rš |  j } |  j  | |  _ |  j |  _ | |  _ qM |  j  | | k rÆ |  j  | } | |  _ qM |  j  | |  j k  rM |  j  | |  _ | |  _ qM qM Wd  S(   Ni    i   (   R   R'   R(   R$   R"   R#   t   rangeR   (   R   t
   second_maxt   ii(    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/simplex.pyR0   ƒ   s"    				c         C   sp   |  j  d d |  _ xC t |  j d ƒ D]. } | |  j k r( |  j |  j  | 7_ q( q( W|  j |  j _ d  S(   Ni    i   (   R   R   R;   R   R$   (   R   R=   (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/simplex.pyR1   –   s
    c         C   sE   |  j  d |  j |  j |  j |  j |  _ |  j |  j ƒ |  _ d  S(   Ng      ð?(   R   R   R   R$   R   R   R3   (   R   (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/simplex.pyR2   �   s    ,c         C   s>   |  j  d |  j |  j |  j |  _ |  j |  j ƒ |  _ d  S(   Ng      ð?(   R   R   R   R    R   R5   (   R   (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/simplex.pyR4   ¡   s    %c         C   s©   |  j  |  j k rV d |  _ |  j  |  _ |  j d |  j |  j |  j |  j |  _ n: d |  _ |  j |  _ |  j d |  j |  j |  j	 |  _ |  j
 |  j ƒ |  _ d  S(   Ni   g      ð?i    (   R'   R3   R%   R9   R   R   R   R$   R!   R   R   R8   (   R   (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/simplex.pyR7   ¥   s    	/	%c         C   sˆ   x� t  |  j d ƒ D]l } | |  j k r |  j |  j |  j |  j | |  j |  j |  j | <|  j |  j | ƒ |  j | <q q Wd  S(   Ni   (   R;   R   R"   R   R   R   R   (   R   R=   (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/simplex.pyR:   °   s    7c         C   s   | |  j  |  j <d  S(   N(   R   R$   (   R   R   (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/simplex.pyR6   ¶   s    c         C   s   |  j  |  j S(   N(   R   R"   (   R   (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/simplex.pyt   get_solutionº   s    c         C   s   |  j  |  j S(   N(   R   R"   (   R   (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/simplex.pyt	   get_score½   s    c         C   s   |  j  j | ƒ S(   N(   R   t   target(   R   t   point(    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/simplex.pyR   À   s    (   t   __name__t
   __module__t   __doc__R   R   R   R*   R0   R1   R2   R4   R7   R:   R6   R>   R?   R   (    (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/simplex.pyR      s*   			 									t   test_functionc           B   s   e  Z d  „  Z d „  Z RS(   c      	   C   sÉ   | |  _  g  |  _ x7 t |  j  d ƒ D]" } |  j j t j |  j  ƒ ƒ q& Wt d |  j  d |  j d |  d d ƒ |  _ |  j j ƒ  |  _	 x: t
 |  j  ƒ D]) } t |  j	 | | d d ƒ s˜ t ‚ q˜ Wd  S(   Ni   R   R   R   R   g»½×Ùß|Û=gñhãˆµøä>(   t   nt   starting_simplexR;   R   R   t   random_doubleR   t	   optimizerR>   t   xt   xrangeR   t   AssertionError(   R   RF   R=   (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/simplex.pyR   Å   s    		 	c         C   sH   d } x1 t  |  j ƒ D]  } | | | | d d 7} q W| d 7} | S(   Ng        i   g       @g      ð?(   R;   RF   (   R   R   t   resultR=   (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/simplex.pyR@   Ò   s
    
(   RB   RC   R   R@   (    (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/simplex.pyRE   Ä   s   	t   test_rosenbrock_functionc           B   s   e  Z d  d „ Z d „  Z RS(   i   c      	   C   sþ   | |  _  | d |  _ g  |  _ t j |  j d ƒ |  _ x> t |  j d ƒ D]) } |  j j t j |  j ƒ |  j ƒ qK Wt	 d |  j d |  j d |  d d ƒ |  _
 |  j
 j ƒ  |  _ t |  j d d	 ƒ d
 k  s× t ‚ t |  j d d	 ƒ d
 k  sú t ‚ d  S(   Ni   i   R   R   R   R   g»½×Ùß|Û=i    g      ð?g�íµ ÷Æ°>(   t   dimRF   RG   R   R   RJ   R;   R   RH   R   RI   R>   R,   RL   (   R   RO   R=   (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/simplex.pyR   Ú   s    		'	#c         C   s]   d } xP t  | d |  j !| |  j ƒ D]. \ } } d | d d | | | d } q' W| S(   Ni    i   g       @g      Y@(   t   zipRO   (   R   R   RM   RJ   t   y(    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/simplex.pyR@   é   s    -&(   RB   RC   R   R@   (    (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/simplex.pyRN   Ù   s   c          C   sZ   t  j d ƒ xF t d ƒ D]8 }  t d ƒ t d ƒ t d ƒ t d ƒ t d ƒ q Wd  S(   Ni    i
   i   i   i   i   (   R   t   set_random_seedRK   RN   RE   (   R=   (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/simplex.pyt   runò   s    



t   __main__t   OKN(   t
   __future__R    t   scitbx.array_familyR   t   libtbx.utilsR   t   libtbx.test_utilsR   t   objectR   RE   RN   RS   RB   (    (    (    s<   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/simplex.pyt   <module>   s   ¾		