Skip to content

Commit

Permalink
Merge pull request #1461 from BenHBLiu/develop
Browse files Browse the repository at this point in the history
update
  • Loading branch information
weihuayi authored Jan 17, 2025
2 parents 5c33415 + 7acfdc4 commit 3bd5d12
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 129 deletions.
72 changes: 23 additions & 49 deletions fealpy/opt/crayfish_opt_alg.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,64 +23,38 @@ def __init__(self, option) -> None:


def run(self):

option = self.options
x = option["x0"]
N = option["NP"]
MaxIT = option["MaxIters"]
dim = option["ndim"]
lb, ub = option["domain"]

fit = self.fun(x)
fit = self.fun(self.x)
gbest_idx = bm.argmin(fit)
gbest_f = fit[gbest_idx]
gbest = x[gbest_idx]
fit_new = bm.zeros((N,))
curve = bm.zeros((1, MaxIT))
D_pl = bm.zeros((1, MaxIT))
D_pt = bm.zeros((1, MaxIT))
Div = bm.zeros((1, MaxIT))
global_position = gbest
global_fitness = gbest_f
for it in range(0, MaxIT):
Div[0, it] = bm.sum(bm.sum(bm.abs(bm.mean(x, axis=0) - x))/N)
# exploration percentage and exploitation percentage
D_pl[0, it] = 100 * Div[0, it] / bm.max(Div)
D_pt[0, it] = 100 * bm.abs(Div[0, it] - bm.max(Div)) / bm.max(Div)
self.gbest_f = fit[gbest_idx]
self.gbest = self.x[gbest_idx]
fit_new = bm.zeros((self.N,))
global_position = self.gbest
global_fitness = self.gbest_f
for it in range(0, self.MaxIT):
self.D_pl_pt(it)

C = 2 - (it / MaxIT)
C = 2 - (it / self.MaxIT)
temp = bm.random.rand(1) * 15 + 20
xf = (gbest + global_position) / 2
xf = (self.gbest + global_position) / 2
p = 0.2 * (1 / (bm.sqrt(bm.array(2 * bm.pi) * 3))) * bm.exp(bm.array(- (temp - 25) ** 2 / (2 * 3 ** 2)))
rand = bm.random.rand(N, 1)
rr = bm.random.rand(4, N, dim)

z = bm.random.randint(0, N, (N,))
rand = bm.random.rand(self.N, 1)

gbest = gbest.reshape(1, dim)
self.gbest = self.gbest.reshape(1, self.dim)

P = 3 * bm.random.rand(N) * fit / (gbest_f + 2.2204e-16)
P = 3 * bm.random.rand(self.N) * fit / (self.gbest_f + 2.2204e-16)

x_new = ((temp > 30) * ((rand < 0.5) * (x + C * bm.random.rand(N, dim) * (xf - x)) +
(rand > 0.5) * (x - x[z] + xf)) +
(temp <= 30) * ((P[:, None] > 2) * (x + bm.cos(2 * bm.random.rand(N, dim) * bm.pi) * gbest * p - bm.sin(2 * bm.pi * bm.random.rand(N, dim) * gbest * p)) +
(P[:, None] <= 2) * ((x - gbest) * p + p * bm.random.rand(N, dim) * x)))
x_new = x_new + (lb - x_new) * (x_new < lb) + (ub - x_new) * (x_new > ub)
x_new = ((temp > 30) * ((rand < 0.5) * (self.x + C * bm.random.rand(self.N, self.dim) * (xf - self.x)) +
(rand > 0.5) * (self.x - self.x[bm.random.randint(0, self.N, (self.N,))] + xf)) +
(temp <= 30) * ((P[:, None] > 2) * (self.x + bm.cos(2 * bm.random.rand(self.N, self.dim) * bm.pi) * self.gbest * p - bm.sin(2 * bm.pi * bm.random.rand(self.N, self.dim) * self.gbest * p)) +
(P[:, None] <= 2) * ((self.x - self.gbest) * p + p * bm.random.rand(self.N, self.dim) * self.x)))
x_new = x_new + (self.lb - x_new) * (x_new < self.lb) + (self.ub - x_new) * (x_new > self.ub)
fit_new = self.fun(x_new)
# for i in range(N):
# fit_new[i] = self.fun(x_new[i])
mask = fit_new < fit
x, fit = bm.where(mask[:, None], x_new, x), bm.where(mask, fit_new, fit)
self.x, fit = bm.where(mask[:, None], x_new, self.x), bm.where(mask, fit_new, fit)
newbest_id = bm.argmin(fit_new)
(global_position, global_fitness) = (x_new[newbest_id], fit_new[newbest_id]) if fit_new[newbest_id] < global_fitness else (global_position, global_fitness)
gbest_idx = bm.argmin(fit)
if fit[gbest_idx] < gbest_f:
gbest_f = fit[gbest_idx]
gbest = x[gbest_idx].reshape(1, dim)
gbest = gbest.flatten()
curve[0, it] = gbest_f
self.gbest = gbest
self.gbest_f = gbest_f
self.curve = curve[0]
self.D_pl = D_pl[0]
self.D_pt = D_pt[0]
self.update_gbest(self.x, fit)

