Xfunction input arguments must be purely symbolic

1,851 views
Skip to first unread message

Dominique Vercammen

unread,
Dec 12, 2014, 1:35:04 AM12/12/14
to casadi...@googlegroups.com
Hello,

When running the following code:

from casadi import *

x
= MX.sym('x')
y
= MX.sym('y')

f
= x**2+y**2

V
= vertcat([x,y])

nlp
= MXFunction(nlpIn(x=V),nlpOut(f=f))


I get the following error 


XFunctionInternal::XFunctionInternal: Xfunction input arguments must be purely symbolic.

Argument #0 is not symbolic.


I guess this means my V should be defined as an MX.sym, and then splitted into x and y.


Now the problem is, I cannot do this because of the way V is defined in our code (by adding more and more variables along the way), and I also cannot use SX instead of MX as the variables V are to be used as input to an integrator and it seems to me these inputs should always be MX (is this correct?).


Do you think there is any way of solving this within the constraints specified by the previous paragraph?


Kind regards,


Dominique


Joel Andersson

unread,
Dec 12, 2014, 2:35:45 AM12/12/14
to casadi...@googlegroups.com
Hi Dominique!

The efficient way to do this is to use a "vertsplit" instead of a "vertcat":

from casadi import *

V = MX.sym('V',2)
x, y = vertsplit(V)

= x**2+y**2 

nlp 
= MXFunction(nlpIn(x=V),nlpOut(f=f))

Check the documentation for vertsplit for more details.

Best regards,
Joel

Joel Andersson

unread,
Dec 12, 2014, 2:36:56 AM12/12/14
to casadi...@googlegroups.com
Maybe I should have read what you wrote better. You say it's not possible. Why not? Joel

Dominique Vercammen

unread,
Dec 12, 2014, 3:03:41 AM12/12/14
to casadi...@googlegroups.com
In our code, users define states, controls, parameters, etc and each time one of these is defined, variables are added. To be able to use vertsplit, the variables should only be defined at the end, when the total number of variables is known. It would be possible to do this, but would involve more work rewriting classes..

There is also no way to use SX as inputs to integrators? Because that would actually be the easiest to implement.

Dominique

Joris Gillis

unread,
Dec 12, 2014, 6:05:43 AM12/12/14
to casadi...@googlegroups.com
Dear Dominique,

As Joel hinted at, the proper way to do this is to refactor your code such that you do know all needed variables before constructing the NLP.
Once you construct the master MX, you let all individual decision variables depend on it.
You can do this manually with vertsplit, or you can make use of 'structures' (*).

Having said that, your question sparked a little discussion internally whether we could allow MXFunction to work as 'expected' in your example, such that refactoring on your part is not needed.

Joel has a pretty clear idea of how this can be done, but it will take some time before that feature would be added, if at all.
As an alternative and for your convenience, I've created a litte helper function (**) that will do the correct transformation such that your code will work.
It is hacky and not very efficient but why not give it a shot?

Best regards,
  Joris Gillis

(*) http://casadi.sourceforge.net/tutorials/tools/structure.pdf
(**) https://gist.github.com/jgillis/6c8d3a4b9567e39ba30e
Reply all
Reply to author
Forward
0 new messages