
    !#h41                       d dl mZ d dlZd dlmZ d dlZd dlmZ d dlm	Z	 d dlm
Z
 d dlmZ d dlmZ d d	lmZ d d
lmZ erd dlmZ nd dlmZ  ed      Z ee      Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZddd	 	 	 	 	 	 	 	 	 ddZdddddd	 	 	 	 	 	 	 	 	 	 	 	 	 ddZy)    )annotationsN)TYPE_CHECKING)AcquisitionFunctionParams)eval_acqf_no_gradeval_acqf_with_grad)normalize_one_param)sample_normalized_params)	ScaleType)
get_logger)_LazyImportzscipy.optimizec           
     B    t              dk(  r||dfS |j                         d	 fd}t        j                  |   z  D cg c]	  }dd|z  f c}t	        j
                  |      d      \  }}	}
|	 |kD  r|
d   dkD  r|z  <   |	 dfS ||dfS c c}w )
a'  
    This function optimizes the acquisition function using preconditioning.
    Preconditioning equalizes the variances caused by each parameter and
    speeds up the convergence.

    In Optuna, acquisition functions use Matern 5/2 kernel, which is a function of `x / l`
    where `x` is `normalized_params` and `l` is the corresponding lengthscales.
    Then acquisition functions are a function of `x / l`, i.e. `f(x / l)`.
    As `l` has different values for each param, it makes the function ill-conditioned.
    By transforming `x / l` to `zl / l = z`, the function becomes `f(z)` and has
    equal variances w.r.t. `z`.
    So optimization w.r.t. `z` instead of `x` is the preconditioning here and
    speeds up the convergence.
    As the domain of `x` is [0, 1], that of `z` becomes [0, 1/l].
    r   Fc                J    | z  <   t              \  }}| |    z  fS Nr   )scaled_xfvalgradacqf_paramscontinuous_indiceslengthscalesnormalized_paramss      S/var/www/html/sandstorm/venv/lib/python3.12/site-packages/optuna/_gp/optim_mixed.pynegative_acqf_with_gradz1_gradient_ascent.<locals>.negative_acqf_with_grad6   sB    08<0G,-*;8IJt /00<?@@          )funcx0boundspgtolmaxiternitT)r   
np.ndarrayreturnztuple[float, np.ndarray])lencopysofmin_l_bfgs_bmathsqrt)r   initial_paramsinitial_fvalr   r   tolr   sscaled_cont_x_optneg_fval_optinfor   s   `  ``      @r   _gradient_ascentr2      s    . !#e44&++-A A -/,<,<$/0<?$01qAE
1iin-)|T 	}|#Ua0AL0P,-!L=$77L%00 2s   Bc                    ||||   k7     }t        j                  |d d d f   t        |      d      }||d d |f<   t        | |      }t        j                  |      }||   |kD  r||d d f   ||   dfS ||dfS )Nr   )axisTF)nprepeatr%   r   argmax)	r   r+   r,   	param_idxchoiceschoices_except_current
all_paramsfvalsbest_idxs	            r   _exhaustive_searchr>   N   s     %Wy0I%IJ>$'2C8N4OVWXJ5Jq)|k:6EyyHX%8Q;'x$??L%00r   c                    t              dk(  r||dfS dfd} ||         }t        j                  |   |         sJ || i|j                         d fddfd}d}	t	        j
                  |d   |	z
  |   d   |	z   fd	|
      }
 ||
j                        } |       }||k7  r||kD  r|   <   |dfS ||dfS )Nr   Fc           	         t        t        j                  t        j                  |       dt	              dz
              }t        | |dz
     z
        t        | |   z
        k  r|dz
  S |S )Nr   )intr5   clipsearchsortedr%   abs)xigridss     r   find_nearest_indexz1_discrete_line_search.<locals>.find_nearest_indexn   sb    q11c%j1nEFAa!e,-AaL0AAq1uHqHr   c                x    j                  |       }||S |    <   t        t                     }|| <   |S r   )getfloatr   )rF   	cache_valnegvalr   rG   negative_fval_cacher   r8   s      r   negative_acqf_with_cachez7_discrete_line_search.<locals>.negative_acqf_with_cachey   sV    '++A.	 ',Qx)$ )+7HIJJ!'Ar   c           	     6   | d   k  s| d   kD  rt         j                  S t        t        j                  t        j                  |       dt              dz
              }|dz
  } |       |      }}|   | z
  |   |   z
  z  }d|z
  }||z  ||z  z   S )Nr   r   g      ?)r5   infrA   rB   rC   r%   )	rE   rightleftneg_acqf_leftneg_acqf_rightw_leftw_rightrG   rO   s	          r   interpolated_negative_acqfz9_discrete_line_search.<locals>.interpolated_negative_acqf   s    uQx<1uRy=66MBGGBOOE15q#e*q.IJqy(@(FH`I
~ ,"uU|eDk'AB,%.(@@@r   g-q=r   rQ   brent)bracketmethodr-   T)rE   rK   r$   rA   )rF   rA   r$   rK   )rE   rK   r$   rK   )r%   r5   iscloser&   r'   minimize_scalarrE   )r   r+   r,   r8   rG   xtolrH   current_choice_irY   EPSresopt_idxfval_optrO   rN   r   s   `  ``        @@@r   _discrete_line_searchre   b   s    5zQe44I *.*CD::nY/7G1HIII+l];&++-
 

A C


