Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] [relatively easy fix] gen_one_shot_kg_initial_conditions should error when provided inter-point constraints #1851

Open
esantorella opened this issue May 31, 2023 · 1 comment
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@esantorella
Copy link
Member

esantorella commented May 31, 2023

🐛 Bug

See #1220 for context. optimize_acqf does not support inter-point constraints -- those whose indices are two-dimensional -- when the acquisition function is qKnowledgeGradient. This is because gen_one_shot_kg_initial_conditions does not support inter-point constraints. Since typing constraints correctly is confusing, BoTorch should raise an informative error rather than leaving the user to get a confusing IndexError and wonder if they typed the constraints wrong :)

To reproduce

from botorch.acquisition import qKnowledgeGradient
from botorch.fit import fit_gpytorch_mll
from botorch.models import SingleTaskGP
from gpytorch.mlls import ExactMarginalLogLikelihood

x = torch.rand((20, 2))
y = x.sum(1, keepdim=True) - 1
model = SingleTaskGP(train_X=x, train_Y=y)
mll = ExactMarginalLogLikelihood(model.likelihood, model)
 _ = fit_gpytorch_mll(mll)

bounds = torch.tensor([[0, 0], [1, 1]]).to(x)
q = 2
# should have
# samples[:, 0, 0] * 1 + samples[:, 1, 1] >= 0.5
# where samples is n x q x d
indices = torch.tensor([[0, 0], [1, 1]])
inequality_constraints = [(indices, torch.ones(2).to(x), 0.5)]

acqf = qKnowledgeGradient(model, num_fantasies=8)
candidates, acq_vals = optimize_acqf(
        acq_function=acqf,
        bounds=bounds,
        q=q,
        num_restarts=10,
        raw_samples=64,
        inequality_constraints=inequality_constraints,
)

** Stack trace/error message **

 1) IndexError: index 3 is out of bounds for dimension 0 with size 2
      File "pytorch/botorch/test/optim/test_optimize.py", line 265, in ftest_optimize_acqf_kg_constrained
        candidates, acq_vals = optimize_acqf(
      File "botorch/optim/optimize.py", line 557, in optimize_acqf
        return _optimize_acqf(opt_acqf_inputs)
      File "botorch/optim/optimize.py", line 586, in _optimize_acqf
        return _optimize_acqf_batch(
      File "botorch/optim/optimize.py", line 269, in _optimize_acqf_batch
        batch_initial_conditions = opt_inputs.get_ic_generator()(
      File "botorch/optim/initializers.py", line 570, in gen_one_shot_kg_initial_conditions
        fantasy_cands, fantasy_vals = optimize_acqf(
      File "botorch/optim/optimize.py", line 557, in optimize_acqf
        return _optimize_acqf(opt_acqf_inputs)
      File "botorch/optim/optimize.py", line 586, in _optimize_acqf
        return _optimize_acqf_batch(
      File "botorch/optim/optimize.py", line 269, in _optimize_acqf_batch
        batch_initial_conditions = opt_inputs.get_ic_generator()(
      File "botorch/optim/initializers.py", line 415, in gen_batch_initial_conditions
        X_rnd = sample_q_batches_from_polytope(
      File "botorch/optim/initializers.py", line 272, in sample_q_batches_from_polytope
        samples = get_polytope_samples(
      File "botorch/utils/sampling.py", line 800, in get_polytope_samples
        constraints = normalize_linear_constraints(bounds, inequality_constraints)
      File "botorch/utils/sampling.py", line 754, in normalize_linear_constraints
        lower, upper = bounds[:, index]

Expected Behavior

An informative error should be raised explaining that this is not supported. I think the fix here is that gen_one_shot_kg_initial_conditions should raise an UnsupportedError or NotImplementedError if provided inter-point constraints. However, I'm not 100% sure that gen_one_shot_kg_initial_conditions is the right place for the error, so whoever fixes this should carefully verify that the error is accurate and that usages that don't trigger the error do work.

@esantorella esantorella added bug Something isn't working good first issue Good for newcomers labels May 31, 2023
@Balandat
Copy link
Contributor

Balandat commented Jun 1, 2023

I have been messing with this recently to enable this for a certain kind of budget constraints where we can generate the one-shot initial conditions by doing some sampling from the simplex. It's a bad hack right now, but I'll try to figure out how to make this more reasonable, in which case we should be able to support this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants