mutation_01_hill_movement
This function mutates a solution using a Gaussian or Uniform distribution. Hill Climbing movement.
x_i_new, of_i_new,\
fit_i_new, neof = mutation_01_hill_movement(obj_function, x_i_old,
x_lower, x_upper,
n_dimensions,
pdf, cov,
none_variable=None)
Input variables
Name | Description | Type |
---|---|---|
obj_function | Objective function. The Metapy user defined this function | Py function (def) |
x_i_old | Current design variables of the \(i\) agent | List |
x_lower | Lower limit of the design variables | List |
x_upper | Upper limit of the design variables | List |
n_dimensions | Problem dimension | Integer |
pdf | Probability density function used in random generator. Options: 'gaussian' or 'uniform' | String |
cov | Coefficient of variation in percentage | Float |
none_variable | None variable. Default is None. User can use this variable in objective function | None, list, float, dictionary, str 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 mutation process | String |
Theory
See Hill Climbing movement.
Example 1
Use the mutation_01_hill_movement
function to generate a new solution from an existing solution, applying a coefficient of variation of 15% in current design variables. Use the range \(\mathbf{x}_L = [1.0, 1.0]\) and \(\mathbf{x}_L = [5.0, 5.0]\). Consider current solution \(\mathbf{x}_i = [2.0, 2.0]\). Use a uniform distribution to generate.
# Import
from metapy_toolbox import mutation_01_hill_movement # or import *
# Data
xL = [1, 1]
xU = [5, 5]
d = len(xL)
sigma = 15 # 15%
xI = [2, 2]
pdf = 'uniform'
# Objective function
def obj_function(x, _):
"""Example objective function"""
x0 = x[0]
x1 = x[1]
of = x0 ** 2 + x1 ** 2
return of
# Call function
xII, ofINew, fitINew, neof, report = mutation_01_hill_movement(obj_function, xI, xL, xU,
d, pdf, sigma)
# Output details
print('x New: ', xII)
print('of New: ', ofINew)
print('fit New: ', fitINew)
print('number of evalutions objective function: ', neof)
x New: [2.2555966876941307, 2.1080630093627852]
of New: 9.531646068980415
fit New: 0.09495191857475814
number of evalutions objective function: 1
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
.
current x = [2, 2]
Dimension 0: mean = 2, sigma = 0.3, neighbor = 2.2555966876941307
Dimension 1: mean = 2, sigma = 0.3, neighbor = 2.1080630093627852
update x = [2.2555966876941307, 2.1080630093627852], of = 9.531646068980415, fit = 0.09495191857475814