o
    /wi>                     @  sL  d dl mZ d dlmZ d dlmZ d dlmZ d dlmZ d dlZd dl	Z	d dl	m
Z
 d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ e r`d dlmZ e	jeZG dd deZdddddddCd!d"ZdDd$d%Z 					dEdFd&d'Z!dGd+d,Z"dHd0d1Z#	2	2dIdJd=d>Z$	2	2dIdKdAdBZ%dS )L    )annotations)Callable)Sequence)Any)
NamedTupleN)_deprecated)_CONSTRAINTS_KEY)Study)"_get_pareto_front_trials_by_trials)FrozenTrial)
TrialState)_imports_make_hovertext)goc                   @  sN   e Zd ZU ded< ded< ded< ded< ded< d	ed
< ded< ded< dS )_ParetoFrontInfoint	n_targetsz	list[str]target_names%list[tuple[FrozenTrial, list[float]]]best_trials_with_valuesnon_best_trials_with_valuesinfeasible_trials_with_valuesz	list[int]
axis_orderboolinclude_dominated_trialshas_constraintsN)__name__
__module____qualname____annotations__ r!   r!   _/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/optuna/visualization/_pareto_front.pyr      s   
 r   T)r   r   r   constraints_functargetsstudyr	   r   list[str] | Noner   r   r   list[int] | Noner#   /Callable[[FrozenTrial], Sequence[float]] | Noner$   return'go.Figure'c                C  s"   t   t| |||||}t|S )a  Plot the Pareto front of a study.

    .. seealso::
        Please refer to :ref:`multi_objective` for the tutorial of the Pareto front visualization.

    Args:
        study:
            A :class:`~optuna.study.Study` object whose trials are plotted for their objective
            values. The number of objectives must be either 2 or 3 when ``targets`` is :obj:`None`.
        target_names:
            Objective name list used as the axis titles. If :obj:`None` is specified,
            "Objective {objective_index}" is used instead. If ``targets`` is specified
            for a study that does not contain any completed trial,
            ``target_name`` must be specified.
        include_dominated_trials:
            A flag to include all dominated trial's objective values.
        axis_order:
            A list of indices indicating the axis order. If :obj:`None` is specified,
            default order is used. ``axis_order`` and ``targets`` cannot be used at the same time.

            .. warning::
                Deprecated in v3.0.0. This feature will be removed in the future. The removal of
                this feature is currently scheduled for v5.0.0, but this schedule is subject to
                change. See https://github.com/optuna/optuna/releases/tag/v3.0.0.
        constraints_func:
            An optional function that computes the objective constraints. It must take a
            :class:`~optuna.trial.FrozenTrial` and return the constraints. The return value must
            be a sequence of :obj:`float` s. A value strictly larger than 0 means that a
            constraint is violated. A value equal to or smaller than 0 is considered feasible.
            This specification is the same as in, for example,
            :class:`~optuna.samplers.NSGAIISampler`.

            If given, trials are classified into three categories: feasible and best, feasible but
            non-best, and infeasible. Categories are shown in different colors. Here, whether a
            trial is best (on Pareto front) or not is determined ignoring all infeasible trials.

            .. warning::
                Deprecated in v4.0.0. This feature will be removed in the future. The removal of
                this feature is currently scheduled for v6.0.0, but this schedule is subject to
                change. See https://github.com/optuna/optuna/releases/tag/v4.0.0.
        targets:
            A function that returns targets values to display.
            The argument to this function is :class:`~optuna.trial.FrozenTrial`.
            ``axis_order`` and ``targets`` cannot be used at the same time.
            If ``study.n_objectives`` is neither 2 nor 3, ``targets`` must be specified.

            .. note::
                Added in v3.0.0 as an experimental feature. The interface may change in newer
                versions without prior notice.
                See https://github.com/optuna/optuna/releases/tag/v3.0.0.

    Returns:
        A :class:`plotly.graph_objects.Figure` object.
    )r   check_get_pareto_front_info_get_pareto_front_plot)r%   r   r   r   r#   r$   infor!   r!   r"   plot_pareto_front%   s
   @r/   r.   c              
   C  s  | j }| j}|s#t| j| j|| jdddt| j| j|| jdddg}n&t| j| j|| jdddt| j| j|| jdddt| j| j|| jdddg}| jd	krctj	d
| j
| jd  | j
| jd  d}ntj	d
| j
| jd  | j
| jd  | j
| jd	  dd}tj||dS )Nz%{text}<extra>Trial</extra>T)hovertemplatedominated_trialsz %{text}<extra>Best Trial</extra>Fz&%{text}<extra>Infeasible Trial</extra>)r0   
infeasiblez$%{text}<extra>Feasible Trial</extra>   zPareto-front Plotr      )titlexaxis_titleyaxis_title)r6   r7   zaxis_title)r5   scene)datalayout)r   r   _make_scatter_objectr   r   r   r   r   r   Layoutr   Figure)r.   r   r   r:   r;   r!   r!   r"   r-   m   sv   
r-   c              
   C  s<  |d urt jjdddd}t|t |d ur&t jjdddd}t|t |d ur2|d ur2tdg }g }d	}	| jd	tj	fd
D ]C}
|d ur`d}	t
tdd ||
rZ||
 n||
 qA|
