Ejemplo n.º 1
0
def test_gsa_run():
    def square(x):
        return np.sum(x**2)

    def hook(optimizer, space, function):
        return

    new_function = function.Function(pointer=square)

    hyperparams = {'G': 100}

    new_gsa = gsa.GSA(hyperparams=hyperparams)

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

    history = new_gsa.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 gsa failed to converge.'
Ejemplo n.º 2
0
def test_gsa_hyperparams():
    hyperparams = {
        'G': 2.467,
    }

    new_gsa = gsa.GSA(hyperparams=hyperparams)

    assert new_gsa.G == 2.467
Ejemplo n.º 3
0
def test_gsa_params():
    params = {
        'G': 2.467,
    }

    new_gsa = gsa.GSA(params=params)

    assert new_gsa.G == 2.467
Ejemplo n.º 4
0
def test_gsa_update():
    search_space = search.SearchSpace(n_agents=10,
                                      n_variables=2,
                                      lower_bound=[0, 0],
                                      upper_bound=[10, 10])

    new_gsa = gsa.GSA()
    new_gsa.compile(search_space)

    new_gsa.update(search_space, 1)
Ejemplo n.º 5
0
def test_gsa_hyperparams_setter():
    new_gsa = gsa.GSA()

    try:
        new_gsa.G = 'a'
    except:
        new_gsa.G = 0.1

    try:
        new_gsa.G = -1
    except:
        new_gsa.G = 0.1

    assert new_gsa.G == 0.1
Ejemplo n.º 6
0
def test_gsa_compile():
    search_space = search.SearchSpace(n_agents=10,
                                      n_variables=2,
                                      lower_bound=[0, 0],
                                      upper_bound=[10, 10])

    new_gsa = gsa.GSA()
    new_gsa.compile(search_space)

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

    assert new_gsa.velocity == np.array([1])
Ejemplo n.º 7
0
def test_gsa_calculate_mass():
    new_gsa = gsa.GSA()

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

    search_space.agents[0].fit = 1

    search_space.agents.sort(key=lambda x: x.fit)

    mass = new_gsa._calculate_mass(search_space.agents)

    assert len(mass) > 0
Ejemplo n.º 8
0
def test_gsa_calculate_force():
    new_gsa = gsa.GSA()

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

    search_space.agents[0].fit = 1

    search_space.agents.sort(key=lambda x: x.fit)

    mass = new_gsa._calculate_mass(search_space.agents)

    gravity = 1

    force = new_gsa._calculate_force(search_space.agents, mass, gravity)

    assert force.shape[0] > 0
Ejemplo n.º 9
0
def test_gsa_build():
    new_gsa = gsa.GSA()

    assert new_gsa.built == True
Ejemplo n.º 10
0
def test_gsa_update_position():
    new_gsa = gsa.GSA()

    position = new_gsa._update_position(1, 1)

    assert position == 2