main content

minimize function with many local minima -凯发k8网页登录

this example shows how to find a local minimum of a function using simulated annealing. the example presents two approaches for minimizing: working at the command line and using the optimize live editor task.

de jong's fifth function is a two-dimensional function with many (25) local minima. the function is available when you run this example. in the following plot, it is unclear which of the local minima is the global minimum.

dejong5fcn

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

many standard optimization algorithms become stuck in local minima. because the simulated annealing algorithm performs a wide random search, the chance of being trapped in a local minimum is decreased.

note:because simulated annealing uses random number generators, each time you run this algorithm you can get different results. see for more information.

minimize at the command line

to run the simulated annealing algorithm without constraints, call simulannealbnd at the command line using the objective function in dejong5fcn.m, referenced by the anonymous function @dejong5fcn in the following code.

rng(10,'twister') % for reproducibility
fun = @dejong5fcn;
[x,fval] = simulannealbnd(fun,[0 0])
optimization terminated: change in best function value less than options.functiontolerance.
x = 1×2
  -16.1292  -15.8214
fval = 6.9034

in the results:

  • x is the final point returned by the algorithm.

  • fval is the objective function value at the final point.

minimize using the optimize live editor task

you can also run the minimization using the optimize live editor task, which provides a visual approach.

  • create a new live script by clicking the new live script button in the file section on the home tab.

new_live_script.png

  • insert an optimize live editor task. click the insert tab and then, in the code section, select task > optimize.

optimizelet_insert.png

optimizelet_choose.png

  • click the solver-based task.

optimizelet_initial.png

  • for use in entering problem data, insert a new section by clicking the section break button on the insert tab. new sections appear above and below the task.

  • in the new section above the task, enter the following code to define the initial point and the objective function.

x0 = [0 0];
fun = @dejong5fcn;
rng default % for reproducibility
  • to place these variables into the workspace, run the section by pressing ctrl enter.

  • in the specify problem type section of the task, click the objective > nonlinear button.

  • select solver > simulannealbnd - simulated annealing algorithm.

  • in the select problem data section of the task, select objective function > function handle and then choose fun.

  • select initial point (x0) > x0.

optimizelet_simulannealbnd_setup.png

  • in the display progress section of the task, select the best value plot.

  • to run the solver, click the options button at the top right of the task window, and select run section. the plot appears in a separate figure window and in the task output area. note that your plot might be different from the one shown, because simulannealbnd is a stochastic algorithm.

optimizelet_simulannealbnd_plot.png

  • to see the solution and best objective function value, look at the top of the task.

optimizelet_simulannealbnd_solution.png

  • the optimize live editor task returns variables named solution and objectivevalue to the workspace.

  • to view the values these variables, enter the following code in the section below the task.

disp(solution)
disp(objectivevalue)
  • run the section by pressing ctrl enter.

the end of this example has the optimize task in its final state.

live task

figure simulated annealing contains an axes object. the axes object with title best function value: 10.7632, xlabel iteration, ylabel function value contains a line object which displays its values using only markers.

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

both the optimize live editor task and the command line allow you to formulate and solve problems, and they give identical results. the command line is more streamlined, but provides less help for choosing a solver, setting up the problem, and choosing options such as plot functions. you can also start a problem using optimize, and then generate code for command line use, as in constrained nonlinear problem using optimize live editor task or solver.

see also

related topics

网站地图