Write Constraints
Consult 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 Write Constraints. Try consulting these pertinent links to sections:
Note
The surrogateopt
solver
uses a different syntax for nonlinear constraints than other solvers, and requires finite
bounds on all components. For details, see the function reference page and Convert Nonlinear Constraints Between surrogateopt Form and Other Solver Forms.
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:
GlobalSearch
requires bounds for its scatter-search point generation. If you do not provide bounds,GlobalSearch
bounds each component below by-9999
and above by10001
. 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 by1000
. 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 of0
as the lower bound and1
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
andpatternsearch
do not require bounds, although they can use bounds.
Ensure ga Options Maintain Feasibility
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.
Note
When a problem has integer constraints, ga
ensures that all
operators (mutation, crossover, and creation) return feasible populations with respect to
bounds, linear constraints, and integer constraints at each iteration. This feasibility
holds to within a small tolerance.
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 Vectorize 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 = 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.