Skip to Main Content Skip to Search
Product Documentation

Constraints

Links to Optimization Toolbox Documentation

Many Global Optimization Toolbox functions accept bounds, linear constraints, or nonlinear constraints. To see how to include these constraints in your problem, see Writing Constraints in the Optimization Toolbox documentation. Try consulting these pertinent links to sections:

Set Bounds

It is more important to set bounds for global solvers than for local solvers. Global solvers use bounds in a variety of ways:

Gradients and Hessians

If you use GlobalSearch or MultiStart with fmincon, your nonlinear constraint functions can return derivatives (gradient or Hessian). For details, see Gradients and Hessians.

Vectorized Constraints

The ga and patternsearch solvers optionally compute the nonlinear constraint functions of a collection of vectors in one function call. This method can take less time than computing the objective functions of the vectors serially. This method is called a vectorized function call.

For the solver to compute in a vectorized manner, you must vectorize both your objective (fitness) function and nonlinear constraint function. For details, see Vectorizing the Objective and Constraint Functions.

As an example, suppose your nonlinear constraints for a three-dimensional problem are

The following code gives these nonlinear constraints in a vectorized fashion, assuming that the rows of your input matrix x are your population or input vectors:

function [c ceq] = nlinconst(x)

c(:,1) = x(:,1).^2/4 + x(:,2).^2/9 + x(:,3).^2/25 - 6;
c(:,2) = cosh(x(:,1) + x(:,2)) - x(:,3);
ceq = x(:,1).*x(:,2).*x(:,3) - 2;

For example, minimize the vectorized quadratic function

function y = vfun(x)
y = -x(:,1).^2 - x(:,2).^2 - x(:,3).^2;

over the region with constraints nlinconst using patternsearch:

options = psoptimset('CompletePoll','on','Vectorized','on');
[x fval] = patternsearch(@vfun,[1,1,2],[],[],[],[],[],[],...
    @nlinconst,options)
Optimization terminated: mesh size less than options.TolMesh
 and constraint violation is less than options.TolCon.

x =
    0.2191    0.7500   12.1712

fval =
 -148.7480

Using ga:

options = gaoptimset('Vectorized','on');
[x fval] = ga(@vfun,3,[],[],[],[],[],[],@nlinconst,options)
Optimization terminated: maximum number of generations exceeded.

x =
   -1.4098   -0.1216   11.6664

fval =
 -138.1066

For this problem patternsearch computes the solution far more quickly and accurately.

  


Free Optimization Interactive Kit

Learn how to use optimization to solve systems of equations, fit models to data, or optimize system performance.

Get free kit

Trials Available

Try the latest version of optimization products.

Get trial software
 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS