-
Notifications
You must be signed in to change notification settings - Fork 0
/
ALADIN_subAlg.m
373 lines (305 loc) · 11.9 KB
/
ALADIN_subAlg.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
% Solves a non-convex optimization problem in consensus form via the ALADIN
function [ xopt, logg , tot_time ] = ALADIN_subAlg( obj_funs,cons_funs,A_mats,b,x0,...
lam0,lbx,ubx,Sig,opts, U, L)
%get solver for QP step
QPsolver = opts.solveQP;
% get dimensions
NsubSys = length(obj_funs); %number of partitions
Ncons = size(A_mats{1},1); %number of equality constraints
% build global A matrix
A = horzcat(A_mats{:});
%% build local subproblems and CasADi functions
import casadi.*
rhoCas = SX.sym('rho',1,1);
lamCas = SX.sym('lam',length(lam0),1);
tic
nx = zeros(NsubSys,1);
for i=1:NsubSys
% discrete{i} = opts.discrete{i}; %1 denotes integer, 0 denotes real-valued variable
discrete{i} = zeros(1,length(x0{i}));
nx(i) = length(x0{i}); %number of variables in partition i
xCas = SX.sym('y',nx(i),1); %local decision variables
diff_x = find(ones(1,nx(i))-discrete{i}); %identify real variables for differentiation
yCas = SX.sym('x',nx(i),1); %parameterize QP solution
x_opt{i} = x0{i}; %initial point given to local solvers
% parameter vector of CasADi
pCas = [ rhoCas;
lamCas;
yCas];
% objective function for local NLP's
obj_fun_Loc_Cas = obj_funs{i}(xCas) + lamCas'*A_mats{i}*xCas ...
+ rhoCas/2*(xCas - yCas)'*Sig{i}*(xCas - yCas);
%Gradient of local objective
gCas = gradient(obj_funs{i}(xCas),xCas(diff_x));
g_fun{i} = Function(['g' num2str(i)],{xCas},{gCas});
% local inequality constraints
cons_Cas = cons_funs{i}(xCas);
% Jacobian of local constraints
Jac_Cas = jacobian(cons_Cas,xCas(diff_x));
Jac_Fun{i} = Function(['Jac' num2str(i)],{xCas},{Jac_Cas});
Nloc_cons{i}=size(Jac_Cas,1); %number of local inequality constraints
%Hessian of local objective
kappa_Cas = SX.sym('kapp',Nloc_cons{i},1);
rho_Cas = SX.sym('rho',1,1);
%The third term of the Hessian is to ensure positive-definiteness.
%Otherwise, the QP will be non-convex
Hess_Loc_Cass = hessian(obj_funs{i}(xCas)+kappa_Cas'*cons_Cas,xCas(diff_x)) + rho_Cas*Sig{i}(diff_x,diff_x);
Hess_Loc_Fun{i} = Function(['H' num2str(i)],{xCas,kappa_Cas,rho_Cas},{Hess_Loc_Cass});
% set up local solvers
solver_struct = struct('x',xCas,'f',obj_fun_Loc_Cas,'g',cons_Cas,'p',pCas);
nlp_opts.print_time = 0;
nlp_opts.ipopt.print_level = 0;
if sum(discrete{i}) == 0
nnlp{i} = nlpsol('solver','ipopt',solver_struct,nlp_opts);
else
MINLPsolver_opts.discrete = discrete{i};
minnlp{i} = nlpsol('solver','bonmin',solver_struct,opts.MINLPsolver_opts);
nnlp{i} = nlpsol('solver','ipopt',solver_struct,nlp_opts); %needed for obtaining langrange multipliers from MIP subprobs
end
end
toc
%% Initialization
i = 1;
yy = x0;
lam = lam0;
delx = inf;
rho = opts.rho0;
mu = opts.mu0;
tot_time = 0;
stop = 0;
%setup a log for some key vectors
logg = struct();
logg.X = vertcat(x0{:});
logg.delY = [];
logg.Kappa = [];
logg.lambda = [];
logg.obj = [];
logg.ConViol = [];
logg.status = 'timeout';
%track objective function compared to centralized solution
xx = SX.sym('x', [sum(nx), 1]);
cent_obj = 0;
var_count = 0;
for fun = 1:NsubSys
cent_obj = cent_obj + obj_funs{fun}(xx(var_count+1:var_count+nx(fun)));
var_count = var_count+nx(fun);
end
cent_obj_fun = Function('f',{xx},{cent_obj});
if ~isempty(opts.cent_sol)
figure(1);
set(gcf,'position',[10 30 400 300])
hold on;
grid on;
plot(opts.maxiter,full(cent_obj_fun(opts.cent_sol)),'-mo','MarkerEdgeColor','k','MarkerFaceColor',[.49 0 .63]);
title('Objective Function Value')
xlabel('Iterations')
logg.ObjVal = full(cent_obj_fun(logg.X(:,1)));
end
%track consensus constraint violation
ConViol = sum(abs(A*vertcat(yy{:}) - b));
figure(2);
set(gcf,'position',[420 30 400 300])
hold on;
grid on;
plot(1,ConViol,'-mo','MarkerEdgeColor','k','MarkerFaceColor',[.49 1 .63]);
title('Equality Constraint Violation')
xlabel('Iterations')
axis([1 opts.maxiter 0 1])
%%
while i <= opts.maxiter && stop == 0
sub_time = zeros(1,NsubSys);
for j=1:NsubSys
tic
% set up parameter vector for local NLP's
pNum = [ rho;
lam;
yy{j}];
% solve local MINLP's
if sum(discrete{j})>0
sol = minnlp{j}('x0' , x_opt{j},...
'p', pNum,...
'lbx', lbx{j},...
'ubx', ubx{j},...
'lbg', -inf*ones(Nloc_cons{j},1), ...
'ubg', zeros(Nloc_cons{j},1));
%resolve with fixed integer vars to obtain kappas
relbx{j}=lbx{j};
reubx{j}=ubx{j};
relbx{j}(find(discrete{j}))=full(sol.x(find(discrete{j})));
reubx{j}(find(discrete{j}))=full(sol.x(find(discrete{j})));
resol= nnlp{j}('x0' , full(sol.x).',...
'p', pNum,...
'lbx', relbx{j},...
'ubx', reubx{j},...
'lbg', -inf*ones(Nloc_cons{j},1), ...
'ubg', zeros(Nloc_cons{j},1));
if ~or(strcmp(minnlp{j}.stats.return_status,'SUCCESS'),strcmp(minnlp{j}.stats.return_status,'OPTIMAL'))
%this may make the solution jump around
sol.x = resol.x;
% keyboard
warning( '%s, Iteration: %i, Partition: %i',minnlp{j}.stats.return_status,i,j)
end
%Store local solutions
x_opt{j} = full(sol.x);
kappa_opt{j} = full(resol.lam_g);
else
sol = nnlp{j}('x0' , x_opt{j},...
'p', pNum,...
'lbx', lbx{j},...
'ubx', ubx{j},...
'lbg', -inf*ones(Nloc_cons{j},1), ...
'ubg', zeros(Nloc_cons{j},1));
if ~or(strcmp(nnlp{j}.stats.return_status,'Solve_Succeeded'),...
strcmp(nnlp{j}.stats.return_status,'Solved_To_Acceptable_Level'))
warning( nnlp{j}.stats.return_status )
end
%Store local solutions
x_opt{j} = full(sol.x);
kappa_opt{j} = full(sol.lam_g);
end
% active set detection
subprob_cons = full(cons_funs{j}(x_opt{j}));
inact = subprob_cons<-1e-5;
kappa_opt{j}(inact)= 0;
% Jacobians of inequality constraints
%(lower left component of AQP)
Ci = full(Jac_Fun{j}(x_opt{j}));
for d = 1:length(discrete{j})
if discrete{j}(d)==1
Ci(:,d) = 0;
end
end
C{j} = Ci(~inact,:); % eliminate inactive entries
% evaluate gradients and hessians for the QP
Hess_Loc_FunEval{j} = Hess_Loc_Fun{j}(x_opt{j},kappa_opt{j},rho);
g_fun_eval{j} = g_fun{j}(x_opt{j});
% eigenvalue decomposition of the hessian
[V,D] = eig(full(Hess_Loc_FunEval{j}));
e = diag(D);
% % flip the eigenvalues
% e = abs(e);
% modify zero and negative eigenvalues (regularization)
reg = 1e-4;
e(e<reg) = reg; % 1e-4
Hess_Loc_FunEval{j} = V*diag(e)*transpose(V);
sub_time(i) = toc;
end
tot_time = tot_time + max(sub_time);
%Termination Check
%%%% Add check on objective value %%%%
if max(abs(horzcat(A_mats{:})*vertcat(x_opt{:})-b))<opts.eps
xopt = vertcat(x_opt{:});
disp("Terminating due to EQ constraint satisfaction...")
logg.status = 'SUCCESS';
break
end
% build the QP
rhsQP = b-A*vertcat(x_opt{:});
C_QP = rref(blkdiag(C{:}));
%remove zero rows from C_QP
CQP_rows=size(C_QP,1);
r=1;
while r <= CQP_rows
if sum(abs(C_QP(r,:)))==0
C_QP(r,:)=[];
CQP_rows = CQP_rows-1;
else
r=r+1;
end
end
%linsolve and pinv don't seem to allow for sparse matrices
if or(strcmp(QPsolver,'linsolve'),strcmp(QPsolver,'pinv'))
HQP = full(blkdiag(Hess_Loc_FunEval{:},mu*eye(Ncons)));
else
HQP = sparse(blkdiag(Hess_Loc_FunEval{:},mu*eye(Ncons)));
end
gQP = full(vertcat(g_fun_eval{:},lam));
AQP = [A -eye(Ncons); C_QP zeros(CQP_rows,Ncons)];
bQP = [rhsQP;zeros(CQP_rows,1)];
%check positive-definiteness of HQP. TURN OFF FOR SPEED
% [~,p] = chol(HQP);
% if and(p>0,or(strcmp(QPsolver,'linsolve'),strcmp(QPsolver,'backslash')))
% warning('linsolve may not return optimal value for non-positive definite hessians')
% keyboard
% end
%remove ints from AQP
ints = find(horzcat(discrete{:}));
AQP(:,ints)=[];
% solve global QP
tic
[QP_sol, lamges] = solveQP(HQP,gQP,AQP,bQP,QPsolver);
tot_time = tot_time + toc;
delx = QP_sol(1:(end-Ncons));
alpha_1 = 1; alpha_2 = 1; alpha_3 = 1;
%%
% lambda only for cons. constr.
lam = lam + alpha_3*(lamges(1:Ncons) - lam);
% primal variable update
ctr = 1;
for j=1:NsubSys
delta_x{j} = zeros(nx(j),1);
for var = 1:nx(j)
delta_x{j}(var) = delx(ctr);
ctr = ctr + 1;
end
yy{j} = yy{j} + alpha_1*(x_opt{j}-yy{j}) + alpha_2*delta_x{j};
end
% logging
logg.X = [logg.X vertcat(x_opt{:})];
logg.delY = [logg.delY vertcat(delta_x{:})];
logg.Kappa = [logg.Kappa vertcat(kappa_opt{:})];
logg.lambda = [logg.lambda lam];
logg.obj = [logg.obj full(cent_obj_fun(vertcat(x_opt{:})))];
logg.ConViol = [logg.ConViol A*vertcat(x_opt{:}) - b];
% early termionation check
if i>opts.min_iter
if max(abs(horzcat(A_mats{:})*vertcat(x_opt{:})-b))<logg.ConViol(end) %convergence has started
if full(logg.ObjVal(i-1)) > full(obj) %converge from above
if full(obj) < L
logg.status = "Terminated by L";
stop = 1;
end
else %converge from below
if full(obj) > U
logg.status = "Terminated by U";
stop = 1;
end
end
end
end
% plotting
InOpt = cent_obj_fun(logg.X(:,i+1)+logg.delY(:,i));
ConViol = max(abs(A*vertcat(x_opt{:}) - b));
x = i;
figure(1);
y = full(InOpt);
hold on;
grid on;
plot(x,y,'-mo','MarkerEdgeColor','k','MarkerFaceColor',[.49 1 .63]);
figure(2);
z = full(ConViol);
hold on;
grid on;
plot(x+1,z,'-mo','MarkerEdgeColor','k','MarkerFaceColor',[.49 1 .63]);
%next iteration
i = i+1;
%updating rho and mu slightly at each iteration can help improve
%convergence, although this is a heuristic tool
if rho < opts.rhoMax
rho = rho*opts.rhoUpdate;
sprintf('rho: %d',rho)
end
if mu < opts.muMax
mu = mu*opts.muUpdate;
sprintf('mu: %d',mu)
end
if i > opts.maxiter
disp("Terminating due to iteration limit...")
end
if stop==1
disp("Terminating due to B&B bounds...")
end
end
xopt = vertcat(x_opt{:});
logg.time = tot_time;
end