-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollocation.py
More file actions
152 lines (105 loc) · 3.82 KB
/
collocation.py
File metadata and controls
152 lines (105 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import numpy as np
from casadi import *
import runge_kutta as rk
def collocation_equations(f,x,u,t,h,c,A,b):
"""
G,X,x_next = collocation_equations(f,t,x,h,c,A,b)
Returns
G - a CasADi symbolic expression such that G == 0
corresponds to the Runge-Kutta equations.
X - CasADi variables for the states at the collocation points
x_next - A CasADi symbolic variable for the value of the
state at the end of the RK step. This is consistent with
multiple shooting. For single shooting, the variable x_next
could be eliminated .
Inputs
f - A function of the form x_dot = f(t,x) for the differential equation
t - The initial time for evaluation
x - An initial condition for the Runge-Kutta step
u - An input which is held constant over the step
h - A step size
c,A,b - encodes a Butcher Tableau
"""
n = np.prod(x.shape)
x0 = x.reshape((n,1))
s = len(c)
X = MX.sym('X',(n,s))
t_eval = t + h * np.array(c)
F_list = []
for i in range(s):
fi = f(t_eval[i],X[:,i],u)
F_list.append(fi)
F = horzcat(*F_list)
G = x0 @ np.ones((1,s)) + h * F@A.T - X
x_next = MX.sym('x_next',(n,1))
G = vertcat(G.reshape((n*s,1)),x_next - x0 - h*F@b.reshape((s,1)))
return G,X,x_next
def collocation_extra_constraints(g,X,u):
"""
G,lb,ub = collocation_extra_constraints(g,X,u)
Extra inequality constraints of the form
g(x,u) <= 0
which are to be enforced at all of the collocation points.
"""
m = X.shape[1]
G_list = []
for i in range(m):
G_list.append(g(X[:,i],u))
G = vertcat(*G_list)
lb = -np.inf * np.ones(G.shape)
ub = np.zeros(G.shape)
return G,lb,ub
def collocation_optimize(f,g,Time,x0,nU,c):
"""
"""
c,A,b = rk.collocation_tableau(c)
U_sym = MX.sym('U',(nU,len(Time)-1))
x_vars = []
collocation_vars = []
G_list = []
ub_list = []
lb_list = []
for k in range(len(Time)-1):
h = Time[k+1]-Time[k]
t = Time[k]
if k == 0:
x_cur = np.copy(x0)
else:
x_cur = x_next
u_cur = U_sym[:,k]
G_col,X,x_next = collocation_equations(f,x_cur,u_cur,t,h,c,A,b)
x_vars.append(x_next)
collocation_vars.append(X.reshape((np.prod(X.shape),1)))
G_col_flat = rk.flatten(G_col)
G_list.append(G_col_flat)
ub_list.append(np.zeros(G_col_flat.shape))
lb_list.append(np.zeros(G_col_flat.shape))
if k == 0:
# We need to ensure that the first input is feasible
X_all = horzcat(DM(x0),X,x_next)
else:
X_all = horzcat(X,x_next)
G_con,lb_con,ub_con = collocation_extra_constraints(g,X_all,u_cur)
G_list.append(G_con)
ub_list.append(ub_con)
lb_list.append(lb_con)
G_all = vertcat(*G_list)
ub_all = vertcat(*ub_list)
lb_all = vertcat(*lb_list)
X_traj = horzcat(*x_vars)
Collocation_traj = vertcat(*collocation_vars)
Z = vertcat(rk.flatten(X_traj),rk.flatten(U_sym),Collocation_traj)
nlp = {'x' : Z, 'f' : X_traj[0,-1], 'g' : G_all}
opts = {'ipopt' : {'print_level' : 0},
'print_time' : False,
'error_on_fail': True}
solver = nlpsol('solver','ipopt',nlp,opts)
try:
res = solver(ubg = ub_all,lbg=lb_all)
X_opt = res['x'][:np.prod(X_traj.shape)].reshape(X_traj.shape)
X_opt = np.array(horzcat(x0,X_opt))
U_opt = res['x'][np.prod(X_traj.shape):np.prod(X_traj.shape)+np.prod(U_sym.shape)]
U_opt = np.array(U_opt).reshape(U_sym.shape)
return X_opt,U_opt
except RuntimeError:
return None