optimize odes in parallel
this example shows how to optimize parameters of an ode.
it also shows how to avoid computing the objective and nonlinear constraint
function twice when the ode solution returns both. the example compares
patternsearch
and ga
in terms of time
to run the solver and the quality of the solutions.
you need a parallel computing toolbox™ license to use parallel computing.
step 1. define the problem.
the problem is to change the position and angle of a cannon to fire a projectile as far as possible beyond a wall. the cannon has a muzzle velocity of 300 m/s. the wall is 20 m high. if the cannon is too close to the wall, it has to fire at too steep an angle, and the projectile does not travel far enough. if the cannon is too far from the wall, the projectile does not travel far enough either.
air resistance slows the projectile. the resisting force is proportional to the square of the velocity, with proportionality constant 0.02. gravity acts on the projectile, accelerating it downward with constant 9.81 m/s2. therefore, the equations of motion for the trajectory x(t) are
where
the initial position x0
and initial velocity
xp0
are 2-d vectors. however, the initial height
x0(2)
is 0, so the initial position depends only on the
scalar x0(1)
. and the initial velocity xp0
has magnitude 300 (the muzzle velocity), so depends only on the initial angle, a
scalar. for an initial angle th
,
xp0
= 300*(cos(th),sin(th))
.
therefore, the optimization problem depends only on two scalars, so it is a 2-d
problem. use the horizontal distance and the angle as the decision
variables.
step 2. formulate the ode model.
ode solvers require you to formulate your model as a first-order system. augment the trajectory vector (x1(t),x2(t)) with its time derivative (x'1(t),x'2(t)) to form a 4-d trajectory vector. in terms of this augmented vector, the differential equation becomes
write the differential equation as a function file, and save it on your matlab® path.
function f = cannonfodder(t,x) f = [x(3);x(4);x(3);x(4)]; % initial, gets f(1) and f(2) correct nrm = norm(x(3:4)) * .02; % norm of the velocity times constant f(3) = -x(3)*nrm; % horizontal acceleration f(4) = -x(4)*nrm - 9.81; % vertical acceleration
visualize the solution of the ode starting 30 m from the wall at an angle of
pi/3
.
step 3. solve using patternsearch.
the problem is to find initial position x0(1)
and initial
angle x0(2)
to maximize the distance from the wall the
projectile lands. because this is a maximization problem, minimize the negative
of the distance (see ).
to use patternsearch
to solve this problem, you must
provide the objective, constraint, initial guess, and options.
these two files are the objective and constraint functions. copy them to a folder on your matlab path.
function f = cannonobjective(x) x0 = [x(1);0;300*cos(x(2));300*sin(x(2))]; sol = ode45(@cannonfodder,[0,15],x0); % find the time t when y_2(t) = 0 zerofnd = fzero(@(r)deval(sol,r,2),[sol.x(2),sol.x(end)]); % then find the x-position at that time f = deval(sol,zerofnd,1); f = -f; % take negative of distance for maximization function [c,ceq] = cannonconstraint(x) ceq = []; x0 = [x(1);0;300*cos(x(2));300*sin(x(2))]; sol = ode45(@cannonfodder,[0,15],x0); if sol.y(1,end) <= 0 % projectile never reaches wall c = 20 - sol.y(2,end); else % find when the projectile crosses x = 0 zerofnd = fzero(@(r)deval(sol,r,1),[sol.x(2),sol.x(end)]); % then find the height there, and subtract from 20 c = 20 - deval(sol,zerofnd,2); end
notice that the objective and constraint functions set their input variable
x0
to a 4-d initial point for the ode solver. the ode
solver does not stop if the projectile hits the wall. instead, the constraint
function simply becomes positive, indicating an infeasible initial value.
the initial position x0(1)
cannot be above 0, and it is
futile to have it be below –200. (it should be near –20 because, with no air
resistance, the longest trajectory would start at –20 at an angle
pi/4
.) similarly, the initial angle
x0(2)
cannot be below 0, and cannot be above
pi/2
. set bounds slightly away from these initial
values:
lb = [-200;0.05];
ub = [-1;pi/2-.05];
x0 = [-30,pi/3]; % initial guess
set the usecompletepoll
option to true
.
this gives a higher-quality solution, and enables direct comparison with
parallel processing, because computing in parallel requires this setting.
opts = optimoptions('patternsearch','usecompletepoll',true);
call patternsearch
to solve the problem.
tic % time the solution [xsolution,distance,eflag,outpt] = patternsearch(@cannonobjective,x0,... [],[],[],[],lb,ub,@cannonconstraint,opts) toc
optimization terminated: mesh size less than options.meshtolerance and constraint violation is less than options.constrainttolerance. xsolution = -28.8123 0.6095 distance = -125.9880 eflag = 1 outpt = struct with fields: function: @cannonobjective problemtype: 'nonlinearconstr' pollmethod: 'gpspositivebasis2n' maxconstraint: 0 searchmethod: [] iterations: 5 funccount: 269 meshsize: 8.9125e-07 rngstate: [1×1 struct] message: 'optimization terminated: mesh size less than options.meshtolerance↵ and constraint violation is less than options.constrainttolerance.' elapsed time is 0.792152 seconds.
starting the projectile about 29 m from the wall at an angle 0.6095 radian results in the farthest distance, about 126 m. the reported distance is negative because the objective function is the negative of the distance to the wall.
visualize the solution.
x0 = [xsolution(1);0;300*cos(xsolution(2));300*sin(xsolution(2))]; sol = ode45(@cannonfodder,[0,15],x0); % find the time when the projectile lands zerofnd = fzero(@(r)deval(sol,r,2),[sol.x(2),sol.x(end)]); t = linspace(0,zerofnd); % equal times for plot xs = deval(sol,t,1); % interpolated x values ys = deval(sol,t,2); % interpolated y values plot(xs,ys) hold on plot([0,0],[0,20],'k') % draw the wall xlabel('horizontal distance') ylabel('trajectory height') legend('trajectory','wall','location','nw') ylim([0 70]) hold off
step 4. avoid calling the expensive subroutine twice.
both the objective and nonlinear constraint function call the ode solver to
calculate their values. use the technique in to avoid
calling the solver twice. the runcannon
file implements this
technique. copy this file to a folder on your matlab path.
function [x,f,eflag,outpt] = runcannon(x0,opts) if nargin == 1 % no options supplied opts = []; end xlast = []; % last place ode solver was called sol = []; % ode solution structure fun = @objfun; % the objective function, nested below cfun = @constr; % the constraint function, nested below lb = [-200;0.05]; ub = [-1;pi/2-.05]; % call patternsearch [x,f,eflag,outpt] = patternsearch(fun,x0,[],[],[],[],lb,ub,cfun,opts); function y = objfun(x) if ~isequal(x,xlast) % check if computation is necessary x0 = [x(1);0;300*cos(x(2));300*sin(x(2))]; sol = ode45(@cannonfodder,[0,15],x0); xlast = x; end % now compute objective function % first find when the projectile hits the ground zerofnd = fzero(@(r)deval(sol,r,2),[sol.x(2),sol.x(end)]); % then compute the x-position at that time y = deval(sol,zerofnd,1); y = -y; % take negative of distance end function [c,ceq] = constr(x) ceq = []; if ~isequal(x,xlast) % check if computation is necessary x0 = [x(1);0;300*cos(x(2));300*sin(x(2))]; sol = ode45(@cannonfodder,[0,15],x0); xlast = x; end % now compute constraint functions % first find when the projectile crosses x = 0 zerofnd = fzero(@(r)deval(sol,r,1),[sol.x(1),sol.x(end)]); % then find the height there, and subtract from 20 c = 20 - deval(sol,zerofnd,2); end end
reinitialize the problem and time the call to
runcannon
.
x0 = [-30;pi/3]; tic [xsolution,distance,eflag,outpt] = runcannon(x0,opts); toc
optimization terminated: mesh size less than options.meshtolerance and constraint violation is less than options.constrainttolerance. elapsed time is 0.670715 seconds.
the solver ran faster than before. if you examine the solution, you see that the output is identical.
step 5. compute in parallel.
try to save more time by computing in parallel. begin by opening a parallel pool of workers.
parpool
starting parpool using the 'local' profile ... connected to the parallel pool (number of workers: 6). ans = processpool with properties: connected: true numworkers: 6 cluster: local attachedfiles: {} autoaddclientpath: true idletimeout: 30 minutes (30 minutes remaining) spmdenabled: true
set the options to use parallel computing, and rerun the solver.
opts = optimoptions('patternsearch',opts,'useparallel',true); x0 = [-30;pi/3]; tic [xsolution,distance,eflag,outpt] = runcannon(x0,opts); toc
optimization terminated: mesh size less than options.meshtolerance and constraint violation is less than options.constrainttolerance. elapsed time is 1.894515 seconds.
in this case, parallel computing was slower. if you examine the solution, you see that the output is identical.
step 6. compare with the genetic algorithm.
you can also try to solve the problem using the genetic algorithm. however, the genetic algorithm is usually slower and less reliable.
as explained in , you cannot save time when using
ga
by the nested function technique used by
patternsearch
in step 4. instead, call
ga
in parallel by setting the appropriate options.
options = optimoptions('ga','useparallel',true); rng default % for reproducibility tic % time the solution [xsolution,distance,eflag,outpt] = ga(@cannonobjective,2,... [],[],[],[],lb,ub,@cannonconstraint,options) toc
optimization terminated: average change in the fitness value less than options.functiontolerance and constraint violation is less than options.constrainttolerance. xsolution = -37.6217 0.4926 distance = -122.2181 eflag = 1 outpt = struct with fields: problemtype: 'nonlinearconstr' rngstate: [1×1 struct] generations: 4 funccount: 9874 message: 'optimization terminated: average change in the fitness value less than options.functiontolerance↵ and constraint violation is less than options.constrainttolerance.' maxconstraint: 0 hybridflag: [] elapsed time is 12.529131 seconds.
the ga
solution is not as good as the
patternsearch
solution: 122 m versus 126 m.
ga
takes more time: about 12 s versus under 2 s;
patternsearch
takes even less time in serial and
nested, less than 1 s. running ga
serially takes even longer,
about 30 s in one test run.