Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def optimizer_reset(self, options):
opt = LocalBestPSO(10, 2, options)
opt.optimize(sphere, 10)
opt.reset()
return opt
def optimizer_history(self, options):
opt = LocalBestPSO(10, 2, options)
opt.optimize(sphere, 1000)
return opt
def test_local_wrong_kwargs(func):
"""Tests kwargs are passed the objective function for when kwargs do not exist"""
# setup optimizer
options = {"c1": 0.5, "c2": 0.3, "w": 0.9, "k": 2, "p": 2}
x_max = 10 * np.ones(2)
x_min = -1 * x_max
bounds = (x_min, x_max)
opt_ps = LocalBestPSO(
n_particles=100, dimensions=2, options=options, bounds=bounds
)
# run it
with pytest.raises(TypeError) as excinfo:
cost, pos = opt_ps.optimize(func, 1000, print_step=10, c=1, d=100)
assert "unexpected keyword" in str(excinfo.value)
def test_local_uneeded_kwargs(func):
"""Tests kwargs are passed the objective function for when kwargs do not exist"""
# setup optimizer
options = {"c1": 0.5, "c2": 0.3, "w": 0.9, "k": 2, "p": 2}
x_max = 10 * np.ones(2)
x_min = -1 * x_max
bounds = (x_min, x_max)
opt_ps = LocalBestPSO(
n_particles=100, dimensions=2, options=options, bounds=bounds
)
# run it
with pytest.raises(TypeError) as excinfo:
cost, pos = opt_ps.optimize(func, 1000, a=1)
assert "unexpected keyword" in str(excinfo.value)
def test_local_missed_kwargs(func):
"""Tests kwargs are passed the objective function for when kwargs do not exist"""
# setup optimizer
options = {"c1": 0.5, "c2": 0.3, "w": 0.9, "k": 2, "p": 2}
x_max = 10 * np.ones(2)
x_min = -1 * x_max
bounds = (x_min, x_max)
opt_ps = LocalBestPSO(
n_particles=100, dimensions=2, options=options, bounds=bounds
)
# run it
with pytest.raises(TypeError) as excinfo:
cost, pos = opt_ps.optimize(func, 1000, a=1)
assert "missing 1 required positional argument" in str(excinfo.value)
def test_local_no_kwargs(func):
"""Tests if no kwargs/args are passed properly to the objective function for when kwargs are present"""
# setup optimizer
options = {"c1": 0.5, "c2": 0.3, "w": 0.9, "k": 2, "p": 2}
x_max = 10 * np.ones(2)
x_min = -1 * x_max
bounds = (x_min, x_max)
opt_ps = LocalBestPSO(
n_particles=100, dimensions=2, options=options, bounds=bounds
)
# run it
cost, pos = opt_ps.optimize(func, iters=1000)
assert np.isclose(cost, 0, rtol=1e-03)
assert np.isclose(pos[0], 1.0, rtol=1e-03)
assert np.isclose(pos[1], 1.0, rtol=1e-03)
def test_local_correct_pos(self, options):
""" Test to check local optimiser returns the correct position corresponding to the best cost """
opt = LocalBestPSO(n_particles=10, dimensions=2, options=options)
cost, pos = opt.optimize(sphere, iters=5)
# find best pos from history
min_cost_idx = np.argmin(opt.cost_history)
min_pos_idx = np.argmin(sphere(opt.pos_history[min_cost_idx]))
assert np.array_equal(opt.pos_history[min_cost_idx][min_pos_idx], pos)
def test_local_kwargs(func):
"""Tests if kwargs are passed properly to the objective function for when kwargs are present"""
# setup optimizer
options = {"c1": 0.5, "c2": 0.3, "w": 0.9, "k": 2, "p": 2}
x_max = 10 * np.ones(2)
x_min = -1 * x_max
bounds = (x_min, x_max)
opt_ps = LocalBestPSO(
n_particles=100, dimensions=2, options=options, bounds=bounds
)
# run it
cost, pos = opt_ps.optimize(func, 1000, a=1, b=100)
assert np.isclose(cost, 0, rtol=1e-03)
assert np.isclose(pos[0], 1.0, rtol=1e-03)
assert np.isclose(pos[1], 1.0, rtol=1e-03)
#Benchmark PSO Global Best
import numpy as np
import pyswarms as ps
from pyswarms.utils.functions import single_obj as fx
options = {'c1': 0.5, 'c2': 0.3, 'w': 0.9, 'k':2, 'p':2}
optimizer = ps.single.LocalBestPSO(n_particles=10, dimensions=2, options=options)
cost, pos = optimizer.optimize(fx.sphere_func, print_step=100, iters=1000, verbose=3)