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

TypeError: 'bool' object is not subscriptable #109

Closed
justinlent opened this issue May 9, 2018 · 3 comments
Closed

TypeError: 'bool' object is not subscriptable #109

justinlent opened this issue May 9, 2018 · 3 comments

Comments

@justinlent
Copy link

justinlent commented May 9, 2018

  • PySwarms version: 0.19
  • Python version: 3.6
  • Operating System: Mac OS

Description

Describe what you were trying to get done.
Run a GlobalBestOptimization

Tell us what happened, what went wrong, and what you expected to happen.
I have a fairly complex objective function, but I can get it to run and return the expected values when running outside of pyswarms. but when I kick off an optimization i get this 'bool' object is not subsriptable TypeError after what appears to be the 1st iteration of the optimization (or maybe it's at the beginning of the 2nd iteration).

What I Did

# Create bounds
br = 0.2
max_bound = np.array( [ -1.0+br, 0.75,    0.5,    0.20,   0.9475,    0.9,    0.85,    0.8,    -1.5+br, 1.25,    6.0 ] )
min_bound = np.array( [ -1.0,    0.75-br, 0.5-br, 0.0-br, 0.9475-br, 0.9-br, 0.85-br, 0.8-br, -1.5,    1.25-br, 4.0 ] )

print( "Running strategy optimization with, # of parameters (p-swarm dimensions):", len(max_bound) )

bounds = (min_bound, max_bound)

# Initialize swarm
options = {'c1': 0.5, 'c2': 0.3, 'w':0.9}

# Call instance of PSO with bounds argument
optimizer = ps.single.GlobalBestPSO(n_particles=2, dimensions=11, options=options, bounds=bounds)

# Perform optimization
cost, pos = optimizer.optimize(strat_obj_func_wrapper, print_step=5, iters=100, verbose=3)

Then running the optimizer results in the following output and traceback:

Running strategy optimization with, # of parameters (p-swarm dimensions): 11

 opt_run_inputs_multiple: [[-0.88239126  0.63317997  0.41620925 -0.06704406  0.93384025  0.77059523
   0.80944611  0.63739677 -1.43850201  1.10322874  5.4486611 ]
 [-0.90738229  0.57249487  0.30730725 -0.07771865  0.85251403  0.85871976
   0.68927081  0.62064886 -1.36711124  1.10884316  4.84986957]]
-0.07815433772752717
-0.00789521337447165

 opt_run_inputs_multiple: [[-0.88239126  0.63317997  0.41620925 -0.06704406  0.93384025  0.77059523
   0.80944611  0.63739677 -1.43850201  1.10322874  5.4486611 ]
 [-0.90738229  0.57249487  0.30730725 -0.07771865  0.85251403  0.85871976
   0.68927081  0.62064886 -1.36711124  1.10884316  4.84986957]]
-0.07815433772752717
-0.00789521337447165
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-547-2949e347e2b5> in <module>()
     15 
     16 # Perform optimization
---> 17 cost, pos = optimizer.optimize(strat_obj_func_wrapper, print_step=5, iters=100, verbose=3)

~/python/anaconda3510/anaconda3/envs/quant_py364/lib/python3.6/site-packages/pyswarms/single/global_best.py in optimize(self, objective_func, iters, print_step, verbose)
    151             pbest_cost = np.where(~m, pbest_cost, current_cost)
    152             # Create 2-D mask to update positions
--> 153             _m = np.repeat(m[:, np.newaxis], self.dimensions, axis=1)
    154             self.personal_best_pos = np.where(~_m, self.personal_best_pos,
    155                                               self.pos)

TypeError: 'bool' object is not subscriptable
@ljvmiranda921
Copy link
Owner

Hi @justinlent , sorry for the late reply.

I wonder what shape your custom objective function returns. As of now, custom functions should
always return a vector of shape (n_particles, ) where each element contains the fitness of each particle.

In order to interface properly with pyswarms, the objective function should:

  1. Be able to take a matrix of shape (n_particles, n_dimensions)
  2. Be able to return a vector of shape (n_particles, ) with the fitness for each particle.

Thanks and I hope it helps.

@justinlent
Copy link
Author

Thanks! This appears to have fixed it for me!

@ljvmiranda921
Copy link
Owner

Glad it helped! Goodluck! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants