コード例 #1
0
def test_update_monotonic_best_score_glob_maximize():
    """Test if the particle swarm optimizer monotonically converges for maximization problems."""
    pso = ParticleSwarmOptimizer(func=opt_func_inv, maximize=True, particles=20)

    params = {'x': (-1, 1), 'y': (-1, 1)}
    pso.init(params=params, random_state=1)

    scores = [pso.score]
    for i in range(100):
        pso.update(params)
        scores.append(pso.score)

    assert all(scores[i+1] >= scores[i] for i in range(len(scores)-1))
コード例 #2
0
def test_update_monotonic_best_scores_maximize():
    """Test if each particle of the particle swarm optimizer monotonically converges for maximization problems."""
    pso = ParticleSwarmOptimizer(func=opt_func_inv, maximize=True, particles=20)

    params = {'x': (-1, 1), 'y': (-1, 1)}
    pso.init(params=params, random_state=1)

    scores = {p: [pso._score_all[p]] for p in range(20)}
    for i in range(100):
        pso.update(params)
        for particle in range(20):
            scores[particle] = scores[particle] + [pso._score_all[particle]]

    assert all(all(scores[particle][i+1] >= scores[particle][i] for i in range(len(scores[particle])-1))
               for particle in range(20))