Skip to content

Commit

Permalink
Merge pull request #19 from firestrand/bugfix_cec_3
Browse files Browse the repository at this point in the history
Bugfix remaining cec functions.
  • Loading branch information
thieu1995 authored Sep 20, 2023
2 parents 045b71f + be53f3c commit 7685aac
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 143 deletions.
58 changes: 19 additions & 39 deletions opfunu/cec_based/cec2005.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,8 @@ def __init__(self, ndim=None, bounds=None, f_shift="data_high_cond_elliptic_rot"
def evaluate(self, x, *args):
self.n_fe += 1
self.check_solution(x, self.dim_max, self.dim_supported)
ndim = len(x)
z = (np.dot((x - self.f_shift), self.f_matrix)) ** 2
results = [(10 ** 6) ** (idx / (ndim - 1)) * z[idx] ** 2 for idx in range(0, ndim)]
return np.sum(results) + self.f_bias
z = (np.dot((x - self.f_shift), self.f_matrix))
return operator.elliptic_func(z) + self.f_bias


class F42005(CecBenchmark):
Expand Down Expand Up @@ -316,10 +314,7 @@ def __init__(self, ndim=None, bounds=None, f_shift="data_rosenbrock", f_bias=390
def evaluate(self, x, *args):
self.n_fe += 1
self.check_solution(x, self.dim_max, self.dim_supported)
ndim = len(x)
z = x - self.f_shift + 1
results = [(100 * (z[idx] ** 2 - z[idx + 1]) ** 2 + (z[idx] - 1) ** 2) for idx in range(0, ndim - 1)]
return np.sum(results) + self.f_bias
return operator.rosenbrock_func(x - self.f_shift, shift=1.0) + self.f_bias


class F72005(CecBenchmark):
Expand Down Expand Up @@ -370,11 +365,8 @@ def __init__(self, ndim=None, bounds=None, f_shift="data_griewank", f_matrix="gr
def evaluate(self, x, *args):
self.n_fe += 1
self.check_solution(x, self.dim_max, self.dim_supported)
ndim = len(x)
z = np.dot((x - self.f_shift), self.f_matrix)
vt1 = np.sum(z ** 2) / 4000 + 1
results = [np.cos(z[idx] / np.sqrt(idx + 1)) for idx in range(0, ndim)]
return vt1 - np.sum(results) + self.f_bias
return operator.griewank_func(z) + self.f_bias


class F82005(CecBenchmark):
Expand Down Expand Up @@ -428,10 +420,8 @@ def __init__(self, ndim=None, bounds=None, f_shift="data_ackley", f_matrix="ackl
def evaluate(self, x, *args):
self.n_fe += 1
self.check_solution(x, self.dim_max, self.dim_supported)
ndim = len(x)
z = np.dot((x - self.f_shift), self.f_matrix)
result = -20 * np.exp(-0.2 * np.sqrt(np.sum(z ** 2) / ndim)) - np.exp(np.sum(np.cos(2 * np.pi * z)) / ndim)
return result + 20 + np.e + self.f_bias
return operator.ackley_func(z) + self.f_bias


class F92005(CecBenchmark):
Expand Down Expand Up @@ -484,7 +474,7 @@ def evaluate(self, x, *args):
self.n_fe += 1
self.check_solution(x, self.dim_max, self.dim_supported)
z = x - self.f_shift
return np.sum(z**2 - 10*np.cos(2*np.pi*z) + 10) + self.f_bias
return operator.rastrigin_func(z) + self.f_bias


class F102005(CecBenchmark):
Expand Down Expand Up @@ -536,7 +526,7 @@ def evaluate(self, x, *args):
self.n_fe += 1
self.check_solution(x, self.dim_max, self.dim_supported)
z = np.dot((x - self.f_shift), self.f_matrix)
return np.sum(z ** 2 - 10 * np.cos(2 * np.pi * z) + 10) + self.f_bias
return operator.rastrigin_func(z) + self.f_bias


class F112005(CecBenchmark):
Expand Down Expand Up @@ -592,12 +582,8 @@ def __init__(self, ndim=None, bounds=None, f_shift="data_weierstrass", f_matrix=
def evaluate(self, x, *args):
self.n_fe += 1
self.check_solution(x, self.dim_max, self.dim_supported)
ndim = len(x)
z = np.dot((x - self.f_shift), self.f_matrix)
k = np.arange(0, self.k_max+1)
result1 = [np.sum(self.a**k * np.cos(2*np.pi*self.b**k*(z[idx] + 0.5))) for idx in range(0, ndim)]
result2 = ndim * np.sum(self.a**k * np.cos(np.pi*self.b**k))
return np.sum(result1) - result2 + self.f_bias
return operator.weierstrass_norm_func(z, self.a, self.b, self.k_max) + self.f_bias


class F122005(CecBenchmark):
Expand Down Expand Up @@ -707,10 +693,7 @@ def __init__(self, ndim=None, bounds=None, f_shift="data_EF8F2", f_bias=-130.):
def evaluate(self, x, *args):
self.n_fe += 1
self.check_solution(x, self.dim_max, self.dim_supported)
ndim = len(x)
z = x - self.f_shift + 1
results = [self.f8__(self.f2__(z[idx:idx + 2])) for idx in range(0, ndim-1)]
return np.sum(results) + self.f8__(self.f2__([z[-1], z[0]])) + self.f_bias
return operator.grie_rosen_cec_func(x - self.f_shift) + self.f_bias


class F142005(CecBenchmark):
Expand Down Expand Up @@ -756,15 +739,12 @@ def __init__(self, ndim=None, bounds=None, f_shift="data_E_ScafferF6", f_matrix=
self.f_global = f_bias
self.x_global = self.f_shift
self.paras = {"f_shift": self.f_shift, "f_matrix": self.f_matrix, "f_bias": self.f_bias}
self.fxy__ = operator.scaffer_func

def evaluate(self, x, *args):
self.n_fe += 1
self.check_solution(x, self.dim_max, self.dim_supported)
ndim = len(x)
z = np.dot((x - self.f_shift), self.f_matrix)
results = [self.fxy__(z[idx:idx + 2]) for idx in range(0, ndim - 1)]
return np.sum(results) + self.fxy__([z[-1], z[0]]) + self.f_bias
return operator.expanded_schaffer_f6_func(z) + self.f_bias


class F152005(CecBenchmark):
Expand Down Expand Up @@ -821,7 +801,7 @@ def fi__(self, x, idx):
if idx == 0 or idx == 1:
return operator.rastrigin_func(x)
elif idx == 2 or idx == 3:
return operator.weierstrass_func(x)
return operator.weierstrass_norm_func(x)
elif idx == 4 or idx == 5:
return operator.griewank_func(x)
elif idx == 6 or idx == 7:
Expand Down Expand Up @@ -905,7 +885,7 @@ def fi__(self, x, idx):
if idx == 0 or idx == 1:
return operator.rastrigin_func(x)
elif idx == 2 or idx == 3:
return operator.weierstrass_func(x)
return operator.weierstrass_norm_func(x)
elif idx == 4 or idx == 5:
return operator.griewank_func(x)
elif idx == 6 or idx == 7:
Expand Down Expand Up @@ -1031,7 +1011,7 @@ def fi__(self, x, idx):
elif idx == 4 or idx == 5:
return operator.sphere_func(x)
elif idx == 6 or idx == 7:
return operator.weierstrass_func(x)
return operator.weierstrass_norm_func(x)
else:
return operator.griewank_func(x)

Expand Down Expand Up @@ -1151,13 +1131,13 @@ def __init__(self, ndim=None, bounds=None, f_shift="data_hybrid_func3", f_matrix

def fi__(self, x, idx):
if idx == 0 or idx == 1:
return operator.rotated_expanded_scaffer_func(x)
return operator.rotated_expanded_schaffer_func(x)
elif idx == 2 or idx == 3:
return operator.rastrigin_func(x)
elif idx == 4 or idx == 5:
return operator.f8f2_func(x)
return operator.grie_rosen_cec_func(x)
elif idx == 6 or idx == 7:
return operator.weierstrass_func(x)
return operator.weierstrass_norm_func(x)
else:
return operator.griewank_func(x)

Expand Down Expand Up @@ -1292,11 +1272,11 @@ def __init__(self, ndim=None, bounds=None, f_shift="data_hybrid_func4", f_matrix

def fi__(self, x, idx):
if idx == 0:
return operator.weierstrass_func(x)
return operator.weierstrass_norm_func(x)
elif idx == 1:
return operator.rotated_expanded_scaffer_func(x)
return operator.rotated_expanded_schaffer_func(x)
elif idx == 2:
return operator.f8f2_func(x)
return operator.grie_rosen_cec_func(x)
elif idx == 3:
return operator.ackley_func(x)
elif idx == 4:
Expand Down
20 changes: 6 additions & 14 deletions opfunu/cec_based/cec2008.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def __init__(self, ndim=None, bounds=None, f_shift="sphere_shift_func_data", f_b
def evaluate(self, x, *args):
self.n_fe += 1
self.check_solution(x, self.dim_max, self.dim_supported)
return np.sum((x - self.f_shift) ** 2) + self.f_bias
return operator.sphere_func(x - self.f_shift) + self.f_bias


class F22008(CecBenchmark):
Expand Down Expand Up @@ -147,10 +147,7 @@ def __init__(self, ndim=None, bounds=None, f_shift="rosenbrock_shift_func_data",
def evaluate(self, x, *args):
self.n_fe += 1
self.check_solution(x, self.dim_max, self.dim_supported)
ndim = len(x)
z = x - self.f_shift + 1
results = [100 * (z[idx] ** 2 - z[idx + 1]) ** 2 + (z[idx] - 1) ** 2 for idx in range(0, ndim-1)]
return np.sum(results) + self.f_bias
return operator.rosenbrock_func(x - self.f_shift, shift=1.0) + self.f_bias


class F42008(CecBenchmark):
Expand Down Expand Up @@ -198,7 +195,7 @@ def evaluate(self, x, *args):
self.n_fe += 1
self.check_solution(x, self.dim_max, self.dim_supported)
z = x - self.f_shift
return np.sum(z ** 2 - 10 * np.cos(2 * np.pi * z) + 10) + self.f_bias
return operator.rastrigin_func(z) + self.f_bias


class F52008(CecBenchmark):
Expand Down Expand Up @@ -245,11 +242,8 @@ def __init__(self, ndim=None, bounds=None, f_shift="griewank_shift_func_data", f
def evaluate(self, x, *args):
self.n_fe += 1
self.check_solution(x, self.dim_max, self.dim_supported)
ndim = len(x)
z = x - self.f_shift
t1 = np.sum(z ** 2) / 4000
t2 = np.prod([np.cos(z[idx] / np.sqrt(idx + 1)) for idx in range(0, ndim)])
return t1 - t2 + 1 + self.f_bias
return operator.griewank_func(z) + self.f_bias


class F62008(CecBenchmark):
Expand Down Expand Up @@ -296,10 +290,8 @@ def __init__(self, ndim=None, bounds=None, f_shift="ackley_shift_func_data", f_b
def evaluate(self, x, *args):
self.n_fe += 1
self.check_solution(x, self.dim_max, self.dim_supported)
ndim = len(x)
z = x - self.f_shift
return -20 * np.exp(-0.2*np.sqrt(np.sum(z**2)/ndim)) - np.exp(np.sum(np.cos(2*np.pi*z))/ndim) + 20 + np.e + self.f_bias

return operator.ackley_func(z) + self.f_bias

class F72008(CecBenchmark):
"""
Expand All @@ -311,7 +303,7 @@ class F72008(CecBenchmark):
latex_formula = r'F_1(x) = \sum_{i=1}^D z_i^2 + bias, z=x-o,\\ x=[x_1, ..., x_D]; o=[o_1, ..., o_D]: \text{the shifted global optimum}'
latex_formula_dimension = r'2 <= D <= 100'
latex_formula_bounds = r'x_i \in [-100.0, 100.0], \forall i \in [1, D]'
latex_formula_global_optimum = r'\text{Global optimum: } x^* = o, F_1(x^*) = bias = -450.0'
latex_formula_global_optimum = r'\text{Global optimum: } x^* = unknown, F_1(x^*) = unknown'
continuous = True
linear = False
convex = False
Expand Down
77 changes: 47 additions & 30 deletions opfunu/cec_based/cec2014.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ def __init__(self, ndim=None, bounds=None, f_shift="shift_data_4", f_matrix="M_4
def evaluate(self, x, *args):
self.n_fe += 1
self.check_solution(x, self.dim_max, self.dim_supported)
z = np.dot(self.f_matrix, 2.048*(x - self.f_shift)/100) + 1
return operator.rosenbrock_func(z) + self.f_bias
z = np.dot(self.f_matrix, 2.048*(x - self.f_shift)/100)
return operator.rosenbrock_func(z, shift=1.0) + self.f_bias


class F52014(F12014):
Expand Down Expand Up @@ -187,7 +187,7 @@ def evaluate(self, x, *args):
self.n_fe += 1
self.check_solution(x, self.dim_max, self.dim_supported)
z = np.dot(self.f_matrix, 0.5*(x - self.f_shift)/100)
return operator.weierstrass_func(z) + self.f_bias
return operator.weierstrass_norm_func(z) + self.f_bias


class F72014(F12014):
Expand Down Expand Up @@ -410,7 +410,7 @@ def evaluate(self, x, *args):
self.n_fe += 1
self.check_solution(x, self.dim_max, self.dim_supported)
z = np.dot(self.f_matrix, 5*(x - self.f_shift)/100)
return operator.happy_cat_func(z) + self.f_bias
return operator.happy_cat_func(z, shift=-1.0) + self.f_bias


class F142014(F12014):
Expand Down Expand Up @@ -441,7 +441,7 @@ def evaluate(self, x, *args):
self.n_fe += 1
self.check_solution(x, self.dim_max, self.dim_supported)
z = np.dot(self.f_matrix, 5*(x - self.f_shift)/100)
return operator.hgbat_func(z) + self.f_bias
return operator.hgbat_func(z, shift=-1.0) + self.f_bias


class F152014(F12014):
Expand Down Expand Up @@ -469,7 +469,7 @@ def __init__(self, ndim=None, bounds=None, f_shift="shift_data_15", f_matrix="M_
def evaluate(self, x, *args):
self.n_fe += 1
self.check_solution(x, self.dim_max, self.dim_supported)
z = np.dot(self.f_matrix, 5*(x - self.f_shift)/100) + 1
z = np.dot(self.f_matrix, 5*(x - self.f_shift)/100)
return operator.expanded_griewank_rosenbrock_func(z) + self.f_bias


Expand Down Expand Up @@ -601,9 +601,16 @@ class F182014(F172014):

def __init__(self, ndim=None, bounds=None, f_shift="shift_data_18", f_matrix="M_18_D", f_shuffle="shuffle_data_18_D", f_bias=1800.):
super().__init__(ndim, bounds, f_shift, f_matrix, f_shuffle, f_bias)
self.g1 = operator.bent_cigar_func
self.g2 = operator.hgbat_func
self.g3 = operator.rastrigin_func

def evaluate(self, x, *args):
self.n_fe += 1
self.check_solution(x, self.dim_max, self.dim_supported)
z = x - self.f_shift
z1 = np.concatenate((z[self.idx1], z[self.idx2], z[self.idx3]))
mz = np.dot(self.f_matrix, z1)
return (operator.bent_cigar_func(mz[:self.n1]) +
operator.hgbat_func(mz[self.n1:self.n2], shift=-1.0) +
operator.rastrigin_func(mz[self.n2:]) + self.f_bias)


class F192014(F172014):
Expand Down Expand Up @@ -646,18 +653,17 @@ def __init__(self, ndim=None, bounds=None, f_shift="shift_data_19", f_matrix="M_
self.n3 = int(np.ceil(self.p[2] * self.ndim)) + self.n2
self.idx1, self.idx2 = self.f_shuffle[:self.n1], self.f_shuffle[self.n1:self.n2]
self.idx3, self.idx4 = self.f_shuffle[self.n2:self.n3], self.f_shuffle[self.n3:self.ndim]
self.g1 = operator.griewank_func
self.g2 = operator.weierstrass_func
self.g3 = operator.rosenbrock_func
self.g4 = operator.expanded_scaffer_f6_func

def evaluate(self, x, *args):
self.n_fe += 1
self.check_solution(x, self.dim_max, self.dim_supported)
z = x - self.f_shift
z1 = np.concatenate((z[self.idx1], z[self.idx2], z[self.idx3], z[self.idx4]))
mz = np.dot(self.f_matrix, z1)
return self.g1(mz[:self.n1]) + self.g2(mz[self.n1:self.n2]) + self.g3(mz[self.n2:self.n3]) + self.g4(mz[self.n3:]) + self.f_bias
return (operator.griewank_func(mz[:self.n1]) +
operator.weierstrass_func(mz[self.n1:self.n2]) +
operator.rosenbrock_func(mz[self.n2:self.n3], shift=1.0) +
operator.expanded_scaffer_f6_func(mz[self.n3:]) + self.f_bias)


class F202014(F192014):
Expand Down Expand Up @@ -693,11 +699,17 @@ class F202014(F192014):

def __init__(self, ndim=None, bounds=None, f_shift="shift_data_20", f_matrix="M_20_D", f_shuffle="shuffle_data_20_D", f_bias=2000.):
super().__init__(ndim, bounds, f_shift, f_matrix, f_shuffle, f_bias)
self.g1 = operator.hgbat_func
self.g2 = operator.discus_func
self.g3 = operator.expanded_griewank_rosenbrock_func
self.g4 = operator.rastrigin_func

def evaluate(self, x, *args):
self.n_fe += 1
self.check_solution(x, self.dim_max, self.dim_supported)
z = x - self.f_shift
z1 = np.concatenate((z[self.idx1], z[self.idx2], z[self.idx3], z[self.idx4]))
mz = np.dot(self.f_matrix, z1)
return (operator.hgbat_func(mz[:self.n1], shift=-1.0) +
operator.discus_func(mz[self.n1:self.n2]) +
operator.expanded_griewank_rosenbrock_func(mz[self.n2:self.n3]) +
operator.rastrigin_func(mz[self.n3:]) + self.f_bias)

class F212014(F172014):
"""
Expand Down Expand Up @@ -740,20 +752,19 @@ def __init__(self, ndim=None, bounds=None, f_shift="shift_data_21", f_matrix="M_
self.n4 = int(np.ceil(self.p[3] * self.ndim)) + self.n3
self.idx1, self.idx2, self.idx3 = self.f_shuffle[:self.n1], self.f_shuffle[self.n1:self.n2], self.f_shuffle[self.n2:self.n3]
self.idx4, self.idx5 = self.f_shuffle[self.n3:self.n4], self.f_shuffle[self.n4:self.ndim]
self.g1 = operator.expanded_scaffer_f6_func
self.g2 = operator.hgbat_func
self.g3 = operator.rosenbrock_func
self.g4 = operator.modified_schwefel_func
self.g5 = operator.elliptic_func


def evaluate(self, x, *args):
self.n_fe += 1
self.check_solution(x, self.dim_max, self.dim_supported)
z = x - self.f_shift
z1 = np.concatenate((z[self.idx1], z[self.idx2], z[self.idx3], z[self.idx4], z[self.idx5]))
mz = np.dot(self.f_matrix, z1)
return self.g1(mz[:self.n1]) + self.g2(mz[self.n1:self.n2]) + self.g3(mz[self.n2:self.n3]) +\
self.g4(mz[self.n3:self.n4]) + self.g5(mz[self.n4:]) + self.f_bias
return (operator.expanded_scaffer_f6_func(mz[:self.n1]) +
operator.hgbat_func(mz[self.n1:self.n2], shift=-1.0) +
operator.rosenbrock_func(mz[self.n2:self.n3], shift=1.0) +
operator.modified_schwefel_func(mz[self.n3:self.n4]) +
operator.elliptic_func(mz[self.n4:]) + self.f_bias)


class F222014(F212014):
Expand Down Expand Up @@ -789,12 +800,18 @@ class F222014(F212014):

def __init__(self, ndim=None, bounds=None, f_shift="shift_data_22", f_matrix="M_22_D", f_shuffle="shuffle_data_22_D", f_bias=2200.):
super().__init__(ndim, bounds, f_shift, f_matrix, f_shuffle, f_bias)
self.g1 = operator.katsuura_func
self.g2 = operator.happy_cat_func
self.g3 = operator.expanded_griewank_rosenbrock_func
self.g4 = operator.modified_schwefel_func
self.g5 = operator.ackley_func

def evaluate(self, x, *args):
self.n_fe += 1
self.check_solution(x, self.dim_max, self.dim_supported)
z = x - self.f_shift
z1 = np.concatenate((z[self.idx1], z[self.idx2], z[self.idx3], z[self.idx4], z[self.idx5]))
mz = np.dot(self.f_matrix, z1)
return (operator.katsuura_func(mz[:self.n1]) +
operator.happy_cat_func(mz[self.n1:self.n2], shift=-1.0) +
operator.expanded_griewank_rosenbrock_func(mz[self.n2:self.n3]) +
operator.modified_schwefel_func(mz[self.n3:self.n4]) +
operator.ackley_func(mz[self.n4:]) + self.f_bias)

class F232014(CecBenchmark):
"""
Expand Down
Loading

0 comments on commit 7685aac

Please sign in to comment.