binomial_crossover


x_i_new, of_i_new, fit_i_new, neof, report = binomial_crossover(of_function, parent_0, parent_1, p_c, n_dimensions, x_upper, x_lower, none_variable=None)

Input variables

Name Description Type
of_function Objective function. The Metapy user defined this function Py function (def)
parent_0 Current design variables of the first parent List
parent_1 Current design variables of the second parent List
p_c Crossover probability rate (% * 0.01) Float
n_dimensions Problem dimension Integer
x_upper Upper limit of the design variables List
x_lower Represents the lower limits of the range for the problem variables. List
none_variable None variable. Default is None. Use in objective function None, List, Float, Dictionary, String or any

Output variables

Name Description Type
x_i_new Update variables of the i agent L'ist
of_i_new Update objective function value of the i agent Float
fit_i_new Update fitness value of the i agent Float
neof New solution indicator. It is a Boolean value (1 to indicate a new solution) Integer
report Report about the crossover process String

Example 1

from metapy_toolbox import binomial_crossover

# Data
father1 = [4.3, 3.1, 4.8, 3.5, 4.8]
father2 = [3.9, 4.0, 1.6, 1.7, 4.2]
p_c = 0.30
nDimensions = len(father1)
xUpper = [5, 5, 5, 5, 5]
xLower = [1, 1, 1, 1, 1]
noneVariable = None

# Objective function
def objFunction(x, _):
    """Example objective function"""
    x0 = x[0]
    x1 = x[1]
    of = x0 ** 2 + x1 ** 2
    return of

# Call function
xNew, ofNew, fitNew, neof, report = binomial_crossover(objFunction, father1, father2, p_c, nDimensions, xUpper, xLower, noneVariable)

# Output details
print('x new ', xNew)
print('of new ', ofNew)
print('fit new', fitNew)
print('number of evalutions objective function', neof)
x new  [4.3, 3.1, 1.6, 1.7, 4.2]
of new  28.1
fit new 0.034364261168384876
number of evalutions objective function 2

To check the movement report just apply the following instruction.

# Report details
arq = "report_example.txt"

# Writing report
with open(arq, "w") as file:
    file.write(report)

Open report_example.txt.

    Crossover operator - uniform crossover
    current p0 = [4.3, 3.1, 4.8, 3.5, 4.8]
    current p1 = [3.9, 4.0, 1.6, 1.7, 4.2]
    random number = 0.24575911573158438 < p_c = 0.3
    cut parent_0 -> of_a 4.3
    cut parent_1 -> of_b 3.9
    random number = 0.2597531030020548 < p_c = 0.3
    cut parent_0 -> of_a 3.1
    cut parent_1 -> of_b 4.0
    random number = 0.4561316060102293 >= 0.50
    cut parent_1 -> of_a 1.6
    cut parent_0 -> of_b 4.8
    random number = 0.506621634648928 >= 0.50
    cut parent_1 -> of_a 1.7
    cut parent_0 -> of_b 3.5
    random number = 0.5382296135101402 >= 0.50
    cut parent_1 -> of_a 4.2
    cut parent_0 -> of_b 4.8
    offspring a = [4.3, 3.1, 1.6, 1.7, 4.2], of_a = 28.1
    offspring b = [3.9, 4.0, 4.8, 3.5, 4.8], of_b = 31.21
    update pos = [4.3, 3.1, 1.6, 1.7, 4.2], of = 28.1, fit = 0.034364261168384876