
    -ji                        S SK Jr  S SKrS SKrS SKJr            SS jr            S	S jr          SS jr	          SS jr
g)
    )annotationsN)compute_hypervolumec                   U R                   S   S:X  a  X R                   S   ::  d   eU R                   S   n[        R                  " U R                   S   5      nU R                  5       n[        R                  " U[        R
                  S S 24   USS9n[        R                  " U[        S9n[        U5       H  n	[        R                  " Xv-
  SS9n
[        R                  " U
5      nXU      X'   Xk   R                  5       n[        R                  " XI-
  [        S9nSX'   X]   nX}   nXm   n[        R                  " US   US U2S4   5      US U2S4'   [        R                  " US   X{S 2S4   5      X{S 2S4'   M     U$ )N   r   axisdtypeF   )shapenparangecopyrepeatnewaxiszerosintrangeprodargmaxonesboolminimum)rank_i_loss_valsrank_i_indicessubset_sizereference_pointn_trialssorted_indicessorted_loss_vals
rect_diagsselected_indicesicontribs	max_index	loss_valskeeps                 R/home/james-whalen/.local/lib/python3.13/site-packages/optuna/_hypervolume/hssp.py_solve_hssp_2dr*   
   sy    !!"%*{>T>TUV>W/WWW%%a(HYY/55a89N',,.?2::q=98!LJxx37;77:8rBIIh'	,I-FG$/446	wwx|40'-%
+1$&JJy|Z

TU=V$W
:I:q=!$&JJy|Z
TU=V$W
:q=!        c           
        [         R                  " U5      (       a%  [        R                  " U [        R                  5      $ [        R
                  " USS2[        R                  4   USS 5      n[        R                  " X1-
  SS9n[        R                  " U5      n[        R                  " X[        R                  " X5SS2S4   -
  SS9-
  5      n SnUR                  S   S:*  n	[        R                  " U * 5       H|  n
Xz   (       a  [        R                  =oU
'   M"  X
   U:  a  M,  U	(       a&  X   R                  5       US'   [        X#SS9nX-
  X
'   OXj   [        XZ   U5      -
  X
'   [        X
   U5      nM~     U $ )	a  Lazy update the hypervolume contributions.

(1) Lazy update of the hypervolume contributions
S=selected_indices - {indices[max_index]}, T=selected_indices, and S' is a subset of S.
As we would like to know argmax H(T v {i}) in the next iteration, we can skip HV
calculations for j if H(T v {i}) - H(T) > H(S' v {j}) - H(S') >= H(T v {j}) - H(T).
We used the submodularity for the inequality above. As the upper bound of contribs[i] is
H(S' v {j}) - H(S'), we start to update from i with a higher upper bound so that we can
skip more HV calculations.

(2) A simple cheap-to-evaluate contribution upper bound
The HV difference only using the latest selected point and a candidate is a simple, yet
obvious, contribution upper bound. Denote t as the latest selected index and j as an unselected
index. Then, H(T v {j}) - H(T) <= H({t} v {j}) - H({t}) holds where the inequality comes from
submodularity. We use the inclusion-exclusion principle to calculate the RHS.
Nr   r   r   g           T)assume_pareto)mathisinfr   	full_likeinfmaximumr   r   r   r   argsortr   r   max)r%   pareto_loss_valuesselected_vecsr   hv_selectedintersecinclusive_hvsis_contrib_infmax_contribis_hv_calc_fastr$   hv_pluss               r)   _lazy_contribs_updater?   -   sR   . zz+||Hbff--zz,Q

];]3B=OPHGGO@qIMXXm,Nzz"''/QUO*KRS"TTH K(..q1Q6OZZ	"(*.K1+;$  2 5 : : <M")-X\]G!/HK'*-@o-^^HK(+{3 #" Or+   c           	        [         R                  " U5      R                  5       (       d  US U $ UR                  U:X  a  U$ U R                  S   S:X  a  [        XX#5      $ X!R                  :  d   eX0-
  nU R                  u  pV[         R                  " USS9n[         R                  " U[        S9n[         R                  " X&45      n	[         R                  " U5      n
Sn[        U5       H  n[        [         R                  " U5      5      nXU   -  nX   X'   X   R                  5       X'   [         R                  " UR                  [        S9nSX'   X~   nX   n
X   n XS-
  :X  a    X   $ [!        XpU	S US-    X;5      nM     X   $ )Nr   r   r   r
   r   Fr   )r   isfiniteallsizer   r*   r   r   r   emptyr   r   r   r   r   r   r?   )r   r   r   r   diff_of_loss_vals_and_ref_pointn_solutionsn_objectivesr%   r#   r7   indiceshvkr&   r(   s                  r)   _solve_hssp_on_unique_loss_valsrK   d   s    ;;'++--l{++k)b!Q&.]],,,,,&5&H#"2"8"8[ww6R@Hxx37HHk89Mii$G	
B;		(+,	
y!!%0+6;;=wwx}}D1>-+1a ++	 )gA(>
  $ ++r+   c                F   X!R                   :X  a  U$ [        R                  " U SSS9u  pEUR                   nXb:  aX  [        R                  " UR                   [        S9nSXu'   [        R
                  " UR                   5      U)    nSXxSX&-
   '   X   $ [        XEX#5      n	X   $ )aP  Solve a hypervolume subset selection problem (HSSP) via a greedy algorithm.

This method is a 1-1/e approximation algorithm to solve HSSP.

For further information about algorithms to solve HSSP, please refer to the following
paper:

- `Greedy Hypervolume Subset Selection in Low Dimensions
   <https://doi.org/10.1162/EVCO_a_00188>`__
Tr   )return_indexr	   r
   N)rC   r   uniquer   r   r   rK   )
r   r   r   r   rank_i_unique_loss_valsindices_of_unique_loss_valsn_uniquechosenduplicated_indices$selected_indices_of_unique_loss_valss
             r)   _solve_hssprU      s      )));=99t!<8 +//H.--T:.2+YY~':':;VGD?C":K$:;<%%+Jk,( ??r+   )
r   
np.ndarrayr   rV   r   r   r   rV   returnrV   )r%   rV   r6   rV   r7   rV   r   rV   r8   floatrW   rV   )
__future__r   r/   numpyr   optuna._hypervolume.wfgr   r*   r?   rK   rU    r+   r)   <module>r]      s    "   7        	 
  F44"4 4  	4
 4 4n(, (,(, (,  	(,
 (,V!@ !@!@ !@  	!@
 !@r+   