-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfmindescent.m
More file actions
720 lines (608 loc) · 21.4 KB
/
Copy pathfmindescent.m
File metadata and controls
720 lines (608 loc) · 21.4 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
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
classdef fmindescent < handle
properties(Constant)
name = 'fmindescent';
end
properties
options = [];
end
properties(SetAccess = public, Hidden = true)
% Inputs
fun = [];
x0 = [];
A = [];
b = [];
Aeq = [];
beq = [];
lb = [];
ub = [];
nonlcon = [];
nDV = [];
% Merit function variables
f0 = []; % Initial, unpeanalized objective function value
aFac = []; % Scaling parameter for merit function
lambda = []; % Lagrange variables for "Augmented Lagrange (AL) method"
nG = []; % Total number of constraints
Asum = []; % Sum all columns in A
AeqSum = []; % Sum all columns in Aeq
g = []; % all constraints at latest evaluation. Stores as g <=0 in the following orderfields
% [gnl;gnleq;-gnleq;glin;glineq;-glineq;-x+this.lb;x-this.ub];
% Second order Quasi-Newton Methods
Hinv = []; % Inverse of hessian DFP method
H = []; % Hessian BFGS method
iH = 0; % Hessian update counter
% Iteration history
history = struct();
% Switch
initialized = false;
end
methods
% Construct
function this = fmindescent(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,varargin)
% initialize data structures
if nargin < 1 || ~isa(fun,'function_handle')
error([this.name,': fun is required to be a function handle'])
else
this.fun = fun;
end
if nargin < 2 || isempty(x0)
this.x0 = [];
else
if isnumeric(x0)
this.x0 = x0(:);
else
error([this.name,' x0 is required to be of type numeric'])
end
end
if nargin < 3 || isempty(A)
this.A = [];
else
if isnumeric(A)
this.A = A;
this.Asum = sum(A,2);
else
error([this.name,' A is required to be of type numeric'])
end
end
if nargin < 4 || isempty(b)
this.b = [];
else
if isnumeric(b)
this.b = b(:);
else
error([this.name,' b is required to be of type numeric'])
end
end
if size(this.A,1) ~= size(this.b,1)
error([this.name,' A and b must contain equal number of rows'])
end
if nargin < 5 || isempty(Aeq)
this.Aeq = [];
else
if isnumeric(Aeq)
this.Aeq = Aeq;
this.AeqSum = sum(Aeq,2);
else
error([this.name,' Aeq is required to be of type numeric'])
end
end
if nargin < 6 || isempty(beq)
this.beq = [];
else
if isnumeric(beq)
this.beq = beq(:);
else
error([this.name,' beq is required to be of type numeric'])
end
end
if size(this.Aeq,1) ~= size(this.beq,1)
error([this.name,' Aeq and beq must contain equal number of rows'])
end
if nargin < 7 || isempty(lb)
this.lb = [];
else
if isnumeric(lb)
this.lb = lb(:);
else
error([this.name,' lb (lower bound) is required to be of type numeric'])
end
end
if nargin < 8 || isempty(ub)
this.ub = [];
else
if isnumeric(ub)
this.ub = ub(:);
else
error([this.name,' ub (upper bound) is required to be of type numeric'])
end
end
if nargin < 9 || isempty(nonlcon)
this.nonlcon = [];
elseif ~isempty(nonlcon)
if ~isa(nonlcon,'function_handle')
error([this.name,' nonlcon is required to be a function handle'])
else
this.nonlcon = nonlcon;
end
end
% check that all sizes match
if ~isempty(this.x0)
this.nDV = numel(this.x0);
end
if ~isempty(this.lb) && ~isempty(this.ub)
if numel(this.lb) ~= numel(this.ub)
error([this.name,' lb and ub not equal dimensions'])
end
end
if ~isempty(this.lb) && ~isempty(this.nDV)
if numel(this.lb) ~= this.nDV
error([this.name,' x0 and lb not equal dimensions'])
end
end
if ~isempty(this.ub) && ~isempty(this.nDV)
if numel(this.ub) ~= this.nDV
error([this.name,' x0 and ub not equal dimensions'])
end
end
if ~isempty(this.A) && ~isempty(this.nDV)
if size(this.A,2) ~= this.nDV
error([this.name,' Columns of A(',num2str(size(this.A,2)),') does not match number of design variables(',num2str(this.nDV),')'])
end
elseif ~isempty(this.A) && isempty(this.nDV)
this.nDV = size(this.A,2);
end
if ~isempty(this.Aeq) && ~isempty(this.nDV)
if size(this.Aeq,2) ~= this.nDV
error([this.name,' Columns of Aeq(',num2str(size(this.A,2)),') does not match number of design variables(',num2str(this.nDV),')'])
end
elseif ~isempty(this.Aeq) && isempty(this.nDV)
this.nDV = size(this.Aeq,2);
end
% initialize options structure
this.options = fmindescent.setOptions(varargin);
% We made it this far
this.initialized = true;
end
% Main function
function [x,fval,exitflag,output] = solve(this)
% Assume the code failed
exitflag = -1;
if strcmpi(this.options.Display,'iter')
fprintf('*********************************************************************************************************')
fprintf('\n \t \t \t \t \t \t fmindescent optimizer')
fprintf('\n*********************************************************************************************************\n')
fprintf('\t %10s \t\t %10s \t \t %10s \t \t %10s \t \t %10s \n','f(x)','Max inf', 'Norm dx', 'nFeval','IterNo');
end
% Allocate iteration history array
% Store function values, maximum infeasibility from non-linear
% constraints, norm of design variable change
this.history.f = zeros(this.options.MaxIterations,1);
this.history.xnorm = zeros(this.options.MaxIterations,1);
if ~isempty(this.nonlcon)
this.history.maxInf = zeros(this.options.MaxIterations,1);
else
% Assumed allocated to empty when not active
this.history.maxInf = [];
end
% Ensure that we have a column vector (nDV,1)
x = this.x0(:);
% evaluate non-linear constraints
if ~isempty(this.nonlcon)
[gnl, gnleq] = this.nonlcon(x);
else
gnl = [];
gnleq = [];
end
% evaluate linear in-equality constraints
if isempty(this.A)
glin = [];
else
glin = this.A*x - this.b;
end
% evaluate linear equality constraints
if isempty(this.Aeq)
glineq = [];
else
glineq = this.Aeq*x - this.beq;
end
% Assemble all constraints as leq<=0 type
this.g = [gnl;gnleq;-gnleq;glin;glineq;-glineq;-x+this.lb;x-this.ub];
% Count total number of constraints
this.nG = size(this.g,1);
% Determine initial infeasibility
maxInf = max([this.g;0]);
if strcmpi(this.options.ConstraintMethod,'AL')
% Initialize lagrange multipliers to zeros
this.lambda = zeros(this.nG,1);
% Update lagrange multipliers based on the initial infeasibilities
this.updateLambda();
end
% evaluate objective function at initial point
this.f0 = this.fun(x);
% Get scaling factor for merit function
this.aFac = max([abs(this.f0),1]);
% Evaluate merit function
fmerit = this.getMeritObj(x,false);
% Store initial objective function value
fOld = fmerit;
% Create empty variable for "old" gradients
dfmerit = [];
dc = [];
alpha = [];
% Set counters and switches
nFeval = 1;
iterNo = 0;
optimize = true;
if strcmpi(this.options.Display,'iter')
fprintf('\t %6.4e \t \t %6.4e \t \t %6.4e \t \t %10i \t \t %10i \n' ,this.f0, maxInf, 0, nFeval ,iterNo);
end
% Main loop
while optimize
% update iteration counter
iterNo = iterNo + 1;
if strcmpi(this.options.ConstraintMethod,'AL')
% update lagrange multipliers
this.updateLambda()
end
% store previous gradients
dfmeritOld = dfmerit;
% evaluate gradients
[~,dfmerit,~,~,dg] = this.getMeritObj(x,true);
% Get decent direction based on user defined algorithm
[dc,ndc] = this.getDecentDirection(dfmerit,dfmeritOld,dc,alpha,dg);
% call linear search method
if ndc > 0
[xNew,alpha,fmerit,fval,nF,exitflag] = this.lineSearch(dc,x);
else
xNew = x;
alpha = 0;
fmerit = fOld;
nF = 0;
end
% Update function evaluation counter
nFeval = nFeval + nF;
maxInf = max([this.g;0]);
optimalityNorm = sqrt((fOld-fmerit)^2);
% check for convergence
if ( (optimalityNorm <= this.options.OptimalityTolerance) || (alpha <=this.options.StepTolerance)) || (iterNo >= this.options.MaxIterations) || (nFeval >= this.options.MaxFunctionEvaluations)
optimize = false;
exitflag = 1;
end
% Update design variables
x = xNew;
% Update "old" design
fOld = fmerit;
% Store iteration history
this.history.f(iterNo) = fval;
this.history.xnorm(iterNo) = alpha;
this.history.maxInf(iterNo) = maxInf;
this.history.nIter = iterNo;
this.history.nFeval = nFeval;
if strcmpi(this.options.Display,'iter')
fprintf('\t %6.4e \t \t %6.4e \t \t %6.4e \t \t %10i \t \t %10i \n' ,fval, maxInf, alpha, nFeval ,iterNo);
end
end % Main loop
this.history.f(iterNo+1:end)=[];
this.history.xnorm(iterNo+1:end)=[];
this.history.maxInf(iterNo+1:end)=[];
output.history = this.history;
if strcmpi(this.options.ConstraintMethod,'AL')
output.lambda = this.lambda./this.aFac;
end
end % Solve function
function postprocess(this)
% Save current "default" window style
defaultWindowStyle=get(0,'DefaultFigureWindowStyle');
% Set new window style to docked
set(0,'DefaultFigureWindowStyle','docked')
% Make iteration vector
ivec = 1:this.history.nIter;
f1=figure();
plot(ivec,this.history.f)
title('Objective')
xlabel('Iteration Number')
ylabel('Objective value')
figure();
plot(ivec,this.history.xnorm)
title('Design change norm')
xlabel('Iteration Number')
yl=ylabel('Norm dx');
set(yl,'Interpreter','none')
figure();
plot(ivec,this.history.maxInf)
title('Maximum infeasibility')
xlabel('Iteration Number')
ylabel('-')
% Jump back to figure 1
figure(f1)
% Restore default window style
set(0,'DefaultFigureWindowStyle',defaultWindowStyle)
end
end % methods
methods (Hidden = true)
function [fmerit,dfmerit,fval,df,dgActive] = getMeritObj(this,x,doDSA)
fmerit = [];
fval = [];
dfmerit = [];
df = [];
dgActive = [];
if nargin < 3 || isempty(doDSA)
doDSA = false;
end
if ~doDSA
fval = this.fun(x);
% evaluate non-linear constraints
if ~isempty(this.nonlcon)
[gnl, gnleq] = this.nonlcon(x);
else
gnl = [];
gnleq = [];
end
% evaluate linear in-equality constraints
if isempty(this.A)
glin = [];
else
glin = this.A*x - this.b;
end
% evaluate linear equality constraints
if isempty(this.Aeq)
glineq = [];
else
glineq = this.Aeq*x - this.beq ;
end
% Assemble all constraints as leq<=0 type
this.g = [gnl;gnleq;-gnleq;glin;glineq;-glineq;-x+this.lb;x-this.ub];
% determine infeasibility
y = max(this.g ,0);
switch this.options.ConstraintMethod
case 'Merit'
fmerit = fval + this.aFac*sum(y*this.options.InfeasibilityPenalization+0.5*y.^2);
case 'AL'
fmerit = fval + this.aFac*sum(y.*this.lambda+this.options.InfeasibilityPenalization*0.5*y.^2);
end
else
[~,df] = this.fun(x);
if ~isempty(this.nonlcon)
[~,~,dgnl,dgneq] = this.nonlcon(x);
else
dgnl = [];
dgneq = [];
end
dgreal = [dgnl';dgneq';-dgneq'; this.A; this.Aeq;-this.Aeq; -eye(this.nDV); eye(this.nDV)];
% Apply active set strategy
Active = this.g>=0;
dgActive = dgreal(Active,:);
switch this.options.ConstraintMethod
case 'Merit'
% Sum sensitivites from all constraints together for each design variable
temp = sum(dgreal(Active),1)';
dg = this.aFac.*(this.options.InfeasibilityPenalization+temp);
case 'AL'
dg = sum(this.aFac.*(this.lambda(Active)+this.options.InfeasibilityPenalization.*dgreal(Active,:)),1)';
end
% Add sensitivites from objective and constraints
dfmerit = df+dg;
end
end
function updateLambda(this)
this.lambda = max(this.lambda + this.options.InfeasibilityPenalization*this.g,0);
end
function [dc,ndc] = getDecentDirection(this,df,dfm1,dcm1,alpha,dg)
switch this.options.Algorithm
case 'CG'
% Conjugate gradient update: Fletcher-Reeves Method
if ~isempty(dcm1)
beta = df'*df/(dfm1'*dfm1); % from second iteration, use CG
dc = -df + beta*dcm1;
else
dc = -df; % first iteration, use stepest decent
end
case 'DFP'
% DFP inverse hessian update (Quasi Newton)
if ~isempty(dcm1)
s = alpha.*dcm1;
y = df-dfm1;
z = this.Hinv*y;
C = -z*z'/(dot(y,z));
B = s*s'/(dot(s,y));
if this.iH >= this.options.HessianRest
this.Hinv = eye(this.nDV);
this.iH = 0;
end
this.iH = this.iH + 1;
this.Hinv = this.Hinv + B + C;
dc = -this.Hinv*df;
else
this.Hinv = eye(this.nDV);
dc = -df;
end
case 'BFGS'
% BFGS hessian update (Quasi Newton)
if ~isempty(dcm1)
if this.iH >= this.options.HessianRest
this.H = eye(this.nDV);
this.iH = 0;
end
s = alpha.*dcm1;
y = df-dfm1;
sHs = s'*this.H*s;
sy = s'*y;
if sy >=0.2*sHs
theta = 1;
r = y;
else
theta = 0.8*sHs/(sHs-sy);
r = theta*y+(1-theta)*this.H*s;
end
this.iH = this.iH + 1;
this.H = this.H - this.H*s*s'*this.H/(sHs)+r*r'/(s'*r);
dc = -this.H/df';
else
this.H = eye(this.nDV);
dc = -df;
end
case 'MFD'
% Get number of active constraints
ng = size(dg,1);
if ng > 0
% Push off parameter
theta = 0.8;
% Define variable type
ndv = numel(df)+1;
vartype = char(ndv,1);
vartype(1:ndv) = 'C';
% Define upper and lower bounds
mfdlb = -ones(ndv,1);
mfdlb(end) = 0; % beta variable
mfdub = ones(ndv,1);
mfdub(end) = 1000; % beta variable
% Specify obj gradient for MFD problem
dBeta = zeros(ndv,1);
dBeta(end) = 1; % beta variable
% Define constraints
mfdA = zeros(ng+1,ndv);
mfdA(1,1:end-1) = df;
mfdA(2:end,1:end-1) = dg;
mfdA(1,end) = -1; % beta variable
mfdA(2:end,end) = -1*theta; % beta variable
mfdB = zeros(ng+1,1);
nleq = ng+1;
ctype = char(nleq,1);
ctype(1:nleq) = 'U';
[temp, ~, exitflag] = glpk (dBeta, mfdA, mfdB, mfdlb, mfdub, ctype, vartype);
dc = temp(1:end-1);
else
dc = -df;
end
otherwise
dc = -df;
end
ndc = norm(dc);
if ndc > 0
dc = dc./norm(dc); % Normalize
end
end
function [xNew, alpha,fmerit,fval,nF,exitflag] = lineSearch(this,df,x)
switch this.options.LineSearch
case 'golden'
[xNew, alpha,fmerit,fval,nF,exitflag] = this.goldenSectionSearch(df,x);
end
end
function [xNew,alpha,fmerit,fval,nF,exitflag] = goldenSectionSearch(this,dc,x)
% Define "golden" constants
phi = (sqrt(5)+1)/2;
invPhi = 1/phi;
invPhi2 = 1/phi^2;
% Set function evaluation counter
nF = 0;
% Initial step length along decent direction
delta = 0.001;
% Lower limit on alpha (step lenght)
alphaL = 0;
% Initial bracketing
yl = this.getMeritObj(x);
alphaU = 0; % Initialize upper limit on step length
alphaUm1 = 0; % Initialize upper limit minus 1
for ii = 0:this.options.MaxFunctionEvaluations-1
alphaUm2 = alphaUm1;
alphaUm1 = alphaU;
alphaU = alphaU + delta * phi^ii;
xu = x + dc*alphaU;
nF = nF + 1;
yu = this.getMeritObj(xu);
if yu > yl
alphaL=alphaUm2;
break
end
yl = yu;
end
% Interval reduction
h = alphaU-alphaL;
if h <= this.options.StepTolerance
alpha = (alphaL+alphaU)/2;
xNew = x + dc*alpha;
nF = nF + 1;
[fmerit,~,fval] = this.getMeritObj(xNew);
exitflag = 1;
return
end
% required steps to reach tolerance
n = ceil(log(this.options.StepTolerance/h)/log(invPhi));
% Define inner interval
c = alphaL + invPhi2*h;
d = alphaL + invPhi*h;
% Evaluate point c
xc = x + dc*c;
nF = nF + 1;
yc = this.getMeritObj(xc);
% Evaluate point d
xd = x + dc*d;
nF = nF + 1;
yd = this.getMeritObj(xd);
for ii = 1:n
if yc < yd
alphaU = d;
d = c;
yd = yc;
h = invPhi*h;
c = alphaL + invPhi2*h;
% Evaluate point c
xc = x + dc*c;
nF = nF + 1;
yc = this.getMeritObj(xc);
else
alphaL = c;
c = d;
yc = yd;
h = invPhi*h;
d = alphaL + invPhi*h;
% Evaluate point d
xd = x + dc*d;
nF = nF + 1;
yd = this.getMeritObj(xd);
end
end
if yc < yd
c = alphaL;
alpha = (c+d)/2;
else
d = alphaU;
alpha = (c+d)/2;
end
% Evaluate final point
xNew = x + dc*alpha;
nF = nF + 1;
[fmerit,~,fval] = this.getMeritObj(xNew);
exitflag = 1;
end
end
methods(Static = true, Hidden = true)
% options initialization
function options = setOptions(input)
% Here you can add new options if needed
p = inputParser;
p.CaseSensitive = false;
% Helper functions for input parser
checkEmpetyOrChar = @(x) (isempty(x) || ischar(x));
checkEmptyOrNumericPositive = @(x) (isempty(x) || (isnumeric(x) && all(x > 0)));
% Set parameters
p.addParameter('Algorithm','CG', @(x) checkEmpetyOrChar(x));
p.addParameter('ConstraintMethod','AL', @(x) checkEmpetyOrChar(x));
p.addParameter('LineSearch','golden', @(x) checkEmpetyOrChar(x));
p.addParameter('Display','off', @(x) checkEmpetyOrChar(x));
p.addParameter('MaxFunctionEvaluations',1000, @(x) checkEmptyOrNumericPositive(x));
p.addParameter('MaxIterations',1000, @(x) checkEmptyOrNumericPositive(x));
p.addParameter('InfeasibilityPenalization',100, @(x) checkEmptyOrNumericPositive(x));
p.addParameter('OptimalityTolerance',1e-5, @(x) checkEmptyOrNumericPositive(x));
p.addParameter('StepTolerance',1e-5, @(x) checkEmptyOrNumericPositive(x));
p.addParameter('HessianRest',15, @(x) checkEmptyOrNumericPositive(x));
% pars input
if nargin < 1 || isempty(input)
parse(p);
else
parse(p,input{:});
end
% Output results to options structure
options = p.Results;
end
end
end