single_point_crossover
Single point crossover operator.
x_i_new, of_i_new, fit_i_new, neof, report = single_point_crossover(of_function, parent_0, parent_1, n_dimensions, x_upper, x_lower, none_variable=None)
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_move | Report about the male moviment process. | String |
Example 1
# Import
from metapy_toolbox import single_point_crossover
# Data
father1 = [3.7, 3.9, 1.8, 3.7, 3.1]
father2 = [1.3, 4.9, 1.7, 3.9, 3.4]
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 = single_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.3, 3.9, 1.8, 3.7, 3.1]
of new 16.9
fit new 0.0558659217877095
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 - Single point
current p0 = [3.7, 3.9, 1.8, 3.7, 3.1]
current p1 = [1.3, 4.9, 1.7, 3.9, 3.4]
cut position 1
cut parent_0 -> of_a [3.7]
cut parent_1 -> of_a [4.9, 1.7, 3.9, 3.4]
cut parent_1 -> of_b [1.3]
cut parent_0 -> of_b [3.9, 1.8, 3.7, 3.1]
offspring a = [3.7, 4.9, 1.7, 3.9, 3.4], of_a = 37.7
offspring b = [1.3, 3.9, 1.8, 3.7, 3.1], of_b = 16.9
update n_dimensions = [1.3, 3.9, 1.8, 3.7, 3.1], of = 16.9, fit = 0.0558659217877095