Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions pyswarm/pso.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
def _cons_f_ieqcons_wrapper(f_ieqcons, args, kwargs, x):
return np.array(f_ieqcons(x, *args, **kwargs))

def pso(func, lb, ub, ieqcons=[], f_ieqcons=None, args=(), kwargs={},

Check failure on line 19 in pyswarm/pso.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this function to always return tuples of the same length.

See more on https://sonarcloud.io/project/issues?id=eggzec_pyswarm&issues=AZ2guOr0r9jJuyXHGDcs&open=AZ2guOr0r9jJuyXHGDcs&pullRequest=16

Check failure on line 19 in pyswarm/pso.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this function to reduce its Cognitive Complexity from 51 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=eggzec_pyswarm&issues=AZ2guOr0r9jJuyXHGDct&open=AZ2guOr0r9jJuyXHGDct&pullRequest=16
swarmsize=100, omega=0.5, phip=0.5, phig=0.5, maxiter=100,
minstep=1e-8, minfunc=1e-8, debug=False, processes=1,
minstep=1e-8, minfunc=1e-8, debug=False, pool=None,
particle_output=False):

Check warning on line 22 in pyswarm/pso.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Function "pso" has 17 parameters, which is greater than the 13 authorized.

See more on https://sonarcloud.io/project/issues?id=eggzec_pyswarm&issues=AZ2guOr0r9jJuyXHGDcr&open=AZ2guOr0r9jJuyXHGDcr&pullRequest=16
"""
Perform a particle swarm optimization (PSO)
Expand Down Expand Up @@ -68,9 +68,10 @@
debug : boolean
If True, progress statements will be displayed every iteration
(Default: False)
processes : int
The number of processes to use to evaluate objective function and
constraints (default: 1)
pool : object
An instantiated multiprocessing.Pool or similar with a map
method for evaluating objective function and constraints
(default: None)
particle_output : boolean
Whether to include the best per-particle position and the objective
values at those.
Expand Down Expand Up @@ -116,15 +117,10 @@
cons = partial(_cons_f_ieqcons_wrapper, f_ieqcons, args, kwargs)
is_feasible = partial(_is_feasible_wrapper, cons)

# Initialize the multiprocessing module if necessary
if processes > 1:
import multiprocessing
mp_pool = multiprocessing.Pool(processes)

# Initialize the particle swarm ############################################
S = swarmsize
D = len(lb) # the number of dimensions each particle has
x = np.random.rand(S, D) # particle positions

Check warning on line 123 in pyswarm/pso.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a "numpy.random.Generator" here instead of this legacy function.

See more on https://sonarcloud.io/project/issues?id=eggzec_pyswarm&issues=AZ2guOr0r9jJuyXHGDcu&open=AZ2guOr0r9jJuyXHGDcu&pullRequest=16
v = np.zeros_like(x) # particle velocities
p = np.zeros_like(x) # best particle positions
fx = np.zeros(S) # current particle function values
Expand All @@ -137,9 +133,9 @@
x = lb + x*(ub - lb)

# Calculate objective and constraints for each particle
if processes > 1:
fx = np.array(mp_pool.map(obj, x))
fs = np.array(mp_pool.map(is_feasible, x))
if pool:
fx = np.array(pool.map(obj, x))
fs = np.array(pool.map(is_feasible, x))
else:
for i in range(S):
fx[i] = obj(x[i, :])
Expand All @@ -161,13 +157,13 @@
g = x[0, :].copy()

# Initialize the particle's velocity
v = vlow + np.random.rand(S, D)*(vhigh - vlow)

Check warning on line 160 in pyswarm/pso.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a "numpy.random.Generator" here instead of this legacy function.

See more on https://sonarcloud.io/project/issues?id=eggzec_pyswarm&issues=AZ2guOr0r9jJuyXHGDcv&open=AZ2guOr0r9jJuyXHGDcv&pullRequest=16

# Iterate until termination criterion met ##################################
it = 1
while it <= maxiter:
rp = np.random.uniform(size=(S, D))

Check warning on line 165 in pyswarm/pso.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a "numpy.random.Generator" here instead of this legacy function.

See more on https://sonarcloud.io/project/issues?id=eggzec_pyswarm&issues=AZ2guOr0r9jJuyXHGDcw&open=AZ2guOr0r9jJuyXHGDcw&pullRequest=16
rg = np.random.uniform(size=(S, D))

Check warning on line 166 in pyswarm/pso.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use a "numpy.random.Generator" here instead of this legacy function.

See more on https://sonarcloud.io/project/issues?id=eggzec_pyswarm&issues=AZ2guOr0r9jJuyXHGDcx&open=AZ2guOr0r9jJuyXHGDcx&pullRequest=16

# Update the particles velocities
v = omega*v + phip*rp*(p - x) + phig*rg*(g - x)
Expand All @@ -179,9 +175,9 @@
x = x*(~np.logical_or(maskl, masku)) + lb*maskl + ub*masku

# Update objectives and constraints
if processes > 1:
fx = np.array(mp_pool.map(obj, x))
fs = np.array(mp_pool.map(is_feasible, x))
if pool:
fx = np.array(pool.map(obj, x))
fs = np.array(pool.map(is_feasible, x))
else:
for i in range(S):
fx[i] = obj(x[i, :])
Expand Down