uniform_crossover


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

Uniform crossover operator.

Input variables

Name Description Type
of_function Is the objective function that will be used to evaluate the result of the crossover Py function (def)
parent_0 Represents the first parent for the crossover List
parent_1 Represents the second parent for the crossover List
n_dimensions Number of dimensions Integer
x_upper Upper limit of the design variables. List
x_lower Lower limit of the design 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. List
of_i_new Update objective function value of the i agent. Float
fit_i_new Update fitness value of the i agent. Float
neof Number of evaluations of the objective function. Integer
report Report about the crossover process String

Example 1

# Import
from metapy_toolbox import uniform_crossover

# Data
father1 = [2.9, 4.3, 1.5, 5.0, 3.5]
father2 = [3.9, 1.2, 2.4, 2.7, 2.7]
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 = uniform_crossover(objFunction, father1, father2, 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  [3.9, 1.2, 1.5, 2.7, 3.5]
of new  16.65
fit new 0.056657223796034
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 = [2.9, 4.3, 1.5, 5.0, 3.5]
    current p1 = [3.9, 1.2, 2.4, 2.7, 2.7]
    random number = 0.23851568162300685 < 0.50
    cut parent_0 -> of_a 2.9
    cut parent_1 -> of_b 3.9
    random number = 0.26714707277389294 < 0.50
    cut parent_0 -> of_a 4.3
    cut parent_1 -> of_b 1.2
    random number = 0.7958990231757312 >= 0.50
    cut parent_1 -> of_a 2.4
    cut parent_0 -> of_b 1.5
    random number = 0.15613086364369877 < 0.50
    cut parent_0 -> of_a 5.0
    cut parent_1 -> of_b 2.7
    random number = 0.7254672965043522 >= 0.50
    cut parent_1 -> of_a 2.7
    cut parent_0 -> of_b 3.5
    offspring a = [2.9, 4.3, 2.4, 5.0, 2.7], of_a = 26.9
    offspring b = [3.9, 1.2, 1.5, 2.7, 3.5], of_b = 16.65
    update pos = [3.9, 1.2, 1.5, 2.7, 3.5], of = 16.65, fit = 0.056657223796034