Skip to content

Extend projection to nonlinear inequality constraints#3316

Open
MrzvskK wants to merge 2 commits into
meta-pytorch:mainfrom
MrzvskK:3280-nonlinear-inequality-projection
Open

Extend projection to nonlinear inequality constraints#3316
MrzvskK wants to merge 2 commits into
meta-pytorch:mainfrom
MrzvskK:3280-nonlinear-inequality-projection

Conversation

@MrzvskK

@MrzvskK MrzvskK commented Jun 4, 2026

Copy link
Copy Markdown

Motivation

Related to #3280.

gen_candidates_scipy already supports nonlinear_inequality_constraints via make_scipy_nonlinear_inequality_constraints, but project_to_feasible_space_via_slsqp only handled linear constraints. After optimization, _optimize_acqf_batch checks feasibility (including nonlinear inequalities) but previously skipped projection whenever nonlinear constraints were present, so slightly infeasible candidates could remain infeasible or trigger errors.

This PR extends projection to nonlinear inequality constraints using the same SciPy path as gen_candidates_scipy, as suggested in the #3280 discussion.

Changes:

botorch/optim/parameter_constraints.py

  • Add nonlinear_inequality_constraints argument to project_to_feasible_space_via_slsqp.
  • Build SciPy constraints with make_scipy_nonlinear_inequality_constraints (same helper as gen_candidates_scipy).
  • Add _get_f_np_wrapper_for_projection so projection can evaluate constraint values/Jacobians without importing gen.py (avoids circular imports).
  • Add validate_feasibility: bool = True to make_scipy_nonlinear_inequality_constraints (default unchanged). Projection passes validate_feasibility=False because repair starts from infeasible points; IC validation behavior is unchanged.
  • Normalize shapeX with _validate_linear_constraints_shape_input for consistent (b, q, d) handling.

botorch/optim/optimize.py

  • Remove the guard that skipped projection when nonlinear_inequality_constraints is not None.
  • Pass nonlinear_inequality_constraints into project_to_feasible_space_via_slsqp for infeasible batches.

Tests

  • test_project_to_feasible_space_via_slsqp_nonlinear: unit disk + half-plane; slightly infeasible point projected to feasibility; linear + nonlinear combo.
  • Update test_optimize_acqf_projection_applied_with_nonlinear_constraints to expect projection is applied and linear constraints are repaired.

Have you read the Contributing Guidelines on pull requests?

Yes. First BoTorch contribution — happy to adjust based on review.

Test Plan

  • pytest test/optim/test_parameter_constraints.py::TestProjectToFeasibleSpace::test_project_to_feasible_space_via_slsqp_nonlinear -v
    Unit disk + half-plane; slightly infeasible point projected to feasibility; linear + nonlinear combo.

  • pytest test/optim/test_parameter_constraints.py test/optim/test_optimize.py -v
    Broader regression (75 passed locally).

  • test_optimize_acqf_projection_applied_with_nonlinear_constraints: projection runs when nonlinear constraints are present; linear inequalities repaired.

  • Compared pytest warnings with/without this diff on the above test files: 117 warnings, unchanged (no new warnings from these changes; existing RuntimeWarnings from retry/IC mocks, etc.).

Notes for reviewers

  • Convention unchanged: nonlinear inequalities are callable(x) >= 0, with the same (callable, is_intrapoint) tuple format as optimize_acqf.
  • Projection objective remains minimum-distance (½‖x - x₀‖²) with SLSQP, consistent with existing linear repair.

Related PRs

N/A


/cc @esantorella @jduerholt FYI for the discussion in #3280

@meta-cla

meta-cla Bot commented Jun 4, 2026

Copy link
Copy Markdown

Hi @MrzvskK!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla

meta-cla Bot commented Jun 4, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla Bot added the CLA Signed Do not delete this pull request or issue due to inactivity. label Jun 4, 2026
@meta-cla

meta-cla Bot commented Jun 4, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@jduerholt jduerholt 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.

Hi @MrzvskK,

I am not a botorch maintainer, but I had a look. Looks overall good to me. Thanks!

Only question and one suggestion regarding code reuse.

Let's see what @esantorella is saying.

Best,

Johannes

return is_feasible.view(x.shape[:-2])


def _get_f_np_wrapper_for_projection(

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.

Could you not just reuse_get_f_np_wrapper here? As in the original acqf optimization? Should also be appicable here:

def _get_f_np_wrapper(shapeX, device, dtype, with_grad):

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 the concern is cyclical dependencies there, since gen.py imports from parameter_constraints.py. We could move _get_f_np_wrapper to a separate utils file and import it into both from there though.

shapeX = _validate_linear_constraints_shape_input(X.shape)
constraints = make_scipy_linear_constraints(
shapeX=X.shape,
shapeX=shapeX,

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.

Why this change?

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.

Seems like it is computed once and reused below if there are non-linear constraints

return is_feasible.view(x.shape[:-2])


def _get_f_np_wrapper_for_projection(

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 the concern is cyclical dependencies there, since gen.py imports from parameter_constraints.py. We could move _get_f_np_wrapper to a separate utils file and import it into both from there though.

shapeX = _validate_linear_constraints_shape_input(X.shape)
constraints = make_scipy_linear_constraints(
shapeX=X.shape,
shapeX=shapeX,

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.

Seems like it is computed once and reused below if there are non-linear constraints

@meta-codesync

meta-codesync Bot commented Jun 22, 2026

Copy link
Copy Markdown

@saitcakmak has imported this pull request. If you are a Meta employee, you can view this in D109305269.

@codecov

codecov Bot commented Jun 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.98%. Comparing base (6953431) to head (715dadf).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3316   +/-   ##
=======================================
  Coverage   99.98%   99.98%           
=======================================
  Files         229      229           
  Lines       22653    22665   +12     
=======================================
+ Hits        22649    22661   +12     
  Misses          4        4           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@MrzvskK MrzvskK force-pushed the 3280-nonlinear-inequality-projection branch from 52fb61e to 32db191 Compare June 22, 2026 13:59
@MrzvskK

MrzvskK commented Jun 22, 2026

Copy link
Copy Markdown
Author

I think the CI failures on Python 3.14 runners (fmin_l_bfgs_b_batched AttributeError, test_mock.py, test_gen.py, etc.) are pre-existing and don't come from this PR. I did not modify those files. Tested locally on Python 3.13:
the same test files pass cleanly on upstream/main. The failures can be due to Python 3.14 compatibility. This PR only has changes to parameter_constraints.py and optimize.py.

@saitcakmak

Copy link
Copy Markdown
Contributor

Python 3.14 CI failures are being fixed in #3328

@MrzvskK

MrzvskK commented Jun 22, 2026

Copy link
Copy Markdown
Author

Python 3.14 CI failures are being fixed in #3328

Seems like other checks all pass.

@saitcakmak

Copy link
Copy Markdown
Contributor

Yes, this is good on your end. Thanks for the PR! I'll have it reviewed internally and land it when the systems allow me to (we have some restrictions around amazon prime day that may temporarily block landing certain changes).

@MrzvskK

MrzvskK commented Jun 22, 2026

Copy link
Copy Markdown
Author

Yes, this is good on your end. Thanks for the PR! I'll have it reviewed internally and land it when the systems allow me to (we have some restrictions around amazon prime day that may temporarily block landing certain changes).

thank you @saitcakmak, we appreciate the ability to add changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Do not delete this pull request or issue due to inactivity.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants