Ë
    ÿÿæi
  ã                  óˆ   — d Z ddlmZ ddlZddlmZ ddlmZ 	 	 	 	 	 	 d
d„Z		 	 	 	 	 	 	 	 dd„Z
	 	 	 	 	 	 d
d„Z	 	 	 	 	 	 dd	„Zy)a²  
The functions in this file are mostly based on BoTorch v0.13.0,
but they are refactored significantly from the original version.

For ``_get_upper_bound_set``, look at:
    * https://github.com/pytorch/botorch/blob/v0.13.0/botorch/utils/multi_objective/box_decompositions/utils.py#L101-L160

For ``_get_box_bounds``, look at:
    * https://github.com/pytorch/botorch/blob/v0.13.0/botorch/utils/multi_objective/box_decompositions/utils.py#L163-L193

For ``_get_non_dominated_box_bounds``, look at:
    * https://github.com/pytorch/botorch/blob/v0.13.0/botorch/utils/multi_objective/box_decompositions/non_dominated.py#L395-L430

The preprocessing for four or fewer objectives, we use the algorithm proposed by:
    Title: A Box Decomposition Algorithm to Compute the Hypervolume Indicator
    Authors: Renaud Lacour, Kathrin Klamroth, and Carlos M. Fonseca
    URL: https://arxiv.org/abs/1510.01963
We refer this paper as Lacour17 in this file.

é    )ÚannotationsN)Úoptuna_warn)Ú_is_pareto_frontc                ód  ‡‡‡	— | j                   \  }Št        j                  ‰«      Št        j                  ‰t        ¬«      Š	d‰	dd…df<   dˆˆˆ	fd„}t        j
                  |g«      }t        j                  d‰‰ft        j                   «      }||d‰‰f<   | D ]  } ||||«      \  }}Œ ||fS )a!  
    This function follows Algorithm 2 of Lacour17.

    Args:
        sorted_pareto_sols: Pareto solutions sorted with respect to the first objective.
        ref_point: The reference point.

    Returns:
        upper_bound_set: The upper bound set, which is ``U(N)`` in the paper. The shape is
        ``(n_bounds, n_objectives)``.
        def_points: The defining points of each vector in ``U(N)``. The shape is
        ``(n_bounds, n_objectives, n_objectives)``.

    NOTE:
        ``pareto_sols`` corresponds to ``N`` and ``upper_bound_set`` corresponds to ``U(N)`` in the
        paper.
        ``def_points`` (the shape is ``(n_bounds, n_objectives, n_objectives)``) is not well
        explained in the paper, but basically, ``def_points[i, j] = z[j]`` of
        ``upper_bound_set[i]``.
    )ÚdtypeTNr   c                óÎ  •— t        j                  | |k  d¬«      }t        |«      s||fS ||   }|j                  d   }| t        j                  t        j
                  ‰t         j                   |«      d¬«      k\  }t        j                  t        j                  |«      d d …t         j                  f   ‰«      |   }t        j                  ‰|df«      |   }|j                  |j                  k(  sJ ‚t        j                  |j                  «      }	||   }
| |
|	|f<   ||   |   }| |   ||	|f<   t        j                  ||    |g«      t        j                  ||    |
g«      fS )Néÿÿÿÿ©Úaxisr   éþÿÿÿé   )ÚnpÚallÚanyÚshapeÚmaxÚwhereÚinfÚtileÚarangeÚnewaxisÚsizeÚvstack)ÚsolÚubsÚdpsÚis_dominatedÚdominated_dpsÚn_boundsÚupdateÚubs_indices_to_updateÚdimensions_to_updateÚindices_for_sweepingÚnew_dpsÚnew_ubsÚn_objectivesÚobjective_indicesÚskip_ineq_judges               €€€úz/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/optuna/_hypervolume/box_decomposition.pyr    z$_get_upper_bound_set.<locals>.update;   sc  ø€ ä—v‘v˜c C™i¨bÔ1ˆÜ�<Ô Ø˜�8ˆOð ˜LÑ)ˆØ ×&Ñ& qÑ)ˆð œŸ™œrŸx™x¨¼"¿&¹&¸À-ÓPÐWYÔZÑZˆô !#§¡¬¯	©	°(Ó(;ºA¼r¿z¹z¸MÑ(JÈLÓ YÐZ`Ñ aÐä!Ÿw™wÐ'8¸8ÀQ¸-ÓHÈÑPÐØ$×)Ñ)Ð-A×-FÑ-FÒFÐFÐFÜ!Ÿy™yÐ)=×)BÑ)BÓCÐàÐ 5Ñ6ˆØ>AˆÐ$Ð&:Ð:Ñ;à�lÑ#Ð$9Ñ:ˆØ>AÐBVÑ>WˆÐ$Ð&:Ð:Ñ;Ü�y‰y˜#˜|˜mÑ,¨gÐ6Ó7¼¿¹ÀCÈÈÑDVÐX_ÐC`Ó9aÐaÐaó    r   )r   ú
np.ndarrayr   r+   r   r+   Úreturnútuple[np.ndarray, np.ndarray])r   r   r   ÚeyeÚboolÚasarrayÚfullr   )
Úsorted_pareto_solsÚ	ref_pointÚ_r    Úupper_bound_setÚ
def_pointsÚsolutionr&   r'   r(   s
          @@@r)   Ú_get_upper_bound_setr8      s´   ú€ ð. +×0Ñ0Ñ€QˆÜŸ	™	 ,Ó/ÐÜ—f‘f˜\´Ô6€Oà €O’A�q�DÑ÷bô8 —j‘j ) Ó-€OÜ—‘˜!˜\¨<Ð8¼2¿6¹6¸'ÓB€JØ:C€JˆqÐ#Ð%6Ð6Ñ7Û&ˆÙ&,¨X°È