jt}|	|d uO }	|d u syt
dd |D r||
 qA||
 qAt|| j}|rt||}ng }t|dkr|	rdnd}td| d |}|d u rt| jdv rt}ntdd5dd}|||}|||}|||}d6d"d#}||p||}|d u r|d urt|}n|d u rt| j}ntd$|dvrtd%||d u r | j}|d u rd&d' t|D }n|}nt||kr/td(| d)|d u r;tt|}nWt||krRtd*| d+| d,t| d)tt||krctd-| d.t||d/ kr~td0| d1t| d2|d/  d)t|dk rtd0| d1t| d3t||||||||	d4S )7Nz`axis_order`z3.0.0z5.0.0)named_verr_verz`constraints_func`z4.0.0z6.0.0z]Using both `targets` and `axis_order` is not supported. Use either `targets` or `axis_order`.F)deepcopystatesTc                 S  s   | dkS )N        r!   )xr!   r!   r"   <lambda>   s    z(_get_pareto_front_info.<locals>.<lambda>c                 s  s    | ]}|d kV  qdS )rD   Nr!   ).0rE   r!   r!   r"   	<genexpr>   s    z)_get_pareto_front_info.<locals>.<genexpr>r   	completedzcompleted and feasiblezYour study does not have any z	 trials. )r3      z`plot_pareto_front` function only supports 2 or 3 objective studies when using `targets` is `None`. Please use `targets` if your objective studies have more than 3 objectives.trialslist[FrozenTrial]r$   (Callable[[FrozenTrial], Sequence[float]]r)   r   c                   sL    fdd| D }|D ]}t |tstdt|qdd t| |D S )Nc                   s   g | ]} |qS r!   r!   )rG   trialr$   r!   r"   
<listcomp>       zL_get_pareto_front_info.<locals>._make_trials_with_values.<locals>.<listcomp>zN`targets` should return a sequence of target values. your `targets` returns {}c                 S  s   g | ]
\}}|t |fqS r!   )list)rG   rN   vr!   r!   r"   rP          )
isinstancer   
ValueErrorformattypezip)rK   r$   target_valuesrS   r!   rO   r"   _make_trials_with_values   s   

z8_get_pareto_front_info.<locals>._make_trials_with_valuestrials_with_values-Sequence[tuple[FrozenTrial, Sequence[float]]]
int | Nonec                 S  s    t | dkrt | d d S d S )Nr   r4   len)r\   r!   r!   r"   _infer_n_targets  s   z0_get_pareto_front_info.<locals>._infer_n_targetszNIf `targets` is specified for empty studies, `target_names` must be specified.zS`plot_pareto_front` function only supports 2 or 3 targets. you used {} targets now.c                 S  s   g | ]}d | qS )z
Objective r!   )rG   ir!   r!   r"   rP   $  s    z*_get_pareto_front_info.<locals>.<listcomp>z/The length of `target_names` is supposed to be .zSize of `axis_order` z
. Expect: z
, Actual: zElements of given `axis_order` z are not unique!.r4   zGiven `axis_order` z contains invalid index z higher than z lower than 0.)r   r   r   r   r   r   r   r   )rK   rL   r$   rM   r)   r   )r\   r]   r)   r^   )r   _DEPRECATION_WARNING_TEMPLATErW   warningswarnFutureWarningrV   
get_trialsr   COMPLETEallmapappendsystem_attrsgetr   r
   
