solver behavior with a nonsmooth problem -凯发k8网页登录
this example shows the importance of choosing an appropriate solver for optimization problems. it also shows that a single point of non-smoothness can cause problems for optimization toolbox™ solvers.
in general, the solver decision tables provide guidance on which solver is likely to work best for your problem. for smooth problems, see optimization decision table. for nonsmooth problems, see first, and for more information consult global optimization toolbox solver characteristics.
a function with a single nonsmooth point
the function is nonsmooth at the point 0, which is the minimizing point. here is a 2-d plot using the matrix norm for the 4-d point .
figure x = linspace(-5,5,51); [xx,yy] = meshgrid(x); zz = zeros(size(xx)); for ii = 1:length(x) for jj = 1:length(x) zz(ii,jj) = sqrt(norm([xx(ii,jj),yy(ii,jj);0,0])); end end surf(xx,yy,zz) xlabel('x(1)') ylabel('x(2)') title('norm([x(1),x(2);0,0])^{1/2}')
this example uses matrix norm for a 2-by-6 matrix x
. the matrix norm relates to the singular value decomposition, which is not as smooth as the euclidean norm. see .
minimize using patternsearch
patternsearch
is the recommended first solver to try for nonsmooth problems. see . start patternsearch
from a nonzero 2-by-6 matrix x0
, and attempt to locate the minimum of . for this attempt, and all others, use the default solver options.
return the solution, which should be near zero, the objective function value, which should likewise be near zero, and the number of function evaluations taken.
fun = @(x)norm([x(1:6);x(7:12)])^(1/2);
x0 = [1:6;7:12];
rng default
x0 = x0 rand(size(x0))
x0 = 2×6
1.8147 2.1270 3.6324 4.2785 5.9575 6.1576
7.9058 8.9134 9.0975 10.5469 11.9649 12.9706
[xps,fvalps,eflagps,outputps] = patternsearch(fun,x0);
optimization terminated: mesh size less than options.meshtolerance.
xps,fvalps,eflagps,outputps.funccount
xps = 2×6
10-4 ×
0.1116 -0.1209 0.3503 -0.0520 -0.1270 0.2031
-0.3082 -0.1526 0.0623 0.0652 0.4479 0.1173
fvalps = 0.0073
eflagps = 1
ans = 10780
patternsearch
reaches a good solution, as evinced by exit flag 1. however, it takes over 10,000 function evaluations to converge.
minimize using fminsearch
the documentation states that fminsearch
sometimes can handle discontinuities, so this is a reasonable option.
[xfms,fvalfms,eflagfms,outputfms] = fminsearch(fun,x0);
exiting: maximum number of function evaluations has been exceeded - increase maxfunevals option. current function value: 3.197063
xfms,fvalfms,eflagfms,outputfms.funccount
xfms = 2×6
2.2640 1.1747 9.0693 8.1652 1.7367 -1.2958
3.7456 1.2694 0.2714 -3.7942 3.8714 1.9290
fvalfms = 3.1971
eflagfms = 0
ans = 2401
using default options, fminsearch
runs out of function evaluations before it converges to a solution. exit flag 0 indicates this lack of convergence. the reported solution is poor.
use particleswarm
particleswarm
is recommended as the next solver to try. see choosing between solvers for nonsmooth problems.
[xpsw,fvalpsw,eflagpsw,outputpsw] = particleswarm(fun,12);
optimization ended: relative change in the objective value over the last options.maxstalliterations iterations is less than options.functiontolerance.
xpsw,fvalpsw,eflagpsw,outputpsw.funccount
xpsw = 1×12
10-12 ×
-0.0386 -0.1282 -0.0560 0.0904 0.0771 -0.0541 -0.1189 0.1290 -0.0032 0.0165 0.0728 -0.0026
fvalpsw = 4.5222e-07
eflagpsw = 1
ans = 37200
particleswarm
finds an even more accurate solution than patternsearch
, but takes over 35,000 function evaluations. exit flag 1 indicates that the solution is good.
use ga
ga
is a popular solver, but is not recommended as the first solver to try. see how well it works on this problem.
[xga,fvalga,eflagga,outputga] = ga(fun,12);
optimization terminated: average change in the fitness value less than options.functiontolerance.
xga,fvalga,eflagga,outputga.funccount
xga = 1×12
-0.0061 -0.0904 0.0816 -0.0484 0.0799 -0.1925 0.0048 0.3581 0.0848 0.0232 0.0237 -0.1294
fvalga = 0.6257
eflagga = 1
ans = 65190
ga
does not find as good a solution as patternsearch
or particleswarm
, and takes about twice as many function evaluations as particleswarm
. exit flag 1 is misleading in this case.
use fminunc
from optimization toolbox
fminunc
is not recommended for nonsmooth functions. see how it performs on this one.
[xfmu,fvalfmu,eflagfmu,outputfmu] = fminunc(fun,x0);
local minimum possible. fminunc stopped because the size of the current step is less than the value of the step size tolerance.
xfmu,fvalfmu,eflagfmu,outputfmu.funccount
xfmu = 2×6
-0.5844 -0.9726 -0.4356 0.1467 0.3263 -0.1002
-0.0769 -0.1092 -0.3429 -0.6856 -0.7609 -0.6524
fvalfmu = 1.1269
eflagfmu = 2
ans = 546
the fminunc
solution is not as good as the ga
solution. however, fminunc
reaches the rather poor solution in relatively few function evaluations. exit flag 2 means you should take care, the first-order optimality conditions are not met at the reported solution.
use fmincon
from optimization toolbox
fmincon
can sometimes minimize nonsmooth functions. see how it performs on this one.
[xfmc,fvalfmc,eflagfmc,outputfmc] = fmincon(fun,x0);
local minimum possible. constraints satisfied. fmincon stopped because the size of the current step is less than the value of the step size tolerance and constraints are satisfied to within the value of the constraint tolerance.
xfmc,fvalfmc,eflagfmc,outputfmc.funccount
xfmc = 2×6
10-9 ×
0.0241 0.0613 -0.1337 -0.0030 0.1065 0.0378
-0.1376 0.0497 0.1443 -0.1176 0.0373 0.1430
fvalfmc = 1.7662e-05
eflagfmc = 2
ans = 881
fmincon
with default options produces an accurate solution after fewer than 1000 function evaluations. exit flag 2 does not mean that the solution is inaccurate, but that the first-order optimality conditions are not met. this is because the gradient of the objective function is not zero at the solution.
summary of results
choosing the appropriate solver leads to better, faster results. this summary shows how disparate the results can be. the solution quality is 'poor'
if the objective function value is greater than 0.1, 'good'
if the value is smaller than 0.01, and 'mediocre'
otherwise.
solver = {'patternsearch';'fminsearch';'particleswarm';'ga';'fminunc';'fmincon'}; solutionquality = {'good';'poor';'good';'poor';'poor';'good'}; fval = [fvalps,fvalfms,fvalpsw,fvalga,fvalfmu,fvalfmc]'; numeval = [outputps.funccount,outputfms.funccount,outputpsw.funccount,... outputga.funccount,outputfmu.funccount,outputfmc.funccount]'; results = table(solver,solutionquality,fval,numeval)
results=6×4 table
solver solutionquality fval numeval
_________________ _______________ __________ _______
{'patternsearch'} {'good'} 0.0072656 10780
{'fminsearch' } {'poor'} 3.1971 2401
{'particleswarm'} {'good'} 4.5222e-07 37200
{'ga' } {'poor'} 0.62572 65190
{'fminunc' } {'poor'} 1.1269 546
{'fmincon' } {'good'} 1.7662e-05 881
another view of the results.
figure hold on for ii = 1:length(fval) clr = rand(1,3); plot(numeval(ii),fval(ii),'o','markersize',10,'markeredgecolor',clr,'markerfacecolor',clr) text(numeval(ii),fval(ii) 0.2,solver{ii},'color',clr); end ylabel('fval') xlabel('numeval') title('reported minimum and evaluations by solver') hold off
while particleswarm
achieves the lowest objective function value, it does so by taking over three times as many function evaluations as patternsearch
, and over 30 times as many as fmincon
.
fmincon
is not generally recommended for nonsmooth problems. it is effective in this case, but this case has just one nonsmooth point.