main content

minimize rastrigin's function -凯发k8网页登录

rastrigin's function

this example shows how to find the minimum of rastrigin's function, a function that is often used to test the genetic algorithm. the example presents two approaches for minimizing: using the optimize live editor task and working at the command line.

for two independent variables, rastrigin's function is defined as

ras(x)=20 x12 x22-10(cos2πx1 cos2πx2).

the rastriginsfcn.m file, which computes the values of rastrigin's function, is available when you run this example. create a surface plot of rastrigin's function.

fsurf(@(x,y)reshape(rastriginsfcn([x(:),y(:)]),size(x)),...
    "meshdensity",100,...
    "showcontours","on",...
    "linestyle",":")

figure contains an axes object. the axes object contains an object of type functionsurface.

as the plot shows, rastrigin's function has many local minima—the “valleys” in the plot. however, the function has just one global minimum, which occurs at the point [0 0] in the x-y plane, where the value of the function is 0. at any local minimum other than [0 0], the value of rastrigin's function is greater than 0. the farther the local minimum is from the origin, the larger the value of the function is at that point.

rastrigin's function is often used to test the genetic algorithm, because its many local minima make it difficult for standard, gradient-based methods to find the global minimum.

minimize using the optimize live editor task

this section explains how to find the minimum of rastrigin's function using the genetic algorithm.

note: because the genetic algorithm uses random number generators, the algorithm returns different results each time you run it.

  • 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 button.

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 number of variables and objective function.

rng default % for reproducibility
nvar = 2;
fun = @rastriginsfcn;
  • 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 > ga - genetic algorithm.

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

  • select number of variables > nvar.

optimizelet_ga_rastriginsfcn.png

  • in the display progress section of the task, select the best fitness 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 ga is a stochastic algorithm.

ga_out.png

  • the points at the bottom of the plot denote the best fitness values, while the points above them denote the averages of the fitness values in each generation. the top of the plot displays the best and mean values, numerically, in the current generation.

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

optimizelet_ga_output.png

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

disp(solution)
disp(objectivevalue)

the value shown is not very close to the actual minimum value of rastrigin's function, which is 0. the topics , , and describe ways to achieve a result that is closer to the actual minimum. or, you can simply rerun the solver to try to obtain a better result.

the final state of the live editor task appears here.

live task
optimization terminated: average change in the fitness value less than options.functiontolerance.

figure genetic algorithm contains an axes object. the axes object with title best: 2.54629 mean: 113.977, xlabel generation, ylabel fitness value contains 2 objects of type line. one or more of the lines displays its values using only markers these objects represent best fitness, mean fitness.

disp(solution)
    0.9785    0.9443
disp(objectivevalue)
    2.5463

minimize at the command line

to find the minimum of rastrigin's function at the command line, enter the following code.

rng default % for reproducibility
options = optimoptions('ga','plotfcn','gaplotbestf');
[solution,objectivevalue] = ga(@rastriginsfcn,2,...
    [],[],[],[],[],[],[],options)
optimization terminated: average change in the fitness value less than options.functiontolerance.

figure genetic algorithm contains an axes object. the axes object with title best: 2.54629 mean: 113.977, xlabel generation, ylabel fitness value contains 2 objects of type line. one or more of the lines displays its values using only markers these objects represent best fitness, mean fitness.

solution = 1×2
    0.9785    0.9443
objectivevalue = 2.5463

the points at the bottom of the plot denote the best fitness values, while the points above them denote the averages of the fitness values in each generation. the top of the plot displays the best and mean values, numerically, in the current generation.

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.

related topics

网站地图