directions_get_non_pareto_front_trialsr`   _loggerwarning_targets_defaultmetric_namesrangerR   setmaxminr   )r%   r   r   r   r#   r$   msgfeasible_trialsinfeasible_trialsr   rN   constraintsbest_trialsnon_best_trials
what_trial_targetsr[   r   r   r   ra   r   rt   r!   r!   r"   r,      s   






	




r,   rN   r   Sequence[float]c                 C  s   | j S N)values)rN   r!   r!   r"   rs   K  s   rs   rK   rL   pareto_trialsc                 C  s$   g }| D ]}||vr| | q|S r   )rl   )rK   r   non_pareto_trialsrN   r!   r!   r"   rp   O  s   
rp   Fr   r   Sequence[int]r\   r]   r0   strr2   r1   'go.Scatter' | 'go.Scatter3d'c              
     s   |pg }t dd |D |||d}| dkr4tj fdd|D  fdd|D dd |D d||d	d
S | dkr_tj fdd|D  fdd|D  fdd|D dd |D d||d	dS J d)Nc                 S  s   g | ]\}}|qS r!   r!   rG   rN   _r!   r!   r"   rP   e  rQ   z(_make_scatter_object.<locals>.<listcomp>)r1   r2   r3   c                      g | ]
\}}| d   qS r   r!   rG   r   r   r   r!   r"   rP   l  rT   c                   r   r4   r!   r   r   r!   r"   rP   m  rT   c                 S     g | ]\}}t |qS r!   r   r   r!   r!   r"   rP   n      markersF)rE   ytextmoder0   marker
showlegendrJ   c                   r   r   r!   r   r   r!   r"   rP   v  rT   c                   r   r   r!   r   r   r!   r"   rP   w  rT   c                   r   )r3   r!   r   r   r!   r"   rP   x  rT   c                 S  r   r!   r   r   r!   r!   r"   rP   y  r   )rE   r   zr   r   r0   r   r   zMust not reach here)_make_markerr   Scatter	Scatter3d)r   r   r   r\   r0   r2   r1   r   r!   r   r"   r<   Y  s:   		r<   Sequence[FrozenTrial]dict[str, Any]c                 C  sx   |r|st | dksJ |rddiS |r%ddddd | D d	d
didS ddddd | D dd|r5dnddddS )Nr   colorz#ccccccg      ?Grey)widthr   c                 S     g | ]}|j qS r!   numberrG   tr!   r!   r"   rP         z _make_marker.<locals>.<listcomp>Bluesr5   Trial)liner   
colorscalecolorbarc                 S  r   r!   r   r   r!   r!   r"   rP     r   Redsz
Best Trialg?r4   (   )r5   rE   xpadr_   )rK   r   r1   r2   r!   r!   r"   r     s(   

r   )r%   r	   r   r&   r   r   r   r'   r#   r(   r$   r(   r)   r*   )r.   r   r)   r*   )NTNNN)r%   r	   r   r&   r   r   r   r'   r#   r(   r$   r(   r)   r   )rN   r   r)   r   )rK   rL   r   rL   r)   rL   )FF)r   r   r   r   r   r   r\   r]   r0   r   r2   r   r1   r   r)   r   )
rK   r   r   r   r1   r   r2   r   r)   r   )&
__future__r   collections.abcr   r   typingr   r   re   optunar   optuna.samplers._baser   optuna.studyr	   optuna.study._multi_objectiver
   optuna.trialr   r   $optuna.visualization._plotly_importsr   optuna.visualization._utilsr   is_successfulr   logging
get_loggerr   rq   r   r/   r-   r,   rs   rp   r<   r   r!   r!   r!   r"   <module>   sR    
HF 

-