tisimst / pyswarm Goto Github PK
View Code? Open in Web Editor NEWParticle swarm optimization (PSO) that supports constraints
Particle swarm optimization (PSO) that supports constraints
Hi
I wonder how will be to generate integer and rounded numbers instead of long float numbers. Currently, Pyswarm generates number like these:
Np: 21.2335149705
H: -8.660139296140425
Which increase the time to reach the solution!
Here is my objective function
`def optimum(x):
H, Np = x
simulate(H, Np)
display(df)
return df['RoCoF']`
Your help is appreciated in advance
Hello everyone.
I would like to know how do I use pyswarm on Windows. I can use it perfectly on Linux, but on Windows, it always crashes when it's called.
Thank you n advance.
I’m trying to optimise a black-box function which needs to obey a constraint. However, I noticed that the objective function is evaluated with values that don’t obey the constraint.
I created a toy problem to verify the behaviour and indeed it seems to be sampling the function while violating constraints.
!pip install --upgrade pyswarm
import numpy as np
iteration = 0
def objective(val):
global iteration
x, y = val
if (x+y)>3 :
print("Sum of X and Y is: ", x+y)
print("Iteration: ", iteration)
iteration=iteration+1
return -20.0 * np.exp(-0.2 * np.sqrt(0.5 * (x**2 + y**2))) - np.exp(0.5 * (np.cos(2 * np.pi * x) + np.cos(2 * np.pi * y))) + np.e + 20
def condition(val):
x, y = val
sum = x + y
return [3 - sum]
ub = [5, 5]
lb = [-5, -5]
xopt, fopt = pso(objective, lb, ub, f_ieqcons=condition, debug=True, maxiter=20)
xopt
Ideally "Sum of X and Y is: " shouldn't be printed, but it is. I cannot evaluate the black-box function outside the constraints since it involves changing a parameters of a live system (And system simply wouldn’t allow those combinations).
Also, I get around 620 "iterations" although I've set maxiter=20.
Was wondering what I'm doing wrong here..
I was wondering if I could make a Pull Request for a more scipy
style bounds field from to the current to something like the version of I've modified?
(func, bounds, ieqcons=[], ...
lower_bound, upper_bound = [], []
for variable_bounds in bounds:
lower_bound.append(variable_bounds[0])
upper_bound.append(variable_bounds[1])
It wouldn't break anything and I could make it backwards compatible also. https://gist.github.com/arose13/2fc85adb1042b89e2eb13ac054789f19
Hello,
I have been using your library for a project.
The repo is currently at version 0.7 but only
version 0.6 is pip
installable. Could you perhaps make a new release at PyPi
?
Thank you in advance.
See this gist I've just made for some suggestions.
Hi maintainers of pyswarm!
This is just a thank you message!
I've used your library two years ago during my undergrad and it helped me a lot in our material science project. It has really been a great help! It's just quite sad that the latest commit in this package was made 2 years ago 😢.
Come grad school, I decided to build a simple PSO module for one of my classes. Last month, I revisited that project, and I decided to turn it into a full-working package. I was inspired by your work that I named it PySwarms (I know I am not creative with names 😞 ). Right now it is still in active development but my vision for that library is to create a research toolbox of different PSO variants.
You can see my vision in the First Major Release issue found here. I am planning to build a more extensible PSO library in which various researchers can try out their models and add them to make PySwarms grow. More importantly, I am also crediting your work in the official documentation and in our README.
Again, thank you so much for this work!
For a specific problem, I need to fix the initial particle positions. How can I do that?
Hi there, I've tried to find a way to define some equal constraints but didn't find one, is there a way to do that?
Can the below log comment can be turned of?
Stopping search: maximum iterations reached
Thank You!
Hi
I wonder how we can be able to generate integer number and short float numbers?
Regards
Sam
Hi all,
I would like to ask about the optimization problems with both integer and float numbers. Is it possible to define the integer variables separately in pyswarm?
Kind regards,
Mert.
Hi tisimst and others,
I've tried to use pyswarm to solve an engineering optimal problem but get Inf fp return when the debug keyworld in PSO is set to be true.
Here's a simple structure of my code:
To be short, it is a 8 variable optimal problem with two inequal constraints
But when I run this with PSO builtin debug mode, the fopt of each generation will always be inf:
Best after iteration 1: [-0.11882721 -0.02219953 -0.11879306 -0.11291183 0.19934821 0.24063626
0.1900103 0.07119408] inf
I'm really puzzled by this, one potential risk in my code is that some times drag in the Drag_Coefficient function or the lift in the Lift_Constraints function may get a bad return(a list with no answer) from other function, in that case will return a default value (like drag=1 in this case, since the predicted object value of drag is around 0.08, far less than 1, which also means that if I get a bad return from other function, I think that this sort of parameter is not good enough to be a candidate of the minimum, so just set a relative large value to eliminate it) , which may influnce the minimal searching. But I still don't know why it's Inf instead of a finite number.
Some hints? Really thanks.
Attach my codes for reference:
def Drag_Coefficient(x):
try:
drag = CST_Shape(Weight_Lower=[x[0], x[1], x[2], x[3]], Weight_Upper=[x[4], x[5], x[6], x[7]],
TE_width=0).Cd([0], 0.1, 100000)[0]
except IndexError:
drag = 1
print(drag)
return drag
def Thickness_Constraint(x):
Thickness = CST_Shape(Weight_Lower=[x[0], x[1], x[2], x[3]], Weight_Upper=[x[4], x[5], x[6], x[7]],
TE_width=0).Thickness(0.25)
return Thickness - 0.10
def Lift_Constraint(x):
try:
lift = CST_Shape(Weight_Lower=[x[0], x[1], x[2], x[3]], Weight_Upper=[x[4], x[5], x[6], x[7]],
TE_width=0).Cl([0], 0.1, 100000)[0]
except IndexError:
return -1
return lift - 0.05
lb = [-0.15, -0.15, -0.15, -0.2, 0.15, 0.1, 0.05, 0.05]
ub = [-0.1, 0, 0, 0, 0.2, 0.3, 0.2, 0.15]
xopt, fopt = pso(Drag_Coefficient, lb, ub, ieqcons=[Thickness_Constraint, Lift_Constraint], debug=True, air_plot=True, phig=1, phip=1)
`
Hi!
Thanks for sharing this useful algorithm!
I have the following issue. This code works well:
from pyswarm import pso
lb = [1]*2
ub = [50]*2
xopt1, fopt1 = pso(abs_slice_density_diff, lb, ub, f_ieqcons=increasing_thresholds_constraint, maxiter=1)
print 'The optimum is at:'
print ' ', xopt1
print 'Optimal function value:'
print ' myfunc: ', fopt1
However, when I change the upper and lower bounds, I sometimes get this:
from pyswarm import pso
lb = [1]*2
ub = [100]*2
xopt1, fopt1 = pso(abs_slice_density_diff, lb, ub, f_ieqcons=increasing_thresholds_constraint, maxiter=1)
print 'The optimum is at:'
print ' ', xopt1
print 'Optimal function value:'
print ' myfunc: ', fopt1
The optimum is at:
[ 5.51644195 44.18486963]
Optimal function value:
myfunc: -0.0828807138052
But, using the same bounds, very often I get this error:
from pyswarm import pso
lb = [1]*2
ub = [100]*2
xopt1, fopt1 = pso(abs_slice_density_diff, lb, ub, f_ieqcons=increasing_thresholds_constraint, maxiter=1)
print 'The optimum is at:'
print ' ', xopt1
print 'Optimal function value:'
print ' myfunc: ', fopt1
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-54-f7fe7752fce8> in <module>()
4 ub = [100]*2
5
----> 6 xopt1, fopt1 = pso(abs_slice_density_diff, lb, ub, f_ieqcons=increasing_thresholds_constraint, maxiter=1)
7
8 print 'The optimum is at:'
/dir/pyswarm/pso.pyc in pso(func, lb, ub, ieqcons, f_ieqcons, args, kwargs, swarmsize, omega, phip, phig, maxiter, minstep, minfunc, debug)
127 # Update the particle's velocity
128 v[i, :] = omega*v[i, :] + phip*rp[i, :]*(p[i, :] - x[i, :]) + \
--> 129 phig*rg[i, :]*(g - x[i, :])
130
131 # Update the particle's position, correcting lower and upper bound
ValueError: operands could not be broadcast together with shapes (0) (2)
If I try something like:
from pyswarm import pso
lb = [1]*8
ub = [50]*8
xopt1, fopt1 = pso(abs_slice_density_diff, lb, ub, f_ieqcons=increasing_thresholds_constraint, maxiter=1)
print 'The optimum is at:'
print ' ', xopt1
print 'Optimal function value:'
print ' myfunc: ', fopt1
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-55-772a37fe9b90> in <module>()
4 ub = [50]*8
5
----> 6 xopt1, fopt1 = pso(abs_slice_density_diff, lb, ub, f_ieqcons=increasing_thresholds_constraint, maxiter=1)
7
8 print 'The optimum is at:'
/dir/pyswarm/pso.pyc in pso(func, lb, ub, ieqcons, f_ieqcons, args, kwargs, swarmsize, omega, phip, phig, maxiter, minstep, minfunc, debug)
127 # Update the particle's velocity
128 v[i, :] = omega*v[i, :] + phip*rp[i, :]*(p[i, :] - x[i, :]) + \
--> 129 phig*rg[i, :]*(g - x[i, :])
130
131 # Update the particle's position, correcting lower and upper bound
ValueError: operands could not be broadcast together with shapes (0) (8)
then I always get the error with that configuration.
I tried to remove the constraint function, but the error persists. I don't know it it makes any sense, but my impression is that the longer is the input array and the bigger are the values it contains, the more likely is to get the error.
Is there something that I should take into account when setting the upper and lower bounds?
I'd love to provide you with my code so you could reproduce the error, but the function that I want to optimize uses a dataset that I cannot share.
Thanks a lot!
I wonder how to implement the binary PSO, where the values of the particles are vectors of 0's and 1's.
It is useful for feature selection, when 0 mean doesn't include the correspond feature for the classification.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.