o
    3Zg9                     @   s>   d g fddZ dd Zdd Zdd Zd d	d
dg fddZd S )Nc                 C   s  dd |D }|}d}d}	t t|D ]a}
t||
 ||
  ||
 d ||
 d ||
< | |g|R  }|	d7 }	||k r>|}q||
  ||
< t||
 ||
  ||
 d ||
 d ||
< | |g|R  }|	d7 }	||k rn|}q||
 ||
< qt t|D ]}
||
 ||
< q{||	fS )a  
        ## given a point, look for a better one nearby
        one coord at a time
        
        f is a function that takes a list of floats (of the same length as point) as an input
        args is a dict of any additional arguments to pass to f
        delta, and point are same-length lists of floats
        prevbest is a float
        
        point and delta are both modified by the function
    c                 S      g | ]}|qS  r   .0xr   r   K/home/webroot/ce-5319-webroot/MLBE4CE/chapters/optimization/hookeNjeeves.py
<listcomp>       z&_hooke_best_nearby.<locals>.<listcomp>               )rangelen_value_in_bounds)fdeltapointprevbestboundsargszminfftmpfevir   r   r   _hooke_best_nearby   s(   ,,r   c                 C   s`   t t| D ]'}| | || d k r|| d | |< q| | || d kr-|| d | |< qdS )z?
        shifts the point so it is within the given bounds
    r
   r   Nr   r   )r   r   r   r   r   r   _point_in_bounds   s   r   c                 C   sL   d}t t| D ]}| | || d k rd}q| | || d kr#d}q|S )z<
        true if the point is in the bounds, else false
    Tr
   Fr   r   )r   r   outr   r   r   r   _is_point_in_bounds   s   r   c                 C   s   | |k r|S | |krt S | S N)hight)vallowhighr   r   r   r      s
   r         ?gư>i  c              
      s  t  }d|d< d|d< dgt| }dgt| }	|du r%dd |D }ndd |D }d	d |D }d}
d
}d
}|D ],}|d
 du rJtd|d
< nt|d
 |d
< |d du r_td|d< q;t|d |d< q;zz0t|| dd |D }dd |D }tt|D ]}t||   ||< || dkr ||< q }| |g|R  }|d7 }|}|}
||k r||kr|d7 }dd |D }t| |||||\}}||7 }d}||k rj|dkrj|}
tt|D ]>}|| || krt||  ||< nt|| ||< || }|| ||< t|| ||  | || d
 || d ||< q|}t| |||||\}}||7 }||kr<n.d
}tt|D ]}d}t|| ||  dt||  kr] nd
}qD||k rj|dks||kr||kr|  } fdd|D }||k r||kstt|D ]	}|| |	|< qW n+ ty } zt	
 \}}}d|d< t|d t|j |d< W Y d}~nd}~ww W ||d< ||d< |
|d< |	|d< |S W ||d< ||d< |
|d< |	|d< |S ||d< ||d< |
|d< |	|d< w )a  
    In this version of the Hooke and Jeeves algorithm, we coerce the function into staying within the given bounds.
    basically, any time the function tries to pick a point outside the bounds we shift the point to the boundary
    on whatever dimension it is out of bounds in. Implementing bounds this way may be questionable from a theory standpoint,
    but that's how COPASI does it, that's how I'll do it too.
    
    Tsuccessmessager	   Nc                 S   s   g | ]}d d gqS r   r   r   r   r   r   r          zhooke.<locals>.<listcomp>c                 S   s   g | ]
}|d  |d gqS )r
   r   r   r   r   r   r   r      s    c                 S   r   r   r   r   r   r   r   r      r   r
   z-infr   infc                 S   r   r   r   r   r   r   r   r   	  r   c                 S   r   r   r   r   r   r   r   r   
  r   c                 S   r   r   r   r   r   r   r   r   "  r   r$   c                    s   g | ]}|  qS r   r   r   rhor   r   r   H  r'   Fz. line number: nitfevalsfunr   )dictr   floatr   r   absr   r   	Exceptionsysexc_infostr	tb_lineno)r   startptr   r*   epsilonitermaxr   resultr   endptfminnfevitersboundxbeforenewxr   
steplengthfbeforenewfevalskeeptmpr   eexc_typeexc_objexc_tbr   r)   r   hooke   s   

2
&0&
rK   )r   r   r   r   rK   r   r   r   r   <module>   s    +
