o
    }oi                     @  sT   d dl mZ d dlZd dlZd dlmZ dddZdddZdddZ	dddZ
dS )    )annotationsN)compute_hypervolume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)   L/home/ubuntu/.local/lib/python3.10/site-packages/optuna/_hypervolume/hssp.py_solve_hssp_2d
   s&    

(*r+   r%   pareto_loss_valuesselected_vecshv_selectedfloatc              	   C  s&  t |rt| tjS t|ddtjf |dd }tj|| dd}t|}t| |tj||dddf  dd } d}|j	d dk}	t
|  D ]A}
||
 r]tj }| |
< qO| |
 |k rdqO|	r|||
  |d< t||dd}|| | |
< n||
 t||
 | | |
< t| |
 |}qO| 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%   r,   r-   r	   r.   intersecinclusive_hvsis_contrib_infmax_contribis_hv_calc_fastr$   hv_plusr)   r)   r*   _lazy_contribs_update-   s.   
"
"r?   c                 C  sJ  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 
|}
d}t|D ]N}tt |}||| 7 }|
| ||< | |  |	|< t j|jtd}d||< || }|
| }
| | } ||d kr || S t|| |	d |d  ||}qR|| 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#   r-   indiceshvkr&   r(   r)   r)   r*   _solve_hssp_on_unique_loss_valsd   s>   


rJ   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)rB   r   uniquer   r   r   rJ   )
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_hssp   s    

rS   )
r   r   r   r   r   r   r	   r   r
   r   )r%   r   r,   r   r-   r   r	   r   r.   r/   r
   r   )
__future__r   r2   numpyr   optuna._hypervolume.wfgr   r+   r?   rJ   rS   r)   r)   r)   r*   <module>   s    

#
7+