minimization using simulated annealing algorithm -凯发k8网页登录
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 .
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® 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
.
minimize using 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: 1.3618
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 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.
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, see .
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.
references
[1] dixon, l. c. w., and g .p. szego (eds.). towards global optimisation 2. north-holland: elsevier science ltd., amsterdam, 1978.