main content

surrogate optimization with nonlinear constraint -凯发k8网页登录

this example shows how to include nonlinear inequality constraints in a surrogate optimization. the example solves an ode with a nonlinear constraint. the example optimize odes in parallel shows how to solve the same problem using other solvers that accept nonlinear constraints.

for a video overview of this example, see .

problem description

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 fires 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.

nonlinear air resistance slows the projectile. the resisting force is proportional to the square of velocity, with the proportionality constant 0.02. gravity acts on the projectile, accelerating it downward with constant 9.81 m/s^2. therefore, the equations of motion for the trajectory x(t) are

d2x(t)dt2=-0.02v(t)v(t)-(0,9.81),

where v(t)=dx(t)/dt.

the initial position x0 and initial velocity xp0 are 2-d vectors. however, the initial height x0(2) is 0, so the initial position is given by the scalar x0(1). the initial velocity has magnitude 300 (the muzzle velocity) and, therefore, depends only on the initial angle, which is a scalar. for an initial angle th, the initial velocity is xp0 = 300*(cos(th),sin(th)). therefore, the optimization problem depends only on two scalars, making it a 2-d problem. use the horizontal distance and initial angle as the decision variables.

formulate 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 (x1(t),x2(t)) to form a 4-d trajectory vector. in terms of this augmented vector, the differential equation becomes

ddtx(t)=[x3(t)x4(t)-0.02(x3(t),x4(t))x3(t)-0.02(x3(t),x4(t))x4(t)-9.81].

the cannonshot file implements this differential equation.

type cannonshot
function f = cannonshot(~,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 this ode starting 30 m from the wall with an initial angle of pi/3. the plotcannonsolution function uses ode45 to solve the differential equation.

type plotcannonsolution
function dist = plotcannonsolution(x)
% change initial 2-d point x to 4-d x0
x0 = [x(1);0;300*cos(x(2));300*sin(x(2))];
sol = ode45(@cannonshot,[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')
ylim([0 100])
legend('trajectory','wall','location','nw')
dist = xs(end);
title(sprintf('distance %f',dist))
hold off

plotcannonsolution uses fzero to find the time when the projectile lands, meaning its height is 0. the projectile lands before time 15 s, so plotcannonsolution uses 15 as the amount of time for the ode solution.

x0 = [-30;pi/3];
dist = plotcannonsolution(x0);

figure contains an axes object. the axes object with title distance 76.750599, xlabel horizontal distance, ylabel trajectory height contains 2 objects of type line. these objects represent trajectory, wall.

prepare optimization

to optimize the initial position and angle, write a function similar to the previous plotting routine. calculate the trajectory starting from an arbitrary horizontal position and initial angle.

include sensible bound constraints. the horizontal position cannot be greater than 0. set an upper bound of –1. similarly, the horizontal position cannot be below –200, so set a lower bound of –200. the initial angle must be positive, so set its lower bound to 0.05. the initial angle should not exceed pi/2; set its upper bound to pi/2 – 0.05.

lb = [-200;0.05];
ub = [-1;pi/2-.05];

write an objective function that returns the negative of the resulting distance from the wall, given an initial position and angle. if the trajectory crosses the wall at a height less than 20, the trajectory is infeasible; this constraint is a nonlinear constraint. the cannonobjcon function implements the objective function calculation. to implement the nonlinear constraint, the function calls fzero to find the time when the x-value of the projectile is zero. the function accounts for the possibility of failure in the fzero function by checking whether, after time 15, the x-value of the projectile is greater than zero. if not, then the function skips the step of finding the time when the projectile passes the wall.

type cannonobjcon
function f = cannonobjcon(x)
    % change initial 2-d point x to 4-d x0
    x0 = [x(1);0;300*cos(x(2));300*sin(x(2))];
    % solve for trajectory
    sol = ode45(@cannonshot,[0,15],x0);
    % find time t when trajectory height = 0
    zerofnd = fzero(@(r)deval(sol,r,2),[1e-2,15]);
    % find the horizontal position at that time
    dist = deval(sol,zerofnd,1);
    % what is the height when the projectile crosses the wall at x = 0?
    if deval(sol,15,1) > 0
        wallfnd = fzero(@(r)deval(sol,r,1),[0,15]);
        height = deval(sol,wallfnd,2);
    else
        height = deval(sol,15,2);
    end
    f.ineq = 20 - height; % height must be above 20
    % take negative of distance for maximization
    f.fval = -dist;
end

you already calculated one feasible initial trajectory. use that value as an initial point.

fx0 = cannonobjcon(x0);
fx0.x = x0;

solve optimization using surrogateopt

set surrogateopt options to use the initial point. for reproducibility, set the random number generator to default. use the 'surrogateoptplot' plot function. run the optimization. to understand the 'surrogateoptplot' plot, see .

opts = optimoptions('surrogateopt','initialpoints',x0,'plotfcn','surrogateoptplot');
rng default
[xsolution,distance,exitflag,output] = surrogateopt(@cannonobjcon,lb,ub,opts)

figure optimization plot function contains an axes object. the axes object with title best: -125.999 incumbent: -125.913 current: -123.14, xlabel number of function evaluations, ylabel objective function contains 9 objects of type line. one or more of the lines displays its values using only markers these objects represent best, incumbent, initial samples, random samples (infeas), random samples, adaptive samples (infeas), adaptive samples, incumbent (infeas), surrogate reset.

surrogateopt stopped because it exceeded the function evaluation limit set by 
'options.maxfunctionevaluations'.
xsolution = 1×2
  -28.4012    0.6161
distance = -125.9987
exitflag = 0
output = struct with fields:
        elapsedtime: 53.4358
          funccount: 200
    constrviolation: 8.0630e-04
               ineq: 8.0630e-04
           rngstate: [1x1 struct]
            message: 'surrogateopt stopped because it exceeded the function evaluation limit set by ...'

plot the final trajectory.

figure
dist = plotcannonsolution(xsolution);

figure contains an axes object. the axes object with title distance 125.998707, xlabel horizontal distance, ylabel trajectory height contains 2 objects of type line. these objects represent trajectory, wall.

the patternsearch solution in optimize odes in parallel shows a final distance of 125.9880, which is almost the same as this surrogateopt solution.

see also

related topics

网站地图