" qC'7!8%)c/JC !'G(11H ""x,'>',W~)$!8T22L%00r   c                    d}| j                   j                  |   }|t        j                  k(  st	        |      |k  rt        | ||||      S t        | |||||      S )N   )search_spacescale_typesr   CATEGORICALr%   r>   re   )r   r+   r,   r8   r9   r_    MAX_INT_EXHAUSTIVE_SEARCH_PARAMS
scale_types           r   _local_search_discreterm      sh     (*$))55i@JY***c'l>^.^!+~|YX_``$y'4
 	
r   g-C6?d   )r-   max_iterc                  | j                   j                  }| j                   j                  }| j                   j                  }t	        j
                  |dk(        d   }| j                  j                  j                         j                         }dt	        j                  ||         z  }	t	        j
                  |dkD        d   j                  t              }
|
D cg c]  }||   t        j                  k(  rt	        j                  ||df         nTt!        t	        j                  ||df   ||df   d||   z  z   ||         t        ||         ||df   ||df   f||          }}|D cg c]=  }t	        j"                  t	        j$                  |      t        j&                        dz  ? }}|j)                         }t+        t-        | |            }d}d }t/        |      D ]i  }||k(  r||fc S t1        | ||||	|      \  }}}|r|}t3        |
||      D ],  \  }}}||k(  r||fc c S t5        | |||||      \  }}}|s+|}. |e||fc S  t6        j9                  d	       ||fS c c}w c c}w )
N        r   r   g      ?)param_valuerl   r   step)initial   rQ   z2local_search_mixed: Local search did not converge.)rh   ri   r   stepsr5   wherekernel_paramsinverse_squared_lengthscalesdetachnumpyr*   astyperA   r   rj   aranger	   mindiffrR   r&   rK   r   ranger2   ziprm   _loggerwarning)r   initial_normalized_paramsr-   ro   ri   r   rv   r   ry   r   discrete_indicesrF   choices_of_discrete_paramsr9   discrete_xtolsbest_normalized_params	best_fval
CONTINUOUSlast_changed_param_updatedr_   s                         r   local_search_mixedr      s    **66K%%,,F$$**E%3,/2 	!!>>EEGMMO ! rww;<NOPPL xx	*1-44S9 ""  1~!6!66 IIfQTl#$IIfQTlF1a4L3q>4QSXYZS[\$[^4q!tfQTl31X	" "$ 2	  	rwww014N  7;;='5KLMIJ%)8_ 7+*I667G"8
4	G !+ #$46PR` a 	'Aw!Q&.	::;Q3Y7D<8#Y %&"	' %*I6677: OOHI"I..s"s   BI,/AI1i   
   )!warmstart_normalized_params_arrayn_preliminary_samplesn_local_searchr-   rngc                  |xs t         j                  j                         }| j                  j                  j
                  d   }|t        j                  d|f      }t        |      |dz
  k  sJ d       t        || j                  |      }t        | |      }t        |t         j                        sJ t        j                  |      }	t        j                  |||	   z
        }
d|
|	<   |
|
j                         z  }
t        j                  |
dkD        }t!        |t        |      z
  dz
  |      }||k(  rt"        j%                  d       t        j&                  |	g      }|dkD  r4|j)                  t        |      |d|
      }t        j*                  ||      }||	d d f   }t-        ||	         }t        j.                  ||d d f   |g      D ]  }t1        | ||	      \  }}||kD  s|}|} ||fS )
Nr   r   zPWe must choose at least 1 best sampled point + given_initial_xs as start points.)r   rq   zBStudy already converged, so the number of local search is reduced.F)sizereplacep)r-   )r5   randomRandomStaterh   ri   shapeemptyr%   r
   r   
isinstancendarrayr7   expsumcount_nonzeror~   r   r   arraychoiceappendrK   vstackr   )r   r   r   r   r-   r   dim
sampled_xsf_valsmax_iprobsn_non_zero_probs_improvementn_additional_warmstartchosen_idxsadditional_idxsbest_xbest_fx_warmstartrE   fs                       r   optimize_acqf_mixedr     s    
(&&(C

"
"
.
.
4
4Q
7C(0,.HHaX,>) 	-..12DDZYZD **?AYAY_bcJ {J7Ffbjj)))IIfE FF6F5M)*EE%L	UYY[E#%#3#3ECK#@  >??!CEa !==\]((E7#K!**
O"8%5 % 
 ii_=q!F6%=!Fyy*[!^"<>_!`a !+{D1v:FF	 6>r   )r   r   r+   r#   r,   rK   r   r#   r   r#   r-   rK   r$   tuple[np.ndarray, float, bool])r   r   r+   r#   r,   rK   r8   rA   r9   r#   r$   r   )r   r   r+   r#   r,   rK   r8   rA   rG   r#   r_   rK   r$   r   )r   r   r+   r#   r,   rK   r8   rA   r9   r#   r_   rK   r$   r   )
r   r   r   r#   r-   rK   ro   rA   r$   tuple[np.ndarray, float])r   r   r   znp.ndarray | Noner   rA   r   rA   r-   rK   r   znp.random.RandomState | Noner$   r   )
__future__r   r)   typingr   r{   r5   optuna._gp.acqfr   r   r   optuna._gp.search_spacer	   r
   r   optuna.loggingr   scipy.optimizeoptimizer'   optunar   __name__r   r2   r>   re   rm   r   r    r   r   <module>r      s   "     5 - / 7 < - % "	%	&B
X
01*0101 01 #	01
 01 
01 $01f1*11 1 	1
 1 $1(@1*@1@1 @1 	@1
 @1 @1 $@1F
*

 
 	

 
 
 $
4 S/*S/)S/ 
	S/
 S/ S/r <@!%(,7*7 (97 	7
 7 
7 
&7 7r   