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

Computation of jac_adj_i0_adj_o0 #61

Closed
pariterre opened this issue Jan 10, 2025 · 2 comments
Closed

Computation of jac_adj_i0_adj_o0 #61

pariterre opened this issue Jan 10, 2025 · 2 comments

Comments

@pariterre
Copy link

Hello there!

I am trying to use l4casadi in an optimal control program. When creating the dynamic constraints I get the following error message: jac_adj_i0_adj_o0 is not provided by L4CasADi. And I says that if I need that feature, I should contact you! So am I B-)

Here is a minimal example (strip from most of the optimal control stuff, apart from the relevant constraint). This example does not make much sense as is, but raises the same error message.

Hope you can help! :)

from casadi import Opti, MX, Function
import l4casadi as l4c
import torch


class NeuralNetworkModel(torch.nn.Module):
    def __init__(self, layer_node_count: tuple[int]):
        super(NeuralNetworkModel, self).__init__()
        layers = torch.nn.ModuleList()
        layers.append(torch.nn.Linear(layer_node_count[0], layer_node_count[-1]))
        self._forward_model = torch.nn.Sequential(*layers)
        self.eval()

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self._forward_model(x)


def main():
    opti = Opti()
    nx = nu = 1
    torch_model = NeuralNetworkModel(layer_node_count=(nu, nx))

    # ---- decision variables ---------
    x = opti.variable(nx, 1)  # state
    u = opti.variable(nu, 1)  # control

    # ---- dynamic constraints --------
    x_sym = MX.sym("x", nx, 1)
    u_sym = MX.sym("u", nu, 1)
    forward_model = l4c.L4CasADi(torch_model, device="cpu")
    f = Function("xdot", [x_sym, u_sym], [x_sym - forward_model(u_sym)])
    opti.subject_to(f(x, u) == 0)  # Adding this line yields the error : jac_adj_i0_adj_o0 is not provided by L4CasADi.

    # ---- solve NLP  ------
    opti.solver("ipopt")
    opti.solve()


if __name__ == "__main__":
    main()
@Tim-Salzmann
Copy link
Owner

Hi,

You can get around this for now by using the full Hessian via:

forward_model = l4c.L4CasADi(torch_model, device="cpu", generate_jac_jac=True, generate_adj1=False, generate_jac_adj1=False)

This might come with some cost to speed. Let me know if this becomes a problem.

Best
Tim

@pariterre
Copy link
Author

Thanks for the answer!

This works! I don't know so far if the speed is an issue. I think the greatest speed issue for our use is the fact that we must use MX and cannot expand; but this is something that, as far as I understand casadi, cannot be overcome anyway when using External.

Thanks for the library!

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

No branches or pull requests

2 participants