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

Using external function in nlpsol codegen #3669

Open
LinusTxtonomy opened this issue Apr 24, 2024 · 0 comments
Open

Using external function in nlpsol codegen #3669

LinusTxtonomy opened this issue Apr 24, 2024 · 0 comments

Comments

@LinusTxtonomy
Copy link

Hi,
I am building an optimization problem that should use external code as part of the objective function. I have built a small example that reproduces the error:

from casadi import *

x = MX.sym('x', 2)
slack = MX.sym('slack')

radius = MX.sym('radius')

J = x[0] + x[1]
obj_function = Function('objective', [x], [J])
obj_function.generate('J.c')
external_obj_function = external('objective', 'J.so', {'enable_fd': True})
J = external_obj_function(x)

g = x[0]**2 + x[1]**2 - radius + slack

prob = {'f': J, 'x': vertcat(x, slack), 'g': g, 'p': radius}

solver = nlpsol('solver', 'ipopt', prob, {'ipopt.print_level': 5})

solver.generate_dependencies('nlp.c')

res = solver(lbg = 0, ubg = 0, lbx = [-inf, -inf, 0], ubx = [inf, inf, inf], p = [4])
print(res)

The objective function code generates and builds without issues but I get compiler errors when trying to compile the generated nlp.c file even though the optimization problem runs fine inside the python example:

nlp.c: In function ‘nlp_smoothing_diff_err’:
nlp.c:211:55: error: ‘static_cast’ undeclared (first use in this function)
  211 |     wk = casadi_smoothing_diff_weights(k, yb, yc, yf, static_cast<casadi_real*>(0));
      |                                                       ^~~~~~~~~~~
nlp.c:211:55: note: each undeclared identifier is reported only once for each function it appears in
nlp.c:24:21: error: expected expression before ‘double’
   24 | #define casadi_real double
      |                     ^~~~~~
nlp.c:211:67: note: in expansion of macro ‘casadi_real’
  211 |     wk = casadi_smoothing_diff_weights(k, yb, yc, yf, static_cast<casadi_real*>(0));
      |                                                                   ^~~~~~~~~~~
nlp.c: In function ‘nlp_f6’:
nlp.c:812:39: error: expected ‘)’ before ‘;’ token
  812 |         if (mid = casadi_f1_checkout();
      |            ~                          ^
      |                                       )

If I manually fix the errors to atleast make the code compile I get this error when loading nlpsol with external code:

symbol lookup error: nlp.so: undefined symbol: objective_incref

How do I link external code to other external code?
I am using the latest casadi version 3.6.5
Regards,
Linus

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

1 participant