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:
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.
It is more important to set bounds for global solvers than for local solvers. Global solvers use bounds in a variety of ways:
GlobalSearchrequires bounds for its scatter-search point generation. If you do not provide bounds,
GlobalSearchbounds each component below by
-9999and above by
10001. However, these bounds can easily be inappropriate.
If you do not provide bounds and do not provide custom start points,
MultiStartbounds each component below by
-1000and above by
1000. However, these bounds can easily be inappropriate.
gauses bounds and linear constraints for its initial population generation. For unbounded problems,
gauses a default of
0as the lower bound and
1as the upper bound for each dimension for initial point generation. For bounded problems, and problems with linear constraints,
gauses the bounds and constraints to make the initial population.
patternsearchdo not require bounds, although they can use bounds.
Ensure ga Options Maintain Feasibility
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
the mutation function does not respect constraints, and your population
can become infeasible. Similarly, some crossover functions can cause
infeasible populations, although the default
respect bounds and linear constraints. Also,
have infeasible points when using custom mutation or crossover functions.
To ensure feasibility, use the default crossover and mutation
ga. Be especially careful that
any custom functions maintain feasibility with respect to bounds and
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
your nonlinear constraint functions can return derivatives (gradient
or Hessian). For details, see Gradients and Hessians.
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
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
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
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
the solution far more quickly and accurately.