optimize using particle swarm -凯发k8网页登录
this example shows how to optimize using the particleswarm
solver.
the objective function in this example is de jong’s fifth function, which is available when you run this example.
dejong5fcn
this function has 25 local minima.
try to find the minimum of the function using the default particleswarm
settings.
fun = @dejong5fcn; nvars = 2; rng default % for reproducibility [x,fval,exitflag] = particleswarm(fun,nvars)
optimization ended: relative change in the objective value over the last options.maxstalliterations iterations is less than options.functiontolerance.
x = 1×2
-31.9521 -16.0176
fval = 5.9288
exitflag = 1
is the solution x
the global optimum? it is unclear at this point. looking at the function plot shows that the function has local minima for components in the range [-50,50]
. so restricting the range of the variables to [-50,50]
helps the solver locate a global minimum.
lb = [-50;-50]; ub = -lb; [x,fval,exitflag] = particleswarm(fun,nvars,lb,ub)
optimization ended: relative change in the objective value over the last options.maxstalliterations iterations is less than options.functiontolerance.
x = 1×2
-16.0079 -31.9697
fval = 1.9920
exitflag = 1
this looks promising: the new solution has lower fval
than the previous one. but is x
truly a global solution? try minimizing again with more particles, to better search the region.
options = optimoptions('particleswarm','swarmsize',100); [x,fval,exitflag] = particleswarm(fun,nvars,lb,ub,options)
optimization ended: relative change in the objective value over the last options.maxstalliterations iterations is less than options.functiontolerance.
x = 1×2
-31.9781 -31.9784
fval = 0.9980
exitflag = 1
this looks even more promising. but is this answer a global solution, and how accurate is it? rerun the solver with a hybrid function. particleswarm
calls the hybrid function after particleswarm
finishes its iterations.
options.hybridfcn = @fmincon; [x,fval,exitflag] = particleswarm(fun,nvars,lb,ub,options)
optimization ended: relative change in the objective value over the last options.maxstalliterations iterations is less than options.functiontolerance.
x = 1×2
-31.9783 -31.9784
fval = 0.9980
exitflag = 1
particleswarm
found essentially the same solution as before. this gives you some confidence that particleswarm
reports a local minimum and that the final x
is the global solution.