Skip to main content
Version: 2.0

MATLAB

Ackley function example in MATLAB


Mathematical formulation of the Ackley function

Ackely function is a standard function used for benchmarking global optimization solvers. It is chosen for the complexity of the function, its multimodal nature and the known global minima.

An nn-dimensional Ackley function has the following form

f(x)=aexp ⁣(b1ni=1nxi2)exp ⁣(1ni=1ncos(cxi))+a+exp(0)f(x) = -a \exp\!\left(-b \sqrt{\frac{1}{n} \sum_{i=1}^{n} x_i^2}\right) - \exp\!\left(\frac{1}{n} \sum_{i=1}^{n} \cos(c x_i)\right) + a + \exp(0)

where a=20,b=15,c=2πa = 20 , b = \frac{1}{5}, c = 2\pi

The global optima of the function is

f(x)=0atx={0,0,...,0}f(x^{*}) = 0 \quad \text{at} \quad x^{*} = \{0,0,...,0\}

Implementation of Ackley function in BQPhy using MATLAB model file

As an example, we are solving the 2 dimensional Ackley function, whose global optima is

f(x)=0wherex={0,0}f(x^{*}) = 0 \quad \text{where} \quad x^{*} = \{0,0\}

The input variables xix_{i} are bounded with the following constraints 10xi10,i{1,2}-10 \leq x_{i} \leq 10 , \quad i \in \{1,2\}

The bounds for the decision variables are to be passed during the execution in BQPhy

You can get the template for the MATLAB model file from the BQPhy website and add the objective and constraints.

Assuming the chromosome is a list of length 2 and type double, the objective function can be written as follows.

a = 20
b = 0.2
c = 2*pi

% Objective Function value
term1 = -a * exp(-b * sqrt(0.5 * (x(1).^2 + x(2).^2)));
term2 = -exp(0.5 * (cos(c*x(1)) + cos(c*x(2))));

objectiveFunctionValue = term1 + term2 + a + exp(1);

The optimization problem does not have any explicit constraints other than that on the decision variables, hence the constraint function is empty.

MATLAB model file for the Ackley function

The model file for this function comes out as follows

function USER_DEFINED_NAME = fitnessEval(x)

a = 20
b = 0.2
c = 2*pi

% Objective Function value
term1 = -a * exp(-b * sqrt(0.5 * (x(1).^2 + x(2).^2)));
term2 = -exp(0.5 * (cos(c*x(1)) + cos(c*x(2))));

objectiveFunctionValue = term1 + term2 + a + exp(1);


% Final fitness
USER_DEFINED_NAME = objectiveFunctionValue
end

Rastrigin function example in MATLAB


Mathematical formulation of the Rastrigin function

The Rastrigin function is one of the standard functions used for benchmarking optimization solvers. It is characterized by its multimodal nature (i.e. it contains multiple local minima) which pose challenges in obtaining the global minima.

The standard form of a nn-dimensional Rastrigin function is as follows

f(x)=10n+i=1n[xi210cos(2πxi)]f(x) = 10n + \sum_{i=1}^{n} \left[ x_i^2 - 10 \cos(2\pi x_i) \right]

The global optima is

f(x)=0atx={0,0,...,0}f(x^{*}) = 0 \quad \text{at} \quad x^{*} = \{0,0,...,0\}

Implementation of Rastrigin function in BQPhy using MATLAB model file

Consider the 4 dimensional Rastrigin function, whose global optimal is

f(x)=0atx={0,0,0,0}f(x^{*}) = 0 \quad \text{at} \quad x^{*} = \{0,0,0,0\}

with the bounds on the variables

5.12xi5.12,i{1,2,3,4}-5.12 \leq x_{i} \leq 5.12 ,\quad i \in \{1,2,3,4\}

The bounds for the decision variables are to be passed during the execution in BQPhy

You can get the template for the MATLAB model file from the BQPhy website and add the objective and constraints.

The objective function snippet can be written as follows.

n = 4;

% Objective Function value
objectiveFunctionValue = 10*n + sum(x.^2 - 10*cos(2*pi*x));

The optimization problem does not have any explicit constraints other than that on the decision variables.

Python model file for the Rastrigin function

The model file for the Rastrigin function can be written as follows

function USER_DEFINED_NAME = fitnessEval(x)
n = 4;

% Objective Function value
objectiveFunctionValue = 10*n + sum(x.^2 - 10*cos(2*pi*x));

% Final fitness
USER_DEFINED_NAME = objectiveFunctionValue;
end