
    Q-j/3                       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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 er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d i ejF                  dk\  rddini  G d d             Z$	 	 	 	 	 	 d!dZ% ed       G d de             Z&d"dZ'd#dZ(y)$    )annotations)	dataclassN)Real)Any)TYPE_CHECKING)experimental_class)CategoricalChoiceType)CategoricalDistribution)FloatDistribution)IntDistribution)BaseSampler)LazyRandomState)create_trial)
TrialState)Iterable)Sequence)BaseDistribution)Study)FrozenTrial)   
   slotsTc                  x    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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       j/Users/jameslopez/projects/TradingBot25/.venv/lib/python3.12/site-packages/optuna/samplers/_brute_force.pyexpandz_TreeNode.expand6   s     == (DO=IJ\EUIK/\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_runningE   s	        c                (    | j                  d g        y N)r(   r+   s    r'   set_leafz_TreeNode.set_leafH   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_pathK   sf     /G+Je
L9((444L111'007L 0H r-   c                    | j                   x} xs | j                   S t        fd|j                         D              S )Nc              3  @   K   | ]  }|j                          y wr/   )is_any_expandable.0childexclude_runnings     r'   	<genexpr>z._TreeNode.is_any_expandable.<locals>.<genexpr>[   s     [IZ5**?;IZ   )r   r   anyvalues)r$   r;   r   s    ` r'   r7   z_TreeNode.is_any_expandableX   s?    %H.&&=doo*==[IZ[[[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_unexpandedr8   s     r'   r<   z-_TreeNode.count_unexpanded.<locals>.<genexpr>b   s     cLb5u--o>Lbr=   )r   r   sumr?   )r$   r;   s    `r'   rC   z_TreeNode.count_unexpanded]   s>    == 'DOO1BBcDMML`L`Lbcccr-   c                  	 | j                   J t        j                  | j                   j                         D cg c]  }|j	                  |       c}t        j
                        }d}||j                         z  }t        j                  |dkD  dd      }||j                         z  }d|z
  |z  ||z  z   	t        	fdt        | j                   j                               D              r=t        | j                   j                               D ]  \  }}|j                  sd	|<    		j                         z  	|j                  t        | j                   j                               	      j                         S c c}w )N)dtypeg      ?r   g      ?g        c              3  R   K   | ]  \  }}|j                    xr |   d kD     yw)r   Nr*   )r9   ir&   weightss      r'   r<   z)_TreeNode.sample_child.<locals>.<genexpr>t   s6      
=5    3WQZ!^3=   $')p)r   nparrayr?   rC   float64rD   wherer>   	enumerater   choicelistr"   item)
r$   rngr;   r:   unexpanded_countsalphaweights_origweights_flatrH   rI   s
            @r'   sample_childz_TreeNode.sample_childd   sT   }}(((HHBF--BVBVBXYBXU##O4BXY**
 (+<+@+@+BBxx 1A 5sC@((**;,.1EE 
%dmm&:&:&<=
 

 &dmm&:&:&<=5##!$GAJ > 	7;;= zz$t}}1134z@EEGG+ Zs   F)r   r   r%   zIterable[float]returnNone)rZ   r[   )r2   z,Iterable[tuple[str, Iterable[float], float]]rZ   z_TreeNode | None)r;   r   rZ   r   )r;   r   rZ   int)rT   znp.random.RandomStater;   r   rZ   float)__name__
__module____qualname__r   __annotations__r   r   r(   r,   r0   r4   r7   rC   rY    r-   r'   r   r   "   sY      "J
!04H-4J(T	\
dHr-   r   c                d   t         j                  t         j                  t         j                  t         j                  f}| j
                  j                  | j                  d|      }t        dt        |      dz         D ])  }||    }|j                  |k(  s|t        |      |z
  fc S  J d       )NF)deepcopystatesrA   zShould not reach)r   COMPLETEPRUNEDRUNNINGFAIL_storageget_all_trials	_study_idrangelennumber)studycurrent_trial_numberre   trialsrH   ts         r'   /_get_non_waiting_trials_and_current_trial_indexrt      s     !!:#4#4j6H6H*//ZF^^**5??USY*ZF1c&kAo&A2J88++3v;?**	 '
 %$$5r-   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 that performs exhaustive search over the define-by-run search space.

    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/   rb   )r$   rp   trials      r'   infer_relative_search_spacez-BruteForceSampler.infer_relative_search_space   	     	r-   c                    i S r/   rb   )r$   rp   r~   r%   s       r'   sample_relativez!BruteForceSampler.sample_relative   r   r-   c                @  	
 j                         }|D ci c]  \  }}t        |      r|| c}}j                         }|D cg c]  \  }}t        |      s| }}}|D ]  		j                  
r)|
j                         k  s%t        
fd|D              s:| j	                  	fd	j
                  j                         D              }|p	j                  j                         r|j                          |j                           y c c}}w c c}}w )Nc              3  R   K   | ]  }t        j                  |               y wr/   )_is_nanget)r9   rK   trial_paramss     r'   r<   z3BruteForceSampler._populate_tree.<locals>.<genexpr>   s"     QA7<#3#3A#67rJ   c              3     K   | ]5  \  }}|vr,|t        |      |j                  j                  |         f 7 y wr/   )_enumerate_candidatesto_internal_reprparams)r9   r   param_distributionr   r~   s      r'   r<   z3BruteForceSampler._populate_tree.<locals>.<genexpr>   sR       ;V6
$6!/ #-.@A*;;ELL<TU
 ;Vs   ;>)
itemsr   r   allr4   distributionsstateis_finishedr0   r,   )treerr   r   params_itemskvnonnan_params_itemsnan_param_namesleafr~   r   s     `      @@r'   _populate_treez BruteForceSampler._populate_tree   s     ||~0<O1GAJq!tOUUW)5DA1DE <<L+|/A/A/CCQQQ== ;@:M:M:S:S:U
D ;;**,MMO$$&/  PDs   DD	DDc                   | j                    }t        ||j                        \  }}|j                  |       t	               }t        |      }	|j                  ||	       | j                  |||j                         |j                  |      sB|j                  | j                  j                  j                  |	      j                               S |j                  |j                  | j                  j                  |            S r/   )ry   rt   ro   popr   r   r(   r   r   r7   to_external_reprrx   rT   rQ   rS   rY   )
r$   rp   r~   r   r   r;   rr   current_idxr   
candidatess
             r'   sample_independentz$BruteForceSampler.sample_independent   s     #888MeUZUaUab

;{*+=>
J
+ 	D&%,,7%%o6%66tyy}}7K7KJ7W7\7\7^__%66!!$))--A r-   c                   | j                    }t        ||j                        \  }}t        |||j                  |j
                        ||<   t               }| j                  ||i        |j                  |      s|j                          y y )N)r   r?   r   r   )
ry   rt   ro   r   r   r   r   r   r7   stop)	r$   rp   r~   r   r?   r;   rr   r   r   s	            r'   after_trialzBruteForceSampler.after_trial  s     #888MeUZUaUab*u||5K^K^
{ {D&"-%%o6JJL 7r-   )NF)rz   z
int | Noner{   r   rZ   r[   )rp   r   r~   r   rZ   dict[str, BaseDistribution])rp   r   r~   r   r%   r   rZ   dict[str, Any])r   r   rr   zlist[FrozenTrial]r   r   rZ   r[   )
rp   r   r~   r   r   strr   r   rZ   r   )
rp   r   r~   r   r   r   r?   zSequence[float] | NonerZ   r[   )r^   r_   r`   __doc__r|   r   r   staticmethodr   r   r   rb   r-   r'   rv   rv      s    -^:#.	$
#.>Y	
 ' '<  	
 - 
0#.7AKa	r-   rv   c                b    t        | t              xr t        j                  t	        |             S r/   )
isinstancer   mathisnanr]   )r   s    r'   r   r     s!    a74::eAh#77r-   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).rA   zUnknown distribution .)r   r   stepr!   decimalDecimalr   lowhighappendr]   r   rR   rm   r
   rn   choices)r   r   r   r   retr&   s         r'   r   r     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-   rb   )rp   r   rq   r\   rZ   ztuple[list[FrozenTrial], int])r   r	   rZ   r   )r   r   rZ   zSequence[float]))
__future__r   dataclassesr   r   r   numbersr   systypingr   r   numpyrL   optuna._experimentalr   optuna.distributionsr	   r
   r   r   optuna.samplersr   "optuna.samplers._lazy_random_stater   optuna.trialr   r   collections.abcr   r   r   optuna.studyr   r   version_infor   rt   rv   r   r   rb   r-   r'   <module>r      s    " !    
     3 6 8 2 0 ' > % # ((5"( F!1!1W!<wo"FZH ZH GZHz%%(+%"%" GA A AH8Hr-   