Ë
    ÿÿæió+  ã                  ó  — 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 erd d	lmZ 	 d	 	 	 	 	 	 	 dd
„Z	 d	 	 	 	 	 dd„Zdddœ	 	 	 	 	 	 	 dd„Zdd„Zdd„Zdd„Zdd„Zddœ	 	 	 	 	 dd„Z	 	 	 	 	 	 	 	 dd„Zdd„Zy)é    )Úannotations)ÚSequence)Úcast)ÚTYPE_CHECKINGN)Ú_get_feasible_trials)ÚStudyDirection)Ú
TrialState)ÚFrozenTrialc                ó  ‡— | D �cg c]"  }|j                   t        j                  k(  sŒ!|‘Œ$ } }|rt        | «      } t	        | «      dk(  rg S t        ˆfd„| D «       «      rt        d«      ‚t        j                  | D ���cg c]5  }t        |j                  ‰«      D ��cg c]  \  }}t        ||«      ‘Œ c}}‘Œ7 c}}}«      }t        |d¬«      }t        | |«      D ��cg c]
  \  }}|sŒ	|‘Œ c}}S c c}w c c}}w c c}}}w c c}}w )Nr   c              3  ó`   •K  — | ]%  }t        |j                  «      t        ‰«      k7  –— Œ' y ­w©N)ÚlenÚvalues)Ú.0ÚtÚ
directionss     €úr/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/optuna/study/_multi_objective.pyÚ	<genexpr>z5_get_pareto_front_trials_by_trials.<locals>.<genexpr>   s#   øè ø€ Ð
<±V°Œ3ˆq�x‰x‹=œC 
›OÕ+±Vùs   ƒ+.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™�A 1§7¡7¬j×.AÑ.AÓ#AŠa˜€FÐBÙÜ% fÓ-ˆÜ
ˆ6ƒ{�aÒØˆ	ä
Ó
<±VÓ
<Ô<ÜØZó
ð 	
ô —*‘*ÙQWÕXÑQWÈA¬S°·±¸:Ô-FÔ	GÑ-F¡T Q¨Ô
˜1˜aÕ
 Ð-FÔ	GÐQWÓXó€Kô   ÀUÔK€HÜ"% f¨hÔ"7ÔEÑ"7‘,�!�Yº9ŠAÐ"7ÒEÐEùò Cùó 
HùÔXùó Fs.   †"C2©C2Â C=ÂC7Â4C=Ã
DÃ*DÃ7C=c                óD   — t        | j                  | j                  |«      S r   )r'   r    r   )Ústudyr!   s     r   Ú_get_pareto_front_trialsr*   +   s   € ô .¨e¯l©l¸E×<LÑ<LÐNaÓbÐbó    )ÚpenaltyÚn_belowc               óÞ  — t        | «      dk(  rt        j                  g t        ¬«      S |xs t        | «      }|dkD  sJ d«       ‚|€t	        | |¬«      S t        |«      t        | «      k7  r$t        dt        |«      ›dt        | «      ›d�«      ‚t        j                  t        | «      d	t        ¬«      }t        j                  |«      }t        j                  | |dk  «      }t        j                  | |dkD  «      }t	        | |   |¬«      ||<   |t        t        j                  |«      «      z  }t        j                  ||   d	¬
«      dz   }|t	        ||   dd…t        j                  f   |¬«      z   ||<   |t        t        j                  |«      «      z  }t        j                  ||    d	¬
«      dz   }|t	        | |   |¬«      z   ||<   t        j                  |d	k7  «      sJ d«       ‚|S )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_rankrF   1   sæ  € ôL ˆ;Ó˜1ÒÜ�x‰x˜¤#Ô&Ð&àÒ)œ˜[Ó)€GØ�QŠ;Ð=Ð=Ó=ˆ;à€Ü,¨[À'ÔJÐJä
ˆ7ƒ|”s˜;Ó'Ò'ÜðÜ�7‹|ˆoÐ4¤3 {Ó#3Ð"5°Qð8ó
ð 	
ô
 �G‰G”C˜Ó$ b´Ô4€EÜ—X‘X˜gÓ&€NÜ—.‘. . °'¸Q±,Ó?€KÜ—N‘N N ?°G¸a±KÓ@€Mô 7°{À;Ñ7OÐY`Ôa€Eˆ+ÑØŒs”2×#Ñ# KÓ0Ó1Ñ1€Gô Ÿ&™&  {Ñ!3¸RÔ@À1ÑDÐØ.Ô1NØ�Ñšq¤"§*¡*˜}Ñ-°wô2ñ €Eˆ-Ñð Œs”2×#Ñ# MÓ2Ó3Ñ3€Gô Ÿ6™6 %¨¨Ñ"8À"ÔEÈÑIÐØ0Ô3PØ�NÑ#¨Wô4ñ €Eˆ.Ñô �6‰6�%˜2‘+ÔÐ?Ð ?Ó?ÐØ€Lr+   c                ó´  — | d d …dd …f   }|j                   d   }t        j                  |t        ¬«      }t        j                  |«      }t        |«      r‚d||d   x}<   t        j                  ||   ||   k  d¬«      }t        t        j                  t        t           t        j                  t        j                     f   ||   «      }t        |«      rŒ‚|S )Nr5   r   r/   T)Úaxis)Úshaper   ÚzerosÚboolÚaranger   r   r   ÚndarrayÚtupler7   r0   Úsignedinteger)Úunique_lexsorted_loss_valuesr$   Ún_trialsr%   Úremaining_indicesÚnew_nondominated_indexÚnondominated_and_not_tops          r   Ú_is_pareto_front_ndrU      sÙ   € ð /ªq°!±"¨uÑ5€KØ× Ñ  Ñ#€HÜ�x‰x˜¬Ô-€HÜLNÏIÉIÐV^ÓL_Ðä
