Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_rosenbrock_output(self):
"""Tests rosenbrock function output."""
self.assertEqual(fx.rosenbrock_func(self.input2).all(),np.zeros(3).all())
def test_bukin6_output(self):
"""Test bukin function output."""
assert np.isclose(fx.matyas_func(self.input),self.target).all()
def test_matyas_bound_fail(outbound):
"""Test matyas bound exception"""
with pytest.raises(ValueError):
x = outbound(b["matyas"].low, b["matyas"].high, size=(3, 2))
fx.matyas(x)
def test_sphere_output(self):
"""Tests sphere function output."""
self.assertEqual(fx.sphere_func(self.input).all(), self.target.all())
def test_beale_output(self):
"""Tests beale function output."""
assert np.isclose(fx.beale_func([3, 0.5] * self.input2),
self.target).all()
def test_rastrigin_bound_fail(self):
"""Test rastrigin bound exception"""
x = - np.random.uniform(low=6,high=100,size=(3,2))
x_ = np.random.uniform(low=6,high=100,size=(3,2))
with self.assertRaises(ValueError):
fx.rastrigin_func(x)
with self.assertRaises(ValueError):
fx.rastrigin_func(x_)
def test_booth_output_size(self):
"""Test booth output size."""
self.assertEqual(fx.booth_func(self.input).shape, self.target_size)
# 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}
optimizer = ps.single.GlobalBestPSO(n_particles=10, dimensions=2, options=options)
cost, pos = optimizer.optimize(fx.sphere_func, print_step100, iters=1000, verbose=3)
'bounds': bounds, 'ftol':ftol,
'options': options, 'optimizer' : optimizer}
return res
def rosenbrock_with_args(x, a, b, c=0):
f = (a - x[:, 0]) ** 2 + b * (x[:, 1] - x[:, 0] ** 2) ** 2 + c
return f
if __name__ == '__main__':
from pyswarms.utils.functions import single_obj as fx
#--------------------------------------------------------------------------
# 1: Rastrigin example:
objfcn = fx.rastrigin
fargs = {}
dimensions = 2
max_bound = 5.12 * np.ones(2)
min_bound = - max_bound
res1 = particleswarm(objfcn, dimensions, args = fargs, use_bnds = True, bounds = (min_bound, max_bound),
iters = 100, n_particles = 10, ftol = -np.inf,
options = {'c1': 0.5, 'c2': 0.3, 'w':0.9},
verbosity = 1)
#--------------------------------------------------------------------------
# 2: Rosenbrock example:
objfcn = rosenbrock_with_args
fargs = {"a": 1.0, "b": 100.0, 'c':0}
dimensions = 2