o
    }oi                     @  s   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rDd dlmZ edG dd deZdS )    )annotations)Sequence)Any)TYPE_CHECKING)experimental_class)optuna_warn)BaseDistribution)BaseSampler)FrozenTrial)
TrialState)Studyz2.4.0c                   @  sV   e Zd ZdZd&dd	Zd'd
dZd(ddZd)ddZd*ddZd+ddZ	d,d#d$Z
d%S )-PartialFixedSamplera  Sampler with partially fixed parameters.

    Example:

        After several steps of optimization, you can fix the value of ``y`` and re-optimize it.

        .. testcode::

            import optuna


            def objective(trial):
                x = trial.suggest_float("x", -1, 1)
                y = trial.suggest_int("y", -1, 1)
                return x**2 + y


            study = optuna.create_study()
            study.optimize(objective, n_trials=10)

            best_params = study.best_params
            fixed_params = {"y": best_params["y"]}
            partial_sampler = optuna.samplers.PartialFixedSampler(fixed_params, study.sampler)

            study.sampler = partial_sampler
            study.optimize(objective, n_trials=10)

    Args:

        fixed_params:
            A dictionary of parameters to be fixed.

        base_sampler:
            A sampler which samples unfixed parameters.

    fixed_paramsdict[str, Any]base_samplerr	   returnNonec                 C  s   || _ || _d S N)_fixed_params_base_sampler)selfr   r    r   R/home/ubuntu/.local/lib/python3.10/site-packages/optuna/samplers/_partial_fixed.py__init__:   s   
zPartialFixedSampler.__init__c                 C  s   | j   d S r   )r   
reseed_rng)r   r   r   r   r   >   s   zPartialFixedSampler.reseed_rngstudyr   trialr
   dict[str, BaseDistribution]c                 C  s0   | j ||}| j D ]	}||v r||= q|S r   )r   infer_relative_search_spacer   keys)r   r   r   search_space
param_namer   r   r   r   A   s   z/PartialFixedSampler.infer_relative_search_spacer    c                 C  s   | j |||S r   )r   sample_relative)r   r   r   r    r   r   r   r"   M   s   z#PartialFixedSampler.sample_relativer!   strparam_distributionr   r   c                 C  s^   || j vr| j||||S | j | }||}||}|s-td| d| d| d |S )NzFixed parameter 'z' with value z" is out of range for distribution .)r   r   sample_independentto_internal_repr	_containsr   )r   r   r   r!   r$   param_valueparam_value_in_internal_repr	containedr   r   r   r&   V   s   



z&PartialFixedSampler.sample_independentc                 C  s   | j || d S r   )r   before_trial)r   r   r   r   r   r   r,   q   s   z PartialFixedSampler.before_trialstater   valuesSequence[float] | Nonec                 C  s   | j |||| d S r   )r   after_trial)r   r   r   r-   r.   r   r   r   r0   t   s   zPartialFixedSampler.after_trialN)r   r   r   r	   r   r   )r   r   )r   r   r   r
   r   r   )r   r   r   r
   r    r   r   r   )
r   r   r   r
   r!   r#   r$   r   r   r   )r   r   r   r
   r   r   )
r   r   r   r
   r-   r   r.   r/   r   r   )__name__
__module____qualname____doc__r   r   r   r"   r&   r,   r0   r   r   r   r   r      s    
%



	
r   N)
__future__r   collections.abcr   typingr   r   optuna._experimentalr   optuna._warningsr   optuna.distributionsr   optuna.samplersr	   optuna.trialr
   r   optuna.studyr   r   r   r   r   r   <module>   s    