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

Closes gh-4240.

(cherry picked from commit 7018c110add3f52885a595da7ae1026afecb0e8f)