
    !#h)4                       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	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 d dlmZ d dlmZ d dlmZ er8d dlZd dlmc mZ d dl mc m!Z! d dl"mc m#Z# d dl$mc m%Z% d dl&mc m'Z( d dlm)Z) n6d dl*m+Z+  e+d      Z e+d      Z( e+d      Z! e+d      Z# e+d      Z e+d      Z%dZ, ed       G d de             Z-	 	 	 	 	 	 ddZ.y)     )annotations)Callable)Sequence)Any)cast)TYPE_CHECKINGN)experimental_class)warn_experimental_argument)BaseDistribution)_CONSTRAINTS_KEY) _process_constraints_after_trial)BaseSampler)LazyRandomState)StudyDirection)FrozenTrial)
TrialState)Study)_LazyImporttorchzoptuna._gp.search_spacezoptuna._gp.gpzoptuna._gp.optim_mixedzoptuna._gp.acqfzoptuna._gp.priorg|=z3.6.0c                      e Zd ZdZdddddd	 	 	 	 	 	 	 	 	 	 	 ddZddZ	 	 	 	 	 	 ddZ	 	 	 	 	 	 dd	Z	 	 	 	 	 	 	 	 dd
Z	 	 	 	 	 	 	 	 ddZ		 	 	 	 	 	 	 	 	 	 ddZ
ddZ	 	 	 	 	 	 	 	 	 	 ddZy)	GPSampleraB  Sampler using Gaussian process-based Bayesian optimization.

    This sampler fits a Gaussian process (GP) to the objective function and optimizes
    the acquisition function to suggest the next parameters.

    The current implementation uses:
        - Matern kernel with nu=2.5 (twice differentiable),
        - Automatic relevance determination (ARD) for the length scale of each parameter,
        - Gamma prior for inverse squared lengthscales, kernel scale, and noise variance,
        - Log Expected Improvement (logEI) as the acquisition function, and
        - Quasi-Monte Carlo (QMC) sampling to optimize the acquisition function.

    .. note::
        This sampler requires ``scipy`` and ``torch``.
        You can install these dependencies with ``pip install scipy torch``.

    Args:
        seed:
            Random seed to initialize internal random number generator.
            Defaults to :obj:`None` (a seed is picked randomly).

        independent_sampler:
            Sampler used for initial sampling (for the first ``n_startup_trials`` trials)
            and for conditional parameters. Defaults to :obj:`None`
            (a random sampler with the same ``seed`` is used).

        n_startup_trials:
            Number of initial trials. Defaults to 10.

        deterministic_objective:
            Whether the objective function is deterministic or not.
            If :obj:`True`, the sampler will fix the noise variance of the surrogate model to
            the minimum value (slightly above 0 to ensure numerical stability).
            Defaults to :obj:`False`.
    N
   F)seedindependent_samplern_startup_trialsdeterministic_objectiveconstraints_funcc                  t        |      | _        |xs  t        j                  j	                  |      | _        t        j                  j                         | _        || _	        t        j                  | _        t        j                  | _        d | _        d | _        d| _        || _        || _        |t)        d       y y )N)r      r   )r   _rngoptunasamplersRandomSampler_independent_samplersearch_spaceIntersectionSearchSpace_intersection_search_space_n_startup_trialspriordefault_log_prior
_log_priorDEFAULT_MINIMUM_NOISE_VAR_minimum_noise_kernel_params_cache _constraints_kernel_params_cache_optimize_n_samples_deterministic_constraints_funcr
   )selfr   r   r   r   r   s         X/var/www/html/sandstorm/venv/lib/python3.12/site-packages/optuna/samplers/_gp/sampler.py__init__zGPSampler.__init__U   s     $D)	$7$c6??;X;X^b;X;c!*0*=*=*U*U*W'!1## 	 &+%D%DDH!VZ-(, 5!1'&'9: (    c                    | j                   j                  j                          | j                  j	                          y N)r    rngr   r$   