Ó&SÑ#ˆ™ð 'ð ˜JÐ&Ð&r*   c                óÜ  — | j                   d   }|dkD  sJ d«       ‚t        j                  dg| j                   ¢­«      }|d d …ddf   |dd d …df<   |d   |dd d …df<   t        j                  |dz
  «      \  }}t        j                  j                  |d¬«      d d …||dz   f   |dd d …dd …f<   | d d …dd …f   |dd d …dd …f<   t        j                  |d   |d   k  d¬«       }|d d …|f   S )Nr	   r   ú8This function is used only for multi-objective problems.é   r   r   r
   )r   r   ÚemptyÚdiag_indicesÚmaximumÚ
accumulater   )r5   r6   r3   r&   ÚboundsÚrowÚcolÚ	not_emptys           r)   Ú_get_box_boundsrD   `   s
  € ð #×(Ñ(¨Ñ,€LØ˜!ÒÐWÐWÓWÐÜ�X‰X�qÐ1˜?×0Ñ0Ñ1Ó2€FØ ¢ A q Ñ)€Fˆ1Ša�ˆ7�OØ ‘l€Fˆ1Ša�ˆ7�OÜ�‰˜|¨aÑ/Ó0�H€CˆÜ—z‘z×,Ñ,¨Z¸bÐ,ÓAÂ!ÀSÈ#ÐPQÉ'À/ÑR€Fˆ1Ša�‘ˆ8ÑØ&¢q¨!©" uÑ-€Fˆ1Ša�‘ˆ8ÑÜ—‘˜˜q™	 V¨A¡YÑ.°RÔ8Ð8€IØ’!�Y�,ÑÐr*   c                óþ   — t        | |«      d    }t        j                  |d¬«      }t        j                  |t        j                  «      }t        |t        |d¬«         |¬«      \  }}t        |||«       \  }}||fS )Nr   r
   T©Úassume_unique_lexsorted)r2   r3   )r8   r   ÚuniqueÚ	full_liker   r   rD   )	r2   r3   Úneg_upper_bound_setÚsorted_neg_upper_bound_setÚpoint_at_infinityÚneg_lower_bound_setÚneg_def_pointsÚbox_upper_boundsÚbox_lower_boundss	            r)   Ú_get_non_dominated_box_boundsrQ   p   sœ   € ô 0Ð0BÀIÓNÈqÑQÐQÐÜ!#§¡Ð+>ÀQÔ!GÐô Ÿ™ Y´·±Ó7Ðô +?Ø5ÜÐ7ÐQUÔVñ
ð $ô	+Ñ'Ð˜ô +:Ø˜^Ð->ó+ð *Ñ&ÐÐ&ð Ð-Ð-Ð-r*   c                ó  — t        j                  t        j                  | «      «      sJ d«       ‚t        j                  | d¬«      }|t	        |d¬«         }| j
                  d   }|dkD  sJ d«       ‚|d	kD  rt        d
«       t        ||«      S )Nz3loss_vals must be clipped before box decomposition.r   r
   TrF   r	   r   r:   é   z’Box decomposition (typically used by `GPSampler`) might be significantly slow for n_objectives > 4. Please consider using another sampler instead.)r   r   ÚisfiniterH   r   r   r   rQ   )Ú	loss_valsr3   Úunique_lexsorted_loss_valsr2   r&   s        r)   Úget_non_dominated_box_boundsrW   Š   s˜   € ô �6‰6”"—+‘+˜iÓ(Ô)Ð`Ð+`Ó`Ð)ä!#§¡¨9¸1Ô!=ÐØ3ÜÐ3ÈTÔRñÐð —?‘? 2Ñ&€Lð ˜!ÒÐWÐWÓWÐØ�aÒÜðOô	
ô
 )Ð);¸YÓGÐGr*   )r2   r+   r3   r+   r,   r-   )r5   r+   r6   r+   r3   r+   r,   r+   )rU   r+   r3   r+   r,   r-   )Ú__doc__Ú
__future__r   Únumpyr   Úoptuna._warningsr   Úoptuna.study._multi_objectiver   r8   rD   rQ   rW   © r*   r)   Ú<module>r^      s™   ðñõ* #ã å (Ý :ð?'Ø"ð?'Ø/9ð?'à"ó?'ðD Øð Ø-7ð ØDNð àó ð .Ø"ð.Ø/9ð.à"ó.ð4HØðHØ&0ðHà"ôHr*   