mechmotum / cyipopt

Cython interface for the interior point optimzer IPOPT
Eclipse Public License 2.0
235 stars 54 forks source link

MAINT: minimize_ipopt: fix late binding bug when defining constraint Jacobians #208

Closed mdhaber closed 1 year ago

mdhaber commented 1 year ago

As reported in https://github.com/mechmotum/cyipopt/pull/194#discussion_r1155423756, minimize_ipopt fails on the scipy.optimize.minimize example with three constraints. This is due to a late binding gotcha when the constraint Jacobians are defined in a loop. This fixes that bug and demonstrates that the example problem is solved successfully after the fix.

Try me before and after. ```python3 from cyipopt import minimize_ipopt fun = lambda x: (x[0] - 1)**2 + (x[1] - 2.5)**2 cons = ({'type': 'ineq', 'fun': lambda x: x[0] - 2 * x[1] + 2}, {'type': 'ineq', 'fun': lambda x: -x[0] - 2 * x[1] + 6}, {'type': 'ineq', 'fun': lambda x: -x[0] + 2 * x[1] + 2}) bnds = ((0, None), (0, None)) res = minimize_ipopt(fun, (2, 0), bounds=bnds, constraints=cons) ```

Incidentally, this also fixes an unreported bug: kwargs was being passed to approx_fprime. but approx_fprime doesn't accept kwargs. Another instance of this bug is fixed in https://github.com/mechmotum/cyipopt/pull/200/files#r1198383113.

moorepants commented 1 year ago

LGTM