reseed_rng)r3   s    r4   r:   zGPSampler.reseed_rngp   s(    		!!,,.r6   c                    i }| j                   j                  |      j                         D ]  \  }}|j                         r|||<    |S r8   )r'   	calculateitemssingle)r3   studytrialr%   namedistributions         r4   infer_relative_search_spacez%GPSampler.infer_relative_search_spacet   sX     "&"A"A"K"KE"R"X"X"Z 	.D,""$!-L	.
 r6   c                    t        j                  ||d n|d d d f   ddd| j                  j                        \  }}|S )Nr   r   g-C6?)!warmstart_normalized_params_arrayn_preliminary_samplesn_local_searchtolr9   )optim_mixedoptimize_acqf_mixedr    r9   )r3   acqf_paramsbest_paramsnormalized_params	_acqf_vals        r4   _optimize_acqfzGPSampler._optimize_acqf   sL     (3'F'F#+T1W1E"&			(
$9 ! r6   c                   t        j                  |      }t        j                  |d      }t        j                  |d      }||z
  t        j
                  t        |      z  }| j                  <t        | j                  j                        t        |j                        k7  rd | _        |j                  t        j                  j                  k(  }g }g }	t        t!        |j"                  ||            D ]  \  }
\  }}}| j                  xr | j                  |
   }t%        |t         j&                        s|J t        j(                  |||| j*                  | j,                  || j.                        }|j1                  |       |	j1                  t3        j4                  t2        j6                  j8                  ||||| t;        t        |      z                || _        |	S )Nr   axisXYis_categorical	log_priorminimum_noiseinitial_kernel_paramsr   	acqf_typekernel_paramsr%   rT   rU   max_Y)gpwarn_and_convert_infnpmeanstdmaximumEPSr.   leninverse_squared_lengthscalesscale_typesr/   gp_search_space	ScaleTypeCATEGORICAL	enumeratezipT
isinstanceKernelParamsTensorfit_kernel_paramsr+   r-   r1   appendacqfcreate_acqf_paramsAcquisitionFunctionTypeLOG_PImax)r3   constraint_valsinternal_search_spacerM   meansstdsstandardized_constraint_valsrV   constraints_kernel_paramsconstraints_acqf_paramsivalsra   rb   cacher\   s                   r4   _get_constraints_acqf_paramsz&GPSampler._get_constraints_acqf_params   s    11/Ba0vvoA.(7%(?2::cSWCX'X$$$0S%%BB6
&22364 59D1.::o>W>W>c>cc$&!"$$-c2N2P2PRWY].^$_ 	 A dC55b$:_:_`a:b  eR%:%:;u}LL00#-//"11&+(,(;(;M &,,];#**''"::AA"/!6'%#c3-/
#	: 1J-&&r6   c           
     t   | j                  |       |i k(  ri S t        j                  f}|j                  d|d      }t	        |      | j
                  k  ri S t        j                  ||      \  }}|j                  t        j                  k(  rdnd}t        j                  |D cg c]  }|t        t        |j                        z  ! c}      }	t!        j"                  |	      }	|	t        j$                  |	      z
  t'        t(        t        j*                  |	            z  }
| j,                  <t	        | j,                  j.                        t	        |j0                        k7  rd | _        t!        j2                  ||
|j0                  t        j4                  j6                  k(  | j8                  | j:                  | j,                  | j<                        }|| _        | j>                  OtA        jB                  t@        jD                  jF                  ||||
      }|t        jH                  |
      d d f   }ntK        ||      \  }}t        jL                  |       }|rt        jN                   nt        j&                  |
