-
Notifications
You must be signed in to change notification settings - Fork 82
/
run_BCM.py
47 lines (36 loc) · 1.71 KB
/
run_BCM.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
#!/usr/bin/env python
# Created by "Thieu" at 08:55, 21/02/2022 ----------%
# Email: nguyenthieu2102@gmail.com %
# Github: https://github.com/thieu1995 %
# --------------------------------------------------%
from models.news.BCM import BaseBCM
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 = 10
lb = [-10] * problem_size
ub = [10] * problem_size
## if you choose this way, the problem_size can be anything you want
## Setting parameters
obj_func = func_sum # Objective function (Your minimum or maximum function)
verbose = True
epoch = 1000
pop_size = 50
md1 = BaseBCM(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)