Setting input variable to discrete values(not integers)

310 views
Skip to first unread message

MOHAN NARAYANAM

unread,
Jul 6, 2022, 9:09:48 PM7/6/22
to CasADi
I am trying to solve an optimal control problem. I have to create a discrete variable that takes only 5 possible values that are not integers. I created a function as shown below whose input 'u' takes discrete values from 0 to 4. The output of the function will be my desired discrete variable. But I am getting an exception. Could anyone help me to solve this problem or suggest some alternative to solve the problem? 

%% Code starts here
import casadi as ca

arr = ca.SX([13.45, 7.57, 5.01, 3.77, 2.83690619])
u = ca.MX.sym('u')

get_ratio=ca.Function('get_ratio',u,arr[u])

%% Code ends here

I am getting the below exception.

Exception: Implicit conversion of symbolic CasADi type to numeric matrix not supported.
This may occur when you pass a CasADi object to a numpy function.
Use an equivalent CasADi function instead of that numpy function.


Thanks a lot in advance,
Hemanth Mohan.  


Bruno M L

unread,
Jul 15, 2022, 1:12:58 PM7/15/22
to CasADi
Hi!

I found one function in the Python API which implements what you want. Maybe it works

u = ca.MX.sym('u')
values = [13.45, 7.57, 5.01, 3.77, 2.83690619]

x_default = [13.45]
out = ca.conditional(u, values, x_default)
get_ratio = ca.Function('get_ratio', [u], [res])

for i in range(len(values)):
    print(get_ratio(i))

Note: For values outside the length of the list, the result is x_default.

I hope it helps!
Reply all
Reply to author
Forward
0 new messages