opfunu.benchmark module¶
- class opfunu.benchmark.Benchmark[source]¶
Bases:
objectDefines an abstract class for optimization benchmark problem.
All subclasses should implement the
evaluatemethod for a particular optimization problem.- bounds¶
The lower/upper bounds of the problem. This a 2D-matrix of [lower, upper] array that contain the lower and upper bounds. By default, each problem has its own bounds. But user can try to put different bounds to test the problem.
- Type
list
- ndim¶
The dimensionality of the problem. It is calculated from bounds
- Type
int
- lb¶
The lower bounds for the problem
- Type
np.ndarray
- ub¶
The upper bounds for the problem
- Type
np.ndarray
- f_global¶
The global optimum of the evaluated function.
- Type
float
- x_global¶
A list of vectors that provide the locations of the global minimum. Note that some problems have multiple global minima, not all of which may be listed.
- Type
np.ndarray
- n_fe¶
The number of function evaluations that the object has been asked to calculate.
- Type
int
- dim_changeable¶
Whether we can change the benchmark function x variable length (i.e., the dimensionality of the problem)
- Type
bool
- property bounds¶
The lower/upper bounds to be used for optimization problem. This a 2D-matrix of [lower, upper] array that contain the lower and upper bounds for the problem. The problem should not be asked for evaluation outside these bounds.
len(bounds) == ndim.
- check_ndim_and_bounds(ndim=None, bounds=None, default_bounds=None)[source]¶
Check the bounds when initializing the object.
- Parameters
ndim (int) – The number of dimensions (variables)
bounds (list, tuple, np.ndarray) – List of lower bound and upper bound, should use default None value
default_bounds (np.ndarray) – List of initial lower bound and upper bound values
- check_solution(x)[source]¶
Raise the error if the problem size is not equal to the solution length
- Parameters
x (np.ndarray) – The solution
- continuous = True¶
- convex = True¶
- create_solution() numpy.ndarray[source]¶
Create a random solution for the current problem
- Returns
solution – The random solution
- Return type
1D-vector
- differentiable = True¶
- evaluate(x)[source]¶
Evaluation of the benchmark function.
- Parameters
x (np.ndarray) – The candidate vector for evaluating the benchmark problem. Must have
len(x) == self.ndim.- Returns
val – the evaluated benchmark function
- Return type
float
- is_ndim_compatible(ndim)[source]¶
Method to support searching the functions with input ndim
- Parameters
ndim (int) – The number of dimensions
- Returns
val – Always true if dim_changeable = True, Else return ndim == self.ndim
- Return type
bool
- is_succeed(x, tol=1e-05)[source]¶
Check if a candidate solution at the global minimum.
- Parameters
x (np.ndarray) – The candidate vector for testing if the global minimum has been reached. Must have
len(x) == self.ndimtol (float) – The evaluated function and known global minimum must differ by less than this amount to be at a global minimum.
- Returns
is_succeed – Answer the question: is the candidate vector at the global minimum?
- Return type
bool
- latex_formula = 'f(\\mathbf{x})'¶
- latex_formula_bounds = 'x_i \\in [-2\\pi, 2\\pi], \\forall i \\in \\llbracket 1, d\\rrbracket'¶
- latex_formula_dimension = 'd \\in \\mathbb{N}_{+}^{*}'¶
- latex_formula_global_optimum = 'f(0, ..., 0)=-1, \\text{ for}, m=5, \\beta=15'¶
- property lb¶
The lower bounds for the problem
- Returns
lb – The lower bounds for the problem
- Return type
1D-vector
- linear = False¶
- modality = True¶
- name = 'Benchmark name'¶
- property ndim¶
The dimensionality of the problem.
- Returns
ndim – The dimensionality of the problem
- Return type
int
- parametric = True¶
- plot_2d(selected_dims=None, n_points=500, ct_cmap='viridis', ct_levels=30, ct_alpha=0.7, fixed_strategy='mean', fixed_values=None, title='Contour map of the function', x_label=None, y_label=None, figsize=(10, 8), filename=None, exts=('.png', '.pdf'), verbose=True)[source]¶
Draw 2D contour of the function.
- Parameters
selected_dims (list, tuple, np.ndarray) – The selected dimensions you want to draw. If your function has only 2 dimensions, it will select (1, 2) automatically.
n_points (int) – The number of points that will be used to draw the contour
ct_cmap (str) – The cmap of matplotlib.pyplot.contourf function (https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.contourf.html)
ct_levels (int) – The levels parameter of contourf function
ct_alpha (float) – The alpha parameter of contourf function
fixed_strategy (str) –
The selected strategy to set values for other dimensions. When your function has > 2 dimensions, you need to set a fixed value for other dimensions to be able to calculate value. List of available strategy: [“min”, “max”, “mean’, “values”, “zero”]
min: Set the other dimensions by its lower bound
max: Set the other dimensions by its upper bound
mean: Set the other dimensions by it average value (lower bound + upper bound) / 2
zero: Set the other dimensions by 0
values: Set the other dimensions by your passed values through the parameter: fixed_values.
fixed_values (list, tuple, np.ndarray) – Fixed values for all dimensions (length should be the same as lower bound), the selected dimensions will be replaced in the drawing process.
title (str) – Title for the figure
x_label (str) – Set the x label
y_label (str) – Set the y label
figsize (tuple, default=(10, 8)) – The figure size with format of tuple (width, height)
filename (str, default = None) – Set the file name, If None, the file will not be saved
exts (list, tuple, np.ndarray) – The list of extensions to save file, for example: (“.png”, “.pdf”, “.jpg”)
verbose (bool) – Show the figure or not. It will not show on linux system.
- plot_3d(selected_dims=None, n_points=500, ct_cmap='viridis', ct_levels=30, ct_alpha=0.7, fixed_strategy='mean', fixed_values=None, title='3D visualization of the function', x_label=None, y_label=None, figsize=(10, 8), filename=None, exts=('.png', '.pdf'), verbose=True)[source]¶
Draw 3D of the function.
- Parameters
selected_dims (list, tuple, np.ndarray) – The selected dimensions you want to draw. If your function has only 2 dimensions, it will select (1, 2) automatically.
n_points (int) – The number of points that will be used to draw the contour
ct_cmap (str) – The cmap of matplotlib.pyplot.contourf function (https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.contourf.html)
ct_levels (int) – The levels parameter of contourf function
ct_alpha (float) – The alpha parameter of contourf function
fixed_strategy (str) –
The selected strategy to set values for other dimensions. When your function has > 2 dimensions, you need to set a fixed value for other dimensions to be able to calculate value. List of available strategy: [“min”, “max”, “mean’, “values”, “zero”]
min: Set the other dimensions by its lower bound
max: Set the other dimensions by its upper bound
mean: Set the other dimensions by it average value (lower bound + upper bound) / 2
zero: Set the other dimensions by 0
values: Set the other dimensions by your passed values through the parameter: fixed_values.
fixed_values (list, tuple, np.ndarray) – Fixed values for all dimensions (length should be the same as lower bound), the selected dimensions will be replaced in the drawing process.
title (str) – Title for the figure
x_label (str) – Set the x label
y_label (str) – Set the y label
figsize (tuple, default=(10, 8)) – The figure size with format of tuple (width, height)
filename (str, default = None) – Set the file name, If None, the file will not be saved
exts (list, tuple, np.ndarray) – The list of extensions to save file, for example: (“.png”, “.pdf”, “.jpg”)
verbose (bool) – Show the figure or not. It will not show on linux system.
- randomized_term = False¶
- scalable = True¶
- separable = False¶
- property ub¶
The upper bounds for the problem
- Returns
ub – The upper bounds for the problem
- Return type
1D-vector
- unimodal = False¶