Minimization Using Simulated Annealing Algorithm
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, seeHow Simulated Annealing Works.
Simple Objective Function
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® filesimple_objective.m
has the following code:
typesimple_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 inputx
, wherex
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 argumenty
.
Minimize Usingsimulannealbnd
To minimize the objective function usingsimulannealbnd
, pass in a function handle to the objective function and a starting pointx0
as the second argument. For reproducibility, set the random number stream.
ObjectiveFunction = @simple_objective; x0 = [0.5 0.5];% Starting pointrngdefault% 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: 2.8401
simulannealbnd
returns four output arguments:
x
— Best point foundfval
— Function value at the best pointexitFlag
— Integer corresponding to the reason the function stoppedoutput
— Information about the optimization steps
Bound Constrained Minimization
You can usesimulannealbnd
to solve problems with bound constraints. Pass lower and upper bounds as vectors. For each coordinatei
, the solver ensures thatlb(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(“最好的函数值发现:% g \ n ', fval);
The best function value found was : -1.03163
The solver finds essentially the same solution as before.
Minimize Using Additional Arguments
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, seePassing Extra Parameters.
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
, andc
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 fileparameterized_objective.m
contains the following code:
typeparameterized_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 argumentx
, but the objective function has four arguments:x
,a
,b
, andc
. To indicate which variable is the argument, use an anonymous function to capture the values of the additional arguments (the constantsa
,b
, andc
). Create a function handleObjectiveFunction
to an anonymous function that takes one inputx
, but callsparameterized_objective
withx
,a
,b
andc
. When you create the function handleObjectiveFunction
, the variablesa
,b
, andc
have values that are stored in the anonymous function.
a = 4; b = 2.1; c = 4;% Define constant valuesObjectiveFunction = @(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×20.0898 -0.7127
fval = -1.0316
The solver finds essentially the same solution as before.
References
[1] Dixon, L. C. W., and G .P. Szego (eds.).Towards Global Optimisation 2.North-Holland: Elsevier Science Ltd., Amsterdam, 1978.