MATLAB Examples

Mixed Integer Optimization of Rastrigin's Function

This example shows how to find the minimum of Rastrigin's function restricted so the first component of x is an integer. The components of x are further restricted to be in the region $5 \pi\le x(1) \le 20\pi,\ -20\pi\le x(2)\le -4\pi$ .


Set up the bounds for your problem

lb = [5*pi,-20*pi];
ub = [20*pi,-4*pi];

Set a plot function so you can view the progress of ga

opts = optimoptions('ga','PlotFcn',@gaplotbestf);

Call the ga solver where x(1) has integer values

rng(1,'twister') % for reproducibility
IntCon = 1;
[x,fval,exitflag] = ga(@rastriginsfcn,2,[],[],[],[],...
Optimization terminated: average change in the penalty fitness value less than options.FunctionTolerance
and constraint violation is less than options.ConstraintTolerance.

x =

   16.0000  -12.9325

fval =


exitflag =


ga converges quickly to the solution.