o
    }oi?                     @  sP  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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 d dlmZ e rbd dlmZ ejeZG dd deZdddddddDd"d#Z dEd%d&Z!					dFdGd'd(Z"dHd,d-Z#dId1d2Z$	3	3dJdKd>d?Z%	3	3dJdLdBdCZ&dS )M    )annotations)Callable)Sequence)Any)
NamedTupleN)_deprecated)optuna_warn)_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"   V/home/ubuntu/.local/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
   @r0   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>)r1   
infeasiblez$%{text}<extra>Feasible Trial</extra>   zPareto-front Plotr      )titlexaxis_titleyaxis_title)r7   r8   zaxis_title)r6   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r0|d ur0tdg }g }d	}	| jd	tjfd
D ]C}
|d ur^d}	t	t
dd ||
rX||
 n||
 q?|
jt}|	|d uO }	|d u swt	dd |D r}||
 q?||
 q?t|| j}|rt||}ng }t|dkr|	rdnd}td| d |}|d u rt| jdv rt}ntdd6dd}|||}|||}|||}d7d"d#}||p||}|d u r|d urt|}n|d u rt| j}ntd$|dvrtd%| 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rQtd+| d,| d-t| d*tt||krbtd.| d/t||d0 kr}td1| d2t| d3|d0  d*t|dk rtd1| d2t| d4t||||||||	d5S )8Nz`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 )rE   Nr"   ).0rF   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. )r4      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"   )rH   trialr%   r"   r#   
<listcomp>       zL_get_pareto_front_info.<locals>._make_trials_with_values.<locals>.<listcomp>zL`targets` should return a sequence of target values. your `targets` returns c                 S  s   g | ]
\}}|t |fqS r"   )list)rH   rO   vr"   r"   r#   rQ          )
isinstancer   
ValueErrortypezip)rL   r%   target_valuesrT   r"   rP   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   r5   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.zD`plot_pareto_front` function only supports 2 or 3 targets. you used z targets now.c                 S  s   g | ]}d | qS )z
Objective r"   )rH   ir"   r"   r#   rQ   $  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!.r5   zGiven `axis_order` z contains invalid index z higher than z lower than 0.)r   r   r   r   r   r   r   r   )rL   rM   r%   rN   r*   r   )r\   r]   r*   r^   )r   _DEPRECATION_WARNING_TEMPLATEformatr   FutureWarningrW   
get_trialsr   COMPLETEallmapappendsystem_attrsgetr	   r   
directions_get_non_pareto_front_trialsr`   _loggerwarning_targets_defaultmetric_namesrangerS   setmaxminr   )r&   r   r   r   r$   r%   msgfeasible_trialsinfeasible_trialsr   rO   constraintsbest_trialsnon_best_trials
what_trial_targetsr[   r   r   r   ra   r   rs   r"   r"   r#   r-      s   








	




r-   rO   r   Sequence[float]c                 C  s   | j S N)values)rO   r"   r"   r#   rr   K  s   rr   rL   rM   pareto_trialsc                 C  s$   g }| D ]}||vr| | q|S r   )rk   )rL   r   non_pareto_trialsrO   r"   r"   r#   ro   O  s   
ro   Fr   r   Sequence[int]r\   r]   r1   strr3   r2   '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"   rH   rO   _r"   r"   r#   rQ   e  rR   z(_make_scatter_object.<locals>.<listcomp>)r2   r3   r4   c                      g | ]
\}}| d   qS r   r"   rH   r   r   r   r"   r#   rQ   l  rU   c                   r   r5   r"   r   r   r"   r#   rQ   m  rU   c                 S     g | ]\}}t |qS r"   r   r   r"   r"   r#   rQ   n      markersF)rF   ytextmoder1   marker
showlegendrK   c                   r   r   r"   r   r   r"   r#   rQ   v  rU   c                   r   r   r"   r   r   r"   r#   rQ   w  rU   c                   r   )r4   r"   r   r   r"   r#   rQ   x  rU   c                 S  r   r"   r   r   r"   r"   r#   rQ   y  r   )rF   r   zr   r   r1   r   r   zMust not reach here)_make_markerr   Scatter	Scatter3d)r   r   r   r\   r1   r3   r2   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rH   tr"   r"   r#   rQ         z _make_marker.<locals>.<listcomp>Bluesr6   Trial)liner   
colorscalecolorbarc                 S  r   r"   r   r   r"   r"   r#   rQ     r   Redsz
Best Trialg?r5   (   )r6   rF   xpadr_   )rL   r   r2   r3   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   )rO   r   r*   r   )rL   rM   r   rM   r*   rM   )FF)r   r   r   r   r   r   r\   r]   r1   r   r3   r   r2   r   r*   r   )
rL   r   r   r   r2   r   r3   r   r*   r   )'
__future__r   collections.abcr   r   typingr   r   optunar   optuna._warningsr   optuna.samplers._baser	   optuna.studyr
   optuna.study._multi_objectiver   optuna.trialr   r   $optuna.visualization._plotly_importsr   optuna.visualization._utilsr   is_successfulr   logging
get_loggerr   rp   r   r0   r.   r-   rr   ro   r=   r   r"   r"   r"   r#   <module>   sR    
HF 

-