Skip to content
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

Merged
merged 36 commits into from
Jun 7, 2018
Merged

Add backend module #115

merged 36 commits into from
Jun 7, 2018

Conversation

ljvmiranda921
Copy link
Owner

@ljvmiranda921 ljvmiranda921 commented May 19, 2018

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 😄

@ljvmiranda921 ljvmiranda921 added enhancement Feature requests v0.2.0 labels May 19, 2018
@ljvmiranda921 ljvmiranda921 self-assigned this May 19, 2018
@ljvmiranda921
Copy link
Owner Author

I was wrong, we should bring back the option where velocity_clamp = None. I thought I can go around it by simply setting clamp = (0,1). The previous commit fixes that.

@ljvmiranda921 ljvmiranda921 requested review from SioKCronin and removed request for SioKCronin May 19, 2018 12:26
@ljvmiranda921
Copy link
Owner Author

I'll probably finish everything first before I request for review 👍 But you can comment whenever you feel

ljvmiranda921 added 5 commits May 20, 2018 10:02
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.
ljvmiranda921 added 2 commits May 20, 2018 10:13
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>
ljvmiranda921 added 7 commits May 21, 2018 20:27
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>
@ljvmiranda921
Copy link
Owner Author

ljvmiranda921 commented May 21, 2018

TODO (This weekend 5/26)

  • Update BinaryPSO and DiscretePSO with new backends (might be unruly so hopefully we can also fix Returning an empty subset as the best subset for feature selection #110 in the process). Where to do the editing: Swarm class
  • Update Documentation (i.e., ReadTheDocs, some examples on how to use the backend, short example in creating your own evolutionary-loop [white-box approach) Make another PR for this

@ljvmiranda921
Copy link
Owner Author

ljvmiranda921 commented May 23, 2018

Should we actually use topologies instead when computing for the best cost and position?

I'm thinking of generalizing the compute_gbest() and compute_gbest_neighborhood() methods in just a single Topology. The idea is that we have a compute_best_particle that changes its behavior depending on the topology present. This changes how best_pos and best_cost is computed and in turn affects the particle's velocity.

best_cost, best_pos = compute_best_particle(swarm, topology='star') # For GlobalBestPSO

Perhaps a rough draft of compute_best_particle() would be:

# 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

Or

from 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 one

from 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

ljvmiranda921 added 21 commits June 6, 2018 21:15
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>
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)
@ljvmiranda921 ljvmiranda921 merged commit 622641b into development Jun 7, 2018
@ljvmiranda921 ljvmiranda921 deleted the add-backend-module branch June 7, 2018 06:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Feature requests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant