Skip to content

Add qConstrainedLogEI - #6744

Merged
kAIto47802 merged 26 commits into
optuna:masterfrom
sawa3030:add-constrained-qlogei
Jul 31, 2026
Merged

kAIto47802 merged 26 commits into
optuna:masterfrom
sawa3030:add-constrained-qlogei

Conversation

@sawa3030

@sawa3030 sawa3030 commented Jul 6, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

PR #6640 introduced a Monte Carlo-based acquisition function for unconstrained single-objective optimization in GPSampler. This PR extends the approach to constrained single-objective optimization.

Description of the changes

  • Add qConstrainedLogEI.
  • Use qConstrainedLogEI as the default acquisition function for constrained single-objective optimization.
  • Use ConditionalGPR in qLogEI and qConstrainedLogEI

@sawa3030 sawa3030 changed the title Add qConstrainedLogEI Add qConstrainedLogEI Jul 6, 2026
@sawa3030

sawa3030 commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

Benchmark

I compared the master branch with this PR by simulating parallel optimization using several values of tau.

The optimization performance was evaluated in terms of the number of completed trials. Overall, this PR improves optimization performance compared with the master branch. However, when the feasible region is small, a relatively large tau overly smooths the constraint indicator, making it more difficult to identify feasible points and consequently degrading optimization performance.

Based on the results shown in the graph below, I selected tau=0.01, which provides a good balance between numerical smoothness and accurate feasibility estimation.

The benchmark uses the following two synthetic problems from Bayesian Optimization with Inequality Constraints (Gardner et al., ICML 2014):

  • Problem 1
    • Objective: cos(2x) cos(y) + sin(x)
    • Constraint: cos(x) cos(y) - sin(x) sin(y) <= 0.5
  • Problem 2
    • Objective: sin(x) + y
    • Constraint: sin(x) sin(y) <= -0.95
Simulation Code
from __future__ import annotations

import argparse
import itertools
import pickle
import time
from pathlib import Path
from typing import Any

import optuna
import optunahub
from optuna.distributions import (
    BaseDistribution,
    CategoricalDistribution,
    FloatDistribution,
    IntDistribution,
)
import numpy as np

def simulate(
    n_workers: int,
    n_trials: int,
    n_startup_trials: int,
    seed: int,
    dataset_id: int,
    tau: float,
    use_qmc: bool,
) -> optuna.Study:
    if n_workers <= 0:
        raise ValueError("n_workers must be >= 1")

    def objective(x: float, y: float) -> float:
        # return float(np.cos(2*x) * np.cos(y) + np.sin(x))
        return float(np.sin(x) + y)

    def constraints(trial: optuna.trial.FrozenTrial) -> tuple[float]:
        x = trial.params["x"]
        y = trial.params["y"]
        # c = float(np.cos(x) * np.cos(y) - np.sin(x) * np.sin(y) - 0.5)
        c = float(np.sin(x)*np.sin(y) + 0.95)
        return (c,)
        
    def feasible(trial: optuna.trial.FrozenTrial) -> bool:
        return all(c <= 0 for c in constraints(trial))

    sampler = optuna.samplers.GPSampler(
        n_startup_trials=n_startup_trials,
        seed=seed,
        constraints_func=constraints,
    )
    sampler._tau = tau
    sampler._use_qmc = use_qmc
    study = optuna.create_study(sampler=sampler)
    start_time = time.perf_counter()

    pending: list[tuple[optuna.Trial, dict[str, Any]] | None] = [None] * n_workers
    n_suggested = 0
    n_completed = 0

    for worker_id in itertools.cycle(range(n_workers)):
        if n_completed >= n_trials:
            break

        if pending[worker_id] is not None:
            previous_trial, previous_params = pending[worker_id]
            value = objective(**previous_params)
            study.tell(previous_trial, value)
            pending[worker_id] = None
            n_completed += 1
            if n_completed >= n_trials:
                break

        if n_suggested < n_trials:
            trial = study.ask()
            x = trial.suggest_float("x", 0.0, 2 * np.pi)
            y = trial.suggest_float("y", 0.0, 2 * np.pi)
            params = {"x": x, "y": y}
            trial.set_user_attr("trial_num", trial._trial_id + 1)  # Simulate cumulative time as trial number + 1
            pending[worker_id] = (trial, params)
            n_suggested += 1

    trials = [
        t
        for t in study.trials[n_startup_trials+n_workers-1:]
        if t.state == optuna.trial.TrialState.COMPLETE and feasible(t)
    ]
    trials = trials[: max(0, n_trials - n_startup_trials - n_workers + 1)]

    new_study = optuna.create_study()
    new_study.add_trials(trials)
    return new_study


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument("--label", required=True)
    parser.add_argument("--out-dir", default="./gp_simulator_results_without_evaltime_n5_bbo_dataset10")
    parser.add_argument("--n-workers", type=int, default=5)
    parser.add_argument("--n-trials", type=int, default=100)
    parser.add_argument("--n-startup-trials", type=int, default=10)
    parser.add_argument("--n-seeds", type=int, default=10)
    parser.add_argument("--dataset-id", type=int, default=10)
    parser.add_argument("--tau", type=float, default=0.01)
    parser.add_argument("--use_qmc", type=bool, default=False)

    study_list: list[optuna.Study] = []
    for seed in range(args.n_seeds):
        study = simulate(
            n_workers=args.n_workers,
            n_trials=args.n_trials,
            n_startup_trials=args.n_startup_trials,
            seed=seed,
            dataset_id=args.dataset_id,
            tau=args.tau,
            use_qmc=args.use_qmc,
        )
        study_list.append(study)

    out_dir = Path(args.out_dir)
    out_dir.mkdir(parents=True, exist_ok=True)
    out_path = out_dir / f"{args.label}.pickle"

    with out_path.open("wb") as f:
        pickle.dump(study_list, f)

    print(f"Saved {len(study_list)} studies to {out_path}")


if __name__ == "__main__":
    main()
Plot Code
from __future__ import annotations

import argparse
from pathlib import Path
import pickle
import re

import matplotlib.pyplot as plt
import optuna
import optunahub
import random


plot_target_over_time = optunahub.load_module(
    "visualization/plot_target_over_time"
).plot_target_over_time


def load_study_list(result_dir: Path, label: str) -> list[optuna.Study]:
    path = result_dir / f"{label}.pickle"
    with path.open("rb") as f:
        study_list = pickle.load(f)
    return study_list


def trim_to_same_length(study_list: list[optuna.Study]) -> list[optuna.Study]:
    min_len = min(len(study.trials) for study in study_list)

    trimmed = []
    for study in study_list:
        new_study = optuna.create_study(directions=study.directions)
        new_study.add_trials(study.trials[:min_len])
        trimmed.append(new_study)

    return trimmed


def get_style(label: str) -> dict[str, str]:
    if label == "master":
        return {
            "color": "blue",
            "marker": "s",
            "ls": "dashed",
            "plot_label": "master",
        }
    elif label == "qConstrainedLogEI":
        return {
            "color": "green",
            "marker": "D",
            "ls": "dashdot",
            # "plot_label": "qLogEI (n_qmc_samples=128)",
            "plot_label": "qConstrainedLogEI",
        }
    elif label == "qConstrainedLogEI-tau0-01":
        return {
            "color": "red",
            "marker": "P",
            "ls": "dashdot",
            "plot_label": "qConstrainedLogEI (tau=0.01)",
        }
    elif label == "qConstrainedLogEI-tau0-1":
        return {
            "color": "purple",
            "marker": "X",
            "ls": "dashdot",
            "plot_label": "qConstrainedLogEI (tau=0.1)",
        }
    elif label == "qConstrainedLogEI-tau1":
        return {
            "color": "brown",
            "marker": "D",
            "ls": "dashdot",
            "plot_label": "qConstrainedLogEI (tau=1.0)",
        }
    elif label == "qConstrainedLogEI-tau10":
        return {
            "color": "black",
            "marker": "s",
            "ls": "dashdot",
            "plot_label": "qConstrainedLogEI (tau=10.0)",
        }
    elif label == "qConstrainedLogEI-tau100":
        return {
            "color": "orange",
            "marker": "*",
            "ls": "dashdot",
            "plot_label": "qConstrainedLogEI (tau=100.0)",
        }
    elif label == "qConstrainedLogEI-tau0-001":
        return {
            "color": "orange",
            "marker": "o",
            "ls": "dashdot",
            "plot_label": "qConstrainedLogEI (tau=0.001)",
        }
    else:
        color = random.choice(["red", "blue", "green", "orange", "purple", "brown", "pink", "gray"])
        return {
            "color": color,
            "marker": "o",
            "ls": "solid",
            "plot_label": label,
        }


def build_plot_title(result_dir: Path) -> str:
    # return f"objective: cos(2*x)*cos(y) + sin(x), constraint: cos(x)*cos(y) - sin(x)*sin(y) - 0.5 <= 0"
    return f"objective: sin(x) + y, constraint: sin(x)*sin(y) + 0.95 <= 0"


