
    -ji+                    D   S SK Jr  S SKJr  S SKJr  S SKJr  S SKrS SK	r	S SK
Jr  S SKJr  S SKJr  \(       a  S S	KJr   S       SS
 jjr S     SS jjrSSS.       SS jjrSS jrSS jrSS jrSS jrSS.     SS jjr        SS jrSS jrg)    )annotations)Sequence)cast)TYPE_CHECKINGN)_get_feasible_trials)StudyDirection)
TrialState)FrozenTrialc                F  ^ U  Vs/ s H$  o3R                   [        R                  :X  d  M"  UPM&     n nU(       a  [        U 5      n [	        U 5      S:X  a  / $ [        U4S jU  5       5      (       a  [        S5      e[        R                  " U  VVVs/ s H7  n[        UR                  T5       VVs/ s H  u  pE[        XE5      PM     snnPM9     snnn5      n[        USS9n[        X5       VVs/ s H  u  p8U(       d  M  UPM     snn$ s  snf s  snnf s  snnnf s  snnf )Nr   c              3  f   >#    U  H&  n[        UR                  5      [        T5      :g  v   M(     g 7fN)lenvalues).0t
directionss     W/home/james-whalen/.local/lib/python3.13/site-packages/optuna/study/_multi_objective.py	<genexpr>5_get_pareto_front_trials_by_trials.<locals>.<genexpr>   s"     
<V3qxx=C
O+Vs   .1zLThe number of the values and the number of the objectives must be identical.Fassume_unique_lexsorted)stater	   COMPLETEr   r   any
ValueErrornpasarrayzipr   _normalize_value_is_pareto_front)	trialsr   consider_constraintr   vdloss_valueson_front	is_paretos	    `       r   "_get_pareto_front_trials_by_trialsr(      s      BA77j.A.A#AaFB%f-
6{a	

<V
<<<Z
 	
 **QWXQWAS:-F	G-FTQ
1
 -F	GQWXK  UKH"%f"7E"7,!9A"7EE C 
HX Fs.   !DDD0DD0DDDc                D    [        U R                  U R                  U5      $ r   )r(   r!   r   )studyr"   s     r   _get_pareto_front_trialsr+   +   s     .ellE<L<LNabb    )penaltyn_belowc                  [        U 5      S:X  a  [        R                  " / [        S9$ U=(       d    [        U 5      nUS:  d   S5       eUc	  [	        XS9$ [        U5      [        U 5      :w  a&  [        S[        U5      < S[        U 5      < S35      e[        R                  " [        U 5      S	[        S9n[        R                  " U5      n[        R                  " U) US:*  5      n[        R                  " U) US:  5      n[	        X   US9X5'   U[        [        R                  " U5      5      -  n[        R                  " X5   S	S
9S-   nU[	        X   SS2[        R                  4   US9-   X6'   U[        [        R                  " U5      5      -  n[        R                  " X4)    S	S
9S-   nU[	        X   US9-   X4'   [        R                  " US	:g  5      (       d   S5       eU$ )a  Calculate non-domination rank based on the fast non-dominated sort algorithm.

The fast non-dominated sort algorithm assigns a rank to each trial based on the dominance
relationship of the trials, determined by the objective values and the penalty values. The
algorithm is based on `the constrained NSGA-II algorithm
<https://doi.org/10.1109/4235.99601>`__, but the handling of the case when penalty
values are None is different. The algorithm assigns the rank according to the following
rules:

1. Feasible trials: First, the algorithm assigns the rank to feasible trials, whose penalty
    values are less than or equal to 0, according to unconstrained version of fast non-
    dominated sort.
2. Infeasible trials: Next, the algorithm assigns the rank from the minimum penalty value of to
    the maximum penalty value.
3. Trials with no penalty information (constraints value is None): Finally, The algorithm
    assigns the rank to trials with no penalty information according to unconstrained version
    of fast non-dominated sort. Note that only this step is different from the original
    constrained NSGA-II algorithm.
Plus, the algorithm terminates whenever the number of sorted trials reaches n_below.

Args:
    loss_values:
        Objective values, which is better when it is lower, of each trials.
    penalty:
        Constraints values of each trials. Defaults to None.
    n_below: The minimum number of top trials required to be sorted. The algorithm will
        terminate when the number of sorted trials reaches n_below. Defaults to None.

Returns:
    An ndarray in the shape of (n_trials,), where each element is the non-domination rank of
    each trial. The rank is 0-indexed. This function guarantees the correctness of the ranks
    only up to the top-``n_below`` solutions. If a solution's rank is worse than the
    top-``n_below`` solution, its rank will be guaranteed to be greater than the rank of
    the top-``n_below`` solution.
r   dtypez#n_below must be a positive integer.Nr.   zIThe length of penalty and loss_values must be same, but got len(penalty)=z and len(loss_values)=.)initial   zAll the rank must be updated.)r   r   arrayint_calculate_nondomination_rankr   fullisnanlogical_andcount_nonzeromaxnewaxisall)	r%   r-   r.   ranksis_penalty_nanis_feasibleis_infeasibletop_rank_infeasibletop_rank_penalty_nans	            r   _fast_non_domination_rankrG   1   s   L ;1xx#&&)[)GQ;===;,[JJ
7|s;''7|o43{#3"5Q8
 	

 GGC$b4EXXg&N...'Q,?KNNN?GaK@M 7{7OY`aEs2##K011G &&!3R@1D.1Nq"**}-w2 E s2##M233G 66%"8"EI03P#W4 E 66%2+? ??Lr,   c                   U S S 2SS 24   nUR                   S   n[        R                  " U[        S9n[        R                  " U5      n[        U5      (       a  SX4S   =n'   [        R                  " X   X   :  SS9n[        [        R                  [        [           [        R                  [        R                     4   XF   5      n[        U5      (       a  M  U$ )Nr6   r   r0   T)axis)shaper   zerosboolaranger   r   r   ndarraytupler8   r1   signedinteger)unique_lexsorted_loss_valuesr%   n_trialsr&   remaining_indicesnew_nondominated_indexnondominated_and_not_tops          r   _is_pareto_front_ndrV      s     /q!"u5K  #Hxx-HLNIIV^L_

 
  FJa,@@(B#%66*[-PPWX$
  !JJuSz288B,<,<#==>7
 
 
  Or,   c                    U R                   S   n[        R                  R                  U S S 2S4   5      n[        R                  " U[
        S9nUSS  US S :  USS & U$ )Nr   r6   r0   r4   )rJ   r   minimum
accumulateonesrL   )rQ   rR   cummin_value1r&   s       r   _is_pareto_front_2dr\      sc    +11!4HJJ))*Fq!t*LMMwwxt,H $}Sb'99HQRLOr,   c                    U R                   u  pUS:X  a)  [        R                  " [        U 5      [        S9nSUS'   U$ US:X  a  [        U 5      $ [        U 5      $ )Nr6   r0   Tr      )rJ   r   rK   r   rL   r\   rV   )rQ   rR   n_objectivesr&   s       r   "_is_pareto_front_for_unique_sortedr`      s\    ;AAXq88C <=TJ		"#?@@"#?@@r,   c                    U(       a  [        U 5      $ [        R                  " U SSS9u  p#[        U5      nXCR                  S5         $ )Nr   T)rI   return_inverser4   )r`   r   uniquereshape)r%   r   rQ   	order_invr&   s        r   r    r       sJ    
 1+>>.0ii!\`.a+ 12NOH %%b)**r,   r2   c                  [        U 5      S:X  d	  Ub(  US::  a"  [        R                  " [        U 5      [        S9$ U R                  u  p#US:X  a   [        R
                  " U S S 2S4   SS9u  pEU$ [        R
                  " U SSS9u  pgUR                  S   n[        U=(       d    [        U5      [        U5      5      n[        R                  " U[        S9nSn	[        R                  " U5      n
XR                  -
  U:  a3  [        USS9nXX   '   X)    n
Xk)    nU	S-  n	XR                  -
  U:  a  M3  XU
'   XWR                  S5         $ )	Nr   r0   r6   T)rb   )rb   rI   r   r4   )r   r   rK   r8   rJ   rc   minrM   sizer    rd   )r%   r.   rR   r_   _rA   rQ   re   n_uniquerankindicesr&   s               r   r9   r9      sH    ;1!4AxxK(44*00Xq99[A.tD /1iiTX_`.a+ +11!4H'>S!=>D`@abGHHXS)EDii!G
\\
!G
+#$@Z^_#'g )$'CI'N$	 \\
!G
+ 'N ""2&''r,   c                H   U R                   nUR                   nU R                  [        R                  :w  a  gUR                  [        R                  :w  a  gUc   eUc   e[	        U5      [	        U5      :w  a  [        S5      e[	        U5      [	        U5      :w  a  [        S5      e[        X25       VVs/ s H  u  pV[        XV5      PM     nnn[        XB5       VVs/ s H  u  pV[        XV5      PM     nnnXx:X  a  g[        S [        Xx5       5       5      $ s  snnf s  snnf )NFTz?Trials with different numbers of objectives cannot be compared.zIThe number of the values and the number of the objectives are mismatched.c              3  .   #    U  H  u  pX:*  v   M     g 7fr    )r   v0v1s      r   r   _dominates.<locals>.<genexpr>   s     R&QFBrx&Qs   )	r   r   r	   r   r   r   r   r   r@   )	trial0trial1r   values0values1r#   r$   normalized_values0normalized_values1s	            r   
_dominatesry      s    mmGmmG||z***||z***
7|s7|#Z[[
7|s:&W
 	
 >A=UV=UTQ*10=UV=@=UV=UTQ*10=UV/Rc*<&QRRR WVs   5DDc                N    U c  [        S5      $ U[        R                  L a  U * n U $ )Ninf)floatr   MAXIMIZE)value	directions     r   r   r      s+    }U|N+++Lr,   )F)r!   zSequence[FrozenTrial]r   Sequence[StudyDirection]r"   rL   returnlist[FrozenTrial])r*   z'optuna.study.Study'r"   rL   r   r   )r%   
np.ndarrayr-   znp.ndarray | Noner.   
int | Noner   r   )rQ   r   r   r   )r%   r   r   rL   r   r   )r%   r   r.   r   r   r   )rs   r
   rt   r
   r   r   r   rL   )r~   zfloat | Noner   r   r   r|   )
__future__r   collections.abcr   typingr   r   numpyr   optuna&optuna.study._constrained_optimizationr   optuna.study._study_directionr   optuna.trialr	   r
   r(   r+   rG   rV   r\   r`   r    r9   ry   r   ro   r,   r   <module>r      s   " $      G 8 # ( !&F!F(F F 	F2 >Ccc6:cc >BY]KK):KLVKK\0	A+" 7; ( ()3 ( (FSS!,S:RS	S@r,   