Skip to content

Commit

Permalink
Adding BSO-related tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
gugarosa committed Jun 25, 2021
1 parent d5f6bb0 commit 9cb3415
Showing 1 changed file with 153 additions and 0 deletions.
153 changes: 153 additions & 0 deletions tests/opytimizer/optimizers/social/test_bso.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
from types import new_class
import numpy as np

from opytimizer.optimizers.social import bso
from opytimizer.spaces import search


def test_bso_params():
params = {
'm': 5,
'p_replacement_cluster': 0.2,
'p_single_cluster': 0.8,
'p_single_best': 0.4,
'p_double_best': 0.5,
'k': 20
}

new_bso = bso.BSO(params=params)

assert new_bso.m == 5

assert new_bso.p_replacement_cluster == 0.2

assert new_bso.p_single_cluster == 0.8

assert new_bso.p_single_best == 0.4

assert new_bso.p_double_best == 0.5

assert new_bso.k == 20


def test_bso_params_setter():
new_bso = bso.BSO()

try:
new_bso.m = 'a'
except:
new_bso.m = 5

assert new_bso.m == 5

try:
new_bso.m = -1
except:
new_bso.m = 5

assert new_bso.m == 5

try:
new_bso.p_replacement_cluster = 'b'
except:
new_bso.p_replacement_cluster = 0.2

assert new_bso.p_replacement_cluster == 0.2

try:
new_bso.p_replacement_cluster = -1
except:
new_bso.p_replacement_cluster = 0.2

assert new_bso.p_replacement_cluster == 0.2

try:
new_bso.p_single_cluster = 'c'
except:
new_bso.p_single_cluster = 0.8

assert new_bso.p_single_cluster == 0.8

try:
new_bso.p_single_cluster = -1
except:
new_bso.p_single_cluster = 0.8

assert new_bso.p_single_cluster == 0.8

try:
new_bso.p_single_best = 'd'
except:
new_bso.p_single_best = 0.4

assert new_bso.p_single_best == 0.4

try:
new_bso.p_single_best = -1
except:
new_bso.p_single_best = 0.4

assert new_bso.p_single_best == 0.4

try:
new_bso.p_double_best = 'e'
except:
new_bso.p_double_best = 0.5

assert new_bso.p_double_best == 0.5

try:
new_bso.p_double_best = -1
except:
new_bso.p_double_best = 0.5

assert new_bso.p_double_best == 0.5

try:
new_bso.k = 'f'
except:
new_bso.k = 20

assert new_bso.k == 20

try:
new_bso.k = -1
except:
new_bso.k = 20

assert new_bso.k == 20


def test_bso_clusterize():
search_space = search.SearchSpace(n_agents=10, n_variables=2,
lower_bound=[0, 0], upper_bound=[10, 10])

new_bso = bso.BSO()

new_bso._clusterize(search_space.agents)


def test_bso_sigmoid():
new_bso = bso.BSO()

x = 0.5

y = new_bso._sigmoid(x)

assert y == 0.6224593312018546


def test_bso_update():
def square(x):
return np.sum(x**2)

search_space = search.SearchSpace(n_agents=50, n_variables=2,
lower_bound=[0, 0], upper_bound=[10, 10])

new_bso = bso.BSO()
new_bso.evaluate(search_space, square)

new_bso.update(search_space, square, 1, 10)

new_bso.p_replacement_cluster = 1
new_bso.update(search_space, square, 1, 10)

0 comments on commit 9cb3415

Please sign in to comment.