multi_point_crossover
x_i_new, of_i_new, fit_i_new, neof, report = multi_point_crossover(of_function, parent_0, parent_1, n_dimensions, x_upper, x_lower, none_variable=None)
This function performs the multi point 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 |
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 multi_point_crossover
# Data
father1 = [1.7, 3.6, 2.7, 1.5, 1.3]
father2 = [3.9, 1.6, 2.5, 3.2, 3.3]
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 = multi_point_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 [1.7, 1.6, 1.7, 1.6, 1.7]
of new 5.45
fit new 0.15503875968992248
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 - multi point crossover
current p0 = [1.7, 3.6, 2.7, 1.5, 1.3]
current p1 = [3.9, 1.6, 2.5, 3.2, 3.3]
cut mask = [0, 1, 0, 1, 0]
offspring a = [1.7, 1.6, 1.7, 1.6, 1.7], of_a = 5.45
offspring b = [3.9, 3.6, 3.9, 3.6, 3.9], of_b = 28.17
update pos = [1.7, 1.6, 1.7, 1.6, 1.7], of = 5.45, fit = 0.15503875968992248