ÐÔ
 ð FJˆÐ,=¸aÑ,@Ð@Ð(ÑBÜ#%§6¡6ØÐ)Ñ*¨[Ð9OÑ-PÑPÐWXô$
Ð ô !Ü�J‰J”uœS‘z¤2§8¡8¬B×,<Ñ,<Ñ#=Ð=Ñ>ØÐ6Ñ7ó
Ðô ÐÕ
 ð €Or+   c                óÂ   — | j                   d   }t        j                  j                  | d d …df   «      }t        j                  |t
        ¬«      }|dd  |d d k  |dd  |S )Nr   r5   r/   r3   )rI   r   ÚminimumÚ
accumulateÚonesrK   )rP   rQ   Úcummin_value1r%   s       r   Ú_is_pareto_front_2dr[   —   sc   € Ø+×1Ñ1°!Ñ4€HÜ—J‘J×)Ñ)Ð*FÂqÈ!ÀtÑ*LÓM€MÜ�w‰w�x¤tÔ,€HØ   Ð$ }°S°bÐ'9Ñ9€HˆQˆR€LØ€Or+   c                ó¶   — | j                   \  }}|dk(  r+t        j                  t        | «      t        ¬«      }d|d<   |S |dk(  rt        | «      S t        | «      S )Nr5   r/   Tr   é   )rI   r   rJ   r   rK   r[   rU   )rP   rQ   Ún_objectivesr%   s       r   Ú"_is_pareto_front_for_unique_sortedr_   Ÿ   s^   € Ø;×AÑAÑ€Xˆ|Ø�qÒÜ—8‘8œCÐ <Ó=ÄTÔJˆØˆ�‰ØˆØ	˜Ò	Ü"Ð#?Ó@Ð@ä"Ð#?Ó@Ð@r+   c                ó�   — |rt        | «      S t        j                  | dd¬«      \  }}t        |«      }||j                  d«         S )Nr   T)rH   Úreturn_inverser3   )r_   r   ÚuniqueÚreshape)r$   r   rP   Ú	order_invr%   s        r   r   r   «   sN   € ñ
 Ü1°+Ó>Ð>ä.0¯i©i¸È!Ð\`Ô.aÑ+Ð  )Ü1Ð2NÓO€Hð �I×%Ñ% bÓ)Ñ*Ð*r+   r1   c               ó¦  — t        | «      dk(  s|�)|dk  r$t        j                  t        | «      t        ¬«      S | j                  \  }}|dk(  r#t        j
                  | d d …df   d¬«      \  }}|S t        j
                  | dd¬«      \  }}|j                  d   }t        |xs t        |«      t        |«      «      }t        j                  |t        ¬«      }d}	t        j                  |«      }
||
j                  z
  |k  r9t        |d¬«      }|	||
|   <   |
|    }
||    }|	dz  }	||
j                  z
  |k  rŒ9|	||
<   ||j                  d«         S )	Nr   r/   r5   T)ra   )ra   rH   r   r3   )r   r   rJ   r7   rI   rb   ÚminrL   Úsizer   rc   )r$   r-   rQ   r^   Ú_r@   rP   rd   Ún_uniqueÚrankÚindicesr%   s               r   r8   r8   »   s^  € ô ˆ;Ó˜1Ò Ð!4¸ÀAºÜ�x‰xœ˜KÓ(´Ô4Ð4à*×0Ñ0Ñ€Xˆ|Ø�qÒÜ—9‘9˜[ª¨A¨Ñ.¸tÔD‰ˆˆ5Øˆô /1¯i©i¸ÐTXÐ_`Ô.aÑ+Ð  )Ø+×1Ñ1°!Ñ4€Hä�'Ò>œSÐ!=Ó>ÄÐD`Ó@aÓb€GÜ�H‰H�X¤SÔ)€EØ€DÜ�i‰i˜Ó!€GØ
�W—\‘\Ñ
! GÒ
+Ü#Ð$@ÐZ^Ô_ˆØ#'ˆˆg�hÑÑ à˜8˜)Ñ$ˆØ'CÀXÀIÑ'NÐ$Ø�‰	ˆð �W—\‘\Ñ
! GÓ
+ð €Eˆ'�Nð �×"Ñ" 2Ó&Ñ'Ð'r+   c                ó>  — | j                   }|j                   }| j                  t        j                  k7  ry|j                  t        j                  k7  ry|€J ‚|€J ‚t	        |«      t	        |«      k7  rt        d«      ‚t	        |«      t	        |«      k7  rt        d«      ‚t        ||«      D ��cg c]  \  }}t        ||«      ‘Œ }}}t        ||«      D ��cg c]  \  }}t        ||«      ‘Œ }}}||k(  ryt        d„ t        ||«      D «       «      S c c}}w c c}}w )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  ó,   K  — | ]  \  }}||k  –— Œ y ­wr   © )r   Úv0Úv1s      r   r   z_dominates.<locals>.<genexpr>û   s   è ø€ ÐRÑ&Q™F˜B ˆr�R�xÑ&Qùs   ‚)	r   r   r	   r   r   r   r   r   r?   )	Útrial0Útrial1r   Úvalues0Úvalues1r"   r#   Únormalized_values0Únormalized_values1s	            r   Ú
_dominatesrw   Þ   s  € ð �m‰m€GØ�m‰m€Gà‡|�|”z×*Ñ*Ò*Øà‡|�|”z×*Ñ*Ò*ØàÐÐÐØÐÐÐä
ˆ7ƒ|”s˜7“|Ò#ÜÐZÓ[Ð[ä
ˆ7ƒ|”s˜:“Ò&ÜØWó
ð 	
ô >AÀÈ*Ô=UÔVÑ=U±T°Q¸Ô*¨1¨aÕ0Ð=UÐÑVÜ=@ÀÈ*Ô=UÔVÑ=U±T°Q¸Ô*¨1¨aÕ0Ð=UÐÑVàÐ/Ò/ØäÑR¤cÐ*<Ð>PÔ&QÓRÓRÐRùó WùÛVs   Â0DÃDc                óJ   — | €t        d«      S |t        j                  u r|  } | S )NÚinf)Úfloatr   ÚMAXIMIZE)ÚvalueÚ	directions     r   r   r   þ   s+   € Ø€}Ü�U‹|Ðà”N×+Ñ+Ñ+Ø�ˆà€Lr+   )F)r    zSequence[FrozenTrial]r   úSequence[StudyDirection]r!   rK   Úreturnúlist[FrozenTrial])r)   z'optuna.study.Study'r!   rK   r   r€   )r$   ú
np.ndarrayr,   znp.ndarray | Noner-   ú
int | Noner   r�   )rP   r�   r   r�   )r$   r�   r   rK   r   r�   )r$   r�   r-   r‚   r   r�   )rq   r
   rr   r
   r   r~   r   rK   )r|   zfloat | Noner}   r   r   rz   )Ú
__future__r   Úcollections.abcr   Útypingr   r   Únumpyr   ÚoptunaÚ&optuna.study._constrained_optimizationr   Úoptuna.study._study_directionr   Úoptuna.trialr	   r
   r'   r*   rF   rU   r[   r_   r   r8   rw   r   rn   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+   