o
    /wi,                     @  s   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 d dlmZ 	d9d:ddZ	d9d;ddZdddd<ddZd=d!d"Zd=d#d$Zd=d%d&Zd>d(d)Zdd*d?d+d,Zd@d0d1ZdAd7d8ZdS )B    )annotations)SequenceN)_get_feasible_trials)StudyDirection)FrozenTrial)
TrialStateFtrialsSequence[FrozenTrial]
directionsSequence[StudyDirection]consider_constraintboolreturnlist[FrozenTrial]c                   s   dd | D } |rt | } t| dkrg S t fdd| D r$tdt fdd| D }t|dd	}d
d t| |D S )Nc                 S  s   g | ]
}|j tjkr|qS  )stater   COMPLETE.0tr   r   Z/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/optuna/study/_multi_objective.py
<listcomp>   s    z6_get_pareto_front_trials_by_trials.<locals>.<listcomp>r   c                 3  s"    | ]}t |jt  kV  qd S N)lenvaluesr   r
   r   r   	<genexpr>   s     z5_get_pareto_front_trials_by_trials.<locals>.<genexpr>zLThe number of the values and the number of the objectives must be identical.c                   s"   g | ]}d d t |j D qS )c                 S     g | ]	\}}t ||qS r   _normalize_valuer   vdr   r   r   r           zA_get_pareto_front_trials_by_trials.<locals>.<listcomp>.<listcomp>)zipr   r   r   r   r   r       s   " Fassume_unique_lexsortedc                 S  s   g | ]\}}|r|qS r   r   )r   r   	is_paretor   r   r   r   #   s    )r   r   any
ValueErrornpasarray_is_pareto_frontr$   )r   r
   r   loss_valueson_frontr   r   r   "_get_pareto_front_trials_by_trials   s   r/   study'optuna.study.Study'c                 C  s   t | j| j|S r   )r/   r   r
   )r0   r   r   r   r   _get_pareto_front_trials&   s   r2   )penaltyn_belowr-   
np.ndarrayr3   np.ndarray | Noner4   
int | Nonec          	      C  sx  t | dkrtjg tdS |pt | }|dksJ d|du r%t| |dS t |t | kr<tdt | dt |  dtjt | d	td}t|}t| |dk}t| |dk}t| | |d||< |tt	|8 }tj
|| d	d
d }|t|| ddtjf |d ||< |tt	|8 }tj
||  d	d
d }|t| | |d ||< t|d	ksJ 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r4   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-   r3   r4   ranksis_penalty_nanis_feasibleis_infeasibletop_rank_infeasibletop_rank_penalty_nanr   r   r   _fast_non_domination_rank,   s>   &
rO   unique_lexsorted_loss_valuesc                 C  s|   | d d dd f }|j d }tj|td}t|}t|r<tj||d k dd}d||d < || }|| }t|s|S )Nr>   r   r8   )axisT)shaper*   zerosr   aranger   r(   )rP   r-   n_trialsr.   nondominated_indicesnondominated_and_not_topr   r   r   _is_pareto_front_ndz   s   
rX   c                 C  sT   | j d }tj| d d df }tj|td}|dd  |d d k |dd < |S )Nr   r>   r8   r<   )rR   r*   minimum
accumulateonesr   )rP   rU   cummin_value1r.   r   r   r   _is_pareto_front_2d   s
   
 r]   c                 C  sH   | j \}}|dkrtjt| td}d|d< |S |dkr t| S t| S )Nr>   r8   Tr      )rR   r*   rS   r   r   r]   rX   )rP   rU   n_objectivesr.   r   r   r   "_is_pareto_front_for_unique_sorted   s   
r`   r&   c                 C  s6   |rt | S tj| ddd\}}t |}||d S )Nr   T)rQ   return_inverser<   )r`   r*   uniquereshape)r-   r&   rP   	order_invr.   r   r   r   r,      s
   r,   r:   c                C  s  t | dks|d ur|dkrtjt | tdS | j\}}|dkr1tj| d d df dd\}}|S tj| ddd\}}|jd }t|pFt |t |}tj|td}d}	t|}
||
j |k rt	|dd}|	||
| < |
|  }
||  }|	d7 }	||
j |k s`|	||
< ||
d S )	Nr   r8   r>   T)ra   )ra   rQ   r%   r<   )r   r*   rS   r@   rR   rb   minrT   sizer,   rc   )r-   r4   rU   r_   _rI   rP   rd   n_uniquerankindicesr.   r   r   r   rA      s*   




rA   trial0r   trial1c                 C  s   | j }|j }| jtjkrdS |jtjkrdS |d usJ |d us"J t|t|kr.tdt|t|kr:tddd t||D }dd t||D }||krTdS tdd	 t||D S )
NFTz?Trials with different numbers of objectives cannot be compared.zIThe number of the values and the number of the objectives are mismatched.c                 S  r   r   r   r    r   r   r   r      r#   z_dominates.<locals>.<listcomp>c                 S  r   r   r   r    r   r   r   r      r#   c                 s  s    | ]	\}}||kV  qd S r   r   )r   v0v1r   r   r   r      s    z_dominates.<locals>.<genexpr>)r   r   r   r   r   r)   r$   rH   )rk   rl   r
   values0values1normalized_values0normalized_values1r   r   r   
_dominates   s&   rs   valuefloat | None	directionr   floatc                 C  s$   | d u rt dS |tju r|  } | S )Ninf)rw   r   MAXIMIZE)rt   rv   r   r   r   r      s
   
r   )F)r   r	   r
   r   r   r   r   r   )r0   r1   r   r   r   r   )r-   r5   r3   r6   r4   r7   r   r5   )rP   r5   r   r5   )r-   r5   r&   r   r   r5   )r-   r5   r4   r7   r   r5   )rk   r   rl   r   r
   r   r   r   )rt   ru   rv   r   r   rw   )
__future__r   collections.abcr   numpyr*   optuna&optuna.study._constrained_optimizationr   optuna.study._study_directionr   optuna.trialr   r   r/   r2   rO   rX   r]   r`   r,   rA   rs   r   r   r   r   r   <module>   s,    
N



# 