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

    new_pio = pio.PIO()
    new_pio.compile(search_space)

    try:
        new_pio.n_p = 'a'
    except:
        new_pio.n_p = 1

    assert new_pio.n_p == 1

    try:
        new_pio.n_p = -1
    except:
        new_pio.n_p = 1

    assert new_pio.n_p == 1

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

    assert new_pio.velocity == np.array([1])
Exemple #2
0
def test_pio_hyperparams():
    hyperparams = {'n_c1': 150, 'n_c2': 200, 'R': 0.2}

    new_pio = pio.PIO(hyperparams=hyperparams)

    assert new_pio.n_c1 == 150

    assert new_pio.n_c2 == 200

    assert new_pio.R == 0.2
Exemple #3
0
def test_pio_calculate_center():
    search_space = search.SearchSpace(n_agents=10,
                                      n_variables=2,
                                      lower_bound=[0, 0],
                                      upper_bound=[10, 10])

    new_pio = pio.PIO()
    new_pio.compile(search_space)

    center = new_pio._calculate_center(search_space.agents)
Exemple #4
0
def test_pio_params():
    params = {"n_c1": 150, "n_c2": 200, "R": 0.2}

    new_pio = pio.PIO(params=params)

    assert new_pio.n_c1 == 150

    assert new_pio.n_c2 == 200

    assert new_pio.R == 0.2
Exemple #5
0
def test_pio_update():
    search_space = search.SearchSpace(n_agents=10,
                                      n_variables=2,
                                      lower_bound=[0, 0],
                                      upper_bound=[10, 10])

    new_pio = pio.PIO()
    new_pio.compile(search_space)

    new_pio.update(search_space, 1)
    new_pio.update(search_space, 175)
Exemple #6
0
def test_pio_hyperparams_setter():
    new_pio = pio.PIO()

    try:
        new_pio.n_c1 = 0.0
    except:
        new_pio.n_c1 = 150

    try:
        new_pio.n_c1 = 0
    except:
        new_pio.n_c1 = 150

    assert new_pio.n_c1 == 150

    try:
        new_pio.n_c2 = 0.0
    except:
        new_pio.n_c2 = 200

    try:
        new_pio.n_c2 = 0
    except:
        new_pio.n_c2 = 200

    assert new_pio.n_c2 == 200

    try:
        new_pio.R = 'a'
    except:
        new_pio.R = 0.2

    try:
        new_pio.R = -1
    except:
        new_pio.R = 0.2

    assert new_pio.R == 0.2
Exemple #7
0
def test_pio_run():
    def square(x):
        return np.sum(x**2)

    def hook(optimizer, space, function):
        return

    new_function = function.Function(pointer=square)

    new_pio = pio.PIO()

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

    history = new_pio.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 pio failed to converge.'
Exemple #8
0
def test_pio_build():
    new_pio = pio.PIO()

    assert new_pio.built == True