|         }tA        jB                  t@        jD                  jF                  ||||
|      }| jQ                  |||      }t@        jR                  jU                  ||      }|rd n|t        jH                  |
|         d d f   }| jW                  ||      }t        jX                  ||      S c c}w )	NFT)deepcopystates	use_cacheg      g      ?rS   )r[   r\   r%   rT   rU   rZ   )-_raise_error_if_multi_objectiver   COMPLETE_get_trialsre   r(   rh   &get_search_space_and_normalized_params	directionr   MINIMIZEr`   arrayr   floatvaluer^   r_   ra   rv   rd   rb   r.   rf   rg   rp   ri   rj   r+   r-   r1   r2   rr   rs   rt   LOG_EIargmax$_get_constraint_vals_and_feasibilityanyinfr   $ConstrainedAcquisitionFunctionParamsfrom_acqf_paramsrO   get_unnormalized_param)r3   r?   r@   r%   r   trialsrx   rM   _sign
score_valsstandardized_score_valsr\   rK   rL   rw   is_feasibleis_all_infeasibler]   r}   normalized_params                       r4   sample_relativezGPSampler.sample_relative   s    	,,U32I%%'""E&D"Qv;///I
 BB6<X	
! >+B+BBXXVTEutE5;;'??TU
,,Z8
#-
0C#Cs3PRPVPVWaPbGc"c$$0S%%BB6
&22364 )-D%,,%%11_5N5N5Z5ZZoo--"&";";$($7$7

 %2! !!)1166==+2#)K ,BII6M,NPQ,QRK+OPUW]+^(O[$&FF;$7 7  1RVVGbff=TU`=a6bE1166==+2#)K '+&G&G!68I'# CCTT4K
 % &ryy1H1U'VXY'YZ   ..{KH55lDTUUC Us   %$L5c                `    | j                  |       | j                  j                  ||||      S r8   )r   r$   sample_independent)r3   r?   r@   
param_nameparam_distributions        r4   r   zGPSampler.sample_independent  s5     	,,U3((;;5*&8
 	
r6   c                <    | j                   j                  ||       y r8   )r$   before_trial)r3   r?   r@   s      r4   r   zGPSampler.before_trial*  s    !!..ue<r6   c                    | j                   t        | j                   |||       | j                  j                  ||||       y r8   )r2   r   r$   after_trial)r3   r?   r@   statevaluess        r4   r   zGPSampler.after_trial-  s?     !!-,T-C-CUESXY!!--eUE6Jr6   )r   z
int | Noner   zBaseSampler | Noner   intr   boolr   z/Callable[[FrozenTrial], Sequence[float]] | NonereturnNone)r   r   )r?   r   r@   r   r   dict[str, BaseDistribution])rK   z 'acqf.AcquisitionFunctionParams'rL   znp.ndarray | Noner   
np.ndarray)rw   r   rx   zgp_search_space.SearchSpacerM   r   r   z$list[acqf.AcquisitionFunctionParams])r?   r   r@   r   r%   r   r   zdict[str, Any])
r?   r   r@   r   r   strr   r   r   r   )r?   r   r@   r   r   r   )
r?   r   r@   r   r   r   r   zSequence[float] | Noner   r   )__name__
__module____qualname____doc__r5   r:   rC   rO   r   r   r   r   r    r6   r4   r   r   /   so   "N  26 "(-LP; ; 0	;
 ; "&; J; 
;6/		#.		$	!5! '! 
	!(2'#2'  ;2' &	2'
 
.2'hUVUV#.UV>YUV	UVn



 

 	


 -

 


=	K	K 	K 		K
 '	K 
	Kr6   r   c                   |D cg c];  }| j                   j                  |j                        j                  t        d      = c}t        fdD              rt        d      t        j                        }t        |j                        dk(  sJ d       t        j                  |dk  d      }t        |t        j                        rJ d	       ||fS c c}w )
Nr   c              3  R   K   | ]  }t        d          t        |      k7     yw)r   N)re   ).0c_constraint_valss     r4   	<genexpr>z7_get_constraint_vals_and_feasibility.<locals>.<genexpr>@  s%     
H!3"#s1v-
Hs   $'z:The number of constraints must be the same for all trials.   z#constraint_vals must be a 2d array.r      rQ   z#MyPy Redefinition for NumPy v2.2.0.)_storageget_trial_system_attrs	_trial_idgetr   r   
ValueErrorr`   r   re   shapeallrn   bool_)r?   r   r@   rw   r   r   s        @r4   r   r   9  s    
  	--eoo>BBCSUWX 
H7G
HHUVVhh/0O$$%*Q,QQ*&&A-A6K+rxx0W2WW0K''s   A C)r?   r   r   zlist[FrozenTrial]r   ztuple[np.ndarray, np.ndarray])/
__future__r   collections.abcr   r   typingr   r   r   numpyr`   r!   optuna._experimentalr	   r
   optuna.distributionsr   optuna.samplers._baser   r   r   "optuna.samplers._lazy_random_stater   optuna.studyr   optuna.trialr   r   r   optuna._gp.acqf_gprr   optuna._gp.gpr^   optuna._gp.optim_mixedrI   optuna._gp.priorr)   optuna._gp.search_spacer%   rh   r   optuna._importsr   rd   r   r   r   r6   r4   <module>r      s    " $ $       3 ; 1 2 B - > ' $ # ""00$$55"+ E!";<O	_	%B67K()D*+E  GFK FK FKR((+("(r6   