-
Notifications
You must be signed in to change notification settings - Fork 333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement a GeneralOptimizer class for new topologies #148
Comments
A GeneralOptimizer is good, just plug-in your desired topology, and optimize away. But I'm on-the-fence that it'll make GlobalBest or LocalBest obsolete, we can keep these optimizers going forward. I see the GeneralOptimizer as a higher-order abstraction of GlobalBest / LocalBest, so this may be good. Any thoughts @SioKCronin ?
Is multilayer PSO "just a different topology?" I read the paper and a part of me thinks it is, but I might be wrong. Still not sure though, might require a second or third opinion (@SioKCronin , maybe @CPapadim can also weigh-in?). For now I think let's go implementing GlobalOptimizer under the import pyswarms as ps
from pyswarms.backend.topology import Foo
my_topology = Foo()
optimizer = ps.single.GlobalOptimizer(topology=my_topology, options) |
I'd love a documentation for this where we just animate swarm trajectories using different topologies as they traverse through a fairly difficult objective function 💯 |
I might start working on this on the weekend after I finished my exams if you like. I have holidays after that so I'd be up for some work on this project 😄. |
Awesome! Looking forward to it and I wish you well in your exams! |
Hey @ljvmiranda921, I have created a
As far as multilayer PSO goes I have left a comment in #75 where I proposed an idea for the implementation (if we even want to include it at the moment) NOTE: The error in the |
Hi @whzup , thanks for stepping up, I really appreciate your enthusiasm. To answer your questions first:
Noted on the
Again, thanks a lot! We really appreciate this! |
Regarding your answer No.3: No, I actually meant the dynamic topology with the two additional options for the distance and the number of neighbours, I'll include this one 😄. Regarding the error: try:
# If there are less than 5 particles they are all connected
if swarm.n_particles < 5:
best_pos = swarm.pbest_pos[np.argmin(swarm.pbest_cost)]
best_cost = np.min(swarm.pbest_cost)
else:
pyramid = Delaunay(swarm.position)
indices, index_pointer = pyramid.vertex_neighbor_vertices
# Insert all the neighbors for each particle in the idx array
idx = np.array([index_pointer[indices[i]:indices[i+1]] for i in range(swarm.n_particles)])
> idx_min = swarm.pbest_cost[idx].argmin(axis=1)
E IndexError: arrays used as indices must be of integer (or boolean) type The error occurs because try:
# If there are less than 5 particles they are all connected
if swarm.n_particles < 5:
best_pos = swarm.pbest_pos[np.argmin(swarm.pbest_cost)]
best_cost = np.min(swarm.pbest_cost)
else:
pyramid = Delaunay(swarm.position)
indices, index_pointer = pyramid.vertex_neighbor_vertices
# Insert all the neighbors for each particle in the idx array
> idx = np.array([index_pointer[indices[i]:indices[i+1]] for i in range(swarm.n_particles)]).astype(int)
E ValueError: setting an array element with a sequence. This error occurs because the array is not filled with arrays of equal length (see this StackOverflow question for a reference. It has an answer but it's quite an ugly workaround). The whole function: try:
# If there are less than 5 particles they are all connected
if swarm.n_particles < 5:
best_pos = swarm.pbest_pos[np.argmin(swarm.pbest_cost)]
best_cost = np.min(swarm.pbest_cost)
else:
pyramid = Delaunay(swarm.position)
indices, index_pointer = pyramid.vertex_neighbor_vertices
# Insert all the neighbors for each particle in the idx array
idx = np.array([index_pointer[indices[i]:indices[i+1]] for i in range(swarm.n_particles)]).astype(int)
idx_min = swarm.pbest_cost[idx].argmin(axis=1)
best_neighbor = idx[np.arange(len(idx)), idx_min]
# Obtain best cost and position
best_cost = np.min(swarm.pbest_cost[best_neighbor])
best_pos = swarm.pbest_pos[
np.argmin(swarm.pbest_cost[best_neighbor])
] For less than 5 particles it just behaves like the I've got another thing that just came to my mind. How should I include the animations in the documentation? In form of a Jupyter notebook? |
I’ll check the Pyramid one and see if we can fix this.
Sure, I suggest (if you are fine) to play with the parameters (what if it’s
highly social or highly cognitive?). The same with LocalBestPSO.
For the documentation in read the docs, a GIF file is good. In the actual
Jupyter notebook, just render it as it is similar to the one we’ve been
doing in visualization.ipynb
On Sat, 30 Jun 2018 at 23:04 Aaron ***@***.***> wrote:
Regarding your answer No.3: No, I actually meant the dynamic topology with
the two additional options for the distance and the number of neighbours,
I'll include this one 😄.
Regarding the error:
If I use it as it is:
try:
# If there are less than 5 particles they are all connected
if swarm.n_particles < 5:
best_pos = swarm.pbest_pos[np.argmin(swarm.pbest_cost)]
best_cost = np.min(swarm.pbest_cost)
else:
pyramid = Delaunay(swarm.position)
indices, index_pointer = pyramid.vertex_neighbor_vertices
# Insert all the neighbors for each particle in the idx array
idx = np.array([index_pointer[indices[i]:indices[i+1]] for i in range(swarm.n_particles)])> idx_min = swarm.pbest_cost[idx].argmin(axis=1)
E IndexError: arrays used as indices must be of integer (or boolean) type
The error occurs because idx is an array of floats. But if I change the
array data type to *int*:
try:
# If there are less than 5 particles they are all connected
if swarm.n_particles < 5:
best_pos = swarm.pbest_pos[np.argmin(swarm.pbest_cost)]
best_cost = np.min(swarm.pbest_cost)
else:
pyramid = Delaunay(swarm.position)
indices, index_pointer = pyramid.vertex_neighbor_vertices
# Insert all the neighbors for each particle in the idx array> idx = np.array([index_pointer[indices[i]:indices[i+1]] for i in range(swarm.n_particles)]).astype(int)
E ValueError: setting an array element with a sequence.
This error occurs because the array is not filled with arrays of equal
length (see this StackOverflow question
<https://stackoverflow.com/questions/43146266/convert-list-of-lists-with-different-lengths-to-a-numpy-array>
for a reference. It has an answer but it's quite an ugly workaround). The
whole function:
try:
# If there are less than 5 particles they are all connected
if swarm.n_particles < 5:
best_pos = swarm.pbest_pos[np.argmin(swarm.pbest_cost)]
best_cost = np.min(swarm.pbest_cost)
else:
pyramid = Delaunay(swarm.position)
indices, index_pointer = pyramid.vertex_neighbor_vertices
# Insert all the neighbors for each particle in the idx array
idx = np.array([index_pointer[indices[i]:indices[i+1]] for i in range(swarm.n_particles)]).astype(int)
idx_min = swarm.pbest_cost[idx].argmin(axis=1)
best_neighbor = idx[np.arange(len(idx)), idx_min]
# Obtain best cost and position
best_cost = np.min(swarm.pbest_cost[best_neighbor])
best_pos = swarm.pbest_pos[
np.argmin(swarm.pbest_cost[best_neighbor])
]
For less than 5 particles it just behaves like the Star topology because
all the particles are connected in that case and the Delaunay triangulation
does not work with less than 5 points. So the error just occurs when there
are more than 5 particles.
I've got another thing that just came to my mind. How should I include the
animations in the documentation? In form of a Jupyter notebook?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#148 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AMWYs6XXyUoYrdDzJVdqSjr2RESKdyPrks5uB4VigaJpZM4U4Z8k>
.
--
Lester James V. Miranda
BS Electronics and Communications Engineering,
Minor in Philosophy (Class of 2016)
Ateneo de Manila University
lester.miranda@ <[email protected]>*o
<http://toki.waseda.jp>bf.ateneo.edu <http://bf.ateneo.edu>*
|
Ok, I resolved the problem with the pyramid topology😄. The idx = np.array([index_pointer[indices[i]:indices[i+1]] for i in range(swarm.n_particles)])
idx_min = np.array([swarm.pbest_cost[idx[i]].argmin() for i in range(idx.size)])
best_neighbor = np.array([idx[i][idx_min[i]] for i in range(idx.size)]).astype(int) All the tests for the optimizers pass now! I'm going to make a PR so you can review it. By the way, I have an idea for the visualization. We could use this as another |
Wow, you're on a roll. Sure, open up a PR and let's discuss over there. |
@ljvmiranda921 I have a question regarding the testing. There are these |
Hi @whzup , which particular fixture are you pointing at? If it returns an |
As proposed in #147 we could implement a new GeneralOptimizer class in addition to the two existing GlobalBest and LocalBest classes. In this class, it will then be possible to select a custom topology for the optimization. We could also include a foundation for multilayer PSO (as proposed in #75).
The only issue I see is that a GeneralOptimizer might make the GlobalBest or LocalBest classes obsolete as it might optimize faster and/or run with less memory consumption especially if we implement higher-level topologies such as Von Neumann or Pyramid (#129).
The text was updated successfully, but these errors were encountered: