示例#1
0
def test_bwo_mutation():
    search_space = search.SearchSpace(n_agents=10,
                                      n_variables=2,
                                      lower_bound=[1, 1],
                                      upper_bound=[10, 10])

    new_bwo = bwo.BWO()

    alpha = new_bwo._mutation(search_space.agents[0])

    assert type(alpha).__name__ == 'Agent'
示例#2
0
def test_bwo_update():
    def square(x):
        return np.sum(x**2)

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

    new_bwo = bwo.BWO()

    new_bwo.update(search_space, square)
示例#3
0
def test_bwo_procreating():
    search_space = search.SearchSpace(n_agents=10,
                                      n_variables=2,
                                      lower_bound=[1, 1],
                                      upper_bound=[10, 10])

    new_bwo = bwo.BWO()

    y1, y2 = new_bwo._procreating(search_space.agents[0],
                                  search_space.agents[1])

    assert type(y1).__name__ == 'Agent'
    assert type(y2).__name__ == 'Agent'
示例#4
0
def test_bwo_hyperparams():
    hyperparams = {
        'pp': 0.6,
        'cr': 0.44,
        'pm': 0.4,
    }

    new_bwo = bwo.BWO(hyperparams=hyperparams)

    assert new_bwo.pp == 0.6

    assert new_bwo.cr == 0.44

    assert new_bwo.pm == 0.4
示例#5
0
def test_bwo_params():
    params = {
        'pp': 0.6,
        'cr': 0.44,
        'pm': 0.4,
    }

    new_bwo = bwo.BWO(params=params)

    assert new_bwo.pp == 0.6

    assert new_bwo.cr == 0.44

    assert new_bwo.pm == 0.4
示例#6
0
def test_bwo_params():
    params = {
        "pp": 0.6,
        "cr": 0.44,
        "pm": 0.4,
    }

    new_bwo = bwo.BWO(params=params)

    assert new_bwo.pp == 0.6

    assert new_bwo.cr == 0.44

    assert new_bwo.pm == 0.4
示例#7
0
def test_bwo_update():
    def square(x):
        return np.sum(x**2)

    new_function = function.Function(pointer=square)

    new_bwo = bwo.BWO()

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

    new_bwo._evaluate(search_space, new_function)

    new_bwo._update(search_space.agents,
                    search_space.n_variables, new_function)

    assert search_space.agents[0].position[0] != 0
示例#8
0
def test_bwo_run():
    def square(x):
        return np.sum(x**2)

    def hook(optimizer, space, function):
        return

    new_function = function.Function(pointer=square)

    new_bwo = bwo.BWO()

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

    history = new_bwo.run(search_space, new_function, pre_evaluation=hook)

    assert len(history.agents) > 0
    assert len(history.best_agent) > 0

    best_fitness = history.best_agent[-1][1]
    assert best_fitness <= constants.TEST_EPSILON, 'The algorithm bwo failed to converge.'
示例#9
0
def test_bwo_hyperparams_setter():
    new_bwo = bwo.BWO()

    try:
        new_bwo.pp = 'a'
    except:
        new_bwo.pp = 0.6

    try:
        new_bwo.pp = -1
    except:
        new_bwo.pp = 0.6

    assert new_bwo.pp == 0.6

    try:
        new_bwo.cr = 'b'
    except:
        new_bwo.cr = 0.44

    try:
        new_bwo.cr = -1
    except:
        new_bwo.cr = 0.44

    assert new_bwo.cr == 0.44

    try:
        new_bwo.pm = 'c'
    except:
        new_bwo.pm = 0.4

    try:
        new_bwo.pm = -1
    except:
        new_bwo.pm = 0.4

    assert new_bwo.pm == 0.4
示例#10
0
def test_bwo_params_setter():
    new_bwo = bwo.BWO()

    try:
        new_bwo.pp = "a"
    except:
        new_bwo.pp = 0.6

    try:
        new_bwo.pp = -1
    except:
        new_bwo.pp = 0.6

    assert new_bwo.pp == 0.6

    try:
        new_bwo.cr = "b"
    except:
        new_bwo.cr = 0.44

    try:
        new_bwo.cr = -1
    except:
        new_bwo.cr = 0.44

    assert new_bwo.cr == 0.44

    try:
        new_bwo.pm = "c"
    except:
        new_bwo.pm = 0.4

    try:
        new_bwo.pm = -1
    except:
        new_bwo.pm = 0.4

    assert new_bwo.pm == 0.4
示例#11
0
def test_bwo_build():
    new_bwo = bwo.BWO()

    assert new_bwo.built == True