Пример #1
0
def test_vpso_update_velocity():
    new_vpso = pso.VPSO()

    velocity, v_velocity = new_vpso._update_velocity(1, 1, 1, 1, 1)

    assert velocity == 0.7
    assert v_velocity == 0
Пример #2
0
def test_vpso_run():
    def square(x):
        return np.sum(x**2)

    def hook(optimizer, space, function):
        return

    new_function = function.Function(pointer=square)

    new_vpso = pso.VPSO()

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

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

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

    best_fitness = history.best_agent[-1][1]
    assert best_fitness <= constants.TEST_EPSILON, 'The algorithm pso failed to converge.'
Пример #3
0
def test_vpso_compile():
    search_space = search.SearchSpace(
        n_agents=2, n_variables=2, lower_bound=[0, 0], upper_bound=[10, 10]
    )

    new_vpso = pso.VPSO()
    new_vpso.compile(search_space)

    try:
        new_vpso.local_position = 1
    except:
        new_vpso.local_position = np.array([1])

    assert new_vpso.local_position == np.array([1])

    try:
        new_vpso.velocity = 1
    except:
        new_vpso.velocity = np.array([1])

    assert new_vpso.velocity == np.array([1])

    try:
        new_vpso.v_velocity = 1
    except:
        new_vpso.v_velocity = np.array([1])

    assert new_vpso.v_velocity == np.array([1])
Пример #4
0
def test_vpso_update():
    search_space = search.SearchSpace(n_agents=2, n_variables=2,
                                      lower_bound=[0, 0], upper_bound=[10, 10])

    new_vpso = pso.VPSO()
    new_vpso.compile(search_space)

    new_vpso.update(search_space)
Пример #5
0
def test_vpso_hyperparams():
    hyperparams = {'w': 2, 'c1': 1.7, 'c2': 1.7}

    new_vpso = pso.VPSO(hyperparams=hyperparams)

    assert new_vpso.w == 2

    assert new_vpso.c1 == 1.7

    assert new_vpso.c2 == 1.7
Пример #6
0
def test_vpso_hyperparams_setter():
    new_vpso = pso.VPSO()

    try:
        new_vpso.w = 'a'
    except:
        new_vpso.w = 1

    try:
        new_vpso.w = -1
    except:
        new_vpso.w = 1

    assert new_vpso.w == 1

    try:
        new_vpso.c1 = 'b'
    except:
        new_vpso.c1 = 1.5

    try:
        new_vpso.c1 = -1
    except:
        new_vpso.c1 = 1.5

    assert new_vpso.c1 == 1.5

    try:
        new_vpso.c2 = 'c'
    except:
        new_vpso.c2 = 1.5

    try:
        new_vpso.c2 = -1
    except:
        new_vpso.c2 = 1.5

    assert new_vpso.c2 == 1.5
Пример #7
0
def test_vpso_update_position():
    new_vpso = pso.VPSO()

    position = new_vpso._update_position(1, 1, 1)

    assert position == 2