
    ,YHh,+                    6   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m	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 d dlmZ d dlmZ d dlmZ e
rd dlmZ e G d d             Z ed       G d de             ZddZ y)    )annotations)Iterable)Sequence)	dataclassN)Any)TYPE_CHECKING)experimental_class)BaseDistribution)CategoricalDistribution)FloatDistribution)IntDistribution)BaseSampler)LazyRandomState)create_trial)FrozenTrial)
TrialState)Studyc                  p    e Zd ZU dZded<   dZded<   dZded<   dd	Zdd
ZddZ		 	 	 	 ddZ
ddZddZy)	_TreeNodeN
str | None
param_namezdict[float, '_TreeNode'] | NonechildrenFbool
is_runningc                   | j                   &|| _        |D ci c]  }|t                c}| _         y | j                  |k7  rt        d| j                   d|       | j                   j	                         t        |      k7  r;t        dt        | j                   j	                                dt        |             y c c}w )Nzparam_name mismatch: z != zsearch_space mismatch: )r   r   r   
ValueErrorkeysset)selfr   search_spacevalues       U/var/www/html/planif/env/lib/python3.12/site-packages/optuna/samplers/_brute_force.pyexpandz_TreeNode.expand)   s     == (DO=IJEUIK/JDM*, #88Ij\!Z[[}}!!#s<'88 -c$--2D2D2F.G-HSQ]M^L_`  9	 Ks   B?c                    d| _         y )NTr   r   s    r"   set_runningz_TreeNode.set_running8   s	        c                (    | j                  d g        y N)r#   r&   s    r"   set_leafz_TreeNode.set_leaf;   s    D"r(   c                    | }|D ]E  \  }}}|j                  ||       |j                  J ||j                  vr y |j                  |   }G |S r*   )r#   r   )r   params_and_search_spacescurrent_noder   r    r!   s         r"   add_pathz_TreeNode.add_path>   sj     /G 	8+Je
L9((444L111'007L	8 r(   c                    | j                   r| j                  rdS dS t        fd| j                   j                         D              S )Nr      c              3  @   K   | ]  }|j                          y wr*   )count_unexpanded).0childexclude_runnings     r"   	<genexpr>z-_TreeNode.count_unexpanded.<locals>.<genexpr>P   s     c5u--o>cs   )r   r   sumvalues)r   r6   s    `r"   r3   z_TreeNode.count_unexpandedK   s>    == 'DOO1BBcDMML`L`Lbcccr(   c                P   | j                   J t        j                  | j                   j                         D cg c]  }|j	                  |       c}t        j
                        t        fdt        | j                   j                               D              r=t        | j                   j                               D ]  \  }}|j                  sd|<    j                         z  |j                  t        | j                   j                                     S c c}w )N)dtypec              3  R   K   | ]  \  }}|j                    xr |   d kD     yw)r   Nr%   )r4   ir!   weightss      r"   r7   z)_TreeNode.sample_child.<locals>.<genexpr>\   s6      
5    3WQZ!^3
s   $'g        )p)r   nparrayr9   r3   float64any	enumerater   r8   choicelistr   )r   rngr6   r5   r=   r>   s        @r"   sample_childz_TreeNode.sample_childR   s    }}(((
 ((BF--BVBVBXYU##O4Y**
  
%dmm&:&:&<=
 

 &dmm&:&:&<= %5##!$GAJ% 	7;;= zz$t}}1134z@@ Zs   D#)r   r   r    zIterable[float]returnNone)rI   rJ   )r-   z,Iterable[tuple[str, Iterable[float], float]]rI   z'_TreeNode' | None)r6   r   rI   int)rG   znp.random.RandomStater6   r   rI   float)__name__
__module____qualname__r   __annotations__r   r   r#   r'   r+   r/   r3   rH    r(   r"   r   r      sS     "J
!04H-4J(T	dAr(   r   z3.1.0c                      e Zd ZdZd	d
dZ	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 ddZe	 	 	 	 	 	 	 	 dd       Z	 	 	 	 	 	 	 	 	 	 ddZ		 	 	 	 	 	 	 	 	 	 ddZ
y)BruteForceSamplera  Sampler using brute force.

    This sampler performs exhaustive search on the defined search space.

    Example:

        .. testcode::

            import optuna


            def objective(trial):
                c = trial.suggest_categorical("c", ["float", "int"])
                if c == "float":
                    return trial.suggest_float("x", 1, 3, step=0.5)
                elif c == "int":
                    a = trial.suggest_int("a", 1, 3)
                    b = trial.suggest_int("b", a, 3)
                    return a + b


            study = optuna.create_study(sampler=optuna.samplers.BruteForceSampler())
            study.optimize(objective)

    Note:
        The defined search space must be finite. Therefore, when using
        :class:`~optuna.distributions.FloatDistribution` or
        :func:`~optuna.trial.Trial.suggest_float`, ``step=None`` is not allowed.

    Note:
        The sampler may fail to try the entire search space in when the suggestion ranges or
        parameters are changed in the same :class:`~optuna.study.Study`.

    Args:
        seed:
            A seed to fix the order of trials as the search order randomly shuffled. Please note
            that it is not recommended using this option in distributed optimization settings since
            this option cannot ensure the order of trials and may increase the number of duplicate
            suggestions during distributed optimization.
        avoid_premature_stop:
            If :obj:`True`, the sampler performs a strict exhaustive search. Please note
            that enabling this option may increase the likelihood of duplicate sampling.
            When this option is not enabled (default), the sampler applies a looser criterion for
            determining when to stop the search, which may result in incomplete coverage of the
            search space. For more information, see https://github.com/optuna/optuna/issues/5780.
    Nc                2    t        |      | _        || _        y r*   )r   _rng_avoid_premature_stop)r   seedavoid_premature_stops      r"   __init__zBruteForceSampler.__init__   s    #D)	%9"r(   c                    i S r*   rQ   )r   studytrials      r"   infer_relative_search_spacez-BruteForceSampler.infer_relative_search_space   	     	r(   c                    i S r*   rQ   )r   r[   r\   r    s       r"   sample_relativez!BruteForceSampler.sample_relative   r^   r(   c                >   |D ]  t        fdj                         D              s&| j                  fdj                  j                         D              }|\j                  j                         r|j                          |j                           y )Nc              3  h   K   | ])  \  }}|j                   v xr j                   |   |k(   + y wr*   )params)r4   r?   vr\   s      r"   r7   z3BruteForceSampler._populate_tree.<locals>.<genexpr>   s3     ]daqELL(AU\\!_-AA]s   /2c              3     K   | ]5  \  }}|vr,|t        |      |j                  j                  |         f 7 y wr*   )_enumerate_candidatesto_internal_reprrc   )r4   r   param_distributionrc   r\   s      r"   r7   z3BruteForceSampler._populate_tree.<locals>.<genexpr>   sM       7
