Skip to content

Commit

Permalink
ENH: support scipy.optimize.Bounds keep_feasible argument
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffgortmaker committed May 21, 2022
1 parent d18a9a1 commit 0faba85
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
13 changes: 11 additions & 2 deletions pyblp/configurations/optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ class Optimization(StringRepresentation):
Options for the optimization routine.
For any non-custom ``method`` other than ``'knitro'`` and ``'return'``, these options will be passed to
``options`` in :func:`scipy.optimize.minimize`. Refer to the SciPy documentation for information about which
options are available for each optimization routine.
``options`` in :func:`scipy.optimize.minimize`, with the exception of ``'keep_feasible'``, which is by default
``True`` and is passed to any ``scipy.optimize.Bounds``. Refer to the SciPy documentation for information about
which options are available for each optimization routine.
If ``method`` is ``'knitro'``, these options should be
`Knitro user options <https://www.artelys.com/docs/knitro//3_referenceManual/userOptions.html>`_. The
Expand Down Expand Up @@ -324,6 +325,14 @@ def gradient_wrapper(values: Array) -> Array:
# by default use the BFGS approximation for the Hessian
hess = scipy_options.get('hess', scipy.optimize.BFGS() if method == 'trust-constr' else None)

# extract and configure any bound feasibility
if 'keep_feasible' in scipy_options:
if bounds is not None:
lb, ub = zip(*bounds)
bounds = scipy.optimize.Bounds(lb, ub, scipy_options['keep_feasible'])
scipy_options = scipy_options.copy()
del scipy_options['keep_feasible']

# call the SciPy function
callback = lambda *_: iteration_callback()
results = scipy.optimize.minimize(
Expand Down
1 change: 1 addition & 0 deletions tests/test_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
pytest.param('slsqp', {}, id="SLSQP"),
pytest.param('l-bfgs-b', {}, id="L-BFGS-B"),
pytest.param('trust-constr', {}, id="trust-region"),
pytest.param('trust-constr', {'keep_feasible': True}, id="trust-region feasible"),
pytest.param('tnc', {}, id="TNC"),
pytest.param('nelder-mead', {}, id="Nelder-Mead"),
pytest.param('powell', {}, id="Powell"),
Expand Down

0 comments on commit 0faba85

Please sign in to comment.