Exemple #1
0
def test_hs_run():
    def square(x):
        return np.sum(x**2)

    def hook(optimizer, space, function):
        return

    new_function = function.Function(pointer=square)

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

    new_hs = hs.HS(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_hs.run(search_space, new_function, pre_evaluation_hook=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 hs failed to converge.'
Exemple #2
0
def test_hs_hyperparams():
    hyperparams = {'HMCR': 0.7, 'PAR': 0.7, 'bw': 10.0}

    new_hs = hs.HS(hyperparams=hyperparams)

    assert new_hs.HMCR == 0.7

    assert new_hs.PAR == 0.7

    assert new_hs.bw == 10.0
Exemple #3
0
def test_hs_generate_new_harmony():
    new_hs = hs.HS()

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

    agent = new_hs._generate_new_harmony(
        search_space.agents[0], search_space.lb, search_space.ub)

    assert agent.fit > 0
Exemple #4
0
def test_hs_hyperparams_setter():
    new_hs = hs.HS()

    new_hs.HMCR = 0.5
    assert new_hs.HMCR == 0.5

    new_hs.PAR = 0.5
    assert new_hs.PAR == 0.5

    new_hs.bw = 5
    assert new_hs.bw == 5
Exemple #5
0
def test_hs_hyperparams_setter():
    new_hs = hs.HS()

    try:
        new_hs.HMCR = 'a'
    except:
        new_hs.HMCR = 0.5

    try:
        new_hs.HMCR = -1
    except:
        new_hs.HMCR = 0.5

    assert new_hs.HMCR == 0.5

    try:
        new_hs.PAR = 'b'
    except:
        new_hs.PAR = 0.5

    try:
        new_hs.PAR = -1
    except:
        new_hs.PAR = 0.5

    assert new_hs.PAR == 0.5

    try:
        new_hs.bw = 'c'
    except:
        new_hs.bw = 5

    try:
        new_hs.bw = -1
    except:
        new_hs.bw = 5

    assert new_hs.bw == 5

    assert new_hs.bw == 5
Exemple #6
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
Exemple #7
0
def test_hs_build():
    new_hs = hs.HS()

    assert new_hs.built == True