self.gbest = self.gbest.flatten()
self.curve[it] = self.gbest_f
13 changes: 0 additions & 13 deletions fealpy/opt/exponential_trigonometric_opt_alg.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,10 @@ def __init__(self, option) -> None:
super().__init__(option)

def run(self):
# options = self.options
# x = options["x0"]
# N = options["NP"]
fit = self.fun(self.x)
# MaxIT = options["MaxIters"]
# dim = options["ndim"]
# lb, ub = options["domain"]
gbest_index = bm.argmin(fit)
self.gbest = self.x[gbest_index]
self.gbest_f = fit[gbest_index]
# self.curve = bm.zeros((MaxIT,))
# self.D_pl = bm.zeros((MaxIT,))
# self.D_pt = bm.zeros((MaxIT,))
# self.Div = bm.zeros((1, MaxIT))
gbest_second = bm.zeros((self.dim,))
# parameters
CEi = 0
Expand All @@ -44,9 +34,6 @@ def run(self):

for it in range(self.MaxIT):
self.D_pl_pt(it)
# self.Div[0, it] = bm.sum(bm.sum(bm.abs(bm.mean(x, axis=0) - x)) / N)
# # exploration percentage and exploitation percentage
# self.D_pl[it], self.D_pt[it] = self.D_pl_pt(self.Div[0, it])

d1 = 0.1 * bm.exp(bm.array(-0.01 * (it + 1))) * bm.cos(bm.array(0.5 * self.MaxIT * (1 - it / self.MaxIT))) # Eq.(10)
d2 = -0.1 * bm.exp(bm.array(-0.01 * (it + 1))) * bm.cos(bm.array(0.5 * self.MaxIT * (1 - it / self.MaxIT))) # Eq.(11)
Expand Down
13 changes: 0 additions & 13 deletions fealpy/opt/grey_wolf_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@ def __init__(self, option) -> None:


def run(self):
# options = self.options
# X = options["x0"]
# N = options["NP"]
fit = self.fun(self.x)
# MaxIT = options["MaxIters"]
# dim = options["ndim"]
# lb, ub = options["domain"]

X_fit_sort = bm.argsort(fit, axis=0)

Expand All @@ -37,16 +31,9 @@ def run(self):
#空列表
self.gbest_f = X_alpha_fit
self.gbest = X_alpha
# self.curve = bm.zeros((MaxIT,))
# self.D_pl = bm.zeros((MaxIT,))
# self.D_pt = bm.zeros((MaxIT,))
# self.Div = bm.zeros((1, MaxIT))

for it in range(0, self.MaxIT):
self.D_pl_pt(it)
# self.Div[0, it] = bm.sum(bm.sum(bm.abs(bm.mean(X, axis=0) - X)) / N)
# # exploration percentage and exploitation percentage
# self.D_pl[it], self.D_pt[it] = self.D_pl_pt(self.Div[0, it])

a = 2 - 2 * it / self.MaxIT

Expand Down
13 changes: 0 additions & 13 deletions fealpy/opt/harris_hawks_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,12 @@ def __init__(self, option) -> None:
super().__init__(option)

def run(self):
# options = self.options
# x = options["x0"]
# N = options["NP"]
fit = self.fun(self.x)
# MaxIT = options["MaxIters"]
# dim = options["ndim"]
# lb, ub = options["domain"]
gbest_index = bm.argmin(fit)
self.gbest = self.x[gbest_index]
self.gbest_f = fit[gbest_index]
# self.curve = bm.zeros((MaxIT,))
# self.D_pl = bm.zeros((MaxIT,))
# self.D_pt = bm.zeros((MaxIT,))
# self.Div = bm.zeros((1, MaxIT))
for it in range(self.MaxIT):
self.D_pl_pt(it)
# self.Div[0, it] = bm.sum(bm.sum(bm.abs(bm.mean(x, axis=0) - x))/N)
# # exploration percentage and exploitation percentage
# self.D_pl[it], self.D_pt[it] = self.D_pl_pt(self.Div[0, it])

