A Python implementation of the CEC 2017 and CEC 2022 single objective optimization benchmark functions. The package currently provides only two-dimensional (2D) implementations.
Important
Due to instability, the second benchmark function was excluded from the CEC2017 implementation, resulting in an index shift of one for all subsequent functions. The indexing is based on the paper: Problem Definitions and Evaluation Criteria for the CEC 2017 Special Session and Competition on Single Objective Real-Parameter Numerical Optimization. Link
from Functions import FunctionsF = Functions()- Official functions (Functions natively defined in 2D according to the original paper):
- CEC2017: C17_1 ↔ C17_9 | C17_20 ↔ C17_27
- CEC2022: C22_1 ↔ C22_5 | C22_9 ↔ C22_12
- Unofficial functions (Higher-dimensional functions adapted for 2D inputs; Hybrid functions)
- CEC2017: U_C17_10 ↔ U_C17_19 | U_C17_28 ↔ U_C17_29
- CEC2022: U_C22_6 ↔ U_C22_8
- Adjusted CEC2022 composite functions to match the surface plots in the paper (unofficial; uses different sigma and lambda values)
- C22_9_Alt ↔ C22_12_Alt
Tip
The functions and their optimal values can also be accessed via arrays!
F.CEC2017 | F.CEC2017_Opts - Official CEC2017 functions
F.CEC2017_Unofficial | F.CEC2017_Unofficial_Opts - Unofficial CEC2017 functions
F.CEC2022 | F.CEC2022_Opts - Official CEC2022 functions
F.CEC2022_Alt | F.CEC2022_Alt_Opts - Adjusted CEC2022 composite functions
F.CEC2022_Unofficial | F.CEC2022_Unofficial_Opts - Unofficial CEC2022 functions
F.AllFunctions | F.AllOpts - All official functions (CEC2017+CEC2022)
F.AllFunctionsWithU | F.AllOptsWithU - All official, adjusted and unofficial functions
# Specifying X and Y coordinates in the -100 <= x <= 100 search range
X = 10
Y = -20
# Determining Z axis value with the selected benchmark function
Z = F.C17_3(X, Y)
print(Z)
# Getting the global optimum value for the specific function
optimum = F.C17_3_Opt
print(optimum)
# Getting the difference between the global optimum and the calculated value Z (absolute error)
Err = abs(Z - optimum)
print(Err)from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
from Functions import Functions
F = Functions()
x = np.linspace(-100, 100, 1000)
y = np.linspace(-100, 100, 1000)
X, Y = np.meshgrid(x, y)
Z = np.empty_like(X)
for i in range(len(X[0])):
for j in range(len(X[0])):
Z[i,j] = F.C22_2(X[i,j],Y[i,j])
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
surf = ax.plot_surface(X, Y, Z, cmap='plasma')
fig.colorbar(surf, shrink=0.5, aspect=5)
ax.xaxis.pane.set_facecolor((1.0, 1.0, 1.0, 0.0))
ax.yaxis.pane.set_facecolor((1.0, 1.0, 1.0, 0.0))
ax.zaxis.pane.set_facecolor((1.0, 1.0, 1.0, 0.0))
ax.set_facecolor((1.0, 1.0, 1.0, 0.0))
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.grid(True)
# contour
fig2 = plt.figure()
ax2 = fig2.add_subplot(111)
cf = ax2.contourf(X, Y, Z, levels=200, cmap='viridis')
cs = ax2.contour(X, Y, Z, levels=15, cmap='autumn', linewidths=0.5)
fig2.colorbar(cf)
ax2.set_xlabel("X")
ax2.set_ylabel("Y")
ax2.set_aspect('equal')
fig2.show()
print(np.max(Z))
plt.show()
























































































