def test_population_creation(): pso = PSO(cost_function=h1, start=[10, 0], verbose=False) pso.set_bounds(lower=[-100, -100], upper=[100, 100]) pso.run(num_iterations=100, num_particles=10) pso.return_ranked_populations() error = np.sum((pso.best - [8.6998, 6.7665]) ** 2) print('True value: [8.6998, 6.7665]. Found:{0}. Error^2 = {1}'.format(pso.best, error)) assert (error < 0.1)
def test_population_creation(): known_sol = [8.6998, 6.7665] pso = PSO(start=[0, 0], verbose=False, shrink_steps=False) pso.set_bounds(lower=[-100, -100], upper=[100, 100]) pso.set_speed(-10, 10) pso.run(num_iterations=100, num_particles=10, cost_function=h1, ) pso.return_ranked_populations() error = np.sum((pso.best.pos - known_sol) ** 2) print('True value: [8.6998, 6.7665]. Found:{0}. Error^2 = {1}'.format( pso.best.pos, error)) assert (error < 0.1)