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
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, callsimulannealbnd
at the command line using the objective function indejong5fcn.m
, referenced by the anonymous function@dejong5fcn
in 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:
x
is the final point returned by the algorithm.fval
is 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.
Create a new live script by clicking theNew Live Scriptbutton in theFilesection on theHometab.
Insert anOptimizeLive Editor task. Click theInsert选项卡,然后在Codesection, selectTask > Optimize.
Click theSolver-basedtask.
For use in entering problem data, insert a new section by clicking theSection Breakbutton on theInserttab. 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;
To place these variables into the workspace, run the section by pressingCtrl+Enter.
In theSpecify problem typesection of the task, click the目标>非线性button.
Select解算器> simulannealbnd——模拟安ealing algorithm.
In theSelect problem datasection of the task, selectObjective function > Function handleand then choose
fun
.SelectInitial point (x0) > x0.
In theDisplay progresssection of the task, select theBest valueplot.
To run the solver, click the options button⁝at 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, because
simulannealbnd
is a stochastic algorithm.To see the solution and best objective function value, look at the top of the task.
TheOptimizeLive Editor task returns variables named
solution
andobjectiveValue
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 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.