E1 = 2 * (1 - (it / self.MaxIT))
q = bm.random.rand(self.N, 1)
Expand Down
13 changes: 0 additions & 13 deletions fealpy/opt/rime_opt_alg.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,12 @@ def __init__(self, option) -> None:


def run(self, w=5):
# options = self.options
# x = options["x0"]
# N = options["NP"]
fit = self.fun(self.x)
# MaxIT = options["MaxIters"]
# dim = options["ndim"]
# lb, ub = options["domain"]
gbest_index = bm.argmin(fit)
self.gbest = self.x[gbest_index]
self.gbest_f = fit[gbest_index]
# self.curve = bm.zeros((MaxIT,))
# self.D_pl = bm.zeros((MaxIT,))
# self.D_pt = bm.zeros((MaxIT,))
# self.Div = bm.zeros((1, MaxIT))
for it in range(0, self.MaxIT):
self.D_pl_pt(it)
# self.Div[0, it] = bm.sum(bm.sum(bm.abs(bm.mean(x, axis=0) - x))/N)
# # exploration percentage and exploitation percentage
# self.D_pl[it], self.D_pt[it] = self.D_pl_pt(self.Div[0, it])
RimeFactor = (bm.random.rand(self.N, 1) - 0.5) * 2 * bm.cos(bm.array(bm.pi * it / (self.MaxIT / 10))) * (1 - bm.round(bm.array(it * w / self.MaxIT)) / w) # Parameters of Eq.(3),(4),(5)
E = ((it + 1) /self.MaxIT) ** 0.5 # Eq.(6)
normalized_rime_rates = fit / (bm.linalg.norm(fit) + 1e-10) # Parameters of Eq.(7)
Expand Down
14 changes: 0 additions & 14 deletions fealpy/opt/sand_cat_swarm_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,13 @@ def __init__(self, option) -> None:
super().__init__(option)

def run(self):
# options = self.options
# x = options["x0"]
# N = options["NP"]
fit = self.fun(self.x)
# MaxIT = options["MaxIters"]
# dim = options["ndim"]
# lb, ub = options["domain"]
gbest_index = bm.argmin(fit)
self.gbest = self.x[gbest_index]
self.gbest_f = fit[gbest_index]


# self.curve = bm.zeros((MaxIT,))
# self.D_pl = bm.zeros((MaxIT,))
# self.D_pt = bm.zeros((MaxIT,))
# self.Div = bm.zeros((1, MaxIT))

for it in range(self.MaxIT):
self.D_pl_pt(it)
# self.Div[0, it] = bm.sum(bm.sum(bm.abs(bm.mean(x, axis=0) - x)) / N)
# self.D_pl[it], self.D_pt[it] = self.D_pl_pt(self.Div[0, it])

rg = 2 - 2 * it / self.MaxIT # Eq.(1)
R = 2 * rg * bm.random.rand(self.N, 1) - rg # Eq.(2)
Expand Down
14 changes: 0 additions & 14 deletions fealpy/opt/squirrel_search_alg.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,7 @@ def __init__(self, option) -> None:
super().__init__(option)

def run(self):
# options = self.options
# x = options["x0"]
# N = options["NP"]
fit = self.fun(self.x)
# MaxIT = options["MaxIters"]
# dim = options["ndim"]
# lb, ub = options["domain"]
gbest_index = bm.argmin(fit)
self.gbest_f = fit[gbest_index]

Expand All @@ -37,15 +31,8 @@ def run(self):
FSn = self.x[index[4 : self.N]]
Gc = 1.9
Pdp = 0.1
# self.curve = bm.zeros((MaxIT,))
# self.D_pl = bm.zeros((MaxIT,))
# self.D_pt = bm.zeros((MaxIT,))
# self.Div = bm.zeros((1, MaxIT))
for it in range(0, self.MaxIT):
self.D_pl_pt(it)
# self.Div[0, it] = bm.sum(bm.sum(bm.abs(bm.mean(x, axis=0) - x))/N)
# # exploration percentage and exploitation percentage
# self.D_pl[it], self.D_pt[it] = self.D_pl_pt(self.Div[0, it])

n2 = bm.random.randint(4, self.N, (1,))

Expand Down Expand Up @@ -108,7 +95,6 @@ def __init__(self, option) -> None:
super().__init__(option)

def run(self):
["NP"]
fit = self.fun(self.x)

gbest_index = bm.argmin(fit)
Expand Down

0 comments on commit 3bd5d12

Please sign in to comment.