$6!/ #-.@A*;;ELL<TUs   ;>)allitemsr/   distributionsstateis_finishedr+   r'   )treetrialsrc   leafr\   s     ` @r"   _populate_treez BruteForceSampler._populate_tree   s    
  	'E]flln]]== ;@:M:M:S:S:U
D ;;**,MMO$$&'	'r(   c                P   | j                    }|j                  dt        j                  t        j                  t        j
                  t        j                  f      }t               }t        |      }|j                  ||       | j                  |fd|D        j                         |j                  |      dk(  r4|j                  | j                  j                  j!                  |            S |j                  |j#                  | j                  j                  |            S )NFdeepcopystatesc              3  V   K   | ]   }|j                   j                   k7  s| " y wr*   )number)r4   tr\   s     r"   r7   z7BruteForceSampler.sample_independent.<locals>.<genexpr>   s      "QELL8P1"Qs   ))r   )rV   
get_trialsr   COMPLETEPRUNEDRUNNINGFAILr   rf   r#   rq   rc   r3   to_external_reprrU   rG   rE   rH   )	r   r[   r\   r   rh   r6   ro   rn   
candidatess	     `      r"   sample_independentz$BruteForceSampler.sample_independent   s     #888!!##!!""	 " 
 {*+=>
J
+ 	D"Qf"QSXS_S_`  1Q6%66tyy}}7K7KJ7WXX%66!!$))--A r(   c                \   | j                    }|j                  dt        j                  t        j                  t        j
                  t        j                  f      }t               }| j                  |fd|D        i        |j                  |      dk(  r|j                          y y )NFrs   c              3     K   | ]B  }|j                   j                   k7  r|n"t        j                  j                          D yw))rl   r9   rc   rk   N)rw   r   rc   rk   )r4   rx   rl   r\   r9   s     r"   r7   z0BruteForceSampler.after_trial.<locals>.<genexpr>   sP        xx5<</ %#%$||&+&9&9	s   AAr   )rV   ry   r   rz   r{   r|   r}   r   rq   r3   stop)r   r[   r\   rl   r9   r6   ro   rn   s     ```   r"   after_trialzBruteForceSampler.after_trial   s     #888!!##!!""	 " 
 {   	
$   1Q6JJL 7r(   )NF)rW   z
int | NonerX   r   rI   rJ   )r[   r   r\   r   rI   dict[str, BaseDistribution])r[   r   r\   r   r    r   rI   dict[str, Any])rn   r   ro   zIterable[FrozenTrial]rc   r   rI   rJ   )
r[   r   r\   r   r   strrh   r
   rI   r   )
r[   r   r\   r   rl   r   r9   zSequence[float] | NonerI   rJ   )rM   rN   rO   __doc__rY   r]   r`   staticmethodrq   r   r   rQ   r(   r"   rS   rS   h   s    -^:#.	$
#.>Y	
 ''!6'@N'	' '2  	
 - 
@&& & 	&
 '& 
&r(   rS   c                   t        | t              r| j                  t        d      t	        j
                  t        | j                              }t	        j
                  t        | j                              }t	        j
                  t        | j                              }g }|}||k  r%|j                  t        |             ||z  }||k  r%|S t        | t              r7t        t        | j                  | j                  dz   | j                              S t        | t              r't        t        t        | j                                     S t        d|  d      )NzjFloatDistribution.step must be given for BruteForceSampler (otherwise, the search space will be infinite).r1   zUnknown distribution .)
isinstancer   stepr   decimalDecimalr   lowhighappendrL   r   rF   ranger   lenchoices)rh   r   r   r   retr!   s         r"   rf   rf   
  s6   $&78""*C  ooc"4"8"89:s#5#:#:;<s#5#:#:;<tmJJuU|$TME tm 
	&	8$((*<*A*AA*EGYG^G^_
 	
 
&(?	@E#0889:;;01C0DAFGGr(   )rh   r
   rI   zSequence[float])!
__future__r   collections.abcr   r   dataclassesr   r   typingr   r   numpyr@   optuna._experimentalr	   optuna.distributionsr
   r   r   r   optuna.samplersr   "optuna.samplers._lazy_random_stater   optuna.trialr   r   r   optuna.studyr   r   rS   rf   rQ   r(   r"   <module>r      s    " $ $ !      3 1 8 2 0 ' > % $ # " HA HA HAV G^ ^ ^BHr(   