BUG: optimize: fix issue with incorrect use of closure for slsqp.

Closes gh-4240.