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

    new_ci = ci.CI()
    new_ci.compile(search_space)

    try:
        new_ci.lower = 1
    except:
        new_ci.lower = np.array([1])

    assert new_ci.lower == 1

    try:
        new_ci.upper = 1
    except:
        new_ci.upper = np.array([1])

    assert new_ci.upper == 1
Example #2
0
def test_gco_compile():
    search_space = search.SearchSpace(n_agents=4,
                                      n_variables=2,
                                      lower_bound=[1, 1],
                                      upper_bound=[10, 10])

    new_gco = gco.GCO()
    new_gco.compile(search_space)

    try:
        new_gco.life = 1
    except:
        new_gco.life = np.array([1])

    assert new_gco.life == np.array([1])

    try:
        new_gco.counter = 1
    except:
        new_gco.counter = np.array([1])

    assert new_gco.counter == np.array([1])
Example #3
0
def test_hho_run():
    def square(x):
        return np.sum(x**2)

    def hook(optimizer, space, function):
        return

    new_function = function.Function(pointer=square)

    new_hho = hho.HHO()

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

    history = new_hho.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 hho failed to converge.'
Example #4
0
def test_hs_run():
    def square(x):
        return np.sum(x**2)

    new_function = function.Function(pointer=square)

    hyperparams = {
        'HMCR': 0.7,
        'PAR': 0.7,
        'bw': 10
    }

    new_hs = hs.HS(hyperparams=hyperparams)

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

    history = new_hs.run(search_space, new_function)

    assert len(history.agents) > 0
    assert len(history.best_agent) > 0
Example #5
0
def test_wwo_compile():
    search_space = search.SearchSpace(n_agents=50,
                                      n_variables=2,
                                      lower_bound=[0, 0],
                                      upper_bound=[10, 10])

    new_wwo = wwo.WWO()
    new_wwo.compile(search_space)

    try:
        new_wwo.height = 1
    except:
        new_wwo.height = np.array([1])

    assert new_wwo.height == np.array([1])

    try:
        new_wwo.length = 1
    except:
        new_wwo.length = np.array([1])

    assert new_wwo.length == np.array([1])
Example #6
0
def test_ssd_compile():
    search_space = search.SearchSpace(n_agents=10,
                                      n_variables=2,
                                      lower_bound=[0, 0],
                                      upper_bound=[10, 10])

    new_ssd = ssd.SSD()
    new_ssd.compile(search_space)

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

    assert new_ssd.local_position == 1

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

    assert new_ssd.velocity == 1
Example #7
0
def test_gsa_update_velocity():
    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)

    velocity = new_gsa._update_velocity(force[0], mass[0], 1)

    assert velocity[0] != 0
Example #8
0
def test_sghs_run():
    def square(x):
        return np.sum(x**2)

    def hook(optimizer, space, function):
        return

    new_function = function.Function(pointer=square)

    new_sghs = hs.SGHS(hyperparams={'LP': 10})

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

    history = new_sghs.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 ihs failed to converge.'
Example #9
0
def test_coa_compile():
    search_space = search.SearchSpace(n_agents=10,
                                      n_variables=2,
                                      lower_bound=[0, 0],
                                      upper_bound=[10, 10])

    new_coa = coa.COA()
    new_coa.compile(search_space)

    try:
        new_coa.n_c = 'a'
    except:
        new_coa.n_c = 1

    assert new_coa.n_c == 1

    try:
        new_coa.n_c = -1
    except:
        new_coa.n_c = 1

    assert new_coa.n_c == 1
Example #10
0
def test_af_compile():
    search_space = search.SearchSpace(n_agents=10,
                                      n_variables=2,
                                      lower_bound=[0, 0],
                                      upper_bound=[10, 10])

    new_af = af.AF()
    new_af.compile(search_space)

    try:
        new_af.p_distance = 1
    except:
        new_af.p_distance = np.array([1])

    assert new_af.p_distance == np.array([1])

    try:
        new_af.g_distance = 1
    except:
        new_af.g_distance = np.array([1])

    assert new_af.g_distance == np.array([1])
Example #11
0
def test_kh_local_alpha():
    search_space = search.SearchSpace(n_agents=5,
                                      n_variables=2,
                                      lower_bound=[0, 0],
                                      upper_bound=[10, 10])

    new_kh = kh.KH()
    new_kh.compile(search_space)

    distance, eucl_distance = new_kh._sensing_distance(search_space.agents, 0)

    neighbours = new_kh._get_neighbours(search_space.agents, 0, distance,
                                        eucl_distance)

    alpha = new_kh._local_alpha(
        search_space.agents[0],
        search_space.agents[-1],
        search_space.agents[0],
        neighbours,
    )

    assert alpha.shape == (2, 1) or alpha == 0
Example #12
0
def test_kh_compile():
    search_space = search.SearchSpace(n_agents=5,
                                      n_variables=2,
                                      lower_bound=[0, 0],
                                      upper_bound=[10, 10])

    new_kh = kh.KH()
    new_kh.compile(search_space)

    try:
        new_kh.motion = 1
    except:
        new_kh.motion = np.array([1])

    assert new_kh.motion == np.array([1])

    try:
        new_kh.foraging = 1
    except:
        new_kh.foraging = np.array([1])

    assert new_kh.foraging == np.array([1])
Example #13
0
def test_rfo_compile():
    search_space = search.SearchSpace(n_agents=10,
                                      n_variables=2,
                                      lower_bound=[0, 0],
                                      upper_bound=[10, 10])

    new_rfo = rfo.RFO()
    new_rfo.compile(search_space)

    try:
        new_rfo.n_replacement = "a"
    except:
        new_rfo.n_replacement = 1

    assert new_rfo.n_replacement == 1

    try:
        new_rfo.n_replacement = -1
    except:
        new_rfo.n_replacement = 1

    assert new_rfo.n_replacement == 1
Example #14
0
def test_eho_compile():
    search_space = search.SearchSpace(n_agents=20,
                                      n_variables=2,
                                      lower_bound=[0, 0],
                                      upper_bound=[10, 10])

    new_eho = eho.EHO()
    new_eho.compile(search_space)

    try:
        new_eho.n_ci = 'a'
    except:
        new_eho.n_ci = 1

    assert new_eho.n_ci == 1

    try:
        new_eho.n_ci = -1
    except:
        new_eho.n_ci = 1

    assert new_eho.n_ci == 1
Example #15
0
def test_ba_compile():
    search_space = search.SearchSpace(n_agents=10,
                                      n_variables=2,
                                      lower_bound=[0, 0],
                                      upper_bound=[10, 10])

    new_ba = ba.BA()
    new_ba.compile(search_space)

    try:
        new_ba.frequency = 1
    except:
        new_ba.frequency = np.array([1])

    assert new_ba.frequency == np.array([1])

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

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

    try:
        new_ba.loudness = 1
    except:
        new_ba.loudness = np.array([1])

    assert new_ba.loudness == np.array([1])

    try:
        new_ba.pulse_rate = 1
    except:
        new_ba.pulse_rate = np.array([1])

    assert new_ba.pulse_rate == np.array([1])
Example #16
0
def test_search_initialize_agents():
    new_search_space = search.SearchSpace()

    assert new_search_space.agents[0].position[0] != 0