-
Notifications
You must be signed in to change notification settings - Fork 82
/
test_GBO.py
50 lines (39 loc) · 2.08 KB
/
test_GBO.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python
# ------------------------------------------------------------------------------------------------------%
# Created by "Thieu" at 22:41, 11/01/2022 %
# %
# Email: nguyenthieu2102@gmail.com %
# Homepage: https://www.researchgate.net/profile/Nguyen_Thieu2 %
# Github: https://github.com/thieu1995 %
# ------------------------------------------------------------------------------------------------------%
from models.news.GBO import BaseGBO
from numpy import sum, pi, exp, sqrt, cos
## You can create whatever function you want here
def func_sum(solution):
return sum(solution ** 2)
def func_ackley(solution):
a, b, c = 20, 0.2, 2 * pi
d = len(solution)
sum_1 = -a * exp(-b * sqrt(sum(solution ** 2) / d))
sum_2 = exp(sum(cos(c * solution)) / d)
return sum_1 - sum_2 + a + exp(1)
## You can create different bound for each dimension like this
# lb = [-15, -10, -3, -15, -10, -3, -15, -10, -3, -15, -10, -3, -15, -10, -3, -100, -40, -50]
# ub = [15, 10, 3, 15, 10, 3, 15, 10, 3, 15, 10, 3, 15, 10, 3, 20, 200, 1000]
## if you choose this way, the problem_size need to be same length as lb and ub
## Or bound is the same for all dimension like this
problem_size = 100
lb = [-100] * problem_size
ub = [100] * problem_size
## if you choose this way, the problem_size can be anything you want
## Setting parameters
obj_func = func_ackley # Objective function (Your minimum or maximum function)
verbose = True
epoch = 1000
pop_size = 50
md1 = BaseGBO(obj_func, lb, ub, verbose, epoch, pop_size)
best_pos1, best_fit1, list_loss1 = md1.train()
# return : the global best solution, the fitness of global best solution and the loss of training process in each epoch/iteration
print(md1.solution[0])
print(md1.solution[1])
print(md1.loss_train)