Main Content

Minimize Function with Many Local Minima

De Jong's Fifth Function

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 theOptimizeLive Editor task.

De Jong's fifth function is a two-dimensional function with many (25) local minima. In the following plot, it is unclear which of these 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. SeeReproduce Your Resultsfor more information.

Minimize at the Command Line

To run the simulated annealing algorithm without constraints, callsimulannealbndat the command line using the objective function indejong5fcn.m, referenced by the anonymous function@dejong5fcnin the following code.

rng(10,'twister')% for reproducibilityfun = @dejong5fcn; [x,fval] = simulannealbnd(fun,[0 0])
Optimization terminated: change in best function value less than options.FunctionTolerance. x = -16.1292 -15.8214 fval = 6.9034

In the results:

  • xis the final point returned by the algorithm.

  • fvalis the objective function value at the final point.

Minimize Using theOptimizeLive Editor Task

You can also run the minimization using theOptimizeLive Editor task, which provides a visual approach.

  1. Create a new live script by clicking theNew Live Scriptbutton in theFilesection on theHometab.

    New Live Script button

  2. Insert anOptimizeLive Editor task. Click theInsert选项卡,然后在Codesection, selectTask > Optimize.

    Inserting Optimize Live Editor task

    Optimize task in Live Editor: Choose between problem-based (recommended) and solver-based

  3. Click theSolver-basedtask.

    Optimize Live Editor task

  4. For use in entering problem data, insert a new section by clicking theSection Breakbutton on theInserttab. New sections appear above and below the task.

  5. 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;
  6. To place these variables into the workspace, run the section by pressingCtrl+Enter.

  7. In theSpecify problem typesection of the task, click the目标>非线性button.

  8. Select解算器> simulannealbnd——模拟安ealing algorithm.

  9. In theSelect problem datasection of the task, selectObjective function > Function handleand then choosefun.

  10. SelectInitial point (x0) > x0.

  11. In theDisplay progresssection of the task, select theBest valueplot.

  12. To run the solver, click the options buttonat the top right of the task window, and selectRun 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, becausesimulannealbndis a stochastic algorithm.

    Plot showing best function value decreasing to 10.7632 in about 1300 iterations

  13. To see the solution and best objective function value, look at the top of the task.

    Optimize returns solution and objectiveValue variables

    TheOptimizeLive Editor task returns variables namedsolutionandobjectiveValueto the workspace.

  14. To view the values these variables, enter the following code in the section below the task.

    disp(solution) disp(objectiveValue)
  15. Run the section by pressingCtrl+Enter.

    disp(solution)
    -32.0285 -0.1280
    disp(objectiveValue)
    10.7632

Both theOptimizeLive 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 usingOptimize, and then generate code for command line use, as inSolve a Constrained Nonlinear Problem, Solver-Based.

See Also

Related Topics