From 9cb34158dd7b8c8684b9dd06613a0176ca45cb64 Mon Sep 17 00:00:00 2001 From: Gustavo Rosa Date: Fri, 25 Jun 2021 16:49:29 -0300 Subject: [PATCH] Adding BSO-related tests. --- .../opytimizer/optimizers/social/test_bso.py | 153 ++++++++++++++++++ 1 file changed, 153 insertions(+) diff --git a/tests/opytimizer/optimizers/social/test_bso.py b/tests/opytimizer/optimizers/social/test_bso.py index e69de29b..1b781049 100644 --- a/tests/opytimizer/optimizers/social/test_bso.py +++ b/tests/opytimizer/optimizers/social/test_bso.py @@ -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)