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

User-provided initial positions #119

Closed
szhan opened this issue May 31, 2018 · 6 comments
Closed

User-provided initial positions #119

szhan opened this issue May 31, 2018 · 6 comments
Labels
enhancement Feature requests

Comments

@szhan
Copy link

szhan commented May 31, 2018

Is your feature request related to a problem? Please describe.
I would like to specify the starting positions of every particle in a swarm.

Describe the solution you'd like
I see that init_pos can be specified, but it only allows the user to specify a starting position for the swarm's center. Looking at the code of pyswarms.base.base_single, it seems that reset() needs to be extended or modified. That is, I need to update how self.pos is being initialized. So, I would provide a list of starting positions to self.pos. Is there an already existing alternative to this?

@ljvmiranda921
Copy link
Owner

Hi @szhan , currently there is no explicit implementation for that, but we're currently developing a feature for this in #115 and #117 . The only workaround we can have here is to initialize the swarm as usual, then set the pos attribute into the positions my_own_initial_pos you need (making sure that the shape of my_own_init_pos is the same as pos):

import numpy as np
import pyswarms as ps

# Your own initial positions of shape (3,3)
my_own_init_pos = np.array([[1,2,3], [4,5,6], [7,8,9]])

# Initialize optimizer, this creates a position and velocity matrix of shape (3,3)
options = {'c1':0.5, 'c2':0.4,'w':0.9} 
optimizer = ps.single.GlobalBestPSO(n_particles=3, dimensions=3, options=options)

# Provide your own init_pos
optimizer.pos = my_own_init_pos.copy()

# Do optimization below
# optimizer.optimizer()...

I realized that this functionality is actually important upon releasing, so hopefully the next update will give this capability. For now, I hope this workaround works.

@ljvmiranda921 ljvmiranda921 added enhancement Feature requests v0.2.0 labels Jun 1, 2018
@szhan
Copy link
Author

szhan commented Jun 2, 2018

Thanks, @ljvmiranda921! Also, I would like to help with the development of PySwarms, as I find it quite useful in my work.

@szhan
Copy link
Author

szhan commented Jun 3, 2018

I have tried the workaround you suggested, but I am encountering an error that I cannot make sense of. I wrote a wrapper to feed GlobalBestPSO() some initial positions to solve some BBOB function.

When trying to have it solve the Rastrigin function, I got the following error:

ValueError: Input for Rastrigin function must be within [-5.12, 5.12].

The initial positions are clearly within bounds. I also checked the source code, and even tried the condition that checks for the bounds. All seems to be in order. What makes it even more puzzling is that it works perfectly fine for the Ackley function! Am I not seeing something very obvious?

My wrapper is as follows:

import itertools
import numpy as np
import pyswarms as ps
from pyswarms.utils.functions import single_obj as fx

def run_global_best_pso(n_dims, test_func, n_inds, n_gens,\
                        initial_positions=None,\
                        c1=0.5, c2=0.3, w=0.9,\
                        random_seed=12345
        ):
        options = {'c1':c1, 'c2':c2, 'w':w}

        optimizer = ps.single.GlobalBestPSO(n_particles=n_inds, dimensions=n_dims, options=options)
        if initial_positions is not None:
                optimizer.pos = np.array(initial_positions).copy()

        stats = optimizer.optimize(test_func, iters=n_gens)
        pos_history = optimizer.get_pos_history

        return(pos_history)

if __name__ == "__main__":
        n_inds = 100
        n_gens = 10000

        initial_positions = list(itertools.repeat([2.0, 3.0, -3.0], n_inds))

        run_global_best_pso(n_dims=3, test_func=fx.rastrigin_func,\
                                n_inds=n_inds, n_gens=n_gens,\
                                initial_positions=initial_positions)

Update: It works when using different values for the initial positions to solve Rastrigin, e.g., [2.0, 0, -0.02] works but [2.0, 3.0, -0.02] does not.

@ljvmiranda921
Copy link
Owner

Okay, that's weird. Hmmm, let me check on this later. Thanks for reporting this @szhan !
You mentioned that you want to help in development. Currently, the pipeline is on rewriting the backend of PySwarms for better control. You can check this on PR #115 and #117 . Unfortunately I am still writing my thesis that's why it's a bit stale. Not yet sure which part you can dive-in, currently I'm impelemnting the Ring Topology, maybe you can help by implementing other topologies?

@szhan
Copy link
Author

szhan commented Jun 5, 2018

Thanks, @ljvmiranda921 ! I am actually quite interested in topology selection. So, I think I will help with implementing additional topologies first.

@ljvmiranda921
Copy link
Owner

ljvmiranda921 commented Jun 6, 2018

Awesome, let me update you once I need help. Just need to clean up what I'm doing first then I'll ping you right away!

ljvmiranda921 pushed a commit that referenced this issue Jun 7, 2018
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 <[email protected]>
ljvmiranda921 pushed a commit that referenced this issue Jun 7, 2018
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 <[email protected]>
ljvmiranda921 pushed a commit that referenced this issue Jun 7, 2018
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 <[email protected]>
ljvmiranda921 pushed a commit that referenced this issue Jun 7, 2018
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 <[email protected]>
ljvmiranda921 pushed a commit that referenced this issue Jun 7, 2018
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 <[email protected]>
ljvmiranda921 pushed a commit that referenced this issue Jun 14, 2018
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 <[email protected]>
ljvmiranda921 pushed a commit that referenced this issue Jun 14, 2018
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 <[email protected]>
ljvmiranda921 pushed a commit that referenced this issue Jun 14, 2018
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 <[email protected]>
ljvmiranda921 pushed a commit that referenced this issue Jun 14, 2018
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 <[email protected]>
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

No branches or pull requests

2 participants