
    (^i                        d dl mZ d dlZd dlZd dlmZ 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 	 	 	 	 d	dZ	 	 	 	 	 	 	 	 	 	 ddZ		 	 	 	 	 	 	 	 	 	 ddZ
y)
    )annotationsN)compute_hypervolumec                   | j                   d   dk(  r|| j                   d   k  sJ | j                   d   }t        j                  | j                   d         }| j                         }t        j                  |t        j
                  d d f   |d      }t        j                  |t              }t        |      D ]  }	t        j                  ||z
  d      }
t        j                  |
      }|||      ||	<   ||   j                         }t        j                  ||	z
  t              }d||<   ||   }||   }||   }t        j                  |d   |d |df         |d |df<   t        j                  |d   ||d df         ||d df<    |S )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                 Y/var/www/html/hubwallet-dev/venv/lib/python3.12/site-packages/optuna/_hypervolume/hssp.py_solve_hssp_2dr*   
   s    !!"%*{>N>T>TUV>W/WWW%%a(HYY/55a89N',,.?2::q=98!LJxx37; X77:(88rBIIh'	,^I-FG$Y/446	wwx!|40Y'-%
+D1$&JJy|Z

TU=V$W
:I:q=!$&JJy|Z	
TU=V$W
9:q=!X      c           
        t        j                  |      r$t        j                  | t        j                        S t        j
                  |ddt        j                  f   |dd       }t        j                  ||z
  d      }t        j                  |      }t        j                  | |t        j                  ||dddf   z
  d      z
        } d}|j                  d   dk  }	t        j                  |        D ]|  }
||
   rt        j                  x}| |
<   | |
   |k  r'|	r-||
   j                         |d<   t        ||d      }||z
  | |
<   n||
   t        ||
   |      z
  | |
<   t        | |
   |      }~ | S )	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?   -   sq   . zz+||Hbff--zz,Q

];]3B=OPHGGO.@@qIMXXm,Nzz-"''/HQUO*KRS"TTH K(..q1Q6OZZ	" 4!(*.K(1+A;$  21 5 : : <M")-X\]G!K/HQK'*-@!o-^^HQK(1+{34" Or+   c           	     (   t        j                  |      j                         s|d | S |j                  |k(  r|S | j                  d   dk(  rt        | |||      S ||j                  k  sJ || z
  }| j                  \  }}t        j                  |d      }t        j                  |t              }t        j                  ||f      }	t        j                  |      }
d}t        |      D ]  }t        t        j                  |            }|||   z  }|
|   ||<   | |   j                         |	|<   t        j                  |j                  t              }d||<   ||   }|
|   }
| |   } ||dz
  k(  r ||   S t!        || |	d |dz    ||      } ||   S )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&._]],,,,,&58H&H#"2"8"8[,ww6R@Hxx37HHk<89Mii$G	
B; 
		(+,	
hy!!%i0+I6;;=awwx}}D1YD>$-+D1a *++	 )&gA(>QS

$ *++r+   c                \   ||j                   k(  r|S t        j                  | dd      \  }}|j                   }||k  r]t        j                  |j                   t              }d||<   t        j
                  |j                         |    }d||d||z
   <   ||   S t        ||||      }	||	   S )ah  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      n)));=99t!<88 +//H+.--T:.2*+YY~':':;VGD?C!":K($:;<f%%+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+   