Note: This page has been translated by MathWorks. Click here to see

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

This example shows how to create and minimize an objective function using the simulated annealing algorithm (`simulannealbnd`

function) in Global Optimization Toolbox. For algorithmic details, see How Simulated Annealing Works.

The objective function to minimize is a simple function of two variables:

min f(x) = (4 - 2.1*x1^2 + x1^4/3)*x1^2 + x1*x2 + (-4 + 4*x2^2)*x2^2; x

This function is known as "cam," as described in L.C.W. Dixon and G.P. Szego [1].

To implement the objective function calculation, the MATLAB file `simple_objective.m`

has the following code:

`type simple_objective`

function y = simple_objective(x) %SIMPLE_OBJECTIVE Objective function for PATTERNSEARCH solver % Copyright 2004 The MathWorks, Inc. x1 = x(1); x2 = x(2); y = (4-2.1.*x1.^2+x1.^4./3).*x1.^2+x1.*x2+(-4+4.*x2.^2).*x2.^2;

All Global Optimization Toolbox solvers assume that the objective has one input `x`

, where `x`

has as many elements as the number of variables in the problem. The objective function computes the scalar value of the objective function and returns it in its single output argument `y`

.

`simulannealbnd`

To minimize the objective function using `simulannealbnd`

, pass in a function handle to the objective function and a starting point `x0`

as the second argument. For reproducibility, set the random number stream.

ObjectiveFunction = @simple_objective; x0 = [0.5 0.5]; % Starting point rng default % For reproducibility [x,fval,exitFlag,output] = simulannealbnd(ObjectiveFunction,x0)

Optimization terminated: change in best function value less than options.FunctionTolerance.

`x = `*1×2*
-0.0896 0.7130

fval = -1.0316

exitFlag = 1

`output = `*struct with fields:*
iterations: 2948
funccount: 2971
message: 'Optimization terminated: change in best function value less than options.FunctionTolerance.'
rngstate: [1x1 struct]
problemtype: 'unconstrained'
temperature: [2x1 double]
totaltime: 3.8543

`simulannealbnd`

returns four output arguments:

`x`

— Best point found`fval`

— Function value at the best point`exitFlag`

— Integer corresponding to the reason the function stopped`output`

— Information about the optimization steps

You can use `simulannealbnd`

to solve problems with bound constraints. Pass lower and upper bounds as vectors. For each coordinate `i`

, the solver ensures that `lb(i) <= x(i) <= ub(i)`

. Impose the bounds `–64 <= x(i) <= 64`

.

lb = [-64 -64]; ub = [64 64];

Run the solver with the lower and upper bound arguments.

[x,fval,exitFlag,output] = simulannealbnd(ObjectiveFunction,x0,lb,ub);

Optimization terminated: change in best function value less than options.FunctionTolerance.

`fprintf('The number of iterations was : %d\n', output.iterations);`

The number of iterations was : 2428

`fprintf('The number of function evaluations was : %d\n', output.funccount);`

The number of function evaluations was : 2447

`fprintf('The best function value found was : %g\n', fval);`

The best function value found was : -1.03163

The solver finds essentially the same solution as before.

Sometimes you want an objective function to be parameterized by extra arguments that act as constants during the optimization. For example, in the previous objective function, you might want to replace the constants 4, 2.1, and 4 with parameters that you can change to create a family of objective functions. For more information, see Passing Extra Parameters (Optimization Toolbox).

Rewrite the objective function to take three additional parameters in a new minimization problem.

min f(x) = (a - b*x1^2 + x1^4/3)*x1^2 + x1*x2 + (-c + c*x2^2)*x2^2; x

`a`

, `b`

, and `c`

are parameters to the objective function that act as constants during the optimization (they are not varied as part of the minimization). To implement the objective function calculation, the MATLAB file `parameterized_objective.m`

contains the following code:

`type parameterized_objective`

function y = parameterized_objective(x,p1,p2,p3) %PARAMETERIZED_OBJECTIVE Objective function for PATTERNSEARCH solver % Copyright 2004 The MathWorks, Inc. x1 = x(1); x2 = x(2); y = (p1-p2.*x1.^2+x1.^4./3).*x1.^2+x1.*x2+(-p3+p3.*x2.^2).*x2.^2;

Again, you need to pass in a function handle to the objective function as well as a starting point as the second argument.

`simulannealbnd`

calls the objective function with just one argument `x`

, but the objective function has four arguments: `x`

, `a`

, `b`

, and `c`

. To indicate which variable is the argument, use an anonymous function to capture the values of the additional arguments (the constants `a`

, `b`

, and `c`

). Create a function handle `ObjectiveFunction`

to an anonymous function that takes one input `x`

, but calls `parameterized_objective`

with `x`

, `a`

, `b`

and `c`

. When you create the function handle `ObjectiveFunction`

, the variables `a`

, `b`

, and `c`

have values that are stored in the anonymous function.

```
a = 4; b = 2.1; c = 4; % Define constant values
ObjectiveFunction = @(x) parameterized_objective(x,a,b,c);
x0 = [0.5 0.5];
[x,fval] = simulannealbnd(ObjectiveFunction,x0)
```

Optimization terminated: change in best function value less than options.FunctionTolerance.

`x = `*1×2*
0.0898 -0.7127

fval = -1.0316

The solver finds essentially the same solution as before.

[1] Dixon, L. C. W., and G .P. Szego (eds.). *Towards Global Optimisation 2.* North-Holland: Elsevier Science Ltd., Amsterdam, 1978.