
    Di+                       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     c/home/ubuntu/crypto_trading_bot/.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        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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N#W4 E. 66%2+? ??Lr+   c                   | d d dd f   }|j                   d   }t        j                  |t              }t        j                  |      }t        |      rd||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5K  #Hxx-HLNIIV^L_

  FJ,=a,@@(B#%66)*[9O-PPWX$
  !JJuSz288B,<,<#==>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!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 )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88C <=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+>>.0ii!\`.a+ )12NOH 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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   
_dominatesrw      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!   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+   