Skip to main content
Version: 2.0

Rastrigin


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 C++ 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 C++ model file from the BQPhy website and add the objective and constraints.

The objective function snippet can be written as follows.


auto objective = [](const std::vector<double>& x, int dimensionality) {
const double PI = std::acos(-1);
double fitnessValue = 40.0;
for(int i = 0 ; i < 4 ; i++)
{
fitnessValue += x[i]*x[i] - 10*std::cos(2*PI*x[i]);
}

return fitnessValue;
}

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

You can impose the No_Penalty method to solve this problem.

std::vector<std::function<double(const std::vector<double>&, int)>> constraints = {};

std::string penalty_type = "No_Penalty";

You can also create a dummy constraint and solve it with Static_Penalty method and penalty coefficient be set to 0.

auto constraint1 = [](const std::vector<double>& x, int dimensionality) {
double violation = 0.0;
return violation;
};

//------------------------Add the constraints to a vector------------------------//
std::vector<std::function<double(const >std::vector<double>&, int)>> constraints = {constraint1};
//------------------ Penalty coefficients for the constraints -------------------//
// Define penalty parameters
std::string penalty_type = "Static_Penalty";
std::vector<double> penaltyCoefficients = {0.0};

Running the model file in BQPhy platform

The code for the model file is attached in the end of the page

Follow the following steps to run the model file

  • Add the model file to the platform

    Refer the Models page of the documentation for help on adding the model file

  • Create an execution for the model to run the optimization problem. Since the problem consists of 2 continuous decision variables, the following parameters are fixed

    Number of decision variables = 4
    Type of variables = Continuous
    Upper Bound = (Global) 5.12
    Lower Bound = (Global) -5.12

    Refer Executions page of the documentation to learn more on the required parameters and on running the model file.

C++ model file for the Rastrigin function

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

#include <cmath>
#include <vector>
#include <cstdlib>
#include <string>
#include <iostream>
#include <functional>
#include "optimizationProblem.h"


//=====================================================================================================
// Set the correct number of design variables. OPTIONAL
extern "C" int calculateDesignVariables()
{
// TODO: Return the number of variables (e.g., size of the solution vector 'x')
return 10; // <--- Change this value based on your specific problem
}
//=====================================================================================================
// Define objective and constraint functions, and select penalty type.
extern "C" void defineModel(OptimizationProblem& problem)
{
//------------------------ Define Objective functions ------------------------//
auto objective = [](const std::vector<double>& x, int dimensionality) {
const double PI = std::acos(-1);
double fitnessValue = 40.0;
for(int i = 0 ; i < 4 ; i++)
{
fitnessValue += x[i]*x[i] - 10*std::cos(2*PI*x[i]);
}

return fitnessValue;
};

//------------------------Add the constraints to a vector------------------------//
std::vector<std::function<double(const std::vector<double>&, int)>> constraints = {};
//------------------ Penalty coefficients for the constraints -------------------//
// Define penalty parameters
std::string penalty_type = "No_Penalty";
// std::vector<double> penaltyCoefficients = {100.0, 100.0};

if(penalty_type == "No_Penalty") {
/////////// Using No-Penalty /////////////
problem.Set_OptimizationProblem(objective, constraints, PenaltyType::No_Penalty);
}
else if(penalty_type == "Death_Penalty") {
/////////// Using Death Penalty /////////////
problem.Set_OptimizationProblem(objective, constraints, PenaltyType::Death_Penalty);
}
else if(penalty_type == "Static_Penalty") {
///////////// Using Static Penalty with a fixed penalty value /////////////
problem.Set_OptimizationProblem(objective, constraints, PenaltyType::Static_Penalty, penaltyCoefficients);
}
else if(penalty_type == "Debs_Penalty") {
/////////// Using Deb's approach /////////////
problem.Set_OptimizationProblem(objective, constraints, PenaltyType::Debs_Penalty);
}
}

//=====================================================================================================
// Process or save results after optimization finishes. OPTIONAL
extern "C" void postprocess(const std::vector<double>& x, std::string outputFilePath, int Trial_id)
{
// TODO: Implement custom logic to process the Best solution vector 'x' after optimization for each Trial
// Write output to a file using 'outputFilePath'
}