-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrajopt_midpoint.m
145 lines (142 loc) · 4.5 KB
/
trajopt_midpoint.m
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
function [ot, ou, oh, process] = trajopt_midpoint(plant, N, Tmin, Tmax, xDim, uDim, jcost, startpos, endpos)
%% Initialization of parameters
umax = 10;
alpha = 0.1; betasucc = 1.2; betafail = 0.5; rhoinit = 1;
lambda = 200;
tlambda = 10;
lambda_inc = 1.5;
Kp = 60;
Kc = 200;
Kt = 50;
rhothres = 0.0001;
xtol = 0.0001;
ftol = 0.0001;
ctol = 0.000001;
phis = [];
phihats = [];
etas = [];
etahats = [];
tcosts = [];
jcosts = [];
ucosts = [];
rhos = [];
deltahats = [];
deltas = [];
converge_cnt = 0;
%% Set the inital trajectory.
ot = [startpos rand(xDim,N-2)*2*pi-pi endpos];
ou = rand(uDim,N)*10-5;
oT = 4;
oh = ones(1,N-1)*oT/(N-1);
%initial value of phi.
rho = rhoinit;
%% Convex sequential optimization
cvx_quiet(true);
tic
for epoch = 1:Kp
rho = rhoinit;
for j = 1:Kc
eta = zeros(xDim, N);
A = cell(N-1,1);
B = cell(N-1,1);
S = cell(N-1,1);
F = cell(N-1,1);
for t = 1:N-1
x0 = ot(:,t); u0 = ou(:,t);
x1 = ot(:,t+1); u1 = ou(:,t+1);
[f,df] = plant.dynamics(0,(x0 + x1)/2,(u0 + u1)/2);
F{t} = f;
A{t} = full(df(:,2:1+xDim));
B{t} = full(df(:,2+xDim:end));
eta(:,t) = x1 - x0 - oh(t)*f;
end
oldphi = tlambda*sum(oh)+(jcost(ot) + sum_square(ou(:))) + lambda*sum(abs(eta(:)));
for i = 1:Kt
cvx_begin
variable nt(xDim, N);
variable u(uDim, N);
variable h(1,N-1);
etahat = cvx(zeros(xDim, N-1));
% Initial and final conditions.
nt(:,1) == startpos;
nt(:,N) == endpos;
for t = 1:N-1
x0 = ot(:,t); u0 = ou(:,t);
x1 = ot(:,t+1); u1 = ou(:,t+1);
etahat(:,t) = nt(:,t+1) - nt(:,t) - F{t}*(h(t)) - A{t}*(nt(:,t) +nt(:,t+1)-x0-x1)/2*oh(t) - B{t}*(u(:,t)+u(:,t+1)-u0-u1)/2*oh(t);
%etahat(:,t) == 0;
end
h >= Tmin/(N-1);
h <= Tmax/(N-1);
% Trust region constraints.
abs(nt(:) - ot(:)) <= rho;
abs(u(:) - ou(:)) <= rho;
abs(h(:) - oh(:)) <= rho;
% Torque limit.
abs(u(:)) <= umax;
minimize(tlambda*sum(h)+(jcost(nt) + sum_square(u(:)))+ lambda*sum(abs(etahat(:))));
cvx_end
% Calculate actual costs.
eta = zeros(xDim, N-1);
for t = 1:N-1
% Penalty for ynamics constraints
x0 = nt(:,t); u0 = u(:,t);
x1 = nt(:,t+1); u1 = u(:,t+1);
[f] = plant.dynamics(0,(x0+x1)/2,(u0+u1)/2);
eta(:,t) = nt(:,t+1) - nt(:,t) - h(t)*f;
end
cons = sum(abs(eta(:)));
phi = tlambda*sum(h)+(jcost(nt) + sum_square(u(:))/4)+ lambda*cons;
delta = oldphi - phi;
phihat = cvx_optval;
deltahat = oldphi - phihat;
phis = [phis phi];
phihats = [phihats phihat];
etas = [etas sum(abs(eta(:)))];
etahats = [etahats sum(abs(etahat(:)))];
jcosts = [jcosts jcost(ot)];
ucosts = [ucosts sum_square(ou(:))];
deltas = [deltas delta];
deltahats = [deltahats deltahat];
tcosts = [tcosts sum(oh)];
deltax = sum(abs(nt(:) - ot(:)));
disp(cvx_status);
disp(' epoch j i etahat eta rho')
disp([epoch j i sum(abs(etahat(:))) cons rho])
disp(' phihat phi deltahat delta deltax rho')
disp([phihat phi deltahat delta deltax rho])
rhos = [rhos rho];
if isnan(phihat) || isinf(phihat) || delta <= alpha*deltahat
rho = betafail*rho;
else
rho = betasucc*rho;
% Accept the solution
ot = nt; ou = u; oh = h; oT = sum(h);
%oldphi = phi;
break;
end
if(rho < rhothres)
break;
end
end
if (abs(deltax) < xtol || (abs(delta) < ftol && abs(deltahat) < ftol) || rho < rhothres)
break;
end
end
if (cons <= ctol)
break;
else
lambda = lambda*lambda_inc;
end
end
tot = toc
process.phis = phis;
process.jcosts = jcosts;
process.ucosts = ucosts;
process.tcosts = tcosts;
process.rhos = rhos;
process.etas = etas;
process.phihats = phihats;
process.etahats = etahats;
process.deltas = deltas;
process.deltahats = deltahats;