Example #1
0
def test_bmrfo_cyclone_foraging():
    new_bmrfo = bmrfo.BMRFO()

    boolean_space = boolean.BooleanSpace(n_agents=100, n_variables=2)

    cyclone = new_bmrfo._cyclone_foraging(boolean_space.agents,
                                          boolean_space.best_agent.position, 0,
                                          1, 100)

    assert cyclone[0].item() is False or cyclone[0].item() is True

    cyclone = new_bmrfo._cyclone_foraging(boolean_space.agents,
                                          boolean_space.best_agent.position, 1,
                                          1, 100)

    assert cyclone[0].item() is False or cyclone[0].item() is True

    cyclone = new_bmrfo._cyclone_foraging(boolean_space.agents,
                                          boolean_space.best_agent.position, 0,
                                          1, 1)

    assert cyclone[0].item() is False or cyclone[0].item() is True

    cyclone = new_bmrfo._cyclone_foraging(boolean_space.agents,
                                          boolean_space.best_agent.position, 1,
                                          1, 1)

    assert cyclone[0].item() is False or cyclone[0].item() is True
Example #2
0
def test_bmrfo_params():
    params = {
        'S': r.generate_binary_random_number(size=(1, 1))
    }

    new_bmrfo = bmrfo.BMRFO(params=params)

    assert new_bmrfo.S == 0 or new_bmrfo.S == 1
Example #3
0
def test_bmrfo_somersault_foraging():
    new_bmrfo = bmrfo.BMRFO()

    boolean_space = boolean.BooleanSpace(n_agents=100, n_variables=2)

    somersault = new_bmrfo._somersault_foraging(
        boolean_space.agents[0].position, boolean_space.best_agent.position)

    assert somersault[0].item() is False or somersault[0].item() is True
Example #4
0
def test_bmrfo_chain_foraging():
    new_bmrfo = bmrfo.BMRFO()

    boolean_space = boolean.BooleanSpace(n_agents=100, n_variables=2)

    chain = new_bmrfo._chain_foraging(boolean_space.agents,
                                      boolean_space.best_agent.position, 0)

    assert chain[0].item() is False or chain[0].item() is True
Example #5
0
def test_bmrfo_params_setter():
    new_bmrfo = bmrfo.BMRFO()

    try:
        new_bmrfo.S = "a"
    except:
        new_bmrfo.S = r.generate_binary_random_number(size=(1, 1))

    assert new_bmrfo.S == 0 or new_bmrfo.S == 1
Example #6
0
def test_bmrfo_update():
    new_function = function.Function(pointer=Knapsack(
        values=(55, 10, 47, 5, 4), weights=(95, 4, 60, 32, 23), max_capacity=100))

    new_bmrfo = bmrfo.BMRFO()

    boolean_space = boolean.BooleanSpace(n_agents=100, n_variables=5)

    new_bmrfo.update(boolean_space, new_function, 1, 20)
Example #7
0
def test_bmrfo_chain_foraging():
    new_bmrfo = bmrfo.BMRFO()

    boolean_space = boolean.BooleanSpace(n_agents=5,
                                         n_iterations=20,
                                         n_variables=2)

    chain = new_bmrfo._chain_foraging(boolean_space.agents,
                                      boolean_space.best_agent.position, 1)

    assert chain[0] == False or chain[0] == True
Example #8
0
def test_bmrfo_run():
    def square(x):
        return np.sum(x**2)

    def hook(optimizer, space, function):
        return

    new_function = function.Function(pointer=square)

    new_bmrfo = bmrfo.BMRFO()

    boolean_space = boolean.BooleanSpace(n_agents=5,
                                         n_iterations=20,
                                         n_variables=2)

    history = new_bmrfo.run(boolean_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 bmrfo failed to converge.'
Example #9
0
def test_bmrfo_run():
    def hook(optimizer, space, function):
        return

    new_function = function.Function(
        pointer=Knapsack(values=[55, 10, 47, 5, 4],
                         weights=[95, 4, 60, 32, 23],
                         max_capacity=100))

    new_bmrfo = bmrfo.BMRFO()

    boolean_space = boolean.BooleanSpace(n_agents=2,
                                         n_iterations=10,
                                         n_variables=5)

    history = new_bmrfo.run(boolean_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 bmrfo failed to converge.'
Example #10
0
def test_bmrfo_build():
    new_bmrfo = bmrfo.BMRFO()

    assert new_bmrfo.built == True