pyswarm 0.7.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyswarm/__init__.py +17 -0
- pyswarm/pso.py +234 -0
- pyswarm-0.7.0.dist-info/METADATA +94 -0
- pyswarm-0.7.0.dist-info/RECORD +6 -0
- pyswarm-0.7.0.dist-info/WHEEL +5 -0
- pyswarm-0.7.0.dist-info/top_level.txt +1 -0
pyswarm/__init__.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"""
|
|
2
|
+
================================================================================
|
|
3
|
+
pyswarm: Particle swarm optimization (PSO) with constraint support
|
|
4
|
+
================================================================================
|
|
5
|
+
|
|
6
|
+
Authors: Abraham Lee
|
|
7
|
+
Sebastian Castillo-Hair
|
|
8
|
+
Copyright: 2013-2015
|
|
9
|
+
|
|
10
|
+
"""
|
|
11
|
+
# from __future__ import absolute_import
|
|
12
|
+
|
|
13
|
+
__author__ = 'Abraham Lee'
|
|
14
|
+
__version__ = '0.7.0'
|
|
15
|
+
|
|
16
|
+
from pyswarm.pso import *
|
|
17
|
+
|
pyswarm/pso.py
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
from functools import partial
|
|
2
|
+
import numpy as np
|
|
3
|
+
|
|
4
|
+
def _obj_wrapper(func, args, kwargs, x):
|
|
5
|
+
return func(x, *args, **kwargs)
|
|
6
|
+
|
|
7
|
+
def _is_feasible_wrapper(func, x):
|
|
8
|
+
return np.all(func(x)>=0)
|
|
9
|
+
|
|
10
|
+
def _cons_none_wrapper(x):
|
|
11
|
+
return np.array([0])
|
|
12
|
+
|
|
13
|
+
def _cons_ieqcons_wrapper(ieqcons, args, kwargs, x):
|
|
14
|
+
return np.array([y(x, *args, **kwargs) for y in ieqcons])
|
|
15
|
+
|
|
16
|
+
def _cons_f_ieqcons_wrapper(f_ieqcons, args, kwargs, x):
|
|
17
|
+
return np.array(f_ieqcons(x, *args, **kwargs))
|
|
18
|
+
|
|
19
|
+
def pso(func, lb, ub, ieqcons=[], f_ieqcons=None, args=(), kwargs={},
|
|
20
|
+
swarmsize=100, omega=0.5, phip=0.5, phig=0.5, maxiter=100,
|
|
21
|
+
minstep=1e-8, minfunc=1e-8, debug=False, processes=1,
|
|
22
|
+
particle_output=False):
|
|
23
|
+
"""
|
|
24
|
+
Perform a particle swarm optimization (PSO)
|
|
25
|
+
|
|
26
|
+
Parameters
|
|
27
|
+
==========
|
|
28
|
+
func : function
|
|
29
|
+
The function to be minimized
|
|
30
|
+
lb : array
|
|
31
|
+
The lower bounds of the design variable(s)
|
|
32
|
+
ub : array
|
|
33
|
+
The upper bounds of the design variable(s)
|
|
34
|
+
|
|
35
|
+
Optional
|
|
36
|
+
========
|
|
37
|
+
ieqcons : list
|
|
38
|
+
A list of functions of length n such that ieqcons[j](x,*args) >= 0.0 in
|
|
39
|
+
a successfully optimized problem (Default: [])
|
|
40
|
+
f_ieqcons : function
|
|
41
|
+
Returns a 1-D array in which each element must be greater or equal
|
|
42
|
+
to 0.0 in a successfully optimized problem. If f_ieqcons is specified,
|
|
43
|
+
ieqcons is ignored (Default: None)
|
|
44
|
+
args : tuple
|
|
45
|
+
Additional arguments passed to objective and constraint functions
|
|
46
|
+
(Default: empty tuple)
|
|
47
|
+
kwargs : dict
|
|
48
|
+
Additional keyword arguments passed to objective and constraint
|
|
49
|
+
functions (Default: empty dict)
|
|
50
|
+
swarmsize : int
|
|
51
|
+
The number of particles in the swarm (Default: 100)
|
|
52
|
+
omega : scalar
|
|
53
|
+
Particle velocity scaling factor (Default: 0.5)
|
|
54
|
+
phip : scalar
|
|
55
|
+
Scaling factor to search away from the particle's best known position
|
|
56
|
+
(Default: 0.5)
|
|
57
|
+
phig : scalar
|
|
58
|
+
Scaling factor to search away from the swarm's best known position
|
|
59
|
+
(Default: 0.5)
|
|
60
|
+
maxiter : int
|
|
61
|
+
The maximum number of iterations for the swarm to search (Default: 100)
|
|
62
|
+
minstep : scalar
|
|
63
|
+
The minimum stepsize of swarm's best position before the search
|
|
64
|
+
terminates (Default: 1e-8)
|
|
65
|
+
minfunc : scalar
|
|
66
|
+
The minimum change of swarm's best objective value before the search
|
|
67
|
+
terminates (Default: 1e-8)
|
|
68
|
+
debug : boolean
|
|
69
|
+
If True, progress statements will be displayed every iteration
|
|
70
|
+
(Default: False)
|
|
71
|
+
processes : int
|
|
72
|
+
The number of processes to use to evaluate objective function and
|
|
73
|
+
constraints (default: 1)
|
|
74
|
+
particle_output : boolean
|
|
75
|
+
Whether to include the best per-particle position and the objective
|
|
76
|
+
values at those.
|
|
77
|
+
|
|
78
|
+
Returns
|
|
79
|
+
=======
|
|
80
|
+
g : array
|
|
81
|
+
The swarm's best known position (optimal design)
|
|
82
|
+
f : scalar
|
|
83
|
+
The objective value at ``g``
|
|
84
|
+
p : array
|
|
85
|
+
The best known position per particle
|
|
86
|
+
pf: arrray
|
|
87
|
+
The objective values at each position in p
|
|
88
|
+
|
|
89
|
+
"""
|
|
90
|
+
|
|
91
|
+
assert len(lb)==len(ub), 'Lower- and upper-bounds must be the same length'
|
|
92
|
+
assert hasattr(func, '__call__'), 'Invalid function handle'
|
|
93
|
+
lb = np.array(lb)
|
|
94
|
+
ub = np.array(ub)
|
|
95
|
+
assert np.all(ub>lb), 'All upper-bound values must be greater than lower-bound values'
|
|
96
|
+
|
|
97
|
+
vhigh = np.abs(ub - lb)
|
|
98
|
+
vlow = -vhigh
|
|
99
|
+
|
|
100
|
+
# Initialize objective function
|
|
101
|
+
obj = partial(_obj_wrapper, func, args, kwargs)
|
|
102
|
+
|
|
103
|
+
# Check for constraint function(s) #########################################
|
|
104
|
+
if f_ieqcons is None:
|
|
105
|
+
if not len(ieqcons):
|
|
106
|
+
if debug:
|
|
107
|
+
print('No constraints given.')
|
|
108
|
+
cons = _cons_none_wrapper
|
|
109
|
+
else:
|
|
110
|
+
if debug:
|
|
111
|
+
print('Converting ieqcons to a single constraint function')
|
|
112
|
+
cons = partial(_cons_ieqcons_wrapper, ieqcons, args, kwargs)
|
|
113
|
+
else:
|
|
114
|
+
if debug:
|
|
115
|
+
print('Single constraint function given in f_ieqcons')
|
|
116
|
+
cons = partial(_cons_f_ieqcons_wrapper, f_ieqcons, args, kwargs)
|
|
117
|
+
is_feasible = partial(_is_feasible_wrapper, cons)
|
|
118
|
+
|
|
119
|
+
# Initialize the multiprocessing module if necessary
|
|
120
|
+
if processes > 1:
|
|
121
|
+
import multiprocessing
|
|
122
|
+
mp_pool = multiprocessing.Pool(processes)
|
|
123
|
+
|
|
124
|
+
# Initialize the particle swarm ############################################
|
|
125
|
+
S = swarmsize
|
|
126
|
+
D = len(lb) # the number of dimensions each particle has
|
|
127
|
+
x = np.random.rand(S, D) # particle positions
|
|
128
|
+
v = np.zeros_like(x) # particle velocities
|
|
129
|
+
p = np.zeros_like(x) # best particle positions
|
|
130
|
+
fx = np.zeros(S) # current particle function values
|
|
131
|
+
fs = np.zeros(S, dtype=bool) # feasibility of each particle
|
|
132
|
+
fp = np.ones(S)*np.inf # best particle function values
|
|
133
|
+
g = [] # best swarm position
|
|
134
|
+
fg = np.inf # best swarm position starting value
|
|
135
|
+
|
|
136
|
+
# Initialize the particle's position
|
|
137
|
+
x = lb + x*(ub - lb)
|
|
138
|
+
|
|
139
|
+
# Calculate objective and constraints for each particle
|
|
140
|
+
if processes > 1:
|
|
141
|
+
fx = np.array(mp_pool.map(obj, x))
|
|
142
|
+
fs = np.array(mp_pool.map(is_feasible, x))
|
|
143
|
+
else:
|
|
144
|
+
for i in range(S):
|
|
145
|
+
fx[i] = obj(x[i, :])
|
|
146
|
+
fs[i] = is_feasible(x[i, :])
|
|
147
|
+
|
|
148
|
+
# Store particle's best position (if constraints are satisfied)
|
|
149
|
+
i_update = np.logical_and((fx < fp), fs)
|
|
150
|
+
p[i_update, :] = x[i_update, :].copy()
|
|
151
|
+
fp[i_update] = fx[i_update]
|
|
152
|
+
|
|
153
|
+
# Update swarm's best position
|
|
154
|
+
i_min = np.argmin(fp)
|
|
155
|
+
if fp[i_min] < fg:
|
|
156
|
+
fg = fp[i_min]
|
|
157
|
+
g = p[i_min, :].copy()
|
|
158
|
+
else:
|
|
159
|
+
# At the start, there may not be any feasible starting point, so just
|
|
160
|
+
# give it a temporary "best" point since it's likely to change
|
|
161
|
+
g = x[0, :].copy()
|
|
162
|
+
|
|
163
|
+
# Initialize the particle's velocity
|
|
164
|
+
v = vlow + np.random.rand(S, D)*(vhigh - vlow)
|
|
165
|
+
|
|
166
|
+
# Iterate until termination criterion met ##################################
|
|
167
|
+
it = 1
|
|
168
|
+
while it <= maxiter:
|
|
169
|
+
rp = np.random.uniform(size=(S, D))
|
|
170
|
+
rg = np.random.uniform(size=(S, D))
|
|
171
|
+
|
|
172
|
+
# Update the particles velocities
|
|
173
|
+
v = omega*v + phip*rp*(p - x) + phig*rg*(g - x)
|
|
174
|
+
# Update the particles' positions
|
|
175
|
+
x = x + v
|
|
176
|
+
# Correct for bound violations
|
|
177
|
+
maskl = x < lb
|
|
178
|
+
masku = x > ub
|
|
179
|
+
x = x*(~np.logical_or(maskl, masku)) + lb*maskl + ub*masku
|
|
180
|
+
|
|
181
|
+
# Update objectives and constraints
|
|
182
|
+
if processes > 1:
|
|
183
|
+
fx = np.array(mp_pool.map(obj, x))
|
|
184
|
+
fs = np.array(mp_pool.map(is_feasible, x))
|
|
185
|
+
else:
|
|
186
|
+
for i in range(S):
|
|
187
|
+
fx[i] = obj(x[i, :])
|
|
188
|
+
fs[i] = is_feasible(x[i, :])
|
|
189
|
+
|
|
190
|
+
# Store particle's best position (if constraints are satisfied)
|
|
191
|
+
i_update = np.logical_and((fx < fp), fs)
|
|
192
|
+
p[i_update, :] = x[i_update, :].copy()
|
|
193
|
+
fp[i_update] = fx[i_update]
|
|
194
|
+
|
|
195
|
+
# Compare swarm's best position with global best position
|
|
196
|
+
i_min = np.argmin(fp)
|
|
197
|
+
if fp[i_min] < fg:
|
|
198
|
+
if debug:
|
|
199
|
+
print('New best for swarm at iteration {:}: {:} {:}'\
|
|
200
|
+
.format(it, p[i_min, :], fp[i_min]))
|
|
201
|
+
|
|
202
|
+
p_min = p[i_min, :].copy()
|
|
203
|
+
stepsize = np.sqrt(np.sum((g - p_min)**2))
|
|
204
|
+
|
|
205
|
+
if np.abs(fg - fp[i_min]) <= minfunc:
|
|
206
|
+
print('Stopping search: Swarm best objective change less than {:}'\
|
|
207
|
+
.format(minfunc))
|
|
208
|
+
if particle_output:
|
|
209
|
+
return p_min, fp[i_min], p, fp
|
|
210
|
+
else:
|
|
211
|
+
return p_min, fp[i_min]
|
|
212
|
+
elif stepsize <= minstep:
|
|
213
|
+
print('Stopping search: Swarm best position change less than {:}'\
|
|
214
|
+
.format(minstep))
|
|
215
|
+
if particle_output:
|
|
216
|
+
return p_min, fp[i_min], p, fp
|
|
217
|
+
else:
|
|
218
|
+
return p_min, fp[i_min]
|
|
219
|
+
else:
|
|
220
|
+
g = p_min.copy()
|
|
221
|
+
fg = fp[i_min]
|
|
222
|
+
|
|
223
|
+
if debug:
|
|
224
|
+
print('Best after iteration {:}: {:} {:}'.format(it, g, fg))
|
|
225
|
+
it += 1
|
|
226
|
+
|
|
227
|
+
print('Stopping search: maximum iterations reached --> {:}'.format(maxiter))
|
|
228
|
+
|
|
229
|
+
if not is_feasible(g):
|
|
230
|
+
print("However, the optimization couldn't find a feasible design. Sorry")
|
|
231
|
+
if particle_output:
|
|
232
|
+
return g, fg, p, fp
|
|
233
|
+
else:
|
|
234
|
+
return g, fg
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pyswarm
|
|
3
|
+
Version: 0.7.0
|
|
4
|
+
Summary: Particle swarm optimization (PSO) with constraint support
|
|
5
|
+
Home-page: https://github.com/tisimst/pyswarm
|
|
6
|
+
Author: Abraham Lee
|
|
7
|
+
Author-email: tisimst@gmail.com
|
|
8
|
+
License: BSD License
|
|
9
|
+
Keywords: PSO,particle swarm optimization,optimization,python
|
|
10
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
11
|
+
Classifier: Intended Audience :: Education
|
|
12
|
+
Classifier: Intended Audience :: Science/Research
|
|
13
|
+
Classifier: License :: OSI Approved :: BSD License
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python
|
|
16
|
+
Classifier: Programming Language :: Python :: 2.6
|
|
17
|
+
Classifier: Programming Language :: Python :: 2.7
|
|
18
|
+
Classifier: Programming Language :: Python :: 3
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.2
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.3
|
|
21
|
+
Classifier: Topic :: Education
|
|
22
|
+
Classifier: Topic :: Scientific/Engineering
|
|
23
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
24
|
+
Classifier: Topic :: Scientific/Engineering :: Physics
|
|
25
|
+
Classifier: Topic :: Software Development
|
|
26
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
27
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
28
|
+
Classifier: Topic :: Utilities
|
|
29
|
+
Requires-Dist: numpy
|
|
30
|
+
Dynamic: author
|
|
31
|
+
Dynamic: author-email
|
|
32
|
+
Dynamic: classifier
|
|
33
|
+
Dynamic: description
|
|
34
|
+
Dynamic: home-page
|
|
35
|
+
Dynamic: keywords
|
|
36
|
+
Dynamic: license
|
|
37
|
+
Dynamic: requires-dist
|
|
38
|
+
Dynamic: summary
|
|
39
|
+
|
|
40
|
+
=========================================================
|
|
41
|
+
Particle swarm optimization (PSO) with constraint support
|
|
42
|
+
=========================================================
|
|
43
|
+
|
|
44
|
+
The ``pyswarm`` package is a gradient-free, evolutionary optimization package
|
|
45
|
+
for python that supports constraints.
|
|
46
|
+
|
|
47
|
+
What's New
|
|
48
|
+
==========
|
|
49
|
+
|
|
50
|
+
This release features multiprocessing support.
|
|
51
|
+
|
|
52
|
+
Requirements
|
|
53
|
+
============
|
|
54
|
+
|
|
55
|
+
- NumPy
|
|
56
|
+
|
|
57
|
+
Installation and download
|
|
58
|
+
=========================
|
|
59
|
+
|
|
60
|
+
See the `package homepage`_ for helpful hints relating to downloading
|
|
61
|
+
and installing pyswarm.
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
Source Code
|
|
65
|
+
===========
|
|
66
|
+
|
|
67
|
+
The latest, bleeding-edge, but working, `code
|
|
68
|
+
<https://github.com/tisimst/pyDOE/tree/master/pyswarm>`_
|
|
69
|
+
and `documentation source
|
|
70
|
+
<https://github.com/tisimst/pyswarm/tree/master/doc/>`_ are
|
|
71
|
+
available `on GitHub <https://github.com/tisimst/pyswarm/>`_.
|
|
72
|
+
|
|
73
|
+
Contact
|
|
74
|
+
=======
|
|
75
|
+
|
|
76
|
+
Any feedback, questions, bug reports, or success stores should
|
|
77
|
+
be sent to the `author`_. I'd love to hear from you!
|
|
78
|
+
|
|
79
|
+
License
|
|
80
|
+
=======
|
|
81
|
+
|
|
82
|
+
This package is provided under two licenses:
|
|
83
|
+
|
|
84
|
+
1. The *BSD License*
|
|
85
|
+
2. Any other that the author approves (just ask!)
|
|
86
|
+
|
|
87
|
+
References
|
|
88
|
+
==========
|
|
89
|
+
|
|
90
|
+
- `Particle swarm optimization`_ on Wikipedia
|
|
91
|
+
|
|
92
|
+
.. _author: mailto:tisimst@gmail.com
|
|
93
|
+
.. _Particle swarm optimization: http://en.wikipedia.org/wiki/Particle_swarm_optimization
|
|
94
|
+
.. _package homepage: http://pythonhosted.org/pyswarm
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
pyswarm/__init__.py,sha256=31IekRDjnoW1gOUKWEpt6E6XN1_lAIkoyufJsFp3EBc,454
|
|
2
|
+
pyswarm/pso.py,sha256=_I8XeGGCi_m1TdvT5YhMbfSI01e1TPM_Beg8mddpE5E,8374
|
|
3
|
+
pyswarm-0.7.0.dist-info/METADATA,sha256=yCfgnoMQgTe3lWy6jp_TJUlghGTNWxbThgADUXhbEes,2768
|
|
4
|
+
pyswarm-0.7.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
5
|
+
pyswarm-0.7.0.dist-info/top_level.txt,sha256=vWlGQiprJPdLXlglfuM1_U7yfhgvNGd9JBmwdHjTa2g,8
|
|
6
|
+
pyswarm-0.7.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pyswarm
|