heuristic_crossover
x_i_new, of_i_new, fit_i_new, neof, report = heuristic_crossover(of_function, parent_0, parent_1, n_dimensions, x_upper, x_lower, none_variable=None)
This function performs the heuristic 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 male movement process | String |
Example 1
from metapy_toolbox import heuristic_crossover
# Data
father1 = [1.8, 1.9, 3.0, 4.1, 4.5]
father2 = [2.7, 4.6, 2.7, 3.5, 3.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 = heuristic_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.437692380167753, 1.7086497918174648, 3.2232816656355117, 4.536070168960613, 5.0]
of new 4.986443491070284
fit new 0.167044089114289
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 - Heuristic crossover
current p0 = [1, 1, 1, 1, 1]
current p1 = [10, 10, 10, 10, 10]
random number = 0.7812494156747201
neighbor_a = -6.031244741072481, neighbor_b = 17.03124474107248
random number = 0.11545651624084319
neighbor_a = -0.03910864616758869, neighbor_b = 11.03910864616759
random number = 0.9377837297687475
neighbor_a = -7.440053567918728, neighbor_b = 18.440053567918728
random number = 0.3386905662828823
neighbor_a = -2.048215096545941, neighbor_b = 13.048215096545942
random number = 0.37050392454406256
neighbor_a = -2.334535320896563, neighbor_b = 13.334535320896563
offspring a = [1.0, 1.0, 1.0, 1.0, 1.0], of_a = 5.0
offspring b = [10.0, 10.0, 10.0, 10.0, 10.0], of_b = 50.0
update pos = [1.0, 1.0, 1.0, 1.0, 1.0], of = 5.0, fit = 0.16666666666666666