solve nonlinear problem with integer and nonlinear constraints -凯发k8网页登录
the surrogateopt
solver accepts both integer constraints and nonlinear constraints. compare the solution of a nonlinear problem both with and without integer constraints. the integer constraints cause the solution to lie on a reasonably fine grid.
objective and constraint functions
the objective function is
this objective function is nonnegative, and takes its minimum value of 0 at the point = [1.3333, 1.0370].
the problem has two nonlinear constraint functions.
plot the feasible region for the nonlinear constraints.
[x,y] = meshgrid(-2:.01:3); z = (5*sinh(y./5) >= x.^4); % z=1 where the first constraint is satisfied, z=0 otherwise z = z 2*(5*tanh(x./5) >= y.^2 - 1); % z=2 where the second constraint is satisfied % z=3 where both constraints are satisfied surf(x,y,z,'linestyle','none'); fig = gcf; fig.color = 'w'; % white background view(0,90) xlabel('x_1') ylabel('x_2')
the yellow region shows where both constraints are satisfied.
surrogateopt
requires that the objective and constraint functions are part of the same function, one that returns a structure. the objective function is in the fval
field of the structure, and the constraints are in the ineq
field. these fields are the output of the objconstr
function at the .
scale integer constraints to lie on fine grid
set the problem to have integer constraints in both variables, x(1)
and x(2)
.
intcon = [1 2];
scale the problem so that the variables are scaled by s = 1/10
, where s
multiplies the variables.
s = 0.1; f = @(x)objconstr(x,s);
for this scaling to be effective, you need to scale the bounds by . set the unscaled bounds to and scale each by .
lb = [-2,-2]/s; ub = [3,3]/s;
by using the scaling s
, the problem effectively has spacing of s
in each component x(1)
and x(2)
. plot the integer points as a grid with spacing s
.
hold on grid on ax = gca; sp = -2:s:3; ax.xtick = sp; ax.ytick = sp; ax.layer = 'top'; ax.gridalpha = 1/2; ax.xticklabel = ''; ax.yticklabel = ''; xlabel('x_1') ylabel('x_2') hold off
solve scaled problem
set options to use tighter constraints than the default, and to use the surrogateoptplot
plot function.
opts = optimoptions('surrogateopt','plotfcn',"surrogateoptplot","constrainttolerance",1e-6);
call surrogateopt
to solve the problem.
rng default % for reproducibility [sol,fval,eflag,outpt] = surrogateopt(f,lb,ub,intcon,opts)
surrogateopt stopped because it exceeded the function evaluation limit set by 'options.maxfunctionevaluations'.
sol = 1×2
5 1
fval = 0.8634
eflag = 0
outpt = struct with fields:
elapsedtime: 72.3899
funccount: 200
constrviolation: -0.0375
ineq: [-0.0375 -1.4883]
rngstate: [1x1 struct]
message: 'surrogateopt stopped because it exceeded the function evaluation limit set by ...'
plot the solution as a red circle on the figure. notice that the objective function value is approximately 0.86.
figure(fig); hold on plot3(sol(1)*s,sol(2)*s,5,'ro') hold off
compare to solution without integer constraints
compare the solution with integer constraints to the solution without integer constraints.
[sol2,fval2,eflag2,outpt2] = surrogateopt(f,lb,ub,[],opts)
surrogateopt stopped because it exceeded the function evaluation limit set by 'options.maxfunctionevaluations'.
sol2 = 1×2
4.3631 0.3626
fval2 = 0.8153
eflag2 = 0
outpt2 = struct with fields:
elapsedtime: 42.4193
funccount: 200
constrviolation: -1.7294e-05
ineq: [-1.7294e-05 -1.4339]
rngstate: [1x1 struct]
message: 'surrogateopt stopped because it exceeded the function evaluation limit set by ...'
here, the objective function value is approximately 0.815. the integer constraints increase the objective function value by less than 10%. plot the new solution along with the previous integer solution. zoom in to see the solution points more clearly.
figure(fig) hold on plot3(sol2(1)*s,sol2(2)*s,5,'k*','markersize',12) xlim([0 1]) ylim([-1/2 1/2]) hold off
helper function
this code creates the objconstr
helper function. this function scales the variable x
by the factor s
, returns the objective function value in the fval
field of the f
structure, and returns the nonlinear constraints in the ineq
field of the f
structure.
function f = objconstr(x,s) x = x*s; fun = log(1 3*(x(2) - (x(1)^3 - x(1)))^2 (x(1) - 4/3)^2); c1 = x(1)^4 - 5*sinh(x(2)/5); c2 = x(2)^2 - 5*tanh(x(1)/5) - 1; c = [c1 c2]; f.fval = fun; f.ineq = c; end