def get_trial_count_for_plot(trial: optuna.trial.FrozenTrial) -> float:
    return trial.user_attrs.get("trial_num")


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--result-dir",
        default="./gp_simulator_results_without_evaltime_n5_bbo_dataset10",
        help="Directory containing pickled study lists.",
    )
    parser.add_argument(
        "--labels",
        nargs="+",
        required=True,
        help="Labels to load, e.g. v4.7.0 add-qlogei master",
    )
    parser.add_argument(
        "--output",
        default="async-bench-example_without_evaltime_n5_bbo_dataset10.png",
        help="Output image path.",
    )
    args = parser.parse_args()

    result_dir = Path(args.result_dir)

    _, ax = plt.subplots()

    for label in args.labels:
        study_list = load_study_list(result_dir, label)
        study_list = trim_to_same_length(study_list)

        style = get_style(label)

        plot_target_over_time(
            study_list,
            color=style["color"],
            ax=ax,
            cumtime_func=get_trial_count_for_plot,
            label=style["plot_label"],
            marker=style["marker"],
            ls=style["ls"],
            markevery=10,
        )

    ax.grid(True, which="major", alpha=0.5)
    ax.grid(True, which="minor", alpha=0.2)
    ax.set_xlabel("Number of trials")
    ax.set_ylabel("Best value so far")
    ax.set_title(build_plot_title(result_dir))
    ax.legend()

    plt.savefig(args.output, bbox_inches="tight")
    print(f"Saved to {args.output}")

if __name__ == "__main__":
    main()

Problem1

constrained_trial_num_1

Problem2

constrained_trial_num_2

Comment thread optuna/_gp/acqf.py Outdated
)


class qConstrainedLogEI(qLogEI):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is it possible to encapsulate qLogEI instead of inheriting it?
Although inheritance works fine in Python, nested inheritance usually obfuscates codes, so I'd prefer avoid it.

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.

Maybe we can do so by using composition as is done in other acquisition functions for constrained optimization (i.e., ConstrainedLogEI and ConstrainedLogEHVI). This would also reduce inconsistency in the implementation strategy.

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.

Thank you for the review! I agree that the nested inheritance was hard to follow. I've updated the implementation to encapsulate it instead.

Comment thread optuna/_gp/acqf.py Resolved
@nabenabe0928

Copy link
Copy Markdown
Contributor

@sawa3030
Thank you for the change! I have a general question.
Since the following PR would make this PR shorter, do you think we should wait for the following PR to be merged first?

Cc: @kAIto47802

@sawa3030

Copy link
Copy Markdown
Contributor Author

Yes, that makes sense. Please give us a little time to review #6721 first. I already have a ConditionalGPR-based implementation here, so once #6721 is merged, I’ll merge it into this branch and update this PR accordingly.

Comment thread optuna/_gp/acqf.py Outdated
Comment on lines 162 to 171
@@ -182,40 +169,28 @@ def __init__(
n_samples=n_qmc_samples,
seed=qmc_seed,
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It seems we don't need these instance variables, though we need to define the zero tensor for threshold=-inf.

Comment thread optuna/_gp/acqf.py Outdated
Comment on lines +187 to +188
y_post = self._conditional_gpr.sample(x)
return y_post.clamp_(min=torch.tensor(_EPS, dtype=torch.float64)).log()

@nabenabe0928 nabenabe0928 Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[critical] Somehow q-logEI was not taking the improvement.

Suggested change
y_post = self._conditional_gpr.sample(x)
return y_post.clamp_(min=torch.tensor(_EPS, dtype=torch.float64)).log()
return (self._conditional_gpr.sample(x) - self._threshold).clamp_min_(_EPS).log()

@nabenabe0928

nabenabe0928 commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

@sawa3030
Seems like this commit mixed up the bug but does this affect the benchmarking result between ReLU and logfatplus?

@nabenabe0928

Copy link
Copy Markdown
Contributor

@sawa3030 @kAIto47802
Can we maybe first aim merging the refactoring of q-logEI?

I don't mind who publishes this PR, but if @sawa3030 doesn't mind, I will publish it (the commit is already co-authored) and you two review it.

@sawa3030

Copy link
Copy Markdown
Contributor Author

Thank you very much for reporting the bug and for submitting a PR to fix it! I’ll take a look at #6760 before moving forward with this PR.

@sawa3030

sawa3030 commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor Author

@sawa3030 Seems like this commit mixed up the bug but does this affect the benchmarking result between ReLU and logfatplus?

I reran the benchmark after fixing the issue, and the overall trend does not seem to have changed significantly. fatplus still performs worse than the other options, so I will stick with ReLU. ref: #6640 (comment)

Comment thread optuna/_gp/acqf.py
)


class qConstrainedLogEI(BaseAcquisitionFunc):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What about defining qLogPI and encapsulate it?
We need it for qConstrainedLogEHVI anyway, right?

@sawa3030

Copy link
Copy Markdown
Contributor Author

Thank you for the suggestion! I have added qLogPI and encapsulated it accordingly. PTAL!

