Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

args and kwargs now work fully for minimize_ipopt #197

Merged
merged 1 commit into from
Apr 6, 2023

Conversation

moorepants
Copy link
Collaborator

@moorepants moorepants commented Apr 6, 2023

args & kwargs work with all user funcs in minimize_ipopt

- Passed in args and kwargs to all IpoptProblemWrapper.
- Added a unit test with args and kwargs for all funcs.

@moorepants
Copy link
Collaborator Author

Actually this does fail for passing args/kwargs into the hessian:

==================================================================================================== FAILURES =====================================================================================================
______________________________________________________________________ test_minimize_ipopt_jac_and_hessians_constraints_if_scipy_args_kwargs ______________________________________________________________________

    @pytest.mark.skipif("scipy" not in sys.modules,
                        reason="Test only valid if Scipy available.")
    def test_minimize_ipopt_jac_and_hessians_constraints_if_scipy_args_kwargs(
    ):
        """`minimize_ipopt` works with objective gradient and Hessian
           and constraint jacobians and Hessians."""
        from scipy.optimize import rosen, rosen_der, rosen_hess
    
        rosen2 = lambda x, a, b=1.0: rosen(x)*a*b
        rosen_der2 = lambda x, a, b=1.0: rosen_der(x)*a*b
        rosen_hess2 = lambda x, a, b=1.0: rosen_hess(x)*a*b
    
        x0 = [0.0, 0.0]
        constr = {
            "type": "ineq",
            "fun": lambda x, a, b=1.0: -x[0]**2 - x[1]**2 + 2*a*b,
            "jac": lambda x, a, b=1.0: np.array([-2 * x[0], -2 * x[1]])*a*b,
            "hess": lambda x, v, a, b=1.0: -2 * np.eye(2) * v[0]*a*b,
            "args": (1.0, ),
            "kwargs": {'b': 1.0},
        }
>       res = cyipopt.minimize_ipopt(rosen2, x0,
                                     jac=rosen_der2, hess=rosen_hess2,
                                     args=constr['args'],
                                     kwargs=constr['kwargs'],
                                     constraints=constr)

cyipopt/tests/unit/test_scipy_optional.py:121: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
cyipopt/scipy_interface.py:391: in minimize_ipopt
    x, info = nlp.solve(_x0)
cyipopt/cython/ipopt_wrapper.pyx:642: in ipopt_wrapper.Problem.solve
    raise self.__exception[0], self.__exception[1], self.__exception[2]
cyipopt/cython/ipopt_wrapper.pyx:895: in ipopt_wrapper.hessian_cb
    ret_val = self.__hessian(_x, _lambda, obj_factor)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <cyipopt.scipy_interface.IpoptProblemWrapper object at 0x7f66ba6ef220>, x = array([0., 0.]), lagrange = array([-1.]), obj_factor = 1.0

    def hessian(self, x, lagrange, obj_factor):
>       H = obj_factor * self.obj_hess(x)  # type: ignore
E       TypeError: <lambda>() missing 1 required positional argument: 'a'

cyipopt/scipy_interface.py:202: TypeError
============================================================================================= short test summary info =============================================================================================
FAILED cyipopt/tests/unit/test_scipy_optional.py::test_minimize_ipopt_jac_and_hessians_constraints_if_scipy_args_kwargs - TypeError: <lambda>() missing 1 required positional argument: 'a'
===================================================================================== 1 failed, 13 passed, 1 skipped in 0.92s =====================================================================================

@moorepants moorepants changed the title Added a test for args and kwargs in minimize_ipopt. args and kwargs now work fully for minimize_ipopt Apr 6, 2023
@moorepants
Copy link
Collaborator Author

I'm sorry, I did this forgetting #176. This PR fixes what 176 had and more. There are more comprehensive tests there. We could merge this and then include the tests from #176.

- Passed in args and kwargs to all IpoptProblemWrapper.
- Added a unit test with args and kwargs for all funcs.
@moorepants
Copy link
Collaborator Author

This doesn't break anything, so I'm going to merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant