simulated_binary_crossover


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

This function performs the simulated binary crossover operator. Two new points are generated from the two parent points (offspring).

Input variables

Name Description Type
of_function Objective function Py function (def)
parent_0 Current design variables of the first parent List
parent_1 Current design variables of the second parent List
eta_c Distribution index List
n_dimensions Problem dimension 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 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 simulated_binary_crossover

# Data
father1 = [3.8, 3.0, 2.7, 3.6, 4.5]
father2 = [4.3, 2.5, 2.1, 1.1, 1.8]
eta_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 = simulated_binary_crossover(objFunction, father1, father2, eta_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  [3.6371818435745453, 2.81116885353825, 2.4485231406036525, 3.520665834632936, 5.0]
of new  21.13176208633189
fit new 0.04518393050219797
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 - simulated binary crossover
    current p0 = [3.8, 3.0, 2.7, 3.6, 4.5]
    current p1 = [4.3, 2.5, 2.1, 1.1, 1.8]
    random number = 0.7395012830572558 > 0.50, beta = 1.6512726257018162
    neighbor_a 3.6371818435745453
    neighbor_b 3.6371818435745453
    random number = 0.08019318011743659 <= 0.50, beta = 0.24467541415300118
    neighbor_a 2.81116885353825
    neighbor_b 2.81116885353825
    random number = 0.04682156719983155 <= 0.50, beta = 0.16174380201217442
    neighbor_a 2.4485231406036525
    neighbor_b 2.4485231406036525
    random number = 0.4591449525617629 <= 0.50, beta = 0.9365326677063486
    neighbor_a 3.520665834632936
    neighbor_b 3.520665834632936
    random number = 0.8856693688660848 > 0.50, beta = 3.1112060299510342
    neighbor_a 7.350128140433897
    neighbor_b 7.350128140433897
    offspring a = [3.6371818435745453, 2.81116885353825, 2.4485231406036525, 3.520665834632936, 5.0], of_a = 21.13176208633189
    offspring b = [3.6371818435745453, 2.81116885353825, 2.4485231406036525, 3.520665834632936, 5.0], of_b = 21.13176208633189
    update pos = [3.6371818435745453, 2.81116885353825, 2.4485231406036525, 3.520665834632936, 5.0], of = 21.13176208633189, fit = 0.04518393050219797