Python
Ackley Function Example in Python
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 -dimensional Ackley function has the following form
where
The global optima of the function is
Implementation of Ackley function in BQPhy using Python model file
As an example, we are solving the 2 dimensional Ackley function, whose global optima is
The input variables are bounded with the following constraints
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.
Assuming the chromosome is a list of length 2 and type double, the objective function can be written as follows.
def objective(x: List[float], dimensionality: int) -> float:
a = 20
b = 1/5
c = 2*math.pi
fitnessValue = a + math.e
fitnessValue += -a*math.exp(-b*math.sqrt(0.5*(x[0]**2 + x[1]**2))) - math.exp(0.5*(math.cos(c*x[0]) + math.cos(c*x[1])))
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.
constraints = []
penalty_type = "No_Penalty"
You can also create a dummy constraint and solve it with
Static_Penaltymethod and penalty coefficient be set to 0.def constraint1(x: List[float], dimensionality: int) -> float:
constraintFunc = 0
return constraintFunc
# Create constraints vector
constraints = [constraint1]
# Define penalty parameters
penalty_type = "Static_Penalty"
penalty_coefficients = [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 = 2
Type of variables = Continuous
Upper Bound = (Global) 10
Lower Bound = (Global) -10Refer Executions page of the documentation to learn more on the required parameters and on running the model file.
Python model file for the Ackley function
The model file for this function comes out as follows
from userLibrary_helper import OptimizationProblem, PenaltyType, FitnessEvalParameter
from typing import List
# ===============================================================================================
# DO NOT MODIFY CODE ABOVE THIS LINE
# ===============================================================================================
# Import Libraries
import math
def Model(x):
dimensionality = len(x)
# Define objective function
def objective(x: List[float], dimensionality: int) -> float:
a = 20
b = 1/5
c = 2*math.pi
fitnessValue = a + math.e
fitnessValue += -a*math.exp(-b*math.sqrt(0.5*(x[0]**2 + x[1]**2))) - math.exp(0.5*(math.cos(c*x[0]) + math.cos(c*x[1])))
return fitnessValue
# Define penalty parameters
penalty_type = "No_Penalty" # Options: No_Penalty, Death_Penalty, Static_Penalty, Debs_Penalty
# penalty_coefficients = [0.0] # Make sure number of entries are equivalent to number of constraints
if penalty_type == "No_Penalty":
########### Using No-Penalty ###########
problem = OptimizationProblem(objective, constraints, PenaltyType.No_Penalty)
elif penalty_type == "Death_Penalty":
########### Using Death Penalty ############
problem = OptimizationProblem(objective, constraints, PenaltyType.Death_Penalty)
elif penalty_type == "Static_Penalty":
########### Using Static Penalty with a fixed penalty value ############
problem = OptimizationProblem(objective, constraints, PenaltyType.Static_Penalty, penalty_coefficients)
elif penalty_type == "Debs_Penalty":
########### Using Deb's approach ###########
problem = OptimizationProblem(objective, constraints, PenaltyType.Debs_Penalty)
return problem.evaluate_objective(x, dimensionality)
Rastrigin Function Example in Python
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 -dimensional Rastrigin function is as follows
The global optima is
Implementation of Rastrigin function in BQPhy using Python model file
Consider the 4 dimensional Rastrigin function, whose global optimal is
with the bounds on the variables
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.
def objective(x: List[float], dimensionality: int) -> float:
fitnessValue = 40
for i in range(4):
fitnessValue += x[i]*x[i] - 10*math.cos(2*math.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.
constraints = []
penalty_type = "No_Penalty"
You can also create a dummy constraint and solve it with
Static_Penaltymethod and penalty coefficient be set to 0.def constraint1(x: List[float], dimensionality: int) -> float:
constraintFunc = 0
return constraintFunc
# Create constraints vector
constraints = [constraint1]
# Define penalty parameters
penalty_type = "Static_Penalty"
penalty_coefficients = [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.12Refer Executions page of the documentation to learn more on the required parameters and on running the model file.
Python model file for the Rastrigin function
The model file for the Rastrigin function can be written as follows
from userLibrary_helper import OptimizationProblem, PenaltyType, FitnessEvalParameter
from typing import List
# ===============================================================================================
# DO NOT MODIFY CODE ABOVE THIS LINE
# ===============================================================================================
# Import Libraries
import math
def Model(x):
dimensionality = len(x)
# Define objective function
def objective(x: List[float], dimensionality: int) -> float:
fitnessValue = 40
for i in range(4):
fitnessValue += x[i]*x[i] - 10*math.cos(2*math.pi*x[i])
return fitnessValue
constraints = []
# Define penalty parameters
penalty_type = "No_Penalty" # Options: No_Penalty, Death_Penalty, Static_Penalty, Debs_Penalty
# penalty_coefficients = [0.0] # Make sure number of entries are equivalent to number of constraints
if penalty_type == "No_Penalty":
########### Using No-Penalty ###########
problem = OptimizationProblem(objective, constraints, PenaltyType.No_Penalty)
elif penalty_type == "Death_Penalty":
########### Using Death Penalty ############
problem = OptimizationProblem(objective, constraints, PenaltyType.Death_Penalty)
elif penalty_type == "Static_Penalty":
########### Using Static Penalty with a fixed penalty value ############
problem = OptimizationProblem(objective, constraints, PenaltyType.Static_Penalty, penalty_coefficients)
elif penalty_type == "Debs_Penalty":
########### Using Deb's approach ###########
problem = OptimizationProblem(objective, constraints, PenaltyType.Debs_Penalty)
return problem.evaluate_objective(x, dimensionality)