
    !#h,                       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 	 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)SequenceN)_get_feasible_trials)StudyDirection)FrozenTrial)
TrialStatec                   | 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     Z/var/www/html/sandstorm/venv/lib/python3.12/site-packages/optuna/study/_multi_objective.py	<genexpr>z5_get_pareto_front_trials_by_trials.<locals>.<genexpr>   s"     
<3qxx=C
O+
<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      BA177j.A.A#AaBFB%f-
6{a	

<V
<<Z
 	
 **QWXXAS:-F	GTQ
1a
 	GXK  UKH"%fh"7E,!Y9A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     .ellE<L<LNa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        j                  |      z  }t        j                  ||   d	
      dz   }|t	        ||   ddt        j                  f   |      z   ||<   |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_rankrD   ,   s   L ;1xx#&&)[)GQ;===;,['JJ
7|s;''L>)?K@P?QQRT
 	

 GGC$b4EXXg&N...'Q,?KNNN?GaK@M 7{;7OY`aE+r,,G &&{!3R@1D.1Nq"**}-w2 E- r..G 66%"8"EI03PN#W4 E. 66%2+? ??Lr)   c                &   | d d dd f   }|j                   d   }t        j                  |t              }t        j                  |      }t        |      r;t        j                  ||d   k  d      }d||d   <   ||   }||   }t        |      r;|S )Nr3   r   r-   )axisT)shaper   zerosboolaranger   r   )unique_lexsorted_loss_valuesr"   n_trialsr#   nondominated_indicesnondominated_and_not_tops         r   _is_pareto_front_ndrO   z   s     /q!"u5K  #Hxx-H UWT]T]U k
#%66+A*FQ#O  -1%a()!":;  44LM k
 Or)   c                    | j                   d   }t        j                  j                  | d d df         }t        j                  |t
              }|dd  |d d k  |dd  |S )Nr   r3   r-   r1   )rG   r   minimum
accumulateonesrI   )rK   rL   cummin_value1r#   s       r   _is_pareto_front_2drU      sc    +11!4HJJ))*Fq!t*LMMwwxt,H $}Sb'99HQRLOr)   c                    | j                   \  }}|dk(  r+t        j                  t        |       t              }d|d<   |S |dk(  rt        |       S t        |       S )Nr3   r-   Tr      )rG   r   rH   r   rI   rU   rO   )rK   rL   n_objectivesr#   s       r   "_is_pareto_front_for_unique_sortedrY      s^    ;AAX|q88C <=TJ		"#?@@"#?@@r)   c                    |rt        |       S t        j                  | dd      \  }}t        |      }||j                  d         S )Nr   T)rF   return_inverser1   )rY   r   uniquereshape)r"   r   rK   	order_invr#   s        r   r   r      sN    
 1+>>.0ii!\`.a+ )12NOH I%%b)**r)   r/   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-   r3   T)r[   )r[   rF   r   r1   )r   r   rH   r5   rG   r\   minrJ   sizer   r]   )r"   r+   rL   rX   _r>   rK   r^   n_uniquerankindicesr#   s               r   r6   r6      s^    ;1!4AxxK(44*00X|q99[A.tD5 /1iiTX_`.a+ )+11!4H'>S!=>D`@abGHHXS)EDii!G
W\\
!G
+#$@Z^_#'gh 8)$'CXI'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     RFBrRxRs   )	r   r   r   r   r   r   r   r   r=   )	trial0trial1r   values0values1r    r!   normalized_values0normalized_values1s	            r   
_dominatesrq      s    mmGmmG||z***||z***
7|s7|#Z[[
7|s:&W
 	
 >A*=UVTQ*1a0VV=@*=UVTQ*1a0VV//Rc*<>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   rI   returnlist[FrozenTrial])r'   z'optuna.study.Study'r   rI   ry   rz   )r"   
np.ndarrayr*   znp.ndarray | Noner+   
int | Nonery   r{   )rK   r{   ry   r{   )r"   r{   r   rI   ry   r{   )r"   r{   r+   r|   ry   r{   )rk   r   rl   r   r   rx   ry   rI   )rv   zfloat | Nonerw   r   ry   rt   )
__future__r   collections.abcr   numpyr   optuna&optuna.study._constrained_optimizationr   optuna.study._study_directionr   optuna.trialr   r   r%   r(   rD   rO   rU   rY   r   r6   rq   r   rh   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\4	A+" 7; ( ()3 ( (FSS!,S:RS	S@r)   