Hi,
I'm solving optimal control problems with ode's described by a set of equations which change depending on the state. To write these equations in CasADi I use if_else. This functionality works fine if the set of equations returns a valid number on all the domain. But if I declare something like if_else(x>1,sqrt(x),x) and if x goes negative the optimization solver brakes. At the end of the message I provide you with a piece of code reproducing the problem. Do you have any suggestion to overcome this?
I'm also working with JModelica and I want to import statements involving if_else. What is the syntax for this purpose?. I tried in several ways (for example, y = if x > 0 then x else -x; ) but I get always a compilation error when running compile_fmux.
Thanks for your help!
Andres
----------------------------------------------------------------------
from casadi import *
tf = 10.0
N = 1
ref = -2
x = ssym('x',2)
u = ssym('u',1)
dx0 = (x[1]-ref)*(x[1]-ref) + u*u
dx1 = if_else(x[1]>1,-sqrt(x[1]),-x[1])+u
fx = vertcat([dx0,dx1])
ode=SXFunction(daeIn(x=x,z=[],p=u,t=[]),daeOut(ode=fx))
I = CVodesIntegrator(ode)
I.setOption("tf",tf/N)
I.setOption("verbose",True)
I.setOption('monitor',[ 'inputs' ,'outputs', 'res', 'resB', 'resQB' ,'reset', 'psetupB', 'djacB'])
I.init()
U = msym("U", N)
X0 = [0,2]
X = msym(X0)
for k in range(N):
(X,_,_,_) = I.call( (X,U[k]) )
nlp = MXFunction(nlpIn(x=U),nlpOut(f=X[0],g=[]))
solver = IpoptSolver(nlp)
solver.init()
solver.setInput( 0.0, "x0")
solver.solve()
u_opt = solver.output()