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

Bound Constraints (Optimization Toolbox)

Linear Constraints (Optimization Toolbox)

Nonlinear Constraints (Optimization Toolbox)

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

`GlobalSearch`

requires bounds for its scatter-search point generation. If you do not provide bounds,`GlobalSearch`

bounds each component below by`-9999`

and above by`10001`

. However, these bounds can easily be inappropriate.If you do not provide bounds and do not provide custom start points,

`MultiStart`

bounds each component below by`-1000`

and above by`1000`

. However, these bounds can easily be inappropriate.`ga`

uses bounds and linear constraints for its initial population generation. For unbounded problems,`ga`

uses a default of`0`

as the lower bound and`1`

as the upper bound for each dimension for initial point generation. For bounded problems, and problems with linear constraints,`ga`

uses the bounds and constraints to make the initial population.`simulannealbnd`

and`patternsearch`

do not require bounds, although they can use bounds.

The `ga`

solver generally maintains
strict feasibility with respect to bounds and linear constraints.
This means that, at every iteration, all members of a population satisfy
the bounds and linear constraints.

However, you can set options that cause this feasibility to
fail. For example if you set `MutationFcn`

to `@mutationgaussian`

or `@mutationuniform`

,
the mutation function does not respect constraints, and your population
can become infeasible. Similarly, some crossover functions can cause
infeasible populations, although the default `gacreationlinearfeasible`

does
respect bounds and linear constraints. Also, `ga`

can
have infeasible points when using custom mutation or crossover functions.

To ensure feasibility, use the default crossover and mutation
functions for `ga`

. Be especially careful that
any custom functions maintain feasibility with respect to bounds and
linear constraints.

`ga`

does not enforce linear constraints when there are integer
constraints. Instead, `ga`

incorporates linear constraint violations into
the penalty function. See Integer ga Algorithm.

If you use `GlobalSearch`

or `MultiStart`

with `fmincon`

,
your nonlinear constraint functions can return derivatives (gradient
or Hessian). For details, see Gradients and Hessians.

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 Vectorize the Objective and Constraint Functions.

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

$$\begin{array}{c}\frac{{x}_{1}^{2}}{4}+\frac{{x}_{2}^{2}}{9}+\frac{{x}_{3}^{2}}{25}\le 6\\ {x}_{3}\ge \mathrm{cosh}\left({x}_{1}+{x}_{2}\right)\\ {x}_{1}{x}_{2}{x}_{3}=2.\end{array}$$

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 = optimoptions('patternsearch','UseCompletePoll',true,'UseVectorized',true); [x fval] = patternsearch(@vfun,[1,1,2],[],[],[],[],[],[],... @nlinconst,options) Optimization terminated: mesh size less than options.MeshTolerance and constraint violation is less than options.ConstraintTolerance. x = 0.2191 0.7500 12.1712 fval = -148.7480

Using `ga`

:

options = optimoptions('ga','UseVectorized',true); [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.

- Write Constraints (Optimization Toolbox)
- Vectorize the Objective and Constraint Functions