o
    }oi+                     @  s   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dZ	
d;d=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,dAd-d.ZdBd2d3ZdCd9d:ZdS )D    )annotations)Sequence)cast)TYPE_CHECKINGN)_get_feasible_trials)StudyDirection)
TrialState)FrozenTrialF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   Q/home/ubuntu/.local/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   r1   study'optuna.study.Study'c                 C  s   t | j| j|S r   )r1   r
   r   )r2   r   r   r   r   _get_pareto_front_trials+   s   r4   )penaltyn_belowr/   
np.ndarrayr5   np.ndarray | Noner6   
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r6   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/   r5   r6   ranksis_penalty_nanis_feasibleis_infeasibletop_rank_infeasibletop_rank_penalty_nanr   r   r   _fast_non_domination_rank1   s>   &
rQ   unique_lexsorted_loss_valuesc                 C  s   | d d dd f }|j d }tj|td}t|}t|rJd||d  }< tj|| || k dd}ttjt	t
 tjtj f || }t|s|S )Nr@   r   r:   T)axis)shaper,   zerosr   aranger   r*   r   ndarraytuplerB   r;   signedinteger)rR   r/   n_trialsr0   remaining_indicesnew_nondominated_indexnondominated_and_not_topr   r   r   _is_pareto_front_nd   s   

r^   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@   r:   r>   )rT   r,   minimum
accumulateonesr   )rR   rZ   cummin_value1r0   r   r   r   _is_pareto_front_2d   s
   
 rc   c                 C  sH   | j \}}|dkrtjt| td}d|d< |S |dkr t| S t| S )Nr@   r:   Tr      )rT   r,   rU   r   r   rc   r^   )rR   rZ   n_objectivesr0   r   r   r   "_is_pareto_front_for_unique_sorted   s   
rf   r(   c                 C  s6   |rt | S tj| ddd\}}t |}||d S )Nr   T)rS   return_inverser>   )rf   r,   uniquereshape)r/   r(   rR   	order_invr0   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   r:   r@   T)rg   )rg   rS   r'   r>   )r   r,   rU   rB   rT   rh   minrV   sizer.   ri   )r/   r6   rZ   re   _rK   rR   rj   n_uniquerankindicesr0   r   r   r   rC      s*   




rC   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&   rJ   )rq   rr   r   values0values1normalized_values0normalized_values1r   r   r   
_dominates   s&   ry   valuefloat | None	directionr   floatc                 C  s$   | d u rt dS |tju r|  } | S )Ninf)r}   r   MAXIMIZE)rz   r|   r   r   r   r!      s
   
r!   )F)r
   r   r   r   r   r   r   r   )r2   r3   r   r   r   r   )r/   r7   r5   r8   r6   r9   r   r7   )rR   r7   r   r7   )r/   r7   r(   r   r   r7   )r/   r7   r6   r9   r   r7   )rq   r	   rr   r	   r   r   r   r   )rz   r{   r|   r   r   r}   )
__future__r   collections.abcr   typingr   r   numpyr,   optuna&optuna.study._constrained_optimizationr   optuna.study._study_directionr   optuna.trialr   r	   r1   r4   rQ   r^   rc   rf   r.   rC   ry   r!   r   r   r   r   <module>   s2    
N



# 