Skip to content

Add unit tests for constrained multi-objective GPSampler#6235

Merged
nabenabe0928 merged 2 commits intooptuna:masterfrom
nabenabe0928:test/add-constrained-mo-gp-tests
Aug 6, 2025
Merged

Add unit tests for constrained multi-objective GPSampler#6235
nabenabe0928 merged 2 commits intooptuna:masterfrom
nabenabe0928:test/add-constrained-mo-gp-tests

Conversation

@nabenabe0928
Copy link
Copy Markdown
Contributor

Motivation

This PR adds unit tests for constrained multi-objective GPSampler.

@nabenabe0928 nabenabe0928 added the CI Continuous integration. label Aug 4, 2025
@nabenabe0928 nabenabe0928 added this to the v4.5.0 milestone Aug 4, 2025
@nabenabe0928
Copy link
Copy Markdown
Contributor Author

import optuna
import optunahub


hpolib = optunahub.load_module("benchmarks/hpolib").Problem(
    dataset_id=0, metric_names=["val_loss", "model_size"]
)


def objective(trial: optuna.Trial) -> tuple[float, float]:
    val_loss, model_size = hpolib(trial)
    trial.set_user_attr("constraints", (model_size - 1400, ))
    return val_loss, model_size


def constraints(trial: optuna.trial.FrozenTrial) -> tuple[float]:
    return trial.user_attrs["constraints"]


sampler = optuna.samplers.GPSampler(seed=0, constraints_func=constraints)
study = optuna.create_study(sampler=sampler, directions=hpolib.directions)
study.optimize(objective, n_trials=20)
print(study.best_trials)

I confirmed that the GPSampler works without any errors with the code above.

Copy link
Copy Markdown
Collaborator

@kAIto47802 kAIto47802 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR! I left some minor comments. PTAL :octocat:


import optuna
import optuna._gp.acqf as acqf_module
import optuna._gp.gp as optuna_gp
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import optuna._gp.gp as optuna_gp
import optuna._gp.gp as gp

How about naming like this, following the import in sampler.py.

import optuna._gp.gp as gp

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me keep the module name as is to avoid confusion 🙇

{"a": optuna.distributions.FloatDistribution(0.0, 1.0)}
)
gpr = optuna._gp.gp.fit_kernel_params(
gpr = optuna_gp.fit_kernel_params(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
gpr = optuna_gp.fit_kernel_params(
gpr = gp.fit_kernel_params(

According to the suggestion above, this part needs to be updated.

gpr = gp.fit_kernel_params(

Comment on lines +97 to +100
) -> tuple[float] | tuple[float, float]:
x = trial.suggest_float("x", 0, 1)
if n_objectives == 1:
return (x,)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning a float instead of tuple[float] is sufficient for the single-objective optimization.

Suggested change
) -> tuple[float] | tuple[float, float]:
x = trial.suggest_float("x", 0, 1)
if n_objectives == 1:
return (x,)
) -> float | tuple[float, float]:
x = trial.suggest_float("x", 0, 1)
if n_objectives == 1:
return x

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

@y0z y0z left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Copy Markdown
Collaborator

@kAIto47802 kAIto47802 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the comment. LGTM!

@nabenabe0928 nabenabe0928 merged commit dfd16b6 into optuna:master Aug 6, 2025
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI Continuous integration.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants