Skip to content

Nonlinear constraints to BoFire (integration with acqf + tests + tutorials)#740

Open
MrzvskK wants to merge 26 commits intoexperimental-design:mainfrom
MrzvskK:nonlinearC
Open

Nonlinear constraints to BoFire (integration with acqf + tests + tutorials)#740
MrzvskK wants to merge 26 commits intoexperimental-design:mainfrom
MrzvskK:nonlinearC

Conversation

@MrzvskK
Copy link
Contributor

@MrzvskK MrzvskK commented Mar 4, 2026

Motivation

This is an early attempt, I am sure there will be a few changes needed along the way. But to start with something, so Nonlinear constraints (NonlinearInequalityConstraint and NonlinearEqualityConstraint) were defined in BoFire's domain model but were not integrated into the optimization pipeline. The constraints were extracted from the domain but never passed to BoTorch's optimize_acqf, causing the optimizer to ignore them and propose infeasible candidates.

This PR:

  1. Fixes the constraint integration pipeline in acqf_optimization.py to properly pass constraints to BoTorch
  2. Corrects a critical bug where the is_equality flag was inverted (all constraints were treated as equality constraints)
  3. Enhances torch_tools.py to properly format constraints for BoTorch compatibility
  4. Adds feasibility verification in botorch.py to ensure proposed candidates satisfy all constraints
  5. Provides tests and a tutorial demonstrating usage

Have you read the Contributing Guidelines on pull requests?

Yes

Have you updated CHANGELOG.md?

I will do that later

New Test Suite

Created tests/bofire/strategies/test_nonlinear_constraints.py with 16 comprehensive test cases:

Constraint Types Tested:

  • Single and multiple inequality constraints (2-5 simultaneous)
  • Equality constraints
  • Mixed equality/inequality constraints

Optimization Scenarios:

  • Different acquisition functions (qLogEI, qLogNEI, qUCB)
  • Tight constraints (small feasible regions)
  • Boundary conditions (initial data on constraint manifold)
  • Single-variable constraints
  • Empty feasible regions (proper error handling)

Results:

  • All 16 nonlinear constraint tests passing
  • 352/354 total strategy tests passing (99.4% pass rate)
  • All integration tests passing

Tutorial

Created docs/tutorials/basic_examples/nonlinear_constraints_basic_usage.py demonstrating:

  • Competing reactions optimization with nonlinear constraints
  • Constraint definition using string expressions
  • Initial feasible sampling
  • Multi-objective optimization with constraints
  • Constraint verification utilities

Verification Steps

To verify the changes work:

# Run nonlinear constraint tests
pytest tests/bofire/strategies/test_nonlinear_constraints.py -v
 
# Run full strategy test suite
pytest tests/bofire/strategies/ -v
 
# Run tutorial example - they also exist in .qmd - after revision we can delete .py files 
python docs/tutorials/advanced_examples/nonlinear_advanced.py
python docs/tutorials/advanced_examples/nonlinear_constraints_maximizing_yield.py
 

@LukasHebing
Copy link
Contributor

Hi @MrzvskK,
nonlinear constraints are not handled by the standard BoFire optimizer, but the GA handles those:
https://experimental-design.github.io/bofire/docs/tutorials/advanced_examples/genetic_algorithm.html

Does this help?

@MrzvskK
Copy link
Contributor Author

MrzvskK commented Mar 5, 2026

@LukasHebing, this PR adds functionality to the BotorchOptimizer to handle nonlinear constraints.

@LukasHebing
Copy link
Contributor

@LukasHebing, this PR adds functionality to the BotorchOptimizer to handle nonlinear constraints.

Ok, great :)
I just wanted to make sure that this is not overlooked

@jduerholt jduerholt self-requested a review March 5, 2026 14:04
@MrzvskK
Copy link
Contributor Author

MrzvskK commented Mar 17, 2026

I fixed most of the issues, except of the last failing test, it has something to do with the conda.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants