o
    /wix                     @  sH   d dl mZ d dlZd dlZdd
dZdddZdddZdddZdS )    )annotationsNrank_i_loss_vals
np.ndarrayrank_i_indicessubset_sizeintreference_pointreturnc                 C  s:  | j d dkr|| j d ksJ | j d }t| j d }|  }tj|tjd d f |dd}tj|td}t|D ]_}	tj	|| dd}
t
|
}|||  ||	< ||  }tj||	 td}d||< || }|| }|| }t|d |d |df |d |df< t|d ||d df ||d df< q;|S )N   r   axisdtypeF   )shapenparangecopyrepeatnewaxiszerosr   rangeprodargmaxonesboolminimum)r   r   r   r   n_trialssorted_indicessorted_loss_vals
rect_diagsselected_indicesicontribs	max_index	loss_valskeep r(   U/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/optuna/_hypervolume/hssp.py_solve_hssp_2d   s&    

(*r*   r$   pareto_loss_valuesselected_vecsc           	      C  s   t jj|dd |dd}d}t|  }|D ]/}| | |k r q||  |d< t jj||dd}t|s:|| ntj| |< t| | |}q| S )a  Lazy update 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.
    Nr
   T)assume_paretog        )	optuna_hypervolumecompute_hypervolumer   argsortr   isinfinfmax)	r$   r+   r,   r   hv_selectedmax_contrib%index_from_larger_upper_bound_contribr#   hv_plusr(   r(   r)   _lazy_contribs_update+   s   r9   c                 C  s8  t | s|d | S |j|kr|S | jd dkr"t| |||S ||jk s)J ||  }| j\}}t j|dd}t j|td}t 	||f}	t 
|}
t|D ]G}tt |}|
| ||< | |  |	|< t j|jtd}d||< || }|
| }
| | } ||d kr || S t|| |	d |d  |}qP|| S )Nr
   r   r   r   Fr   )r   isfiniteallsizer   r*   r   r   r   emptyr   r   r   r   r   r   r9   )r   r   r   r   diff_of_loss_vals_and_ref_pointn_solutionsn_objectivesr$   r"   r,   indiceskr%   r'   r(   r(   r)   _solve_hssp_on_unique_loss_valsP   s:   


rC   c           
      C  s   ||j kr|S tj| ddd\}}|j }||k r;tj|j td}d||< t|j |  }d||d||  < || 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)r<   r   uniquer   r   r   rC   )
r   r   r   r   rank_i_unique_loss_valsindices_of_unique_loss_valsn_uniquechosenduplicated_indices$selected_indices_of_unique_loss_valsr(   r(   r)   _solve_hsspy   s    

rL   )
r   r   r   r   r   r   r   r   r	   r   )
r$   r   r+   r   r,   r   r   r   r	   r   )	
__future__r   numpyr   r.   r*   r9   rC   rL   r(   r(   r(   r)   <module>   s    

#
%)