Comment thread optuna/_gp/acqf.py Outdated
super().__init__(gpr.length_scales, search_space)

def eval_acqf(self, x: torch.Tensor) -> torch.Tensor:
def _log_improvement(self, x: torch.Tensor) -> torch.Tensor:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Now that eval_acqf is simple enough to merge this method.

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.

Ok, I’ve updated qConstrainedLogEI so that the unaggregated log improvement is computed internally. Please let me know if I misunderstood your intention.

Comment thread optuna/_gp/acqf.py Outdated
)
super().__init__(gpr.length_scales, search_space)

def _log_prob(self, x: torch.Tensor) -> torch.Tensor:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ditto.
We don't need to separate this method from eval_acqf anymore.

Comment thread optuna/_gp/acqf.py Outdated
search_space=search_space,
threshold=threshold,
n_qmc_samples=n_qmc_samples,
qmc_seed=qmc_seed + i + 1,

@nabenabe0928 nabenabe0928 Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We need to make sure that this value fits in 64bit.
Although the probability is quite low, it is possible to encounter overflow.

Ok, seems the sampler side is doing this job.
But probably, we should use a function with a comment line so that any unintended modifications won't happen.

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.

Thank you for pointing this out" I had overlooked the possibility of overflow. I’ve updated the code to pass qmc_seeds from the sampler side, ensuring that each generated value fits within a 64-bit integer.

@nabenabe0928 nabenabe0928 Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The old version already handles the overflow issue implicitly by limiting the seed range to [0, 1 << 30].
So your approach (the old version) works fine.
But since this handling is very implicit, we should define the max seed value as a global constant variable with a comment line.
Essentially, we don't know why we use 1 << 30 not (1 << 31) - 1.

Since the number of constraints cannot be very large by design ~ 100, the current max seed value is totally fine.

Comment thread optuna/_gp/acqf.py
Comment on lines +348 to +359
y_post = self._acqf._cond_gpr.sample_joint_posterior(x)
log_feasible_improvement = (
(y_post - self._acqf._threshold).clamp_min_(_EPS).log()
if not np.isneginf(self._acqf._threshold)
else torch.zeros_like(y_post, dtype=torch.float64)
)

for acqf in self._constraints_acqf_list:
log_prob = acqf._cond_gpr.sample_joint_posterior(x)
log_feasible_improvement += torch.nn.functional.logsigmoid(
(log_prob - acqf._threshold) / acqf._tau
)

@nabenabe0928 nabenabe0928 Jul 24, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ok, now I got why you needed to define a separated method for per-sample utility values.
Btw, the per-sample value is termed acquisition utility.

But anyways, I'd rather lean toward merging this PR first. (at least, it is not recommended to access private attributes like acqf._cond_gpr and acqf._threshold)
We can work on refactoring in the next PR.

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.

Thank you for the helpful reference. I will handle the refactoring in the next PR!

Comment thread optuna/_gp/acqf.py Outdated
return standard_logei((mean - f0) / (sigma := var.sqrt_())) + sigma.log()


def _aggregate_log_acqf_over_q_batch(log_acqf: torch.Tensor) -> torch.Tensor:

@nabenabe0928 nabenabe0928 Jul 24, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this name is more appropriate.

Suggested change
def _aggregate_log_acqf_over_q_batch(log_acqf: torch.Tensor) -> torch.Tensor:
def _compute_mean_max_utility(log_util_vals: torch.Tensor) -> torch.Tensor:

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.

Thank you for the suggestion. I have updated the name accordingly!

@nabenabe0928 nabenabe0928 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM with the condition of a follow-up PR mentioned above.

Please notice this: @kAIto47802

@nabenabe0928 nabenabe0928 removed their assignment Jul 27, 2026
@nabenabe0928

Copy link
Copy Markdown
Contributor

@sawa3030

Copy link
Copy Markdown
Contributor Author

@nabenabe0928 My apologies for overlooking the QMC seed refactoring, and thank you very much for taking the time to suggest a refactoring plan. I have created draft PR #6779 based on your suggestion. Once this PR #6744 has been merged, I will mark #6779 as ready for review.

Comment thread optuna/_gp/acqf.py Resolved
Co-authored-by: Kaito Baba <[email protected]>

@kAIto47802 kAIto47802 left a comment

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.

Thank you for the update! LGTM

@kAIto47802
kAIto47802 merged commit 4f298f7 into optuna:master Jul 31, 2026
13 checks passed
@y0z y0z added this to the v5.0.0 milestone Jul 31, 2026
@c-bata c-bata added the feature Change that does not break compatibility, but affects the public interfaces. label Aug 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature Change that does not break compatibility, but affects the public interfaces.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants