From 1d077c99ca39de15a54c92ace1dbf8ec8c9fe9f5 Mon Sep 17 00:00:00 2001 From: Sebastian Ament Date: Mon, 10 Jul 2023 13:05:07 -0700 Subject: [PATCH] Loopifying conditional kwarg processing in `_optimize_acqf_batch` (#1923) Summary: Pull Request resolved: https://github.com/pytorch/botorch/pull/1923 Small code simplification, pattern could be reused elsewhere for `kwarg` processing, particularly where a number of fields have to be check. Reviewed By: esantorella Differential Revision: D47297929 fbshipit-source-id: 07574fef862b9e0b06d0f66b1004738e9dcd061f --- botorch/optim/optimize.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/botorch/optim/optimize.py b/botorch/optim/optimize.py index 3431f3e32f..5ec71767ba 100644 --- a/botorch/optim/optimize.py +++ b/botorch/optim/optimize.py @@ -305,14 +305,13 @@ def _optimize_batch_candidates() -> Tuple[Tensor, Tensor, List[Warning]]: # only add parameter constraints to gen_kwargs if they are specified # to avoid unnecessary warnings in _filter_kwargs - if opt_inputs.inequality_constraints is not None: - gen_kwargs["inequality_constraints"] = opt_inputs.inequality_constraints - if opt_inputs.equality_constraints is not None: - gen_kwargs["equality_constraints"] = opt_inputs.equality_constraints - if opt_inputs.nonlinear_inequality_constraints is not None: - gen_kwargs[ - "nonlinear_inequality_constraints" - ] = opt_inputs.nonlinear_inequality_constraints + for constraint_name in [ + "inequality_constraints", + "equality_constraints", + "nonlinear_inequality_constraints", + ]: + if (constraint := getattr(opt_inputs, constraint_name)) is not None: + gen_kwargs[constraint_name] = constraint filtered_gen_kwargs = _filter_kwargs(opt_inputs.gen_candidates, **gen_kwargs)