Skip to content

Commit

Permalink
add _make_nonlinear_constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
jduerholt committed Apr 24, 2023
1 parent 52c1777 commit 24a778d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
1 change: 1 addition & 0 deletions botorch/generation/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ def f_np_wrapper(x: np.ndarray, f: Callable):
nonlinear_inequality_constraints=nonlinear_inequality_constraints,
f_np_wrapper=f_np_wrapper,
x0=x0,
shapeX=shapeX,
)
x0 = _arrayify(x0)

Expand Down
51 changes: 42 additions & 9 deletions botorch/optim/parameter_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,33 @@ def _make_linear_constraints(
return constraints


def _make_nonlinear_constraints(
f_np_wrapper: Callable, nlc: Callable, shapeX: torch.Size
) -> List:
shapeX = _validate_linear_constraints_shape_input(shapeX)
b, q, _ = shapeX
constraints = []

def get_interpoint_constraint(b: int, q: int, nlc: Callable) -> Callable:
return lambda x: nlc(x[b, q])

for i in range(b):
for j in range(q):
f_obj, f_grad = _make_f_and_grad_nonlinear_inequality_constraints(
f_np_wrapper=f_np_wrapper,
nlc=get_interpoint_constraint(b=i, q=j, nlc=nlc),
)
constraints.append(
{
"type": "ineq",
"fun": f_obj,
"jac": f_grad,
}
)

return constraints


def _generate_unfixed_lin_constraints(
constraints: Optional[List[Tuple[Tensor, Tensor, float]]],
fixed_features: Dict[int, float],
Expand Down Expand Up @@ -415,6 +442,7 @@ def make_scipy_nonlinear_inequality_constraints(
nonlinear_inequality_constraints: List[Callable],
f_np_wrapper: Callable,
x0: Tensor,
shapeX: torch.Size,
) -> List[Dict]:
r"""Generate Scipy nonlinear inequality constraints from callables.
Expand Down Expand Up @@ -447,14 +475,19 @@ def make_scipy_nonlinear_inequality_constraints(
"`batch_initial_conditions` must satisfy the non-linear inequality "
"constraints."
)
f_obj, f_grad = _make_f_and_grad_nonlinear_inequality_constraints(
f_np_wrapper=f_np_wrapper, nlc=nlc
)
scipy_nonlinear_inequality_constraints.append(
{
"type": "ineq",
"fun": f_obj,
"jac": f_grad,
}

scipy_nonlinear_inequality_constraints += _make_nonlinear_constraints(
f_np_wrapper=f_np_wrapper, nlc=nlc, shapeX=shapeX
)

# f_obj, f_grad = _make_f_and_grad_nonlinear_inequality_constraints(
# f_np_wrapper=f_np_wrapper, nlc=nlc
# )
# scipy_nonlinear_inequality_constraints.append(
# {
# "type": "ineq",
# "fun": f_obj,
# "jac": f_grad,
# }
# )
return scipy_nonlinear_inequality_constraints

0 comments on commit 24a778d

Please sign in to comment.