main content

simulated annealing options -凯发k8网页登录

this example shows how to create and manage options for the simulated annealing function simulannealbnd using optimoptions in the global optimization toolbox.

optimization problem setup

simulannealbnd searches for a minimum of a function using simulated annealing. for this example we use simulannealbnd to minimize the objective function dejong5fcn. this function is available when you run this example. dejong5fcn is a real-valued function of two variables and has many local minima making it difficult to optimize. there is only one global minimum at x =(-32,-32), where f(x) = 0.998. to define our problem, we must define the objective function, start point, and bounds specified by the range -64 <= x(i) <= 64 for each x(i).

objectivefunction = @dejong5fcn;
startingpoint = [-30 0];
lb = [-64 -64];
ub = [64 64];

the function plotobjective, which is available when you run this example, plots the objective function over the range -64 <= x1 <= 64, -64 <= x2 <= 64.

plotobjective(objectivefunction,[-64 64; -64 64]);
view(-15,150);

figure contains an axes object. the axes object contains 2 objects of type surface, contour.

now, we can run the simulannealbnd solver to minimize our objective function.

rng default % for reproducibility
[x,fval,exitflag,output] = simulannealbnd(objectivefunction,startingpoint,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 : 1095
fprintf('the number of function evaluations was : %d\n', output.funccount);
the number of function evaluations was : 1104
fprintf('the best function value found was : %g\n', fval);
the best function value found was : 2.98211

note that when you run this example, your results may be different from the results shown above because simulated annealing algorithm uses random numbers to generate points.

adding visualization

simulannealbnd can accept one or more plot functions through an 'options' argument. this feature is useful for visualizing the performance of the solver at run time. plot functions are selected using optimoptions. the toolbox contains a set of plot functions to choose from, or you can provide your own custom plot functions.

to select multiple plot functions, set the plotfcn option via the optimoptions function. for this example, we select saplotbestf, which plots the best function value every iteration, saplottemperature, which shows the current temperature in each dimension at every iteration, saplotf, which shows the current function value (remember that the current value is not necessarily the best one), and saplotstopping, which plots the percentage of stopping criteria satisfied every ten iterations.

options = optimoptions(@simulannealbnd, ...
                     'plotfcn',{@saplotbestf,@saplottemperature,@saplotf,@saplotstopping});

run the solver.

simulannealbnd(objectivefunction,startingpoint,lb,ub,options);

figure simulated annealing contains 4 axes objects. axes object 1 with title best function value: 0.998004, xlabel iteration, ylabel function value contains a line object which displays its values using only markers. axes object 2 with title current temperature, xlabel number of variables (2), ylabel current temperature contains an object of type bar. axes object 3 with title current function value: 0.998004, xlabel iteration, ylabel function value contains a line object which displays its values using only markers. axes object 4 with title stopping criteria, xlabel % of criteria met contains an object of type bar.

optimization terminated: change in best function value less than options.functiontolerance.

specifying temperature options

the temperature parameter used in simulated annealing controls the overall search results. the temperature for each dimension is used to limit the extent of search in that dimension. the toolbox lets you specify initial temperature as well as ways to update temperature during the solution process. the two temperature-related options are the initialtemperature and the temperaturefcn.

specifying initial temperature

the default initial temperature is set to 100 for each dimension. if you want the initial temperature to be different in different dimensions then you must specify a vector of temperatures. this may be necessary in cases when problem is scaled differently in each dimension. for example,

options = optimoptions(@simulannealbnd,'initialtemperature',[300 50]);

initialtemperature can be set to a vector of length less than the number of variables (dimension); the solver expands the vector to the remaining dimensions by taking the last element of the initial temperature vector. here we want the initial temperature to be the same in all dimensions so we need only specify the single temperature.

options.initialtemperature = 100;

specifying a temperature function

the default temperature function used by simulannealbnd is called temperatureexp. in the temperatureexp schedule, the temperature at any given step is .95 times the temperature at the previous step. this causes the temperature to go down slowly at first but ultimately get cooler faster than other schemes. if another scheme is desired, e.g. boltzmann schedule or "fast" schedule annealing, then temperatureboltz or temperaturefast can be used respectively. to select the fast temperature schedule, we can update our previously created options, changing temperaturefcn directly.

options.temperaturefcn = @temperaturefast;

specifying reannealing

reannealing is a part of annealing process. after a certain number of new points are accepted, the temperature is raised to a higher value in hope to restart the search and move out of a local minima. performing reannealing too soon may not help the solver identify a minimum, so a relatively high interval is a good choice. the interval at which reannealing happens can be set using the reannealinterval option. here, we reduce the default reannealing interval to 50 because the function seems to be flat in many regions and solver might get stuck rapidly.

options.reannealinterval = 50;

now that we have set up the new temperature options we run the solver again.

[x,fval,exitflag,output] = simulannealbnd(objectivefunction,startingpoint,lb,ub,options);
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 : 1306
fprintf('the number of function evaluations was : %d\n', output.funccount);
the number of function evaluations was : 1321
fprintf('the best function value found was : %g\n', fval);
the best function value found was : 16.4409

reproducing results

simulannealbnd is a nondeterministic algorithm. this means that running the solver more than once without changing any settings may give different results. this is because simulannealbnd utilizes matlab® random number generators when it generates subsequent points and also when it determines whether or not to accept new points. every time a random number is generated the state of the random number generators change.

to see this, two runs of simulannealbnd solver yields:

[x,fval] = simulannealbnd(objectivefunction,startingpoint,lb,ub,options);
optimization terminated: change in best function value less than options.functiontolerance.
fprintf('the best function value found was : %g\n', fval);
the best function value found was : 1.99203

and,

[x,fval] = simulannealbnd(objectivefunction,startingpoint,lb,ub,options);
optimization terminated: change in best function value less than options.functiontolerance.
fprintf('the best function value found was : %g\n', fval);
the best function value found was : 10.7632

in the previous two runs simulannealbnd gives different results.

we can reproduce our results if we reset the states of the random number generators between runs of the solver by using information returned by simulannealbnd. simulannealbnd returns the states of the random number generators at the time simulannealbnd is called in the output argument. this information can be used to reset the states. here we reset the states between runs using this output information so the results of the next two runs are the same.

[x,fval,exitflag,output] = simulannealbnd(objectivefunction,startingpoint,lb,ub,options);
optimization terminated: change in best function value less than options.functiontolerance.
fprintf('the best function value found was : %g\n', fval);
the best function value found was : 20.1535

we reset the state of the random number generator.

strm = randstream.getglobalstream;
strm.state = output.rngstate.state;

now, let's run simulannealbnd again.

[x,fval] = simulannealbnd(objectivefunction,startingpoint,lb,ub,options);
optimization terminated: change in best function value less than options.functiontolerance.
fprintf('the best function value found was : %g\n', fval);
the best function value found was : 20.1535

modifying the stopping criteria

simulannealbnd uses six different criteria to determine when to stop the solver. simulannealbnd stops when the maximum number of iterations or function evaluation is exceeded; by default the maximum number of iterations is set to inf and the maximum number of function evaluations is 3000*numberofvariables. simulannealbnd keeps track of the average change in the function value for maxstalliterations iterations. if the average change is smaller than the function tolerance, functiontolerance, then the algorithm will stop. the solver will also stop when the objective function value reaches objectivelimit. finally the solver will stop after running for maxtime seconds. here we set the functiontolerance to 1e-5.

options.functiontolerance = 1e-5;

run the simulannealbnd solver.

[x,fval,exitflag,output] = simulannealbnd(objectivefunction,startingpoint,lb,ub,options);
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 : 1843
fprintf('the number of function evaluations was : %d\n', output.funccount);
the number of function evaluations was : 1864
fprintf('the best function value found was : %g\n', fval);
the best function value found was : 6.90334

see also

related topics

    网站地图