[docs]@experimental_func("2.8.0")defplot_pareto_front(study:Study,*,target_names:list[str]|None=None,include_dominated_trials:bool=True,axis_order:list[int]|None=None,constraints_func:Callable[[FrozenTrial],Sequence[float]]|None=None,targets:Callable[[FrozenTrial],Sequence[float]]|None=None,)->"Axes":"""Plot the Pareto front of a study. .. seealso:: Please refer to :func:`optuna.visualization.plot_pareto_front` for an example. Args: study: A :class:`~optuna.study.Study` object whose trials are plotted for their objective values. ``study.n_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 a tuple of target values to display. The argument to this function is :class:`~optuna.trial.FrozenTrial`. ``targets`` must be :obj:`None` or return 2 or 3 values. ``axis_order`` and ``targets`` cannot be used at the same time. If the number of 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:`matplotlib.axes.Axes` object. """_imports.check()info=_get_pareto_front_info(study,target_names,include_dominated_trials,axis_order,constraints_func,targets)return_get_pareto_front_plot(info)
def_get_pareto_front_plot(info:_ParetoFrontInfo)->"Axes":ifinfo.n_targets==2:return_get_pareto_front_2d(info)elifinfo.n_targets==3:return_get_pareto_front_3d(info)else:assertFalse,"Must not reach here"def_get_pareto_front_2d(info:_ParetoFrontInfo)->"Axes":# Set up the graph style.plt.style.use("ggplot")# Use ggplot style sheet for similar outputs to plotly._,ax=plt.subplots()ax.set_title("Pareto-front Plot")cmap=plt.get_cmap("tab10")# Use tab10 colormap for similar outputs to plotly.ax.set_xlabel(info.target_names[info.axis_order[0]])ax.set_ylabel(info.target_names[info.axis_order[1]])trial_label:str="Trial"iflen(info.infeasible_trials_with_values)>0:ax.scatter(x=[values[info.axis_order[0]]for_,valuesininfo.infeasible_trials_with_values],y=[values[info.axis_order[1]]for_,valuesininfo.infeasible_trials_with_values],color="#cccccc",label="Infeasible Trial",)trial_label="Feasible Trial"iflen(info.non_best_trials_with_values)>0:ax.scatter(x=[values[info.axis_order[0]]for_,valuesininfo.non_best_trials_with_values],y=[values[info.axis_order[1]]for_,valuesininfo.non_best_trials_with_values],color=cmap(0),label=trial_label,)iflen(info.best_trials_with_values)>0:ax.scatter(x=[values[info.axis_order[0]]for_,valuesininfo.best_trials_with_values],y=[values[info.axis_order[1]]for_,valuesininfo.best_trials_with_values],color=cmap(3),label="Best Trial",)ifinfo.non_best_trials_with_valuesisnotNoneandax.has_data():ax.legend()returnaxdef_get_pareto_front_3d(info:_ParetoFrontInfo)->"Axes":# Set up the graph style.plt.style.use("ggplot")# Use ggplot style sheet for similar outputs to plotly.fig=plt.figure()ax:Axes3D=fig.add_subplot(projection="3d")ax.set_title("Pareto-front Plot")cmap=plt.get_cmap("tab10")# Use tab10 colormap for similar outputs to plotly.ax.set_xlabel(info.target_names[info.axis_order[0]])ax.set_ylabel(info.target_names[info.axis_order[1]])ax.set_zlabel(info.target_names[info.axis_order[2]])trial_label:str="Trial"if(info.infeasible_trials_with_valuesisnotNoneandlen(info.infeasible_trials_with_values)>0):ax.scatter(xs=[values[info.axis_order[0]]for_,valuesininfo.infeasible_trials_with_values],ys=[values[info.axis_order[1]]for_,valuesininfo.infeasible_trials_with_values],zs=[values[info.axis_order[2]]for_,valuesininfo.infeasible_trials_with_values],color="#cccccc",label="Infeasible Trial",)trial_label="Feasible Trial"ifinfo.non_best_trials_with_valuesisnotNoneandlen(info.non_best_trials_with_values)>0:ax.scatter(xs=[values[info.axis_order[0]]for_,valuesininfo.non_best_trials_with_values],ys=[values[info.axis_order[1]]for_,valuesininfo.non_best_trials_with_values],zs=[values[info.axis_order[2]]for_,valuesininfo.non_best_trials_with_values],color=cmap(0),label=trial_label,)ifinfo.best_trials_with_valuesisnotNoneandlen(info.best_trials_with_values):ax.scatter(xs=[values[info.axis_order[0]]for_,valuesininfo.best_trials_with_values],ys=[values[info.axis_order[1]]for_,valuesininfo.best_trials_with_values],zs=[values[info.axis_order[2]]for_,valuesininfo.best_trials_with_values],color=cmap(3),label="Best Trial",)ifinfo.non_best_trials_with_valuesisnotNoneandax.has_data():ax.legend()returnax