-
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
Add backend module #115
Add backend module #115
Conversation
I was wrong, we should bring back the option where |
I'll probably finish everything first before I request for review 👍 But you can comment whenever you feel |
Reference: #111 This commit adds a backend module consisting of various abstractions to common PSO operations. There are two files for this: - generators: for generating swarm positions, velocities, etc. - operators: for various swarm operations (update velocity, etc.) As of now, one gripe I have with my current implementation is that it consists of methods with a lot of parameters (it doesn't look pleasing or concise). My idea is to add a Swarm class that contains the position, velocity, c1, c2, w, etc. And just put them inside the optimizer. We'll see how it goes Signed-off-by: ljvmiranda921 <lester.miranda@obf.ateneo.edu>
Using default as None is not really helpful and adds another boilerplate of checking if init_pos is None. Instead, we just multiply by 1.00, which gives us the indentity of the matrix. Signed-off-by: Lester James V. Miranda <ljvmiranda@gmail.com>
Reference: #111 This commit replaces SwarmBase with backend operations for generating the swarm.
359131c
to
d6c0b6f
Compare
There's really no reason to restrict v_clamp to a certain type. As long as it is an iterable, you are fine. Signed-off-by: Lester James V. Miranda <ljvmiranda@gmail.com>
This commit updates global best with backend operations from the new module Signed-off-by: Lester James V. Miranda <ljvmiranda@gmail.com>
d6c0b6f
to
f873551
Compare
Until they're not implemented yet, let's remove the __init__ files for these functionalities. It may confuse the user of what we can actually do. Signed-off-by: ljvmiranda921 <lester.miranda@obf.ateneo.edu>
This commit adds new methods to the update that interfaces well with the Swarm class: create_swarm() as an easy abstraction for generating new swarms and update_gbest_neighborhood() for LocaBestPSO Signed-off-by: Lester James V. Miranda <ljvmiranda@gmail.com>
This class creates a DataClass-like interface for building swarms. It holds all special attributes in swarm optimization that you can reuse in case the user wants to build their own evolutionary loop. Signed-off-by: Lester James V. Miranda <ljvmiranda@gmail.com>
In this commit, the SwarmBase, LocalBestPSO, and GlobalBestPSO are now using the Swarm class interface as its backend. The user-facing API is still the same though. If you will inspect the optimize() method and compare it with previous commits, the structure is much cleaner and concise. Signed-off-by: Lester James V. Miranda <ljvmiranda@gmail.com>
In this commit, we are now comparing the set() of the reset method rather than checking if the value is None. New tests were also added to accommodate the swarm class. Signed-off-by: ljvmiranda921 <lester.miranda@obf.ateneo.edu>
TODO (This weekend 5/26)
|
Should we actually use topologies instead when computing for the best cost and position? I'm thinking of generalizing the best_cost, best_pos = compute_best_particle(swarm, topology='star') # For GlobalBestPSO Perhaps a rough draft of # compute_best_particle() example
# Perhaps we should define this into another file but automatically imported
# from __init__.py?
def compute_best_particle(swarm, topology):
try:
# A dictionary that maps keys into methods?
# Then if the topology requires additional param, like k or p in `ring`,
# It will just take it from the `options` attribute of the swarm
best_cost, best_pos = topologies[topology](swarm)
except KeyError:
raise
else:
return (best_cost, best_pos)
# So the dictionary looks like this...
topologies = {
'star' : star,
} Hmmm, it really looks funky to me though Orfrom pyswarms.backend import Topology
my_topology = Topology('star')
cost, pos = my_topology.compute_best_particle(swarm) Uhhh... but you still need to map these values though. Another onefrom pyswarms. backend.topologies import Star
my_toplogy = Star()
cost, pos = my_topology.compute_best_particle(swarm) Uhhh... but this means we need to build a class for each topology. Aside from computing the best particle, what other methods should we actually associate to a Topology? Any thoughts here, @SioKCronin |
This commit adds the Topology module's __init__ file. The concept behind this module is that if we can add topologies in a modular fashion, we can easily explore different PSO behaviour. Signed-off-by: Lester James V. Miranda <ljvmiranda@gmail.com>
This commit adds the base Topology class that must be inherited by any topology implementation. It raises a NotImplementedError whenever the compute_gbest(), compute_position(), and compute_velocity() are not called. I decided to put the compute_position() and compute_velocity() methods here because there may be cases where these operations are controlled by the Topology itself. In case, standard implementations are required, pyswarms.backend.operators should suffice. Signed-off-by: Lester James V. Miranda <ljvmiranda@gmail.com>
This commit adds the Star class topology for the GlobalBestPSO implementation. Signed-off-by: Lester James V. Miranda <ljvmiranda@gmail.com>
In this commit, we move the global best computation to the Topology classes themselves, but keep the compute_pbest(), compute_position() and compute_velocity() so that people can reuse it. The position and velocity computations are standard implementations, so this is just imported by the Ring and Star topology classes in their own methods Signed-off-by: Lester James V. Miranda <ljvmiranda@gmail.com>
This commit adds the Ring class topology for the LocalBestPSO implementation. In the next iteration, we should start considering having a strict number of neighbors (static and dynamic). But let's solve the BinaryPSO first before going there. Signed-off-by: Lester James V. Miranda <ljvmiranda@gmail.com>
Both GlobalBestPSO and LocalBestPSO now uses the Topology backend. Hooray! Signed-off-by: Lester James V. Miranda <ljvmiranda@gmail.com>
Add Topologies class
Reference: #119 The name init_pos tends to be confusing. We're not really setting the initial positions here. Instead, we're just defining where the center is whenever the swarm is generated. Next, we'll set an init_pos variable so that we can explicitly set the center Signed-off-by: Lester James V. Miranda <ljvmiranda@gmail.com>
Resolves #119 This commit adds an option to set the initial position in a more exposed way. This also performs some checks to see if the init_pos confirms with the bounds. Signed-off-by: Lester James V. Miranda <ljvmiranda@gmail.com>
Reference: #119 Tests the init_pos feature for swarm generation. Checks cases when: - init_pos and bounds is not None - init_pos and bounds is None - either init_pos or bounds is None (2 cases) Signed-off-by: Lester James V. Miranda <ljvmiranda@gmail.com>
In this commit, we revert the name behavior back into the word options. This is so a more general term and will not semantically lock us when implementing new swarm algorithms. Signed-off-by: Lester James V. Miranda <ljvmiranda@gmail.com>
In this commit, we add a set of binary generators to generate discrete/binary versions of the swarm. Signed-off-by: Lester James V. Miranda <ljvmiranda@gmail.com>
Reference: #119 This commit exposes the init_pos option for user-defined initial positions in the SwarmOptimizer base class and gbest and lbest implementations. Signed-off-by: Lester James V. Miranda <ljvmiranda@gmail.com>
This commit ports base_discrete to use the backend module. We also renamed the DiscreteSwarmBase into DiscreteSwarmOptimizer for better semantic meaning. Signed-off-by: Lester James V. Miranda <ljvmiranda@gmail.com>
This commit ports the BinaryPSO to use the backend module. Python 2.7 dependencies were also removed. Signed-off-by: Lester James V. Miranda <ljvmiranda@gmail.com>
This commit fixes the reset() test in test_binary. Now, we call on the attributes from the Swarm class, not in the DiscreteSwarmOptimizer class. Signed-off-by: Lester James V. Miranda <ljvmiranda@gmail.com>
Port discrete PSO (and other Swarm fixes)
Reference: #111
Branch: Development
The idea for the backend module is that we can abstract away common PSO operations such
as generating swarms or updating velocities or positions. Once we have a module that does that,
we can replace our current implementations (GlobalBest, LocalBest) with the backend operations.
Ideally, this should provide a white-box approach for users who would like to build their own swarm algorithm. They don't need to rely on very clunky classes we provide. They just import the backend and use them in ways they wish.
Another advantage of this approach is that it gives us the ability to test various parts of the GlobalBest and LocalBest implementations. This is really helpful for us as well.
This might interest you, @SioKCronin . You can review my code if you want (or if you have time), but this is still WIP 😄