Maximum_Iterations_Exceeded when using opti stack to solve the simple test problem

53 views
Skip to first unread message

Jerry

unread,
Sep 19, 2019, 4:33:20 AM9/19/19
to CasADi
hi guys, 
    
    I'm new to Casadi , when I try to use Opti stack the solve the 8.1 A simple test problem https://web.casadi.org/docs/#a-simple-test-problem I get into some problem like it can't converge to a solution;

mycode.png

here is my code 

also, I have seen the example in direct_multiple_shooting.py the example work well, the code can be seen here https://github.com/casadi/casadi/blob/master/docs/examples/python/direct_multiple_shooting.py ;
Wish to get some advice from you to figure out the bug in my code.

Regard

Jerry

Jerry

unread,
Sep 19, 2019, 4:35:37 AM9/19/19
to CasADi
My code in text :

from casadi import *

T = 10
N = 20#dt = 0.2s
opti = Opti()

X = opti.variable(2,N+1)
# print(X)
U = opti.variable(1,N)
x0 = X[0,:]
x1 = X[1,:]

f = lambda x,u:vertcat( ((1-x[1])**2*x[0]-x[1]+u[0]),x[1])#x' = f(x,u)

dt = T/N
J = 0
X[0,0]==0
X[1,0]==1
for k in range(N):
k1 = f(X[:,k], U[:,k])
k2 = f(X[:,k]+dt/2*k1, U[:,k])
k3 = f(X[:,k]+dt/2*k2, U[:,k])
k4 = f(X[:,k]+dt*k3, U[:,k])
x_next = X[:,k] + dt/6*(k1+2*k2+2*k3+k4)
opti.subject_to(X[:,k+1]==x_next)
J = J + X[0,k]**2+X[1,k]**2+U[0,k]**2

opti.minimize(J)
opti.subject_to(x1>=-0.25)
opti.subject_to(x0[0] == 0)
opti.subject_to(x1[0] == 1)
opti.set_initial(U,0)
opti.set_initial(x0,0)
opti.set_initial(x1,0)
opti.subject_to(opti.bounded(-1,U,1))

opti.solver("ipopt")
print(opti)
sol = opti.solve()

在 2019年9月19日星期四 UTC+8下午4:33:20,Jerry写道:

Joel Andersson

unread,
Sep 22, 2019, 4:40:21 PM9/22/19
to CasADi
Hi,
Try reducing your code to a minimal failing example. Start by decreasing N, possibly to 1. Also, remove variables. You have to do this yourself - no one will do it for you. Usually it makes sense to start with something simple, that you know works and then change it step by step, making sure that it works in each step.

Joel
Reply all
Reply to author
Forward
0 new messages