-
Notifications
You must be signed in to change notification settings - Fork 1
/
GDILPLS.asv
453 lines (412 loc) · 16.7 KB
/
GDILPLS.asv
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
function [vbest,vlbest,vubest,activevl,activevu,slack,exitSLP] = GDILPLS(S,vl,vu,growth,growthmin,prodind,fixed,R,pvind,noConc,Kp,IC,epsProd,slackTol,vpminfrac)
%%function [vbest,vlbest,vubest,activevl,activevu,slack] =
%GDILPLS(S,vl,vu,growth,growthmin,prodind,fixed,R,pvind,noConc,Kp,IC,epsProd,
%slackTol,vpminfrac)
%The primary function that carris out the first stage of EMILiO.
%--------------------------------------------------------------------------
%Function Called By
%--------------------------------------------------------------------------
%Function Calls
%function Z=evalObj(X,E,F)
% Nested function to evaluate the value of the merit objective defined for
% the gradient-based optimization algorithm.
%
%function [X,Y,exitflag]=McCormick(f,xl,xu)
% Nested function to implement McCormick convex relaxations to esape local
% optimas as well as provide an initial solution to warm-start the SLP
%--------------------------------------------------------------------------
%Inputs
%S - Stoichiometric Matrix
%vl - Lower bound on fluxes
%vu - Upper bound on fluxes
%growth - Index of biomass flux in model.rxns
%growthmin - Minimum required growth rate for growth coupled production
%prodind - Index of target metabolite exchange flux in model.rxns
%--------------------------------------------------------------------------
%Optional Inputs
%fixed
%R - Reduced row echelon form of S
%pvind - Linearly indepedent fluxes used as pivots to reduce S to R
%noConc - Parameter to force reersible reactions to have unidirectional
%flux. 1 - Allows forcing or 0 - Otherwise
%Kp - Objective weight to maximize metabolite production rate
%IC -
%epsProd - Minimum difference in production flux between strain designs for
% them to be classified as different
%slackTol - Completary constraint violation tolerance
%vpminfrac - Fraction of maximum theoretical production to engineer for
%--------------------------------------------------------------------------
%Outputs
%vbest - Modified bounds on all fluxes
%vlbest - Modified/Optimal lower bounds on fluxes
%vubest - Modified/Optimal upper bound on fluxes
%activevl - Reactions with active fluxes at the lower bound
%activevu - Reactions with active fluxes at the upper bound
%slack - Value of slack variables for complementary constriants
%exitSLP - Specifies if SLP has found a solution (1) or not (0)
%--------------------------------------------------------------------------
% Laurence Yang Dec. 10, 2009 -> August 3, 2010
%
% Added convex relaxation
if nargin<7
fixed=[];
end
if nargin<8
[R,pvind]=rref(full([S sparse(Sm,1,0)]));
end
if nargin<10
noConc=0;
end
stepTol = 0; % Step below stepTol indicates convergence of algorithm
wTol = 1e-3; % Duals greater than wTol are considered active constraints
Kkkt=1; % Objective weight to minimize KKT violation
if nargin<11
Kp=1000; % Objective weight to maximize production rate
end
if nargin<12
IC=[];
end
if nargin<13
epsProd= 1e-3; % Constant to minimize vprod in primal objective, relative to max growth
end
if nargin<14
slackTol = 1e-6; % Complementarity constraint violation tolerance
end
if nargin<15
vpminfrac=0.99;
end
vbest=[];
vlbest=[];
vubest=[];
activevl=[];
activevu=[];
exitSLP = 1; % exitflag starts at okay. If solution not found, changes to 0.
Kw=0; % Objective weight to minimize active constraints
maxIter = 30;
sameTol=1e-3; % Modified bounds must differ from original bounds by at least sameTol to be considered modified
objTol = 1e-6;
epsGrowth = 1;
resLS = 1000; % Resolution for line search
maxflux=max([abs(vl(:)); abs(vu(:))]);
KKTviolMax = 4*maxflux*maxflux; % Bound on s, the auxiliary variables ==> KKT violation
[Sm,Sn]=size(S);
% First, assess maximum possible production rate for growth = growthmin
cprod = sparse(1,prodind,-1,1,Sn);
vl2=vl; vu2=vu;
vu2(prodind)=maxflux;
vl2(growth)=growthmin;
v=cplexlp(cprod(:),[],[],S,sparse(Sm,1,0),vl2,vu2);
maxProd = abs(cprod*v);
fprintf('Maximum possible production rate for growth rate of %g h-1: %g\n',[growthmin,maxProd]);
vprodmin = abs(vpminfrac*maxProd);
m = 2*Sn;
[TX,rX,pvind,freeind]=projRREF(R,pvind);
nfree = length(freeind); npv=length(pvind);
vflength = nfree;
vulength = Sn;
vllength = Sn;
mulength = 2*Sn;
wlength = 2*Sn;
slength = 2*Sn;
vfstart = 0;
vustart = vfstart+vflength;
vlstart = vustart+vulength;
mustart = vlstart+vllength;
wstart = mustart+mulength;
sstart = wstart+wlength;
nX = nfree+Sn+Sn+2*Sn+2*Sn; %[dvf; dvu; dvl; dmu; dw]
nY = vflength+vulength+vllength+mulength+wlength+slength;
% [dvf; dvu; dvl; dmu; dw; s]
N = nY;
Nones = ones(1,nX);
pvf = sparse(1:vflength,vfstart+(1:vflength),1,vflength,N);
pvfX = sparse(1:vflength,vfstart+(1:vflength),1,vflength,nX);
pvu = sparse(1:vulength,vustart+(1:vulength),1,vulength,N);
pvl = sparse(1:vllength,vlstart+(1:vllength),1,vllength,N);
pvuX = sparse(1:vulength,vustart+(1:vulength),1,vulength,nX);
pvlX = sparse(1:vllength,vlstart+(1:vllength),1,vllength,nX);
pmu = sparse(1:mulength,mustart+(1:mulength),1,mulength,N);
pmuX = sparse(1:mulength,mustart+(1:mulength),1,mulength,nX);
pw = sparse(1:wlength,wstart+(1:wlength),1,wlength,N);
pwX = sparse(1:wlength,wstart+(1:wlength),1,wlength,nX);
ps = sparse(1:slength,sstart+(1:slength),1,slength,N);
pd = sparse(1:nX,1:nX,1,nX,N); % d=pd*X
%%%%%%%%%%%%%%%
% Maximin % VERY IMPORTANT: Prevent alternate optimal production rate
% that is actually small. Hence, we are actually doing maximin
cboth=sparse([1 1],[growth prodind],[epsGrowth -epsProd],1,Sn);
c=cboth;
%%%%%%%%%%%%%%%
A = [-TX; TX]; % v = rX-TX*vf
E=sparse(1:2*Sn,nfree+2*Sn+(1:2*Sn),1,2*Sn,nX); % dmu = E*d
F=sparse(1:2*Sn,nfree+4*Sn+(1:2*Sn),1,2*Sn,nX); % dw = F*d
vul=vl; vll=vl;
vuu=vu; vlu=vu;
% Irreversible reactions stay irreversible
Irrev = vl==0;
vll(Irrev) = 0;
Irrev = vu==0; %Irreversible in reverse direction
vuu(Irrev) = 0;
% If noConc, disallow forcing reversible reactions.
% This assumes we provide "true" vmin,vmax using fva
if noConc
rev = (vu>0) & (vl<0);
vul(rev)=0;
vlu(rev)=0;
end
% Prevent active constraint of production flux
vul(prodind)=maxflux; vuu(prodind)=maxflux;
vll(prodind)=0; vlu(prodind)=0;
% Other fixed bounds. Note, vld>=vl & vud<=vu.
% Hence, fluxes like ATPM won't get manipulated
if not(isempty(fixed))
vul(fixed)=vu(fixed);
vuu(fixed)=vu(fixed);
vll(fixed)=vl(fixed);
vlu(fixed)=vl(fixed);
end
% Set minimum growth rate
vul(growth)=vu(growth); vuu(growth)=vu(growth);
vll(growth)=growthmin; vlu(growth)=growthmin;
xl=[vl(freeind); vul; vll; sparse(4*Sn,1,0)];
xu=[vu(freeind); vuu; vlu; 2*maxflux*ones(4*Sn,1)];
%%%%%%%%%%%%%%%%%%%%
% Initial solution %
% Use McCormick convex relaxations of bilinear constraints and find an
% initial point that is, hopefully close to optimum
f = Kkkt*ones(1,2*Sn)*ps + Kp*cprod*-TX*pvf;
[X,Y,exitflag]=McCormick(f,xl,xu);
if exitflag==1
fprintf('Found initial solution using McCormick relaxations\n');
fprintf('Relaxed optimal estimated KKT = %g\n',sum(abs(ps*Y)));
fprintf('Relaxed optimal actual KKT = %g\n',sum(abs((E*X).*(F*X))));
fprintf('Relaxed optimal Vprod = %g\n',cprod*(rX-TX*pvfX*X));
else
X=(xl+xu)/2;
fprintf('Failed to find initial solution\n');
end
if not(isempty(IC))
X(1:nfree)=(IC.vl(freeind)+IC.vu(freeind)) / 2;
end
%%%%%%%%%%%%%%%%%%%%
vprod = abs(cprod*(rX-TX*pvfX*X));
slack = 1000;
optimal=0;
objs=[];
set(gcf,'Color','w');
tic
while not(optimal)
step = 100; Xbest = X; objBest = Inf; iter=0; % Reinitialize
f = Kkkt*ones(1,2*Sn)*ps + Kp*cprod*-TX*pvf + Kw*ones(1,2*Sn)*pw; % Outer objective function updated
while iter < maxIter && abs(step) > stepTol
iter = iter+1;
% Re-construct constraint matrices at the new solution
EX = E*X;
FX = F*X;
b = [pvuX*X-rX; rX-pvlX*X];
Aineq = [(FX(:,Nones).*E + EX(:,Nones).*F)*pd-ps];
bineq = [-EX.*FX];
AeqY = [A*pvf+pmu-[pvu;-pvl]; A'*pw];
beqY = [b-A*pvfX*X-pmuX*X; (c*-TX)'-A'*pwX*X];
% Re-construct bounds on delX and s at the new solution
yl = [xl-X; zeros(slength,1)];
yu = [xu-X; KKTviolMax*ones(slength,1)];
% Determine if we can satisfy KKT s.t. target production rate
% Convex relaxation of bilinear constraints
% Solve LP for SLP
[Y,fval,exitflag,details] = cplexlp(f',Aineq,bineq,AeqY,beqY,yl,yu);
if exitflag ~= 1
fprintf('Stopping ILP due to the following CPLEX status:\n');
fprintf([details.message,'\n']);
break;
else
d = Y(1:nX);
%X = X + d;
% Line search necessary for convergence to local optima
% Note: this depends on the objective function, f
exfx=sum((E*X).*(F*X));
edfd=sum((E*d).*(F*d));
exfd=sum((E*X).*(F*d));
fxed=sum((F*X).*(E*d));
delvprod = cprod*-TX*pvfX*d; % rX gets canceled out. Change in vprod
sumdelw = sum(pwX*d);
a0=Kkkt*edfd;
b0=Kkkt*(exfd+fxed)-Kp*delvprod;
c0=Kkkt*exfx-Kp*cprod*(rX-TX*pvfX*X);
%%%%%%%%%%%%%%%
% Line search %
lambdas=linspace(0,1,resLS);
Zs=a0*(lambdas.^2) + b0*lambdas + c0;
minZ=min(Zs);
lambdamin=lambdas(Zs==minZ);
step = lambdamin(1);
%%%%%%%%%%%%%%%%%%%%%%%%%
% If step size is 0, then SLP has converged.
% However, if KKT is still violated, then we can try to escape
% this local optimum and continue the SLP until next
% convergence. For this, we should perturb the solution/objective a bit.
%if step < 1e-8 && slack > slackTol
if step < 1e-8 && not(optimal)
% 1. Simplest heuristic without much theoretical basis
%step = 1;
%fprintf('Converged to local optimum but KKT still violated. Continuing SLP\n');
% 2. Use McCormick relaxation in deviation variables
% relative to current point to determine relaxed KKT
% 3. Use McCormick with bounds tightened to be near the
% current solution and determine relaxed KKT
nrel = 5;
Xms = sparse(nX,nrel);
Objs = sparse(1,nrel);
krel=0;
for pfrac=linspace(0.1,0.9,nrel)
krel=krel+1;
%pfrac = 0.2; % Fraction of feasible range to consider
xl2 = X-pfrac*(X-xl);
xu2 = X+pfrac*(xu-X);
%f2 = Kkkt*ones(1,2*Sn)*ps; %+ Kp*cprod*-TX*pvf;
f2 = Kkkt*ones(1,2*Sn)*ps + Kp*cprod*-TX*pvf; % cprod already has -1
[X2,Y2,exitflag]=McCormick(f2,xl2,xu2);
if exitflag==1
KKT=sum(abs((E*X2).*(F*X2)));
KKTlin=sum(abs(ps*Y2));
vprod2 = abs(cprod*(rX-TX*pvfX*X2));
Xms(:,krel)=X2;
Obj = Kkkt*KKT - Kp*vprod2;
Objs(krel) = Obj;
fprintf('McCormick relaxed optimal KKT near current solution is %g\n',KKT);
fprintf('McCormick relaxed optimal linearized KKT near current solution is %g\n',KKTlin);
fprintf('McCormick relaxed optimal Vprod near current solution is %g\n',vprod2);
else
fprintf('Relaxed problem is infeasible\n');
end
end
bestObj = min(Objs);
bestrel = (Objs == bestObj);
% See if relaxed objective is better than current value
ObjNow = Kkkt*slack - Kp*vprod;
if abs(ObjNow - bestObj) < 1e-6
fprintf('Objective cannot be improved further because no relaxed objective better than current SLP solution exists\n');
fprintf('Try resolving SLP from different initial solution\n');
fprintf('Stopping SLP\n');
exitSLP = 0;
return;
else
X=Xms(:,bestrel);
X=full(X);
step = 1;
fprintf('Better relaxed objective found. Continuing SLP\n');
end
else
% Update to the new point
X = X+step*d;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%
% Print some diagnostics
vprod = abs(cprod*(rX-TX*pvfX*X));
slack = sum(abs((E*X).*(F*X)));
Pviol = sum( [pvuX*X+1e-6;-(pvlX*X-1e-6)] < [-TX;TX]*pvfX*X );
Dviols = abs((c*-TX)' - A'*pwX*X);
Dviol = sum(Dviols);
KKTdual = max(abs(pvfX*X).*Dviols); % Worst eta*x violation due to dual equality constraint violation
boundViol = sum( (X<xl-1e-6)+(X>xu+1e-6) );
fprintf('Production rate: %g (%g%% of max)\n', [vprod 100*vprod/maxProd]);
fprintf('Complementarity violation: %g\n',slack);
fprintf('Worst dual complementary violation: %g\n',KKTdual);
fprintf('Primal constraint violation: %g\n',Pviol);
fprintf('Dual constraint violation: %g\n',Dviol);
fprintf('Bound violation: %g\n',boundViol);
fprintf('Norm of displacement vector: %g\n',norm(d));
fprintf('Step size: %g\n',step);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Evaluate objective at new solution
%obj=slack+Pviol+abs(Dviol)-vprod;
%obj = Kkkt*slack - Kp*vprod;
obj = evalObj(X,E,F);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% See the objective & step size
objs=[objs obj];
subplot(2,1,1);
cla;
hold on;
plot(lambdas,Zs,'g','LineWidth',2);
scatter(lambdamin,minZ,64,'md','filled');
line([step step],[minZ max(Zs)]);
%myarrow([step step],[minZ max(Zs)]);
%myarrow([step step],[max(Zs) minZ]);
xlabel('Step size'); ylabel('Objective');
subplot(2,1,2);
plot(1:iter,objs,'g','LineWidth',2);
xlabel('Iteration'); ylabel('Objective');
drawnow;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Retain solution if it is an improvement
if obj < objBest
Xbest=X;
objBest = obj;
end
optimal = (slack<=slackTol) & (vprod>=vprodmin);
end
if slack>slackTol
fprintf('KKT not satisfied\n');
end
if vprod < vprodmin
fprintf('Minimum production requirement not met\n');
end
end
toc
vbest = rX-TX*pvfX*Xbest;
vlbest = pvlX*Xbest;
vubest = pvuX*Xbest;
activebounds = pwX*Xbest;
activevu = activebounds(1:Sn) > wTol; %Sn
activevl = activebounds(Sn+(1:Sn)) > wTol; %Sn
% Only change active bounds
vlbest(not(activevl))=vl(not(activevl));
vubest(not(activevu))=vu(not(activevu));
% In fact, we can clean results a bit more
% Some bounds might be active, but were already active. e.g.,
% irreversibility constraints or substrate/oxygen uptake constraints
redundvl=abs(vlbest(activevl)-vl(activevl))<sameTol;
redundvu=abs(vubest(activevu)-vu(activevu))<sameTol;
activevl(redundvl)=0;
activevu(redundvu)=0;
vlbest(not(activevl))=vl(not(activevl)); % Re-clean active bounds
vubest(not(activevu))=vu(not(activevu));
activevl=find(activevl);
activevu=find(activevu);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Z=evalObj(X,E,F)
exfx=sum((E*X).*(F*X));
vprod = abs(cprod*(rX-TX*pvfX*X));
Z = Kkkt*exfx - Kp*vprod;
end
function [X,Y,exitflag]=McCormick(f,xl,xu)
EXL = E*xl; FXL = F*xl;
EXU = E*xu; FXU = F*xu;
% McCormick relaxations
Aineq = [(EXL(:,Nones).*F+FXL(:,Nones).*E)*pd-ps;
(EXU(:,Nones).*F + FXU(:,Nones).*E)*pd-ps;
ps-(EXU(:,Nones).*F+FXL(:,Nones).*E)*pd;
ps-(EXL(:,Nones).*F+FXU(:,Nones).*E)*pd];
bineq = [ EXL.*FXL;
EXU.*FXU;
-EXU.*FXL;
-EXL.*FXU];
AeqY = [A*pvf+pmu-[pvu;-pvl]; % Primal constraints
A'*pw]; % Dual constraints
beqY = [sparse(2*Sn,1,0);
(c*-TX)'];
yl = [xl; sparse(slength,1,0)];
%yl = [xl; -KKTviolMax*ones(slength,1)];
yu = [xu; KKTviolMax*ones(slength,1)];
[Y,fval,exitflag,details] = cplexlp(f(:),Aineq,bineq,AeqY,beqY,yl,yu);
if exitflag==1
X = Y(1:nX);
fprintf('Found solution using McCormick relaxations\n');
else
X=[]; Y=[];
fprintf('Failed to find solution with McCormick relaxations\n');
end
end
end