maximizing monochromatic polarized light interference patterns using globalsearch and multistart -凯发k8网页登录
this example shows how to use the functions globalsearch
and multistart
.
introduction
this example shows how global optimization toolbox functions, particularly globalsearch
and multistart
, can help locate the maximum of an electromagnetic interference pattern. for simplicity of modeling, the pattern arises from monochromatic polarized light spreading out from point sources.
the electric field due to source i measured in the direction of polarization at point x and time t is
where is the phase at time zero for source , is the speed of light, is the frequency of the light, is the amplitude of source , and is the distance from source to .
for a fixed point the intensity of the light is the time average of the square of the net electric field. the net electric field is sum of the electric fields due to all sources. the time average depends only on the sizes and relative phases of the electric fields at . to calculate the net electric field, add up the individual contributions using the phasor method. for phasors, each source contributes a vector. the length of the vector is the amplitude divided by distance from the source, and the angle of the vector, is the phase at the point.
for this example, we define three point sources with the same frequency () and amplitude (), but varied initial phase (). we arrange these sources on a fixed plane.
% frequency is proportional to the number of peaks relfreqconst = 2*pi*2.5; amp = 2.2; phase = -[0; 0.54; 2.07]; numsources = 3; height = 3; % all point sources are aligned at [x_i,y_i,z] xcoords = [2.4112 0.2064 1.6787]; ycoords = [0.3957 0.3927 0.9877]; zcoords = height*ones(numsources,1); origins = [xcoords ycoords zcoords];
visualize the interference pattern
now let's visualize a slice of the interference pattern on the plane z = 0.
as you can see from the plot below, there are many peaks and valleys indicating constructive and destructive interference.
% pass additional parameters via an anonymous function: waveintensity_x = @(x) waveintensity(x,amp,phase, ... relfreqconst,numsources,origins); % generate the grid [x,y] = meshgrid(-4:0.035:4,-4:0.035:4); % compute the intensity over the grid z = arrayfun(@(x,y) waveintensity_x([x y]),x,y); % plot the surface and the contours figure surf(x,y,z,'edgecolor','none') xlabel('x') ylabel('y') zlabel('intensity')
posing the optimization problem
we are interested in the location where this wave intensity reaches its highest peak.
the wave intensity () falls off as we move away from the source proportional to . therefore, let's restrict the space of viable solutions by adding constraints to the problem.
if we limit the exposure of the sources with an aperture, then we can expect the maximum to lie in the intersection of the projection of the apertures onto our observation plane. we model the effect of an aperture by restricting the search to a circular region centered at each source.
we also restrict the solution space by adding bounds to the problem. although these bounds may be redundant (given the nonlinear constraints), they are useful since they restrict the range in which start points are generated (see for more information).
now our problem has become:
subject to
where and are the coordinates and aperture radius of the point source, respectively. each source is given an aperture with radius 3. the given bounds encompass the feasible region.
the objective () and nonlinear constraint functions are defined in separate matlab® files, waveintensity.m
and apertureconstraint.m
, respectively, which are listed at the end of this example.
visualization with constraints
now let's visualize the contours of our interference pattern with the nonlinear constraint boundaries superimposed. the feasible region is the interior of the intersection of the three circles (yellow, green, and blue). the bounds on the variables are indicated by the dashed-line box.
% visualize the contours of our interference surface domain = [-3 5.5 -4 5]; figure; ezcontour(@(x,y) arrayfun(@(x,y) waveintensity_x([x y]),x,y),domain,150); hold on % plot constraints g1 = @(x,y) (x-xcoords(1)).^2 (y-ycoords(1)).^2 - 9; g2 = @(x,y) (x-xcoords(2)).^2 (y-ycoords(2)).^2 - 9; g3 = @(x,y) (x-xcoords(3)).^2 (y-ycoords(3)).^2 - 9; h1 = ezplot(g1,domain); h1.color = [0.8 0.7 0.1]; % yellow h1.linewidth = 1.5; h2 = ezplot(g2,domain); h2.color = [0.3 0.7 0.5]; % green h2.linewidth = 1.5; h3 = ezplot(g3,domain); h3.color = [0.4 0.4 0.6]; % blue h3.linewidth = 1.5; % plot bounds lb = [-0.5 -2]; ub = [3.5 3]; line([lb(1) lb(1)],[lb(2) ub(2)],'linestyle','--') line([ub(1) ub(1)],[lb(2) ub(2)],'linestyle','--') line([lb(1) ub(1)],[lb(2) lb(2)],'linestyle','--') line([lb(1) ub(1)],[ub(2) ub(2)],'linestyle','--') title('pattern contours with constraint boundaries')
setting up and solving the problem with a local solver
given the nonlinear constraints, we need a constrained nonlinear solver, namely, fmincon
.
let's set up a problem structure describing our optimization problem. we want to maximize the intensity function, so we negate the values returned form waveintensity
. let's choose an arbitrary start point that happens to be near the feasible region.
for this small problem, we'll use fmincon
's sqp algorithm.
% pass additional parameters via an anonymous function: apertureconstraint_x = @(x) apertureconstraint(x,xcoords,ycoords); % set up fmincon's options x0 = [3 -1]; opts = optimoptions('fmincon','algorithm','sqp'); problem = createoptimproblem('fmincon','objective', ... @(x) -waveintensity_x(x),'x0',x0,'lb',lb,'ub',ub, ... 'nonlcon',apertureconstraint_x,'options',opts); % call fmincon [xlocal,fvallocal] = fmincon(problem)
local minimum found that satisfies the constraints. optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance. xlocal = -0.5000 0.4945 fvallocal = -1.4438
now, let's see how we did by showing the result of fmincon
in our contour plot. notice that fmincon
did not reach the global maximum, which is also annotated on the plot. note that we'll only plot the bound that was active at the solution.
[~,maxidx] = max(z(:)); xmax = [x(maxidx),y(maxidx)] figure contour(x,y,z) hold on % show bounds line([lb(1) lb(1)],[lb(2) ub(2)],'linestyle','--') % create textarrow showing the location of xlocal annotation('textarrow',[0.25 0.21],[0.86 0.60],'textedgecolor',[0 0 0],... 'textbackgroundcolor',[1 1 1],'fontsize',11,'string',{'single run result'}); % create textarrow showing the location of xglobal annotation('textarrow',[0.44 0.50],[0.63 0.58],'textedgecolor',[0 0 0],... 'textbackgroundcolor',[1 1 1],'fontsize',12,'string',{'global max'}); axis([-1 3.75 -3 3])
xmax = 1.2500 0.4450
using globalsearch
and multistart
given an arbitrary initial guess, fmincon
gets stuck at a nearby local maximum. global optimization toolbox solvers, particularly globalsearch
and multistart
, give us a better chance at finding the global maximum since they will try fmincon
from multiple generated initial points (or our own custom points, if we choose).
our problem has already been set up in the problem
structure, so now we construct our solver objects and run them. the first output from run
is the location of the best result found.
% construct a globalsearch object gs = globalsearch; % construct a multistart object based on our globalsearch attributes ms = multistart; rng(4,'twister') % for reproducibility % run globalsearch tic; [xgs,~,~,~,solsgs] = run(gs,problem); toc xgs % run multistart with 15 randomly generated points tic; [xms,~,~,~,solsms] = run(ms,problem,15); toc xms
globalsearch stopped because it analyzed all the trial points. all 14 local solver runs converged with a positive local solver exit flag. elapsed time is 0.229525 seconds. xgs = 1.2592 0.4284 multistart completed the runs from all start points. all 15 local solver runs converged with a positive local solver exit flag. elapsed time is 0.109984 seconds. xms = 1.2592 0.4284
examining results
let's examine the results that both solvers have returned. an important thing to note is that the results will vary based on the random start points created for each solver. another run through this example may give different results. the coordinates of the best results xgs
and xms
printed to the command line. we'll show unique results returned by globalsearch
and multistart
and highlight the best results from each solver, in terms of proximity to the global solution.
the fifth output of each solver is a vector containing distinct minima (or maxima, in this case) found. we'll plot the (x,y) pairs of the results, solsgs
and solsms
, against our contour plot we used before.
% plot globalsearch results using the '*' marker xgs = cell2mat({solsgs(:).x}'); scatter(xgs(:,1),xgs(:,2),'*','markeredgecolor',[0 0 1],'linewidth',1.25) % plot multistart results using a circle marker xms = cell2mat({solsms(:).x}'); scatter(xms(:,1),xms(:,2),'o','markeredgecolor',[0 0 0],'linewidth',1.25) legend('intensity','bound','globalsearch','multistart','location','best') title('globalsearch and multistart results')
relaxing the bounds
with the tight bounds on the problem, both globalsearch
and multistart
were able to locate the global maximum in this run.
finding tight bounds can be difficult to do in practice, when not much is known about the objective function or constraints. in general though, we may be able to guess a reasonable region in which we would like to restrict the set of start points. for illustration purposes, let's relax our bounds to define a larger area in which to generate start points and re-try the solvers.
% relax the bounds to spread out the start points problem.lb = -5*ones(2,1); problem.ub = 5*ones(2,1); % run globalsearch tic; [xgs,~,~,~,solsgs] = run(gs,problem); toc xgs % run multistart with 15 randomly generated points tic; [xms,~,~,~,solsms] = run(ms,problem,15); toc xms
globalsearch stopped because it analyzed all the trial points. all 4 local solver runs converged with a positive local solver exit flag. elapsed time is 0.173760 seconds. xgs = 0.6571 -0.2096 multistart completed the runs from all start points. all 15 local solver runs converged with a positive local solver exit flag. elapsed time is 0.134150 seconds. xms = 2.4947 -0.1439
% show the contours figure contour(x,y,z) hold on % create textarrow showing the location of xglobal annotation('textarrow',[0.44 0.50],[0.63 0.58],'textedgecolor',[0 0 0],... 'textbackgroundcolor',[1 1 1],'fontsize',12,'string',{'global max'}); axis([-1 3.75 -3 3]) % plot globalsearch results using the '*' marker xgs = cell2mat({solsgs(:).x}'); scatter(xgs(:,1),xgs(:,2),'*','markeredgecolor',[0 0 1],'linewidth',1.25) % plot multistart results using a circle marker xms = cell2mat({solsms(:).x}'); scatter(xms(:,1),xms(:,2),'o','markeredgecolor',[0 0 0],'linewidth',1.25) % highlight the best results from each: % globalsearch result in red, multistart result in blue plot(xgs(1),xgs(2),'sb','markersize',12,'markerfacecolor',[1 0 0]) plot(xms(1),xms(2),'sb','markersize',12,'markerfacecolor',[0 0 1]) legend('intensity','globalsearch','multistart','best gs','best ms','location','best') title('globalsearch and multistart results with relaxed bounds')
the best result from globalsearch
is shown by the red square and the best result from multistart
is shown by the blue square.
tuning globalsearch
parameters
notice that in this run, given the larger area defined by the bounds, neither solver was able to identify the point of maximum intensity. we could try to overcome this in a couple of ways. first, we examine globalsearch
.
notice that globalsearch
only ran fmincon
a few times. to increase the chance of finding the global maximum, we would like to run more points. to restrict the start point set to the candidates most likely to find the global maximum, we'll instruct each solver to ignore start points that do not satisfy constraints by setting the startpointstorun
property to bounds-ineqs
. additionally, we will set the maxwaitcycle
and basinradiusfactor
properties so that globalsearch
will be able to identify the narrow peaks quickly. reducing maxwaitcycle
causes globalsearch
to decrease the basin of attraction radius by the basinradiusfactor
more often than with the default setting.
% increase the total candidate points, but filter out the infeasible ones gs = globalsearch(gs,'startpointstorun','bounds-ineqs', ... 'maxwaitcycle',3,'basinradiusfactor',0.3); % run globalsearch tic; xgs = run(gs,problem); toc xgs
globalsearch stopped because it analyzed all the trial points. all 10 local solver runs converged with a positive local solver exit flag. elapsed time is 0.242955 seconds. xgs = 1.2592 0.4284
utilizing multistart
's parallel capabilities
a brute force way to improve our chances of finding the global maximum is to simply try more start points. again, this may not be practical in all situations. in our case, we've only tried a small set so far and the run time was not terribly long. so, it's reasonable to try more start points. to speed the computation we'll run multistart
in parallel if parallel computing toolbox™ is available.
% set the useparallel property of multistart ms = multistart(ms,'useparallel',true); try demoopenedpool = false; % create a parallel pool if one does not already exist % (requires parallel computing toolbox) if max(size(gcp)) == 0 % if no pool parpool demoopenedpool = true; end catch me warning(message('globaloptim:globaloptimdemos:opticalinterferencedemo:nopct')); end % run the solver tic; xms = run(ms,problem,100); toc xms if demoopenedpool % make sure to delete the pool if one was created in this example delete(gcp) % delete the pool end
multistart completed the runs from all start points. all 100 local solver runs converged with a positive local solver exit flag. elapsed time is 0.956671 seconds. xms = 1.2592 0.4284
objective and nonlinear constraints
here we list the functions that define the optimization problem:
function p = waveintensity(x,amp,phase,relfreqconst,numsources,origins) % waveintensity intensity function for opticalinterferencedemo. % 凯发官网入口首页 copyright 2009 the mathworks, inc. d = distancefromsource(x,numsources,origins); ampvec = [sum(amp./d .* cos(phase - d*relfreqconst)); sum(amp./d .* sin(phase - d*relfreqconst))]; % intensity is ||ampvec||^2 p = ampvec'*ampvec;
function [c,ceq] = apertureconstraint(x,xcoords,ycoords) % apertureconstraint aperture constraint function for opticalinterferencedemo. % 凯发官网入口首页 copyright 2009 the mathworks, inc. ceq = []; c = (x(1) - xcoords).^2 (x(2) - ycoords).^2 - 9;
function d = distancefromsource(v,numsources,origins) % distancefromsource distance function for opticalinterferencedemo. % 凯发官网入口首页 copyright 2009 the mathworks, inc. d = zeros(numsources,1); for k = 1:numsources d(k) = norm(origins(k,:) - [v 0]); end
see also
|