Saving pickle CasADi objects

715 views
Skip to first unread message

A.S

unread,
Feb 17, 2021, 12:29:31 AM2/17/21
to CasADi
Hi CasADi forums,

Is it possible to save a CasADi nlp solver objects using pickle? I am getting the usual TypeError: can't pickle SwigPyObject objects message. If not, is there any way to save these solver objects for later use in a file or some other method?

Sincerely,
Alex 

Joel Andersson

unread,
Feb 18, 2021, 1:40:33 PM2/18/21
to CasADi
Alex,
You can save expressions to file using 
solver_object.save("filename.casadi")

Then load it back using .
solver_object = Function.load("filename.casadi")

Joris knows more about this feature.

Joel

A.S

unread,
Feb 18, 2021, 5:17:45 PM2/18/21
to CasADi
Dear Joel,
I am still a little confused on this, for example, I am using opti stack, so I have this line of code but not sure how to proceed:
self.opti.solver('ipopt').

and it says I can repeat the same steps in optistack, so I assume it's possible. Although in my case, I want to save the entire self.opti.solver object. 

Does optistack have an option to save to a file?
Sincerely,
Alex

Joel Andersson

unread,
Feb 18, 2021, 7:41:26 PM2/18/21
to CasADi
Joris would have to answer this. As far as I can tell, you can get the function instance from the opti instance by calling OptiAdvanced::casadi_solver(). And you get a "OptiAdvanced" reference by Opti::advanced().
So maybe self.opti.advanced().casadi_solver().save("filename.casadi") works? I don't think the entire "opti" instance is serializable, but again, Joris knows better.

Joel


Alexander Schperberg

unread,
Feb 20, 2021, 6:59:06 PM2/20/21
to Joel Andersson, CasADi
thanks for the reply. So I did try self.opti.advanced.casadi_solver.save('filename.casadi') (it didn't work if I included the parentheses) and it did save a file, but I don't think the file that it saved is correct (e.g., if I save different files using different solvers, the file output is still the same either way). I looked for several hours online on how to somehow save objects that cannot be serialized (if this is indeed the case for an entire opti instance) but had no luck finding any solutions (unless there's some odd way of saving it to the cloud or anything like that I am not aware of - but I think even in that case the object needs to be serialized somehow). 

--
Sent from CasADi's user forum at http://forum.casadi.org.
---
You received this message because you are subscribed to the Google Groups "CasADi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to casadi-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/casadi-users/2e7383a8-f113-47eb-9ef2-8396be182621n%40googlegroups.com.

Joris Gillis

unread,
Mar 12, 2021, 3:51:58 PM3/12/21
to CasADi
Dear A.S.

Opti is not serializable at the moment indeed.

casadi_solver.save should work, but probably only after you first solve at least once.
The pattern intended on the blog post is to use opti.to_function ( as used in https://web.casadi.org/blog/nlp_sens/ )
the result of that is a CasADi Function with an nlpsol instance embedded. This can also be saved.

As long as your Python objects contain only Function, DM, Sparsity, you can pickle them btw.

Best regards,
  Joris
Reply all
Reply to author
Forward
0 new messages